OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
| 9 #include "base/strings/stringprintf.h" |
9 #include "build/build_config.h" | 10 #include "build/build_config.h" |
10 #include "media/base/mime_util.h" | 11 #include "media/base/mime_util.h" |
| 12 #include "media/base/mime_util_internal.h" |
11 #include "media/media_features.h" | 13 #include "media/media_features.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 | 15 |
| 16 #if defined(OS_ANDROID) |
| 17 #include "base/android/build_info.h" |
| 18 #endif |
| 19 |
14 namespace media { | 20 namespace media { |
| 21 namespace internal { |
| 22 |
| 23 // MIME type for use with IsCodecSupportedOnPlatform() test; type is ignored in |
| 24 // all cases except for when paired with the Opus codec. |
| 25 const char kTestMimeType[] = "foo/foo"; |
| 26 |
| 27 // Helper method for creating a multi-value vector of |kTestStates| if |
| 28 // |test_all_values| is true or if false, a single value vector containing |
| 29 // |single_value|. |
| 30 static std::vector<bool> CreateTestVector(bool test_all_values, |
| 31 bool single_value) { |
| 32 const bool kTestStates[] = {true, false}; |
| 33 if (test_all_values) |
| 34 return std::vector<bool>(kTestStates, kTestStates + arraysize(kTestStates)); |
| 35 return std::vector<bool>(1, single_value); |
| 36 } |
| 37 |
| 38 // Helper method for running IsCodecSupportedOnPlatform() tests that will |
| 39 // iterate over all possible field values for a MimeUtil::PlatformInfo struct. |
| 40 // |
| 41 // To request a field be varied, set its value to true in the |states_to_vary| |
| 42 // struct. If false, the only value tested will be the field value from |
| 43 // |test_states|. |
| 44 // |
| 45 // |test_func| should have the signature <void(const MimeUtil::PlatformInfo&, |
| 46 // MimeUtil::Codec)>. |
| 47 template <typename TestCallback> |
| 48 static void RunCodecSupportTest(const MimeUtil::PlatformInfo& states_to_vary, |
| 49 const MimeUtil::PlatformInfo& test_states, |
| 50 TestCallback test_func) { |
| 51 #define MAKE_TEST_VECTOR(name) \ |
| 52 std::vector<bool> name##_states = \ |
| 53 CreateTestVector(states_to_vary.name, test_states.name) |
| 54 |
| 55 // Stuff states to test into vectors for easy for_each() iteration. |
| 56 MAKE_TEST_VECTOR(has_platform_decoders); |
| 57 MAKE_TEST_VECTOR(has_platform_vp8_decoder); |
| 58 MAKE_TEST_VECTOR(supports_opus); |
| 59 MAKE_TEST_VECTOR(supports_vp9); |
| 60 MAKE_TEST_VECTOR(is_unified_media_pipeline_enabled); |
| 61 #undef MAKE_TEST_VECTOR |
| 62 |
| 63 MimeUtil::PlatformInfo info; |
| 64 |
| 65 #define RUN_TEST_VECTOR(name) \ |
| 66 size_t name##_index = 0; \ |
| 67 for (info.name = name##_states[name##_index]; \ |
| 68 name##_index < name##_states.size(); ++name##_index) |
| 69 |
| 70 RUN_TEST_VECTOR(has_platform_decoders) { |
| 71 RUN_TEST_VECTOR(has_platform_vp8_decoder) { |
| 72 RUN_TEST_VECTOR(supports_opus) { |
| 73 RUN_TEST_VECTOR(supports_vp9) { |
| 74 RUN_TEST_VECTOR(is_unified_media_pipeline_enabled) { |
| 75 for (int codec = MimeUtil::INVALID_CODEC; |
| 76 codec <= MimeUtil::LAST_CODEC; ++codec) { |
| 77 SCOPED_TRACE(base::StringPrintf( |
| 78 "has_platform_decoders=%d, has_platform_vp8_decoder=%d, " |
| 79 "supports_opus=%d, " |
| 80 "supports_vp9=%d, is_unified_media_pipeline_enabled=%d, " |
| 81 "codec=%d", |
| 82 info.has_platform_decoders, info.has_platform_vp8_decoder, |
| 83 info.supports_opus, info.supports_vp9, |
| 84 info.is_unified_media_pipeline_enabled, codec)); |
| 85 test_func(info, static_cast<MimeUtil::Codec>(codec)); |
| 86 } |
| 87 } |
| 88 } |
| 89 } |
| 90 } |
| 91 } |
| 92 #undef RUN_TEST_VECTOR |
| 93 } |
| 94 |
| 95 // Helper method for generating the |states_to_vary| value used by |
| 96 // RunPlatformCodecTest(). Marks all fields to be varied. |
| 97 static MimeUtil::PlatformInfo VaryAllFields() { |
| 98 MimeUtil::PlatformInfo states_to_vary; |
| 99 states_to_vary.has_platform_vp8_decoder = true; |
| 100 states_to_vary.supports_opus = true; |
| 101 states_to_vary.supports_vp9 = true; |
| 102 states_to_vary.is_unified_media_pipeline_enabled = true; |
| 103 states_to_vary.has_platform_decoders = true; |
| 104 return states_to_vary; |
| 105 } |
| 106 |
| 107 static bool HasHevcSupport() { |
| 108 #if BUILDFLAG(ENABLE_HEVC_DEMUXING) |
| 109 #if defined(OS_ANDROID) |
| 110 return base::android::BuildInfo::GetInstance()->sdk_int() >= 21; |
| 111 #else |
| 112 return true; |
| 113 #endif // defined(OS_ANDROID) |
| 114 #else |
| 115 return false; |
| 116 #endif // BUILDFLAG(ENABLE_HEVC_DEMUXING) |
| 117 } |
15 | 118 |
16 TEST(MimeUtilTest, CommonMediaMimeType) { | 119 TEST(MimeUtilTest, CommonMediaMimeType) { |
17 EXPECT_TRUE(IsSupportedMediaMimeType("audio/webm")); | 120 EXPECT_TRUE(IsSupportedMediaMimeType("audio/webm")); |
18 EXPECT_TRUE(IsSupportedMediaMimeType("video/webm")); | 121 EXPECT_TRUE(IsSupportedMediaMimeType("video/webm")); |
19 | 122 |
20 EXPECT_TRUE(IsSupportedMediaMimeType("audio/wav")); | 123 EXPECT_TRUE(IsSupportedMediaMimeType("audio/wav")); |
21 EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-wav")); | 124 EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-wav")); |
22 | 125 |
23 EXPECT_TRUE(IsSupportedMediaMimeType("audio/ogg")); | 126 EXPECT_TRUE(IsSupportedMediaMimeType("audio/ogg")); |
24 EXPECT_TRUE(IsSupportedMediaMimeType("application/ogg")); | 127 EXPECT_TRUE(IsSupportedMediaMimeType("application/ogg")); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } | 207 } |
105 | 208 |
106 // Test without stripping the codec type. | 209 // Test without stripping the codec type. |
107 std::vector<std::string> codecs_out; | 210 std::vector<std::string> codecs_out; |
108 ParseCodecString("avc1.42E01E, mp4a.40.2", &codecs_out, false); | 211 ParseCodecString("avc1.42E01E, mp4a.40.2", &codecs_out, false); |
109 ASSERT_EQ(2u, codecs_out.size()); | 212 ASSERT_EQ(2u, codecs_out.size()); |
110 EXPECT_EQ("avc1.42E01E", codecs_out[0]); | 213 EXPECT_EQ("avc1.42E01E", codecs_out[0]); |
111 EXPECT_EQ("mp4a.40.2", codecs_out[1]); | 214 EXPECT_EQ("mp4a.40.2", codecs_out[1]); |
112 } | 215 } |
113 | 216 |
| 217 TEST(IsCodecSupportedOnPlatformTest, |
| 218 EncryptedCodecsFailWithoutPlatformSupport) { |
| 219 // Vary all parameters except |has_platform_decoders|. |
| 220 MimeUtil::PlatformInfo states_to_vary = VaryAllFields(); |
| 221 states_to_vary.has_platform_decoders = false; |
| 222 |
| 223 // Disable platform decoders. |
| 224 MimeUtil::PlatformInfo test_states; |
| 225 test_states.has_platform_decoders = false; |
| 226 |
| 227 // Every codec should fail since platform support is missing and we've |
| 228 // requested encrypted codecs. |
| 229 RunCodecSupportTest( |
| 230 states_to_vary, test_states, |
| 231 [](const MimeUtil::PlatformInfo& info, MimeUtil::Codec codec) { |
| 232 EXPECT_FALSE(MimeUtil::IsCodecSupportedOnPlatform(codec, kTestMimeType, |
| 233 true, info)); |
| 234 }); |
| 235 } |
| 236 |
| 237 TEST(IsCodecSupportedOnPlatformTest, EncryptedCodecBehavior) { |
| 238 // Vary all parameters except |has_platform_decoders|. |
| 239 MimeUtil::PlatformInfo states_to_vary = VaryAllFields(); |
| 240 states_to_vary.has_platform_decoders = false; |
| 241 |
| 242 // Enable platform decoders. |
| 243 MimeUtil::PlatformInfo test_states; |
| 244 test_states.has_platform_decoders = true; |
| 245 |
| 246 RunCodecSupportTest( |
| 247 states_to_vary, test_states, |
| 248 [](const MimeUtil::PlatformInfo& info, MimeUtil::Codec codec) { |
| 249 const bool result = MimeUtil::IsCodecSupportedOnPlatform( |
| 250 codec, kTestMimeType, true, info); |
| 251 switch (codec) { |
| 252 // These codecs are never supported by the Android platform. |
| 253 case MimeUtil::INVALID_CODEC: |
| 254 case MimeUtil::AC3: |
| 255 case MimeUtil::EAC3: |
| 256 case MimeUtil::MPEG2_AAC_LC: |
| 257 case MimeUtil::MPEG2_AAC_MAIN: |
| 258 case MimeUtil::MPEG2_AAC_SSR: |
| 259 case MimeUtil::THEORA: |
| 260 EXPECT_FALSE(result); |
| 261 break; |
| 262 |
| 263 // These codecs are always available with platform decoder support. |
| 264 case MimeUtil::PCM: |
| 265 case MimeUtil::MP3: |
| 266 case MimeUtil::MPEG4_AAC_LC: |
| 267 case MimeUtil::MPEG4_AAC_SBR_v1: |
| 268 case MimeUtil::MPEG4_AAC_SBR_PS_v2: |
| 269 case MimeUtil::VORBIS: |
| 270 case MimeUtil::H264: |
| 271 EXPECT_TRUE(result); |
| 272 break; |
| 273 |
| 274 // The remaining codecs are not available on all platforms even when |
| 275 // a platform decoder is available. |
| 276 case MimeUtil::OPUS: |
| 277 EXPECT_EQ(info.supports_opus, result); |
| 278 break; |
| 279 |
| 280 case MimeUtil::VP8: |
| 281 EXPECT_EQ(info.has_platform_vp8_decoder, result); |
| 282 break; |
| 283 |
| 284 case MimeUtil::VP9: |
| 285 EXPECT_EQ(info.supports_vp9, result); |
| 286 break; |
| 287 |
| 288 case MimeUtil::HEVC_MAIN: |
| 289 EXPECT_EQ(HasHevcSupport(), result); |
| 290 break; |
| 291 } |
| 292 }); |
| 293 } |
| 294 |
| 295 TEST(IsCodecSupportedOnPlatformTest, ClearCodecBehaviorWithAndroidPipeline) { |
| 296 // Vary all parameters except |is_unified_media_pipeline_enabled|. |
| 297 MimeUtil::PlatformInfo states_to_vary = VaryAllFields(); |
| 298 states_to_vary.is_unified_media_pipeline_enabled = false; |
| 299 |
| 300 // Disable the unified pipeline. |
| 301 MimeUtil::PlatformInfo test_states; |
| 302 test_states.is_unified_media_pipeline_enabled = false; |
| 303 |
| 304 RunCodecSupportTest( |
| 305 states_to_vary, test_states, |
| 306 [](const MimeUtil::PlatformInfo& info, MimeUtil::Codec codec) { |
| 307 const bool result = MimeUtil::IsCodecSupportedOnPlatform( |
| 308 codec, kTestMimeType, false, info); |
| 309 switch (codec) { |
| 310 // These codecs are never supported by the Android platform. |
| 311 case MimeUtil::INVALID_CODEC: |
| 312 case MimeUtil::AC3: |
| 313 case MimeUtil::EAC3: |
| 314 case MimeUtil::MPEG2_AAC_LC: |
| 315 case MimeUtil::MPEG2_AAC_MAIN: |
| 316 case MimeUtil::MPEG2_AAC_SSR: |
| 317 case MimeUtil::THEORA: |
| 318 EXPECT_FALSE(result); |
| 319 break; |
| 320 |
| 321 // These codecs are always available via MediaPlayer. |
| 322 case MimeUtil::PCM: |
| 323 case MimeUtil::MP3: |
| 324 case MimeUtil::MPEG4_AAC_LC: |
| 325 case MimeUtil::MPEG4_AAC_SBR_v1: |
| 326 case MimeUtil::MPEG4_AAC_SBR_PS_v2: |
| 327 case MimeUtil::VORBIS: |
| 328 case MimeUtil::H264: |
| 329 case MimeUtil::VP8: |
| 330 EXPECT_TRUE(result); |
| 331 break; |
| 332 |
| 333 // The remaining codecs depend on the platform version. |
| 334 case MimeUtil::OPUS: |
| 335 EXPECT_EQ(info.supports_opus, result); |
| 336 break; |
| 337 |
| 338 case MimeUtil::VP9: |
| 339 EXPECT_EQ(info.supports_vp9, result); |
| 340 break; |
| 341 |
| 342 case MimeUtil::HEVC_MAIN: |
| 343 EXPECT_EQ(HasHevcSupport(), result); |
| 344 break; |
| 345 } |
| 346 }); |
| 347 } |
| 348 |
| 349 TEST(IsCodecSupportedOnPlatformTest, ClearCodecBehaviorWithUnifiedPipeline) { |
| 350 // Vary all parameters except |is_unified_media_pipeline_enabled|. |
| 351 MimeUtil::PlatformInfo states_to_vary = VaryAllFields(); |
| 352 states_to_vary.is_unified_media_pipeline_enabled = false; |
| 353 |
| 354 // Enable the unified pipeline. |
| 355 MimeUtil::PlatformInfo test_states; |
| 356 test_states.is_unified_media_pipeline_enabled = true; |
| 357 |
| 358 RunCodecSupportTest( |
| 359 states_to_vary, test_states, |
| 360 [](const MimeUtil::PlatformInfo& info, MimeUtil::Codec codec) { |
| 361 const bool result = MimeUtil::IsCodecSupportedOnPlatform( |
| 362 codec, kTestMimeType, false, info); |
| 363 switch (codec) { |
| 364 // These codecs are never supported by the Android platform. |
| 365 case MimeUtil::INVALID_CODEC: |
| 366 case MimeUtil::AC3: |
| 367 case MimeUtil::EAC3: |
| 368 case MimeUtil::THEORA: |
| 369 EXPECT_FALSE(result); |
| 370 break; |
| 371 |
| 372 // These codecs are always supported with the unified pipeline. |
| 373 case MimeUtil::PCM: |
| 374 case MimeUtil::MPEG2_AAC_LC: |
| 375 case MimeUtil::MPEG2_AAC_MAIN: |
| 376 case MimeUtil::MPEG2_AAC_SSR: |
| 377 case MimeUtil::MP3: |
| 378 case MimeUtil::MPEG4_AAC_LC: |
| 379 case MimeUtil::MPEG4_AAC_SBR_v1: |
| 380 case MimeUtil::MPEG4_AAC_SBR_PS_v2: |
| 381 case MimeUtil::OPUS: |
| 382 case MimeUtil::VORBIS: |
| 383 case MimeUtil::VP8: |
| 384 case MimeUtil::VP9: |
| 385 EXPECT_TRUE(result); |
| 386 break; |
| 387 |
| 388 // These codecs are only supported if platform decoders are supported. |
| 389 case MimeUtil::H264: |
| 390 EXPECT_EQ(info.has_platform_decoders, result); |
| 391 break; |
| 392 |
| 393 case MimeUtil::HEVC_MAIN: |
| 394 EXPECT_EQ(HasHevcSupport() && info.has_platform_decoders, result); |
| 395 break; |
| 396 } |
| 397 }); |
| 398 } |
| 399 |
| 400 TEST(IsCodecSupportedOnPlatformTest, OpusOggSupport) { |
| 401 // Vary all parameters; thus use default initial state. |
| 402 MimeUtil::PlatformInfo states_to_vary = VaryAllFields(); |
| 403 MimeUtil::PlatformInfo test_states; |
| 404 |
| 405 RunCodecSupportTest( |
| 406 states_to_vary, test_states, |
| 407 [](const MimeUtil::PlatformInfo& info, MimeUtil::Codec codec) { |
| 408 EXPECT_EQ(info.is_unified_media_pipeline_enabled, |
| 409 MimeUtil::IsCodecSupportedOnPlatform( |
| 410 MimeUtil::OPUS, "audio/ogg", false, info)); |
| 411 }); |
| 412 } |
| 413 |
| 414 } // namespace internal |
114 } // namespace media | 415 } // namespace media |
OLD | NEW |