summaryrefslogtreecommitdiffstats
path: root/media/base/mime_util_unittest.cc
blob: ca1f393723567ca8a4dc7fdd08db2b4c05892525 (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright 2015 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.

#include <stddef.h>

#include "base/macros.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "media/base/mime_util.h"
#include "media/base/mime_util_internal.h"
#include "media/media_features.h"
#include "testing/gtest/include/gtest/gtest.h"

#if defined(OS_ANDROID)
#include "base/android/build_info.h"
#endif

namespace media {
namespace internal {

// MIME type for use with IsCodecSupportedOnPlatform() test; type is ignored in
// all cases except for when paired with the Opus codec.
const char kTestMimeType[] = "foo/foo";

// Helper method for creating a multi-value vector of |kTestStates| if
// |test_all_values| is true or if false, a single value vector containing
// |single_value|.
static std::vector<bool> CreateTestVector(bool test_all_values,
                                          bool single_value) {
  const bool kTestStates[] = {true, false};
  if (test_all_values)
    return std::vector<bool>(kTestStates, kTestStates + arraysize(kTestStates));
  return std::vector<bool>(1, single_value);
}

// Helper method for running IsCodecSupportedOnPlatform() tests that will
// iterate over all possible field values for a MimeUtil::PlatformInfo struct.
//
// To request a field be varied, set its value to true in the |states_to_vary|
// struct.  If false, the only value tested will be the field value from
// |test_states|.
//
// |test_func| should have the signature <void(const MimeUtil::PlatformInfo&,
// MimeUtil::Codec)>.
template <typename TestCallback>
static void RunCodecSupportTest(const MimeUtil::PlatformInfo& states_to_vary,
                                const MimeUtil::PlatformInfo& test_states,
                                TestCallback test_func) {
#define MAKE_TEST_VECTOR(name)      \
  std::vector<bool> name##_states = \
      CreateTestVector(states_to_vary.name, test_states.name)

  // Stuff states to test into vectors for easy for_each() iteration.
  MAKE_TEST_VECTOR(has_platform_decoders);
  MAKE_TEST_VECTOR(has_platform_vp8_decoder);
  MAKE_TEST_VECTOR(supports_opus);
  MAKE_TEST_VECTOR(supports_vp9);
  MAKE_TEST_VECTOR(is_unified_media_pipeline_enabled);
#undef MAKE_TEST_VECTOR

  MimeUtil::PlatformInfo info;

#define RUN_TEST_VECTOR(name)                   \
  size_t name##_index = 0;                      \
  for (info.name = name##_states[name##_index]; \
       name##_index < name##_states.size(); ++name##_index)

  RUN_TEST_VECTOR(has_platform_decoders) {
    RUN_TEST_VECTOR(has_platform_vp8_decoder) {
      RUN_TEST_VECTOR(supports_opus) {
        RUN_TEST_VECTOR(supports_vp9) {
          RUN_TEST_VECTOR(is_unified_media_pipeline_enabled) {
            for (int codec = MimeUtil::INVALID_CODEC;
                 codec <= MimeUtil::LAST_CODEC; ++codec) {
              SCOPED_TRACE(base::StringPrintf(
                  "has_platform_decoders=%d, has_platform_vp8_decoder=%d, "
                  "supports_opus=%d, "
                  "supports_vp9=%d, is_unified_media_pipeline_enabled=%d, "
                  "codec=%d",
                  info.has_platform_decoders, info.has_platform_vp8_decoder,
                  info.supports_opus, info.supports_vp9,
                  info.is_unified_media_pipeline_enabled, codec));
              test_func(info, static_cast<MimeUtil::Codec>(codec));
            }
          }
        }
      }
    }
  }
#undef RUN_TEST_VECTOR
}

// Helper method for generating the |states_to_vary| value used by
// RunPlatformCodecTest(). Marks all fields to be varied.
static MimeUtil::PlatformInfo VaryAllFields() {
  MimeUtil::PlatformInfo states_to_vary;
  states_to_vary.has_platform_vp8_decoder = true;
  states_to_vary.supports_opus = true;
  states_to_vary.supports_vp9 = true;
  states_to_vary.is_unified_media_pipeline_enabled = true;
  states_to_vary.has_platform_decoders = true;
  return states_to_vary;
}

static bool HasHevcSupport() {
#if BUILDFLAG(ENABLE_HEVC_DEMUXING)
#if defined(OS_ANDROID)
  return base::android::BuildInfo::GetInstance()->sdk_int() >= 21;
#else
  return true;
#endif  // defined(OS_ANDROID)
#else
  return false;
#endif  // BUILDFLAG(ENABLE_HEVC_DEMUXING)
}

TEST(MimeUtilTest, CommonMediaMimeType) {
  EXPECT_TRUE(IsSupportedMediaMimeType("audio/webm"));
  EXPECT_TRUE(IsSupportedMediaMimeType("video/webm"));

  EXPECT_TRUE(IsSupportedMediaMimeType("audio/wav"));
  EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-wav"));

  EXPECT_TRUE(IsSupportedMediaMimeType("audio/ogg"));
  EXPECT_TRUE(IsSupportedMediaMimeType("application/ogg"));
#if defined(OS_ANDROID)
  EXPECT_FALSE(IsSupportedMediaMimeType("video/ogg"));
#else
  EXPECT_TRUE(IsSupportedMediaMimeType("video/ogg"));
#endif  // OS_ANDROID

#if defined(OS_ANDROID)
  // HLS is supported on Android API level 14 and higher and Chrome supports
  // API levels 15 and higher, so these are expected to be supported.
  bool kHlsSupported = true;
#else
  bool kHlsSupported = false;
#endif

  EXPECT_EQ(kHlsSupported, IsSupportedMediaMimeType("application/x-mpegurl"));
  EXPECT_EQ(kHlsSupported, IsSupportedMediaMimeType("Application/X-MPEGURL"));
  EXPECT_EQ(kHlsSupported, IsSupportedMediaMimeType(
      "application/vnd.apple.mpegurl"));

#if defined(USE_PROPRIETARY_CODECS)
  EXPECT_TRUE(IsSupportedMediaMimeType("audio/mp4"));
  EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-m4a"));
  EXPECT_TRUE(IsSupportedMediaMimeType("video/mp4"));
  EXPECT_TRUE(IsSupportedMediaMimeType("video/x-m4v"));

  EXPECT_TRUE(IsSupportedMediaMimeType("audio/mp3"));
  EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-mp3"));
  EXPECT_TRUE(IsSupportedMediaMimeType("audio/mpeg"));
  EXPECT_TRUE(IsSupportedMediaMimeType("audio/aac"));

#if BUILDFLAG(ENABLE_MSE_MPEG2TS_STREAM_PARSER)
  EXPECT_TRUE(IsSupportedMediaMimeType("video/mp2t"));
#else
  EXPECT_FALSE(IsSupportedMediaMimeType("video/mp2t"));
#endif
#else
  EXPECT_FALSE(IsSupportedMediaMimeType("audio/mp4"));
  EXPECT_FALSE(IsSupportedMediaMimeType("audio/x-m4a"));
  EXPECT_FALSE(IsSupportedMediaMimeType("video/mp4"));
  EXPECT_FALSE(IsSupportedMediaMimeType("video/x-m4v"));

  EXPECT_FALSE(IsSupportedMediaMimeType("audio/mp3"));
  EXPECT_FALSE(IsSupportedMediaMimeType("audio/x-mp3"));
  EXPECT_FALSE(IsSupportedMediaMimeType("audio/mpeg"));
  EXPECT_FALSE(IsSupportedMediaMimeType("audio/aac"));
#endif  // USE_PROPRIETARY_CODECS
  EXPECT_FALSE(IsSupportedMediaMimeType("video/mp3"));

  EXPECT_FALSE(IsSupportedMediaMimeType("video/unknown"));
  EXPECT_FALSE(IsSupportedMediaMimeType("audio/unknown"));
  EXPECT_FALSE(IsSupportedMediaMimeType("unknown/unknown"));
}

// Note: codecs should only be a list of 2 or fewer; hence the restriction of
// results' length to 2.
TEST(MimeUtilTest, ParseCodecString) {
  const struct {
    const char* const original;
    size_t expected_size;
    const char* const results[2];
  } tests[] = {
    { "\"bogus\"",                  1, { "bogus" }            },
    { "0",                          1, { "0" }                },
    { "avc1.42E01E, mp4a.40.2",     2, { "avc1",   "mp4a" }   },
    { "\"mp4v.20.240, mp4a.40.2\"", 2, { "mp4v",   "mp4a" }   },
    { "mp4v.20.8, samr",            2, { "mp4v",   "samr" }   },
    { "\"theora, vorbis\"",         2, { "theora", "vorbis" } },
    { "",                           0, { }                    },
    { "\"\"",                       0, { }                    },
    { "\"   \"",                    0, { }                    },
    { ",",                          2, { "", "" }             },
  };

  for (size_t i = 0; i < arraysize(tests); ++i) {
    std::vector<std::string> codecs_out;
    ParseCodecString(tests[i].original, &codecs_out, true);
    ASSERT_EQ(tests[i].expected_size, codecs_out.size());
    for (size_t j = 0; j < tests[i].expected_size; ++j)
      EXPECT_EQ(tests[i].results[j], codecs_out[j]);
  }

  // Test without stripping the codec type.
  std::vector<std::string> codecs_out;
  ParseCodecString("avc1.42E01E, mp4a.40.2", &codecs_out, false);
  ASSERT_EQ(2u, codecs_out.size());
  EXPECT_EQ("avc1.42E01E", codecs_out[0]);
  EXPECT_EQ("mp4a.40.2", codecs_out[1]);
}

TEST(IsCodecSupportedOnPlatformTest,
     EncryptedCodecsFailWithoutPlatformSupport) {
  // Vary all parameters except |has_platform_decoders|.
  MimeUtil::PlatformInfo states_to_vary = VaryAllFields();
  states_to_vary.has_platform_decoders = false;

  // Disable platform decoders.
  MimeUtil::PlatformInfo test_states;
  test_states.has_platform_decoders = false;

  // Every codec should fail since platform support is missing and we've
  // requested encrypted codecs.
  RunCodecSupportTest(
      states_to_vary, test_states,
      [](const MimeUtil::PlatformInfo& info, MimeUtil::Codec codec) {
        EXPECT_FALSE(MimeUtil::IsCodecSupportedOnPlatform(codec, kTestMimeType,
                                                          true, info));
      });
}

TEST(IsCodecSupportedOnPlatformTest, EncryptedCodecBehavior) {
  // Vary all parameters except |has_platform_decoders|.
  MimeUtil::PlatformInfo states_to_vary = VaryAllFields();
  states_to_vary.has_platform_decoders = false;

  // Enable platform decoders.
  MimeUtil::PlatformInfo test_states;
  test_states.has_platform_decoders = true;

  RunCodecSupportTest(
      states_to_vary, test_states,
      [](const MimeUtil::PlatformInfo& info, MimeUtil::Codec codec) {
        const bool result = MimeUtil::IsCodecSupportedOnPlatform(
            codec, kTestMimeType, true, info);
        switch (codec) {
          // These codecs are never supported by the Android platform.
          case MimeUtil::INVALID_CODEC:
          case MimeUtil::AC3:
          case MimeUtil::EAC3:
          case MimeUtil::MPEG2_AAC_LC:
          case MimeUtil::MPEG2_AAC_MAIN:
          case MimeUtil::MPEG2_AAC_SSR:
          case MimeUtil::THEORA:
            EXPECT_FALSE(result);
            break;

          // These codecs are always available with platform decoder support.
          case MimeUtil::PCM:
          case MimeUtil::MP3:
          case MimeUtil::MPEG4_AAC_LC:
          case MimeUtil::MPEG4_AAC_SBR_v1:
          case MimeUtil::MPEG4_AAC_SBR_PS_v2:
          case MimeUtil::VORBIS:
          case MimeUtil::H264:
            EXPECT_TRUE(result);
            break;

          // The remaining codecs are not available on all platforms even when
          // a platform decoder is available.
          case MimeUtil::OPUS:
            EXPECT_EQ(info.supports_opus, result);
            break;

          case MimeUtil::VP8:
            EXPECT_EQ(info.has_platform_vp8_decoder, result);
            break;

          case MimeUtil::VP9:
            EXPECT_EQ(info.supports_vp9, result);
            break;

          case MimeUtil::HEVC_MAIN:
            EXPECT_EQ(HasHevcSupport(), result);
            break;
        }
      });
}

TEST(IsCodecSupportedOnPlatformTest, ClearCodecBehaviorWithAndroidPipeline) {
  // Vary all parameters except |is_unified_media_pipeline_enabled|.
  MimeUtil::PlatformInfo states_to_vary = VaryAllFields();
  states_to_vary.is_unified_media_pipeline_enabled = false;

  // Disable the unified pipeline.
  MimeUtil::PlatformInfo test_states;
  test_states.is_unified_media_pipeline_enabled = false;

  RunCodecSupportTest(
      states_to_vary, test_states,
      [](const MimeUtil::PlatformInfo& info, MimeUtil::Codec codec) {
        const bool result = MimeUtil::IsCodecSupportedOnPlatform(
            codec, kTestMimeType, false, info);
        switch (codec) {
          // These codecs are never supported by the Android platform.
          case MimeUtil::INVALID_CODEC:
          case MimeUtil::AC3:
          case MimeUtil::EAC3:
          case MimeUtil::MPEG2_AAC_LC:
          case MimeUtil::MPEG2_AAC_MAIN:
          case MimeUtil::MPEG2_AAC_SSR:
          case MimeUtil::THEORA:
            EXPECT_FALSE(result);
            break;

          // These codecs are always available via MediaPlayer.
          case MimeUtil::PCM:
          case MimeUtil::MP3:
          case MimeUtil::MPEG4_AAC_LC:
          case MimeUtil::MPEG4_AAC_SBR_v1:
          case MimeUtil::MPEG4_AAC_SBR_PS_v2:
          case MimeUtil::VORBIS:
          case MimeUtil::H264:
          case MimeUtil::VP8:
            EXPECT_TRUE(result);
            break;

          // The remaining codecs depend on the platform version.
          case MimeUtil::OPUS:
            EXPECT_EQ(info.supports_opus, result);
            break;

          case MimeUtil::VP9:
            EXPECT_EQ(info.supports_vp9, result);
            break;

          case MimeUtil::HEVC_MAIN:
            EXPECT_EQ(HasHevcSupport(), result);
            break;
        }
      });
}

TEST(IsCodecSupportedOnPlatformTest, ClearCodecBehaviorWithUnifiedPipeline) {
  // Vary all parameters except |is_unified_media_pipeline_enabled|.
  MimeUtil::PlatformInfo states_to_vary = VaryAllFields();
  states_to_vary.is_unified_media_pipeline_enabled = false;

  // Enable the unified pipeline.
  MimeUtil::PlatformInfo test_states;
  test_states.is_unified_media_pipeline_enabled = true;

  RunCodecSupportTest(
      states_to_vary, test_states,
      [](const MimeUtil::PlatformInfo& info, MimeUtil::Codec codec) {
        const bool result = MimeUtil::IsCodecSupportedOnPlatform(
            codec, kTestMimeType, false, info);
        switch (codec) {
          // These codecs are never supported by the Android platform.
          case MimeUtil::INVALID_CODEC:
          case MimeUtil::AC3:
          case MimeUtil::EAC3:
          case MimeUtil::THEORA:
            EXPECT_FALSE(result);
            break;

          // These codecs are always supported with the unified pipeline.
          case MimeUtil::PCM:
          case MimeUtil::MPEG2_AAC_LC:
          case MimeUtil::MPEG2_AAC_MAIN:
          case MimeUtil::MPEG2_AAC_SSR:
          case MimeUtil::MP3:
          case MimeUtil::MPEG4_AAC_LC:
          case MimeUtil::MPEG4_AAC_SBR_v1:
          case MimeUtil::MPEG4_AAC_SBR_PS_v2:
          case MimeUtil::OPUS:
          case MimeUtil::VORBIS:
          case MimeUtil::VP8:
          case MimeUtil::VP9:
            EXPECT_TRUE(result);
            break;

          // These codecs are only supported if platform decoders are supported.
          case MimeUtil::H264:
            EXPECT_EQ(info.has_platform_decoders, result);
            break;

          case MimeUtil::HEVC_MAIN:
            EXPECT_EQ(HasHevcSupport() && info.has_platform_decoders, result);
            break;
        }
      });
}

TEST(IsCodecSupportedOnPlatformTest, OpusOggSupport) {
  // Vary all parameters; thus use default initial state.
  MimeUtil::PlatformInfo states_to_vary = VaryAllFields();
  MimeUtil::PlatformInfo test_states;

  RunCodecSupportTest(
      states_to_vary, test_states,
      [](const MimeUtil::PlatformInfo& info, MimeUtil::Codec codec) {
        EXPECT_EQ(info.is_unified_media_pipeline_enabled,
                  MimeUtil::IsCodecSupportedOnPlatform(
                      MimeUtil::OPUS, "audio/ogg", false, info));
      });
}

}  // namespace internal
}  // namespace media