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
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450
  451
  452
  453
  454
  455
  456
  457
  458
  459
  460
  461
  462
  463
  464
  465
  466
  467
  468
  469
  470
  471
  472
  473
  474
  475
  476
  477
  478
  479
  480
  481
  482
  483
  484
  485
  486

content / browser / media / media_browsertest.cc [blame]

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/media/media_browsertest.h"

#include <memory>
#include <string_view>

#include "base/command_line.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "content/public/browser/gpu_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/shell/common/shell_switches.h"
#include "media/audio/audio_features.h"
#include "media/base/media_switches.h"
#include "media/base/supported_types.h"
#include "media/base/test_data_util.h"
#include "media/media_buildflags.h"
#include "media/mojo/services/gpu_mojo_media_client_test_util.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "url/url_util.h"

// Proprietary codecs require acceleration on Android.
#if BUILDFLAG(IS_ANDROID) && !BUILDFLAG(ENABLE_FFMPEG_VIDEO_DECODERS)
#define REQUIRE_ACCELERATION_ON_ANDROID() \
  if (!is_accelerated())                  \
  return
#else
#define REQUIRE_ACCELERATION_ON_ANDROID()
#endif  // BUILDFLAG(IS_ANDROID)

namespace content {

#if BUILDFLAG(IS_ANDROID)
// Title set by android cleaner page after short timeout.
const char16_t kClean[] = u"CLEAN";
#endif

void MediaBrowserTest::SetUpCommandLine(base::CommandLine* command_line) {
  command_line->AppendSwitchASCII(
      switches::kAutoplayPolicy,
      switches::autoplay::kNoUserGestureRequiredPolicy);
  command_line->AppendSwitch(switches::kExposeInternalsForTesting);

  std::vector<base::test::FeatureRef> enabled_features = {
    media::kBuiltInH264Decoder,

#if BUILDFLAG(IS_ANDROID)
    features::kLogJsConsoleMessages,
#endif

#if BUILDFLAG(ENABLE_HLS_DEMUXER) && BUILDFLAG(USE_PROPRIETARY_CODECS)
    media::kBuiltInHlsPlayer,
#endif
  };

  std::vector<base::test::FeatureRef> disabled_features = {
    // Disable fallback after decode error to avoid unexpected test pass on
    // the fallback path.
    media::kFallbackAfterDecodeError,

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
    // Disable out of process audio on Linux due to process spawn
    // failures. http://crbug.com/986021
    features::kAudioServiceOutOfProcess,
#endif
  };

  scoped_feature_list_.InitWithFeatures(enabled_features, disabled_features);
}

void MediaBrowserTest::RunMediaTestPage(const std::string& html_page,
                                        const base::StringPairs& query_params,
                                        const std::string& expected_title,
                                        bool http) {
  GURL gurl;
  std::string query = media::GetURLQueryString(query_params);
  std::unique_ptr<net::EmbeddedTestServer> http_test_server;
  if (http) {
    http_test_server = std::make_unique<net::EmbeddedTestServer>();
    http_test_server->ServeFilesFromSourceDirectory(media::GetTestDataPath());
    CHECK(http_test_server->Start());
    gurl = http_test_server->GetURL("/" + html_page + "?" + query);
  } else {
    gurl = content::GetFileUrlWithQuery(media::GetTestDataFilePath(html_page),
                                        query);
  }
  std::string final_title = RunTest(gurl, expected_title);
  EXPECT_EQ(expected_title, final_title);
}

std::string MediaBrowserTest::RunTest(const GURL& gurl,
                                      const std::string& expected_title) {
  VLOG(0) << "Running test URL: " << gurl;
  TitleWatcher title_watcher(shell()->web_contents(),
                             base::ASCIIToUTF16(expected_title));
  AddTitlesToAwait(&title_watcher);
  EXPECT_TRUE(NavigateToURL(shell(), gurl));
  std::u16string result = title_watcher.WaitAndGetTitle();

  CleanupTest();
  return base::UTF16ToASCII(result);
}

void MediaBrowserTest::CleanupTest() {
#if BUILDFLAG(IS_ANDROID)
  // We only do this cleanup on Android, as a workaround for a test-only OOM
  // bug. See http://crbug.com/727542
  const std::u16string cleaner_title = kClean;
  TitleWatcher clean_title_watcher(shell()->web_contents(), cleaner_title);
  GURL cleaner_url = content::GetFileUrlWithQuery(
      media::GetTestDataFilePath("cleaner.html"), "");
  EXPECT_TRUE(NavigateToURL(shell(), cleaner_url));
  std::u16string cleaner_result = clean_title_watcher.WaitAndGetTitle();
  EXPECT_EQ(cleaner_result, cleaner_title);
#endif
}

std::string MediaBrowserTest::EncodeErrorMessage(
    const std::string& original_message) {
  url::RawCanonOutputT<char> buffer;
  url::EncodeURIComponent(original_message, &buffer);
  return std::string(buffer.view());
}

void MediaBrowserTest::AddTitlesToAwait(content::TitleWatcher* title_watcher) {
  title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(media::kEndedTitle));
  title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(media::kErrorTitle));
  title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(media::kErrorEventTitle));
  title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(media::kFailedTitle));
}

void MediaBrowserTest::PreRunTestOnMainThread() {
  ContentBrowserTest::PreRunTestOnMainThread();
  media::AddSupplementalCodecsForTesting(GetGpuPreferencesFromCommandLine());
}

// Tests playback and seeking of an audio or video file. Test starts with
// playback then, after X seconds or the ended event fires, seeks near end of
// file; see player.html for details. The test completes when either the last
// 'ended' or an 'error' event fires.
class MediaTest : public testing::WithParamInterface<bool>,
                  public MediaBrowserTest {
 public:
  bool is_accelerated() { return GetParam(); }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    if (!is_accelerated())
      command_line->AppendSwitch(switches::kDisableAcceleratedVideoDecode);
    MediaBrowserTest::SetUpCommandLine(command_line);
  }

  void MaybePlayVideo(std::string_view codec_string,
                      const std::string& file_name) {
    constexpr char kTestVideoPlaybackScript[] = R"({
      const video = document.createElement('video');
      video.canPlayType('video/mp4; codecs=$1') === 'probably';
    })";
    EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
    content::WebContents* web_contents = shell()->web_contents();
    bool result =
        EvalJs(web_contents, JsReplace(kTestVideoPlaybackScript, codec_string))
            .ExtractBool();
    if (!result) {
      return;
    }

    PlayVideo(file_name);
  }

  // Play specified audio over http:// or file:// depending on |http| setting.
  void PlayAudio(const std::string& media_file, bool http = true) {
    PlayMedia("audio", media_file, http);
  }

  // Play specified video over http:// or file:// depending on |http| setting.
  void PlayVideo(const std::string& media_file, bool http = true) {
    PlayMedia("video", media_file, http);
  }

  void PlayMedia(const std::string& tag,
                 const std::string& media_file,
                 bool http) {
    base::StringPairs query_params;
    query_params.emplace_back(tag, media_file);
    RunMediaTestPage("player.html", query_params, media::kEndedTitle, http);
  }

  void RunErrorMessageTest(const std::string& tag,
                           const std::string& media_file,
                           const std::string& expected_error_substring) {
    base::StringPairs query_params;
    query_params.emplace_back(tag, media_file);
    query_params.emplace_back("error_substr",
                              EncodeErrorMessage(expected_error_substring));
    RunMediaTestPage("player.html", query_params, media::kErrorEventTitle,
                     true);
  }

  void RunVideoSizeTest(const char* media_file, int width, int height) {
    std::string expected_title = std::string(media::kEndedTitle) + " " +
                                 base::NumberToString(width) + " " +
                                 base::NumberToString(height);
    base::StringPairs query_params;
    query_params.emplace_back("video", media_file);
    query_params.emplace_back("sizetest", "true");
    RunMediaTestPage("player.html", query_params, expected_title, true);
  }
};

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWebm) {
  PlayVideo("bear.webm");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWebm_FileProtocol) {
  PlayVideo("bear.webm", false);
}

IN_PROC_BROWSER_TEST_P(MediaTest, AudioBearOpusWebm) {
  PlayAudio("bear-opus.webm");
}

IN_PROC_BROWSER_TEST_P(MediaTest, AudioBearOpusMp4) {
  PlayAudio("bear-opus.mp4");
}

IN_PROC_BROWSER_TEST_P(MediaTest, AudioBearOpusOgg) {
  PlayAudio("bear-opus.ogg");
}

IN_PROC_BROWSER_TEST_P(MediaTest, AudioBearOpusOgg_FileProtocol) {
  PlayAudio("bear-opus.ogg", false);
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearSilentWebm) {
  PlayVideo("bear_silent.webm");
}

// We don't expect android devices to support highbit yet.
#if !BUILDFLAG(IS_ANDROID)

// TODO(crbug.com/40242077): DEMUXER_ERROR_NO_SUPPORTED_STREAMS error on
// Fuchsia Arm64.
#if BUILDFLAG(IS_FUCHSIA) && defined(ARCH_CPU_ARM64)
#define MAYBE_VideoBearHighBitDepthVP9 DISABLED_VideoBearHighBitDepthVP9
#else
#define MAYBE_VideoBearHighBitDepthVP9 VideoBearHighBitDepthVP9
#endif
IN_PROC_BROWSER_TEST_P(MediaTest, MAYBE_VideoBearHighBitDepthVP9) {
  PlayVideo("bear-320x180-hi10p-vp9.webm");
}

// TODO(crbug.com/40242077): DEMUXER_ERROR_NO_SUPPORTED_STREAMS error on
// Fuchsia Arm64.
#if BUILDFLAG(IS_FUCHSIA) && defined(ARCH_CPU_ARM64)
#define MAYBE_VideoBear12DepthVP9 DISABLED_VideoBear12DepthVP9
#else
#define MAYBE_VideoBear12DepthVP9 VideoBear12DepthVP9
#endif
IN_PROC_BROWSER_TEST_P(MediaTest, MAYBE_VideoBear12DepthVP9) {
  // Hardware decode on does not reliably support 12-bit.
  if (is_accelerated())
    return;
  PlayVideo("bear-320x180-hi12p-vp9.webm");
}
#endif  // !BUILDFLAG(IS_ANDROID)

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearMp4Vp9) {
  PlayVideo("bear-320x240-v_frag-vp9.mp4");
}

IN_PROC_BROWSER_TEST_P(MediaTest, AudioBearFlacMp4) {
  PlayAudio("bear-flac.mp4");
}

IN_PROC_BROWSER_TEST_P(MediaTest, AudioBearFlac192kHzMp4) {
  PlayAudio("bear-flac-192kHz.mp4");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearMovPcmS16be) {
  PlayAudio("bear_pcm_s16be.mov");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearMovPcmS24be) {
  PlayAudio("bear_pcm_s24be.mov");
}

#if BUILDFLAG(USE_PROPRIETARY_CODECS)
#if BUILDFLAG(ENABLE_HLS_DEMUXER)

IN_PROC_BROWSER_TEST_P(MediaTest, HLSSingleFileBear) {
  REQUIRE_ACCELERATION_ON_ANDROID();
  PlayVideo("bear-1280x720-hls-clear-mpl.m3u8");
}

IN_PROC_BROWSER_TEST_P(MediaTest, HLSMultivariantBitrateBear) {
  REQUIRE_ACCELERATION_ON_ANDROID();
  PlayVideo("hls/multi-bitrate-multivariant-bear/playlist.m3u8");
}

#endif  // BUILDFLAG(ENABLE_HLS_DEMUXER)

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearMp4) {
  REQUIRE_ACCELERATION_ON_ANDROID();
  PlayVideo("bear.mp4");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearSilentMp4) {
  REQUIRE_ACCELERATION_ON_ANDROID();
  PlayVideo("bear_silent.mp4");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearRotated0) {
  REQUIRE_ACCELERATION_ON_ANDROID();
  RunVideoSizeTest("bear_rotate_0.mp4", 1280, 720);
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearRotated90) {
  REQUIRE_ACCELERATION_ON_ANDROID();
  RunVideoSizeTest("bear_rotate_90.mp4", 720, 1280);
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearRotated180) {
  REQUIRE_ACCELERATION_ON_ANDROID();
  RunVideoSizeTest("bear_rotate_180.mp4", 1280, 720);
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearRotated270) {
  REQUIRE_ACCELERATION_ON_ANDROID();
  RunVideoSizeTest("bear_rotate_270.mp4", 720, 1280);
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBear3gpAacH264) {
  REQUIRE_ACCELERATION_ON_ANDROID();
  PlayVideo("bear_h264_aac.3gp");
}

#if BUILDFLAG(ENABLE_HEVC_PARSER_AND_HW_DECODER)
// HEVC video stream with 8-bit 422 range extension profile
IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearMp4Hevc8bit422) {
  MaybePlayVideo("hev1.4.10.L93.9d.8",
                 "bear-1280x720-hevc-8bit-422-no-audio.mp4");
}

// HEVC video stream with 8-bit 444 range extension profile
IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearMp4Hevc8bit444) {
  MaybePlayVideo("hev1.4.10.L93.9e.8",
                 "bear-1280x720-hevc-8bit-444-no-audio.mp4");
}

// HEVC video stream with 10-bit 422 range extension profile
IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearMp4Hevc10bit422) {
  MaybePlayVideo("hev1.4.10.L93.9d.8",
                 "bear-1280x720-hevc-10bit-422-no-audio.mp4");
}

// HEVC video stream with 10-bit 444 range extension profile
IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearMp4Hevc10bit444) {
  MaybePlayVideo("hev1.4.10.L93.9c.8",
                 "bear-1280x720-hevc-10bit-444-no-audio.mp4");
}

// HEVC video stream with 8-bit main profile
IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearMp4Hevc8bit) {
  // TODO(crbug.com/40269930) : For Android, the `canPlayType()` test in
  // `MaybePlayVideo` should be reporting the correct status for HEVC. The below
  // `REQUIRE_ACCELERATION_ON_ANDROID` flag is a temporary fix.
  REQUIRE_ACCELERATION_ON_ANDROID();
  MaybePlayVideo("hev1.1.6.L93.90", "bear-1280x720-hevc-no-audio.mp4");
}

// HEVC video stream with 10-bit main10 profile
IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearMp4Hevc10bit) {
  // TODO(crbug.com/40269930) : For Android, the `canPlayType()` test in
  // `MaybePlayVideo` should be reporting the correct status for HEVC. The below
  // `REQUIRE_ACCELERATION_ON_ANDROID` flag is a temporary fix.
  REQUIRE_ACCELERATION_ON_ANDROID();
  MaybePlayVideo("hev1.2.4.L93.90", "bear-1280x720-hevc-10bit-no-audio.mp4");
}
#endif  // BUILDFLAG(ENABLE_HEVC_PARSER_AND_HW_DECODER)

#if BUILDFLAG(ENABLE_FFMPEG_VIDEO_DECODERS)

// Android devices usually only support baseline, main and high.
IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearHighBitDepthMp4) {
  PlayVideo("bear-320x180-hi10p.mp4");
}

#endif

// Android can't reliably load lots of videos on a page.
// See http://crbug.com/749265
// TODO(crbug.com/40774322): Flaky on Mac.
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_ANDROID)
#define MAYBE_LoadManyVideos DISABLED_LoadManyVideos
#else
#define MAYBE_LoadManyVideos LoadManyVideos
#endif
IN_PROC_BROWSER_TEST_P(MediaTest, MAYBE_LoadManyVideos) {
  // Only run this test in one configuration.
  if (is_accelerated())
    return;
  base::StringPairs query_params;
  RunMediaTestPage("load_many_videos.html", query_params, media::kEndedTitle,
                   true);
}
#endif  // BUILDFLAG(USE_PROPRIETARY_CODECS)

IN_PROC_BROWSER_TEST_P(MediaTest, AudioBearFlac) {
  PlayAudio("bear.flac");
}

IN_PROC_BROWSER_TEST_P(MediaTest, AudioBearFlacOgg) {
  PlayAudio("bear-flac.ogg");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWavAlaw) {
  PlayAudio("bear_alaw.wav");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWavMulaw) {
  PlayAudio("bear_mulaw.wav");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWavPcm) {
  PlayAudio("bear_pcm.wav");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWavPcm3kHz) {
  PlayAudio("bear_3kHz.wav");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWavPcm192kHz) {
  PlayAudio("bear_192kHz.wav");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoTulipWebm) {
  PlayVideo("tulip2.webm");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoEbu3213Primary) {
  PlayVideo("ebu-3213-e-vp9.mp4");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoErrorMissingResource) {
  RunErrorMessageTest("video", "nonexistent_file.webm",
                      "MEDIA_ELEMENT_ERROR: Format error");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoErrorEmptySrcAttribute) {
  RunErrorMessageTest("video", "", "MEDIA_ELEMENT_ERROR: Empty src attribute");
}

IN_PROC_BROWSER_TEST_P(MediaTest, VideoErrorNoSupportedStreams) {
  RunErrorMessageTest("video", "no_streams.webm",
                      "DEMUXER_ERROR_NO_SUPPORTED_STREAMS: FFmpegDemuxer: no "
                      "supported streams");
}

// Covers tear-down when navigating away as opposed to browser exiting.
IN_PROC_BROWSER_TEST_P(MediaTest, Navigate) {
  PlayVideo("bear.webm");
  EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
  EXPECT_FALSE(shell()->web_contents()->IsCrashed());
}

IN_PROC_BROWSER_TEST_P(MediaTest, AudioOnly_XHE_AAC_MP4) {
  if (media::IsSupportedAudioType(
          {media::AudioCodec::kAAC, media::AudioCodecProfile::kXHE_AAC})) {
    PlayAudio("noise-xhe-aac.mp4");
  }
}

INSTANTIATE_TEST_SUITE_P(Default, MediaTest, ::testing::Bool());

}  // namespace content