1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "base/win/windows_version.h"
8 #include "content/browser/media/media_browsertest.h"
9 #include "content/public/common/content_switches.h"
10 #include "content/public/test/browser_test_utils.h"
11 #include "content/shell/browser/shell.h"
12 #if defined(OS_ANDROID)
13 #include "base/android/build_info.h"
16 // Available key systems.
17 const char kClearKeyKeySystem
[] = "webkit-org.w3.clearkey";
19 // Supported media types.
20 const char kWebMAudioOnly
[] = "audio/webm; codecs=\"vorbis\"";
21 const char kWebMVideoOnly
[] = "video/webm; codecs=\"vp8\"";
22 const char kWebMAudioVideo
[] = "video/webm; codecs=\"vorbis, vp8\"";
24 // EME-specific test results and errors.
25 const char kEmeKeyError
[] = "KEYERROR";
26 const char kEmeNotSupportedError
[] = "NOTSUPPORTEDERROR";
28 const char kDefaultEmePlayer
[] = "eme_player.html";
30 // The type of video src used to load media.
38 // MSE is available on all desktop platforms and on Android 4.1 and later.
39 static bool IsMSESupported() {
40 #if defined(OS_ANDROID)
41 if (base::android::BuildInfo::GetInstance()->sdk_int() < 16) {
42 VLOG(0) << "MSE is only supported in Android 4.1 and later.";
45 #endif // defined(OS_ANDROID)
49 // Tests encrypted media playback with a combination of parameters:
50 // - char*: Key system name.
51 // - bool: True to load media using MSE, otherwise use src.
52 class EncryptedMediaTest
: public content::MediaBrowserTest
,
53 public testing::WithParamInterface
<std::tr1::tuple
<const char*, SrcType
> > {
55 // Can only be used in parameterized (*_P) tests.
56 const std::string
CurrentKeySystem() {
57 return std::tr1::get
<0>(GetParam());
60 // Can only be used in parameterized (*_P) tests.
61 SrcType
CurrentSourceType() {
62 return std::tr1::get
<1>(GetParam());
65 void TestSimplePlayback(const std::string
& encrypted_media
,
66 const std::string
& media_type
) {
67 RunSimpleEncryptedMediaTest(
68 encrypted_media
, media_type
, CurrentKeySystem(), CurrentSourceType());
71 void TestFrameSizeChange() {
72 RunEncryptedMediaTest("encrypted_frame_size_change.html",
73 "frame_size_change-av_enc-v.webm", kWebMAudioVideo
,
74 CurrentKeySystem(), CurrentSourceType(), kEnded
);
77 void TestConfigChange() {
78 if (CurrentSourceType() != MSE
|| !IsMSESupported()) {
79 VLOG(0) << "Skipping test - config change test requires MSE.";
83 base::StringPairs query_params
;
84 query_params
.push_back(std::make_pair("keySystem", CurrentKeySystem()));
85 query_params
.push_back(std::make_pair("runEncrypted", "1"));
86 query_params
.push_back(std::make_pair("usePrefixedEME", "1"));
87 RunMediaTestPage("mse_config_change.html", query_params
, kEnded
, true);
90 void RunEncryptedMediaTest(const std::string
& html_page
,
91 const std::string
& media_file
,
92 const std::string
& media_type
,
93 const std::string
& key_system
,
95 const std::string
& expectation
) {
96 if (src_type
== MSE
&& !IsMSESupported()) {
97 VLOG(0) << "Skipping test - MSE not supported.";
101 base::StringPairs query_params
;
102 query_params
.push_back(std::make_pair("mediaFile", media_file
));
103 query_params
.push_back(std::make_pair("mediaType", media_type
));
104 query_params
.push_back(std::make_pair("keySystem", key_system
));
106 query_params
.push_back(std::make_pair("useMSE", "1"));
107 query_params
.push_back(std::make_pair("usePrefixedEME", "1"));
108 RunMediaTestPage(html_page
, query_params
, expectation
, true);
111 void RunSimpleEncryptedMediaTest(const std::string
& media_file
,
112 const std::string
& media_type
,
113 const std::string
& key_system
,
115 RunEncryptedMediaTest(kDefaultEmePlayer
,
124 // We want to fail quickly when a test fails because an error is encountered.
125 void AddWaitForTitles(content::TitleWatcher
* title_watcher
) override
{
126 MediaBrowserTest::AddWaitForTitles(title_watcher
);
127 title_watcher
->AlsoWaitForTitle(base::ASCIIToUTF16(kEmeNotSupportedError
));
128 title_watcher
->AlsoWaitForTitle(base::ASCIIToUTF16(kEmeKeyError
));
131 #if defined(OS_ANDROID)
132 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
133 command_line
->AppendSwitch(
134 switches::kDisableGestureRequirementForMediaPlayback
);
139 using ::testing::Combine
;
140 using ::testing::Values
;
142 #if !defined(OS_ANDROID)
143 // Encrypted media playback with SRC is not supported on Android.
144 INSTANTIATE_TEST_CASE_P(SRC_ClearKey
, EncryptedMediaTest
,
145 Combine(Values(kClearKeyKeySystem
), Values(SRC
)));
146 #endif // !defined(OS_ANDROID)
148 INSTANTIATE_TEST_CASE_P(MSE_ClearKey
, EncryptedMediaTest
,
149 Combine(Values(kClearKeyKeySystem
), Values(MSE
)));
151 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest
, Playback_AudioOnly_WebM
) {
152 TestSimplePlayback("bear-a_enc-a.webm", kWebMAudioOnly
);
155 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest
, Playback_AudioClearVideo_WebM
) {
156 TestSimplePlayback("bear-320x240-av_enc-a.webm", kWebMAudioVideo
);
159 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest
, Playback_VideoAudio_WebM
) {
160 TestSimplePlayback("bear-320x240-av_enc-av.webm", kWebMAudioVideo
);
163 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest
, Playback_VideoOnly_WebM
) {
164 TestSimplePlayback("bear-320x240-v_enc-v.webm", kWebMVideoOnly
);
167 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest
, Playback_VideoClearAudio_WebM
) {
168 TestSimplePlayback("bear-320x240-av_enc-v.webm", kWebMAudioVideo
);
171 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest
, Playback_AudioOnly_WebM_Opus
) {
172 // Opus is not supported on Android. http://crbug.com/318436
173 #if defined(OS_ANDROID)
176 TestSimplePlayback("bear-320x240-opus-a_enc-a.webm", kWebMAudioOnly
);
179 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest
, Playback_VideoAudio_WebM_Opus
) {
180 // Opus is not supported on Android. http://crbug.com/318436
181 #if defined(OS_ANDROID)
184 TestSimplePlayback("bear-320x240-opus-av_enc-av.webm", kWebMAudioVideo
);
187 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest
, Playback_VideoClearAudio_WebM_Opus
) {
188 // Opus is not supported on Android. http://crbug.com/318436
189 #if defined(OS_ANDROID)
192 TestSimplePlayback("bear-320x240-opus-av_enc-v.webm", kWebMAudioVideo
);
195 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest
, ConfigChangeVideo
) {
199 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest
, FrameSizeChangeVideo
) {
200 // Times out on Windows XP. http://crbug.com/171937
202 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
205 TestFrameSizeChange();
208 IN_PROC_BROWSER_TEST_F(EncryptedMediaTest
, UnknownKeySystemThrowsException
) {
209 RunEncryptedMediaTest(kDefaultEmePlayer
,
214 kEmeNotSupportedError
);
217 } // namespace content