Home | History | Annotate | Download | only in media
      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.
      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 "content/browser/media/media_browsertest.h"
     10 #include "content/public/common/content_switches.h"
     11 #include "content/public/test/browser_test_utils.h"
     12 #include "content/shell/browser/shell.h"
     13 #if defined(OS_ANDROID)
     14 #include "base/android/build_info.h"
     15 #endif
     16 
     17 // Available key systems.
     18 const char kClearKeyKeySystem[] = "webkit-org.w3.clearkey";
     19 
     20 // Supported media types.
     21 const char kWebMAudioOnly[] = "audio/webm; codecs=\"vorbis\"";
     22 const char kWebMVideoOnly[] = "video/webm; codecs=\"vp8\"";
     23 const char kWebMAudioVideo[] = "video/webm; codecs=\"vorbis, vp8\"";
     24 
     25 // EME-specific test results and errors.
     26 const char kEmeKeyError[] = "KEYERROR";
     27 const char kEmeNotSupportedError[] = "NOTSUPPORTEDERROR";
     28 
     29 // The type of video src used to load media.
     30 enum SrcType {
     31   SRC,
     32   MSE
     33 };
     34 
     35 namespace content {
     36 
     37 // MSE is available on all desktop platforms and on Android 4.1 and later.
     38 static bool IsMSESupported() {
     39 #if defined(OS_ANDROID)
     40   if (base::android::BuildInfo::GetInstance()->sdk_int() < 16) {
     41     VLOG(0) << "MSE is only supported in Android 4.1 and later.";
     42     return false;
     43   }
     44 #endif  // defined(OS_ANDROID)
     45   return true;
     46 }
     47 
     48 // Tests encrypted media playback with a combination of parameters:
     49 // - char*: Key system name.
     50 // - bool: True to load media using MSE, otherwise use src.
     51 class EncryptedMediaTest : public content::MediaBrowserTest,
     52     public testing::WithParamInterface<std::tr1::tuple<const char*, SrcType> > {
     53  public:
     54   // Can only be used in parameterized (*_P) tests.
     55   const char* CurrentKeySystem() {
     56     return std::tr1::get<0>(GetParam());
     57   }
     58 
     59   // Can only be used in parameterized (*_P) tests.
     60   SrcType CurrentSourceType() {
     61     return std::tr1::get<1>(GetParam());
     62   }
     63 
     64   void TestSimplePlayback(const char* encrypted_media, const char* media_type) {
     65     RunSimpleEncryptedMediaTest(
     66         encrypted_media, media_type, CurrentKeySystem(), CurrentSourceType());
     67   }
     68 
     69   void TestFrameSizeChange() {
     70     RunEncryptedMediaTest("encrypted_frame_size_change.html",
     71                           "frame_size_change-av-enc-v.webm", kWebMAudioVideo,
     72                           CurrentKeySystem(), CurrentSourceType(), kEnded);
     73   }
     74 
     75   void TestConfigChange() {
     76     if (CurrentSourceType() != MSE || !IsMSESupported()) {
     77       VLOG(0) << "Skipping test - config change test requires MSE.";
     78       return;
     79     }
     80 
     81     std::vector<StringPair> query_params;
     82     query_params.push_back(std::make_pair("keysystem", CurrentKeySystem()));
     83     query_params.push_back(std::make_pair("runencrypted", "1"));
     84     RunMediaTestPage("mse_config_change.html", &query_params, kEnded, true);
     85   }
     86 
     87   void RunEncryptedMediaTest(const char* html_page,
     88                              const char* media_file,
     89                              const char* media_type,
     90                              const char* key_system,
     91                              SrcType src_type,
     92                              const char* expectation) {
     93     if (src_type == MSE && !IsMSESupported()) {
     94       VLOG(0) << "Skipping test - MSE not supported.";
     95       return;
     96     }
     97 
     98     std::vector<StringPair> query_params;
     99     query_params.push_back(std::make_pair("mediafile", media_file));
    100     query_params.push_back(std::make_pair("mediatype", media_type));
    101     query_params.push_back(std::make_pair("keysystem", key_system));
    102     if (src_type == MSE)
    103       query_params.push_back(std::make_pair("usemse", "1"));
    104     RunMediaTestPage(html_page, &query_params, expectation, true);
    105   }
    106 
    107   void RunSimpleEncryptedMediaTest(const char* media_file,
    108                                    const char* media_type,
    109                                    const char* key_system,
    110                                    SrcType src_type) {
    111     RunEncryptedMediaTest("encrypted_media_player.html", media_file,
    112                           media_type, key_system, src_type, kEnded);
    113   }
    114 
    115  protected:
    116   // We want to fail quickly when a test fails because an error is encountered.
    117   virtual void AddWaitForTitles(content::TitleWatcher* title_watcher) OVERRIDE {
    118     MediaBrowserTest::AddWaitForTitles(title_watcher);
    119     title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(kEmeNotSupportedError));
    120     title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(kEmeKeyError));
    121   }
    122 
    123 #if defined(OS_ANDROID)
    124   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    125     command_line->AppendSwitch(
    126         switches::kDisableGestureRequirementForMediaPlayback);
    127   }
    128 #endif
    129 };
    130 
    131 using ::testing::Combine;
    132 using ::testing::Values;
    133 
    134 #if !defined(OS_ANDROID)
    135 // Encrypted media playback with SRC is not supported on Android.
    136 INSTANTIATE_TEST_CASE_P(SRC_ClearKey, EncryptedMediaTest,
    137                         Combine(Values(kClearKeyKeySystem), Values(SRC)));
    138 #endif  // !defined(OS_ANDROID)
    139 
    140 INSTANTIATE_TEST_CASE_P(MSE_ClearKey, EncryptedMediaTest,
    141                         Combine(Values(kClearKeyKeySystem), Values(MSE)));
    142 
    143 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_AudioOnly_WebM) {
    144   TestSimplePlayback("bear-a-enc_a.webm", kWebMAudioOnly);
    145 }
    146 
    147 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_AudioClearVideo_WebM) {
    148   TestSimplePlayback("bear-320x240-av-enc_a.webm", kWebMAudioVideo);
    149 }
    150 
    151 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoAudio_WebM) {
    152   TestSimplePlayback("bear-320x240-av-enc_av.webm", kWebMAudioVideo);
    153 }
    154 
    155 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoOnly_WebM) {
    156   TestSimplePlayback("bear-320x240-v-enc_v.webm", kWebMVideoOnly);
    157 }
    158 
    159 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoClearAudio_WebM) {
    160   TestSimplePlayback("bear-320x240-av-enc_v.webm", kWebMAudioVideo);
    161 }
    162 
    163 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, ConfigChangeVideo) {
    164   TestConfigChange();
    165 }
    166 
    167 IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, FrameSizeChangeVideo) {
    168   // Times out on Windows XP. http://crbug.com/171937
    169 #if defined(OS_WIN)
    170   if (base::win::GetVersion() < base::win::VERSION_VISTA)
    171     return;
    172 #endif
    173   TestFrameSizeChange();
    174 }
    175 
    176 IN_PROC_BROWSER_TEST_F(EncryptedMediaTest, UnknownKeySystemThrowsException) {
    177   RunEncryptedMediaTest("encrypted_media_player.html", "bear-a-enc_a.webm",
    178                         kWebMAudioOnly, "com.example.foo", MSE,
    179                         kEmeNotSupportedError);
    180 }
    181 
    182 }  // namespace content
    183