summaryrefslogtreecommitdiff
path: root/chromium/media/base/media_serializers.h
blob: 6333c44170fff29a98d9addf53c836d213c8292b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_BASE_MEDIA_SERIALIZERS_H_
#define MEDIA_BASE_MEDIA_SERIALIZERS_H_

#include <vector>

#include "base/location.h"
#include "base/strings/stringprintf.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/buffering_state.h"
#include "media/base/media_serializers_base.h"
#include "media/base/status.h"
#include "media/base/status_codes.h"
#include "media/base/video_decoder_config.h"
#include "ui/gfx/geometry/size.h"

namespace media {

namespace internal {

// Serializing any const or reference combination.
template <typename T>
struct MediaSerializer<const T> {
  static base::Value Serialize(const T& value) {
    return MediaSerializer<T>::Serialize(value);
  }
};

template <typename T>
struct MediaSerializer<T&> {
  static base::Value Serialize(const T& value) {
    return MediaSerializer<T>::Serialize(value);
  }
};

// Serialize default value.
template <>
struct MediaSerializer<base::Value> {
  static base::Value Serialize(const base::Value& value) {
    return value.Clone();
  }
};

// Serialize vectors of things
template <typename VecType>
struct MediaSerializer<std::vector<VecType>> {
  static base::Value Serialize(const std::vector<VecType>& vec) {
    base::Value result(base::Value::Type::LIST);
    for (const VecType& value : vec)
      result.Append(MediaSerializer<VecType>::Serialize(value));
    return result;
  }
};

// serialize optional types
template <typename OptType>
struct MediaSerializer<base::Optional<OptType>> {
  static base::Value Serialize(const base::Optional<OptType>& opt) {
    return opt ? MediaSerializer<OptType>::Serialize(opt.value())
               : base::Value("unset");  // TODO(tmathmeyer) maybe empty string?
  }
};

// Sometimes raw strings wont template match to a char*.
template <int len>
struct MediaSerializer<char[len]> {
  static inline base::Value Serialize(const char* code) {
    return base::Value(code);
  }
};

// Can't send non-finite double values to a base::Value.
template <>
struct MediaSerializer<double> {
  static inline base::Value Serialize(double value) {
    return std::isfinite(value) ? base::Value(value) : base::Value("unknown");
  }
};

template <>
struct MediaSerializer<int64_t> {
  static inline base::Value Serialize(int64_t value) {
    return MediaSerializer<double>::Serialize(static_cast<double>(value));
  }
};

// Just upcast this to get the NaN check.
template <>
struct MediaSerializer<float> {
  static inline base::Value Serialize(float value) {
    return MediaSerializer<double>::Serialize(value);
  }
};

// Serialization for chromium-specific types.
// Each serializer should be commented like:
// Class/Enum (simple/complex)
// where Classes should take constref arguments, and "simple" methods should
// be declared inline.

// the FIELD_SERIALIZE method can be used whenever the result is a dict named
// |result|.
#define FIELD_SERIALIZE(NAME, CONSTEXPR) \
  result.SetKey(NAME, MediaSerialize(CONSTEXPR))

// Class (simple)
template <>
struct MediaSerializer<gfx::Size> {
  static inline base::Value Serialize(const gfx::Size& value) {
    return base::Value(value.ToString());
  }
};

// Class (simple)
template <>
struct MediaSerializer<gfx::Rect> {
  static inline base::Value Serialize(const gfx::Rect& value) {
    return base::Value(value.ToString());
  }
};

// enum (simple)
template <>
struct MediaSerializer<base::TimeDelta> {
  static inline base::Value Serialize(const base::TimeDelta value) {
    return MediaSerializer<double>::Serialize(value.InSecondsF());
  }
};

// Enum (simple)
template <>
struct MediaSerializer<media::AudioCodec> {
  static inline base::Value Serialize(media::AudioCodec value) {
    return base::Value(GetCodecName(value));
  }
};

// Enum (simple)
template <>
struct MediaSerializer<media::AudioCodecProfile> {
  static inline base::Value Serialize(media::AudioCodecProfile value) {
    return base::Value(GetProfileName(value));
  }
};

// Enum (simple)
template <>
struct MediaSerializer<media::VideoCodec> {
  static inline base::Value Serialize(media::VideoCodec value) {
    return base::Value(GetCodecName(value));
  }
};

// Enum (simple)
template <>
struct MediaSerializer<media::VideoCodecProfile> {
  static inline base::Value Serialize(media::VideoCodecProfile value) {
    return base::Value(GetProfileName(value));
  }
};

// Enum (simple)
template <>
struct MediaSerializer<media::ChannelLayout> {
  static inline base::Value Serialize(media::ChannelLayout value) {
    return base::Value(ChannelLayoutToString(value));
  }
};

// Enum (simple)
template <>
struct MediaSerializer<media::SampleFormat> {
  static inline base::Value Serialize(media::SampleFormat value) {
    return base::Value(SampleFormatToString(value));
  }
};

// Enum (complex)
template <>
struct MediaSerializer<media::EncryptionScheme> {
  static base::Value Serialize(const media::EncryptionScheme& value) {
    std::ostringstream encryptionSchemeString;
    encryptionSchemeString << value;
    return base::Value(encryptionSchemeString.str());
  }
};

// Class (complex)
template <>
struct MediaSerializer<media::VideoTransformation> {
  static base::Value Serialize(const media::VideoTransformation& value) {
    std::string rotation = VideoRotationToString(value.rotation);
    if (value.mirrored)
      rotation += ", mirrored";
    return base::Value(rotation);
  }
};

// Class (simple)
template <>
struct MediaSerializer<media::VideoColorSpace> {
  static inline base::Value Serialize(const media::VideoColorSpace& value) {
    return base::Value(value.ToGfxColorSpace().ToString());
  }
};

// Class (complex)
template <>
struct MediaSerializer<media::HDRMetadata> {
  static base::Value Serialize(const media::HDRMetadata& value) {
    // TODO(tmathmeyer) serialize more fields here potentially.
    base::Value result(base::Value::Type::DICTIONARY);
    FIELD_SERIALIZE("luminance range",
                    base::StringPrintf("%.2f => %.2f",
                                       value.mastering_metadata.luminance_min,
                                       value.mastering_metadata.luminance_max));
    FIELD_SERIALIZE("primaries",
                    base::StringPrintf(
                        "[r:%.4f,%.4f, g:%.4f,%.4f, b:%.4f,%.4f, wp:%.4f,%.4f]",
                        value.mastering_metadata.primary_r.x(),
                        value.mastering_metadata.primary_r.y(),
                        value.mastering_metadata.primary_g.x(),
                        value.mastering_metadata.primary_g.y(),
                        value.mastering_metadata.primary_b.x(),
                        value.mastering_metadata.primary_b.y(),
                        value.mastering_metadata.white_point.x(),
                        value.mastering_metadata.white_point.y()));
    return result;
  }
};

// Class (complex)
template <>
struct MediaSerializer<media::AudioDecoderConfig> {
  static base::Value Serialize(const media::AudioDecoderConfig& value) {
    base::Value result(base::Value::Type::DICTIONARY);
    FIELD_SERIALIZE("codec", value.codec());
    FIELD_SERIALIZE("profile", value.profile());
    FIELD_SERIALIZE("bytes per channel", value.bytes_per_channel());
    FIELD_SERIALIZE("channel layout", value.channel_layout());
    FIELD_SERIALIZE("channels", value.channels());
    FIELD_SERIALIZE("samples per second", value.samples_per_second());
    FIELD_SERIALIZE("sample format", value.sample_format());
    FIELD_SERIALIZE("bytes per frame", value.bytes_per_frame());
    FIELD_SERIALIZE("codec delay", value.codec_delay());
    FIELD_SERIALIZE("has extra data", !value.extra_data().empty());
    FIELD_SERIALIZE("encryption scheme", value.encryption_scheme());
    FIELD_SERIALIZE("discard decoder delay",
                    value.should_discard_decoder_delay());

    // TODO(tmathmeyer) drop the units, let the frontend handle it.
    // use ostringstreams because windows & linux have _different types_
    // defined for int64_t, (long vs long long) so format specifiers dont work.
    std::ostringstream preroll;
    preroll << value.seek_preroll().InMicroseconds() << "us";
    result.SetStringKey("seek preroll", preroll.str());

    return result;
  }
};

// Enum (simple)
template <>
struct MediaSerializer<media::VideoDecoderConfig::AlphaMode> {
  static inline base::Value Serialize(
      media::VideoDecoderConfig::AlphaMode value) {
    return base::Value(value == VideoDecoderConfig::AlphaMode::kHasAlpha
                           ? "has_alpha"
                           : "is_opaque");
  }
};

// Class (complex)
template <>
struct MediaSerializer<media::VideoDecoderConfig> {
  static base::Value Serialize(const media::VideoDecoderConfig& value) {
    base::Value result(base::Value::Type::DICTIONARY);
    FIELD_SERIALIZE("codec", value.codec());
    FIELD_SERIALIZE("profile", value.profile());
    FIELD_SERIALIZE("alpha mode", value.alpha_mode());
    FIELD_SERIALIZE("coded size", value.coded_size());
    FIELD_SERIALIZE("visible rect", value.visible_rect());
    FIELD_SERIALIZE("natural size", value.natural_size());
    FIELD_SERIALIZE("has extra data", !value.extra_data().empty());
    FIELD_SERIALIZE("encryption scheme", value.encryption_scheme());
    FIELD_SERIALIZE("orientation", value.video_transformation());
    FIELD_SERIALIZE("color space", value.color_space_info());
    FIELD_SERIALIZE("hdr metadata", value.hdr_metadata());
    return result;
  }
};

// enum (simple)
template <>
struct MediaSerializer<media::BufferingState> {
  static inline base::Value Serialize(const media::BufferingState value) {
    return base::Value(value == media::BufferingState::BUFFERING_HAVE_ENOUGH
                           ? "BUFFERING_HAVE_ENOUGH"
                           : "BUFFERING_HAVE_NOTHING");
  }
};

// enum (complex)
template <>
struct MediaSerializer<media::BufferingStateChangeReason> {
  static base::Value Serialize(const media::BufferingStateChangeReason value) {
    switch (value) {
      case DEMUXER_UNDERFLOW:
        return base::Value("DEMUXER_UNDERFLOW");
      case DECODER_UNDERFLOW:
        return base::Value("DECODER_UNDERFLOW");
      case REMOTING_NETWORK_CONGESTION:
        return base::Value("REMOTING_NETWORK_CONGESTION");
      case BUFFERING_CHANGE_REASON_UNKNOWN:
        return base::Value("BUFFERING_CHANGE_REASON_UNKNOWN");
    }
  }
};

// Class (complex)
template <media::SerializableBufferingStateType T>
struct MediaSerializer<media::SerializableBufferingState<T>> {
  static base::Value Serialize(
      const media::SerializableBufferingState<T>& value) {
    base::Value result(base::Value::Type::DICTIONARY);
    FIELD_SERIALIZE("state", value.state);

    switch (value.reason) {
      case DEMUXER_UNDERFLOW:
      case DECODER_UNDERFLOW:
      case REMOTING_NETWORK_CONGESTION:
        FIELD_SERIALIZE("reason", value.reason);
        break;

      // Don't write anything here if the reason is unknown.
      case BUFFERING_CHANGE_REASON_UNKNOWN:
        break;
    }

    if (T == SerializableBufferingStateType::kPipeline)
      result.SetBoolKey("for_suspended_start", value.suspended_start);

    return result;
  }
};

// enum (simple)
template <>
struct MediaSerializer<media::StatusCode> {
  static inline base::Value Serialize(media::StatusCode code) {
    return base::Value(static_cast<int>(code));
  }
};

// Class (complex)
template <>
struct MediaSerializer<media::Status> {
  static base::Value Serialize(const media::Status& status) {
    if (status.is_ok())
      return base::Value("Ok");

    base::Value result(base::Value::Type::DICTIONARY);
    FIELD_SERIALIZE("status_code", status.code());
    FIELD_SERIALIZE("status_message", status.message());
    FIELD_SERIALIZE("stack", status.data_->frames);
    FIELD_SERIALIZE("data", status.data_->data);
    FIELD_SERIALIZE("causes", status.data_->causes);
    return result;
  }
};

// Class (complex)
template <>
struct MediaSerializer<base::Location> {
  static base::Value Serialize(const base::Location& value) {
    base::Value result(base::Value::Type::DICTIONARY);
    FIELD_SERIALIZE("file", value.file_name());
    FIELD_SERIALIZE("line", value.line_number());
    return result;
  }
};

#undef FIELD_SERIALIZE

}  // namespace internal

}  // namespace media

#endif  // MEDIA_BASE_MEDIA_SERIALIZERS_H_