blob: 850d16c3d42e2523e6dcb03050d405eb3dca8e05 [file] [log] [blame]
[email protected]63dc9072013-09-12 06:20:471// Copyright 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.
4
5#include "base/command_line.h"
6#include "base/path_service.h"
7#include "base/strings/utf_string_conversions.h"
8#include "base/win/windows_version.h"
9#include "chrome/browser/media/media_browsertest.h"
10#include "chrome/browser/ui/browser.h"
11#include "chrome/browser/ui/tabs/tab_strip_model.h"
12#include "chrome/common/chrome_switches.h"
13#include "content/public/test/browser_test_utils.h"
14
15#include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
16
17#if defined(WIDEVINE_CDM_AVAILABLE) && defined(OS_LINUX)
18#include <gnu/libc-version.h>
19#endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(OS_LINUX)
20
21#if defined(ENABLE_PEPPER_CDMS)
22// Platform-specific filename relative to the chrome executable.
[email protected]1825e772013-10-08 06:39:1123const char kClearKeyCdmAdapterFileName[] =
[email protected]63dc9072013-09-12 06:20:4724#if defined(OS_MACOSX)
25 "clearkeycdmadapter.plugin";
26#elif defined(OS_WIN)
27 "clearkeycdmadapter.dll";
28#elif defined(OS_POSIX)
29 "libclearkeycdmadapter.so";
30#endif
[email protected]1825e772013-10-08 06:39:1131
32const char kClearKeyCdmPluginMimeType[] = "application/x-ppapi-clearkey-cdm";
[email protected]63dc9072013-09-12 06:20:4733#endif // defined(ENABLE_PEPPER_CDMS)
34
35// Available key systems.
[email protected]095ccbe42013-09-26 00:06:4236const char kClearKeyKeySystem[] = "webkit-org.w3.clearkey";
37const char kExternalClearKeyKeySystem[] =
[email protected]63dc9072013-09-12 06:20:4738 "org.chromium.externalclearkey";
39
40// Supported media types.
[email protected]095ccbe42013-09-26 00:06:4241const char kWebMAudioOnly[] = "audio/webm; codecs=\"vorbis\"";
42const char kWebMVideoOnly[] = "video/webm; codecs=\"vp8\"";
43const char kWebMAudioVideo[] = "video/webm; codecs=\"vorbis, vp8\"";
44#if defined(USE_PROPRIETARY_CODECS)
45const char kMP4AudioOnly[] = "audio/mp4; codecs=\"mp4a.40.2\"";
46const char kMP4VideoOnly[] = "video/mp4; codecs=\"avc1.4D4041\"";
47#endif // defined(USE_PROPRIETARY_CODECS)
[email protected]63dc9072013-09-12 06:20:4748
[email protected]2e850f12013-09-18 01:27:2949// EME-specific test results and errors.
[email protected]095ccbe42013-09-26 00:06:4250const char kEmeGkrException[] = "GENERATE_KEY_REQUEST_EXCEPTION";
51const char kEmeKeyError[] = "KEYERROR";
[email protected]63dc9072013-09-12 06:20:4752
53// The type of video src used to load media.
54enum SrcType {
55 SRC,
56 MSE
57};
58
[email protected]63dc9072013-09-12 06:20:4759// Tests encrypted media playback with a combination of parameters:
60// - char*: Key system name.
61// - bool: True to load media using MSE, otherwise use src.
62class EncryptedMediaTest : public MediaBrowserTest,
63 public testing::WithParamInterface<std::tr1::tuple<const char*, SrcType> > {
64 public:
[email protected]1825e772013-10-08 06:39:1165 // Can only be used in parameterized (*_P) tests.
66 const char* CurrentKeySystem() {
67 return std::tr1::get<0>(GetParam());
[email protected]63dc9072013-09-12 06:20:4768 }
69
[email protected]1825e772013-10-08 06:39:1170 // Can only be used in parameterized (*_P) tests.
71 SrcType CurrentSourceType() {
72 return std::tr1::get<1>(GetParam());
[email protected]63dc9072013-09-12 06:20:4773 }
74
[email protected]1825e772013-10-08 06:39:1175#if defined(WIDEVINE_CDM_AVAILABLE)
76 bool IsWidevine(const char* key_system) {
77 return (strcmp(key_system, kWidevineKeySystem) == 0);
78 }
79#endif // defined(WIDEVINE_CDM_AVAILABLE)
80
81 void TestSimplePlayback(const char* encrypted_media, const char* media_type) {
82 RunSimpleEncryptedMediaTest(
83 encrypted_media, media_type, CurrentKeySystem(), CurrentSourceType());
84 }
85
86 void TestFrameSizeChange() {
87#if defined(WIDEVINE_CDM_AVAILABLE)
88 if (IsWidevine(CurrentKeySystem())) {
89 LOG(INFO) << "FrameSizeChange test cannot run with Widevine.";
90 return;
91 }
92#endif // defined(WIDEVINE_CDM_AVAILABLE)
[email protected]63dc9072013-09-12 06:20:4793 RunEncryptedMediaTest("encrypted_frame_size_change.html",
94 "frame_size_change-av-enc-v.webm", kWebMAudioVideo,
[email protected]1825e772013-10-08 06:39:1195 CurrentKeySystem(), CurrentSourceType(), kEnded);
[email protected]63dc9072013-09-12 06:20:4796 }
97
[email protected]1825e772013-10-08 06:39:1198 void TestConfigChange() {
99#if defined(WIDEVINE_CDM_AVAILABLE)
100 if (IsWidevine(CurrentKeySystem())) {
101 LOG(INFO) << "ConfigChange test cannot run with Widevine.";
102 return;
103 }
104#endif // defined(WIDEVINE_CDM_AVAILABLE)
[email protected]63dc9072013-09-12 06:20:47105 std::vector<StringPair> query_params;
[email protected]1825e772013-10-08 06:39:11106 query_params.push_back(std::make_pair("keysystem", CurrentKeySystem()));
[email protected]63dc9072013-09-12 06:20:47107 query_params.push_back(std::make_pair("runencrypted", "1"));
[email protected]1825e772013-10-08 06:39:11108 RunMediaTestPage("mse_config_change.html", &query_params, kEnded, true);
[email protected]63dc9072013-09-12 06:20:47109 }
110
[email protected]1825e772013-10-08 06:39:11111 void RunEncryptedMediaTest(const char* html_page,
112 const char* media_file,
113 const char* media_type,
114 const char* key_system,
115 SrcType src_type,
116 const char* expectation) {
[email protected]63dc9072013-09-12 06:20:47117 std::vector<StringPair> query_params;
118 query_params.push_back(std::make_pair("mediafile", media_file));
119 query_params.push_back(std::make_pair("mediatype", media_type));
120 query_params.push_back(std::make_pair("keysystem", key_system));
121 if (src_type == MSE)
122 query_params.push_back(std::make_pair("usemse", "1"));
123 RunMediaTestPage(html_page, &query_params, expectation, true);
124 }
125
[email protected]1825e772013-10-08 06:39:11126 void RunSimpleEncryptedMediaTest(const char* media_file,
127 const char* media_type,
128 const char* key_system,
129 SrcType src_type) {
130#if defined(WIDEVINE_CDM_AVAILABLE)
131 if (IsWidevine(key_system)) {
132 // Tests that the following happen after trying to play encrypted media:
133 // - webkitneedkey event is fired.
134 // - webkitGenerateKeyRequest() does not fail.
135 // - webkitkeymessage is fired.
136 // - webkitAddKey() triggers a WebKitKeyError since no real key is added.
137 // TODO(shadi): Remove after bots upgrade to precise.
138 // Don't run on lucid bots since the CDM is not compatible (glibc < 2.14)
139#if defined(OS_LINUX)
140 if (strcmp(gnu_get_libc_version(), "2.11.1") == 0) {
141 LOG(INFO) << "Skipping test; not supported on glibc version: "
142 << gnu_get_libc_version();
143 return;
144 }
145#endif // defined(OS_LINUX)
146
147 RunEncryptedMediaTest("encrypted_media_player.html", media_file,
148 media_type, key_system, src_type, kEmeKeyError);
149
150 bool receivedKeyMessage = false;
151 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
152 browser()->tab_strip_model()->GetActiveWebContents(),
153 "window.domAutomationController.send(video.receivedKeyMessage);",
154 &receivedKeyMessage));
155 EXPECT_TRUE(receivedKeyMessage);
156 return;
157 }
158#endif // defined(WIDEVINE_CDM_AVAILABLE)
159
160 RunEncryptedMediaTest("encrypted_media_player.html", media_file,
161 media_type, key_system, src_type, kEnded);
162 }
163
[email protected]63dc9072013-09-12 06:20:47164 protected:
[email protected]2e850f12013-09-18 01:27:29165 // We want to fail quickly when a test fails because an error is encountered.
166 virtual void AddWaitForTitles(content::TitleWatcher* title_watcher) OVERRIDE {
167 MediaBrowserTest::AddWaitForTitles(title_watcher);
168 title_watcher->AlsoWaitForTitle(ASCIIToUTF16(kEmeGkrException));
169 title_watcher->AlsoWaitForTitle(ASCIIToUTF16(kEmeKeyError));
170 }
171
[email protected]63dc9072013-09-12 06:20:47172 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
[email protected]1825e772013-10-08 06:39:11173#if defined(ENABLE_PEPPER_CDMS)
[email protected]63dc9072013-09-12 06:20:47174 RegisterPepperCdm(command_line, kClearKeyCdmAdapterFileName,
175 kExternalClearKeyKeySystem);
[email protected]1825e772013-10-08 06:39:11176#if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
177 RegisterPepperCdm(command_line, kWidevineCdmAdapterFileName,
178 kWidevineKeySystem);
179#endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
180#endif // defined(ENABLE_PEPPER_CDMS) }
181
182#if defined(OS_ANDROID)
183 command_line->AppendSwitch(
184 switches::kDisableGestureRequirementForMediaPlayback);
185#endif // defined(OS_ANDROID)
186
[email protected]63dc9072013-09-12 06:20:47187#if defined(OS_CHROMEOS)
[email protected]1825e772013-10-08 06:39:11188 // Disable GPU video decoder on ChromeOS since these tests run on
189 // linux_chromeos which does not support GPU video decoder.
[email protected]63dc9072013-09-12 06:20:47190 command_line->AppendSwitch(switches::kDisableAcceleratedVideoDecode);
191#endif // defined(OS_CHROMEOS)
192 }
193
[email protected]1825e772013-10-08 06:39:11194#if defined(ENABLE_PEPPER_CDMS)
[email protected]63dc9072013-09-12 06:20:47195 void RegisterPepperCdm(CommandLine* command_line,
196 const std::string& adapter_name,
197 const std::string& key_system) {
198 // Append the switch to register the Clear Key CDM Adapter.
199 base::FilePath plugin_dir;
200 EXPECT_TRUE(PathService::Get(base::DIR_MODULE, &plugin_dir));
201 base::FilePath plugin_lib = plugin_dir.AppendASCII(adapter_name);
[email protected]1825e772013-10-08 06:39:11202 EXPECT_TRUE(base::PathExists(plugin_lib)) << plugin_lib.value();
[email protected]63dc9072013-09-12 06:20:47203 base::FilePath::StringType pepper_plugin = plugin_lib.value();
204 pepper_plugin.append(FILE_PATH_LITERAL("#CDM#0.1.0.0;"));
205#if defined(OS_WIN)
206 pepper_plugin.append(ASCIIToWide(GetPepperType(key_system)));
207#else
208 pepper_plugin.append(GetPepperType(key_system));
209#endif
210 command_line->AppendSwitchNative(switches::kRegisterPepperPlugins,
211 pepper_plugin);
212 }
213
214 // Adapted from key_systems.cc.
215 std::string GetPepperType(const std::string& key_system) {
216 if (key_system == kExternalClearKeyKeySystem)
[email protected]1825e772013-10-08 06:39:11217 return kClearKeyCdmPluginMimeType;
[email protected]63dc9072013-09-12 06:20:47218#if defined(WIDEVINE_CDM_AVAILABLE)
219 if (key_system == kWidevineKeySystem)
[email protected]1825e772013-10-08 06:39:11220 return kWidevineCdmPluginMimeType;
[email protected]63dc9072013-09-12 06:20:47221#endif // WIDEVINE_CDM_AVAILABLE
222
223 NOTREACHED();
224 return "";
225 }
226#endif // defined(ENABLE_PEPPER_CDMS)
227};
228
[email protected]63dc9072013-09-12 06:20:47229INSTANTIATE_TEST_CASE_P(ClearKey, EncryptedMediaTest,
230 ::testing::Combine(
231 ::testing::Values(kClearKeyKeySystem), ::testing::Values(SRC, MSE)));
232
233// External Clear Key is currently only used on platforms that use Pepper CDMs.
234#if defined(ENABLE_PEPPER_CDMS)
235INSTANTIATE_TEST_CASE_P(ExternalClearKey, EncryptedMediaTest,
236 ::testing::Combine(
237 ::testing::Values(kExternalClearKeyKeySystem),
238 ::testing::Values(SRC, MSE)));
239
[email protected]1825e772013-10-08 06:39:11240#if defined(WIDEVINE_CDM_AVAILABLE)
241// This test doesn't fully test playback with Widevine. So we only run Widevine
242// test with MSE (no SRC) to reduce test time.
243INSTANTIATE_TEST_CASE_P(Widevine, EncryptedMediaTest,
244 ::testing::Combine(
245 ::testing::Values(kWidevineKeySystem), ::testing::Values(MSE)));
246#endif // defined(WIDEVINE_CDM_AVAILABLE)
[email protected]63dc9072013-09-12 06:20:47247#endif // defined(ENABLE_PEPPER_CDMS)
248
[email protected]63dc9072013-09-12 06:20:47249IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_AudioOnly_WebM) {
[email protected]1825e772013-10-08 06:39:11250 TestSimplePlayback("bear-a-enc_a.webm", kWebMAudioOnly);
[email protected]63dc9072013-09-12 06:20:47251}
252
253IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_AudioClearVideo_WebM) {
[email protected]1825e772013-10-08 06:39:11254 TestSimplePlayback("bear-320x240-av-enc_a.webm", kWebMAudioVideo);
[email protected]63dc9072013-09-12 06:20:47255}
256
257IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoAudio_WebM) {
[email protected]1825e772013-10-08 06:39:11258 TestSimplePlayback("bear-320x240-av-enc_av.webm", kWebMAudioVideo);
[email protected]63dc9072013-09-12 06:20:47259}
260
261IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoOnly_WebM) {
[email protected]1825e772013-10-08 06:39:11262 TestSimplePlayback("bear-320x240-v-enc_v.webm", kWebMVideoOnly);
[email protected]63dc9072013-09-12 06:20:47263}
264
265IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoClearAudio_WebM) {
[email protected]1825e772013-10-08 06:39:11266 TestSimplePlayback("bear-320x240-av-enc_v.webm", kWebMAudioVideo);
[email protected]63dc9072013-09-12 06:20:47267}
268
[email protected]1825e772013-10-08 06:39:11269IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, ConfigChangeVideo) {
270 TestConfigChange();
271}
272
273IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, FrameSizeChangeVideo) {
[email protected]63dc9072013-09-12 06:20:47274 // Times out on Windows XP. http://crbug.com/171937
275#if defined(OS_WIN)
276 if (base::win::GetVersion() < base::win::VERSION_VISTA)
277 return;
278#endif
[email protected]1825e772013-10-08 06:39:11279 TestFrameSizeChange();
[email protected]63dc9072013-09-12 06:20:47280}
281
282#if defined(USE_PROPRIETARY_CODECS)
283IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoOnly_MP4) {
[email protected]63dc9072013-09-12 06:20:47284 // MP4 without MSE is not support yet, http://crbug.com/170793.
[email protected]1825e772013-10-08 06:39:11285 if (CurrentSourceType() != MSE) {
[email protected]63dc9072013-09-12 06:20:47286 LOG(INFO) << "Skipping test; Can only play MP4 encrypted streams by MSE.";
287 return;
288 }
[email protected]1825e772013-10-08 06:39:11289 TestSimplePlayback("bear-640x360-v_frag-cenc.mp4", kMP4VideoOnly);
[email protected]63dc9072013-09-12 06:20:47290}
291
292IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_AudioOnly_MP4) {
[email protected]63dc9072013-09-12 06:20:47293 // MP4 without MSE is not support yet, http://crbug.com/170793.
[email protected]1825e772013-10-08 06:39:11294 if (CurrentSourceType() != MSE) {
[email protected]63dc9072013-09-12 06:20:47295 LOG(INFO) << "Skipping test; Can only play MP4 encrypted streams by MSE.";
296 return;
297 }
[email protected]1825e772013-10-08 06:39:11298 TestSimplePlayback("bear-640x360-a_frag-cenc.mp4", kMP4AudioOnly);
[email protected]63dc9072013-09-12 06:20:47299}
300#endif // defined(USE_PROPRIETARY_CODECS)
301
[email protected]63dc9072013-09-12 06:20:47302#if defined(WIDEVINE_CDM_AVAILABLE)
303// The parent key system cannot be used in generateKeyRequest.
[email protected]1825e772013-10-08 06:39:11304IN_PROC_BROWSER_TEST_F(EncryptedMediaTest, WVParentThrowsException) {
[email protected]63dc9072013-09-12 06:20:47305 RunEncryptedMediaTest("encrypted_media_player.html", "bear-a-enc_a.webm",
[email protected]2e850f12013-09-18 01:27:29306 kWebMAudioOnly, "com.widevine", SRC, kEmeGkrException);
[email protected]63dc9072013-09-12 06:20:47307}
[email protected]63dc9072013-09-12 06:20:47308#endif // defined(WIDEVINE_CDM_AVAILABLE)