1 // Copyright 2014 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 "build/build_config.h"
7 #include "chrome/browser/media/webrtc/webrtc_browsertest_base.h"
8 #include "chrome/browser/media/webrtc/webrtc_browsertest_common.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_tabstrip.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/common/content_features.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/public/test/browser_test.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "media/base/media_switches.h"
20 #include "net/test/embedded_test_server/embedded_test_server.h"
21 #include "testing/gtest/include/gtest/gtest-param-test.h"
22
23 static const char kMainWebrtcTestHtmlPage[] =
24 "/webrtc/webrtc_jsep01_test.html";
25
26 enum class TargetVideoCaptureImplementation {
27 DEFAULT,
28 #if defined(OS_WIN)
29 WIN_MEDIA_FOUNDATION
30 #endif
31 };
32
33 const TargetVideoCaptureImplementation kTargetVideoCaptureImplementations[] = {
34 TargetVideoCaptureImplementation::DEFAULT,
35 #if defined(OS_WIN)
36 TargetVideoCaptureImplementation::WIN_MEDIA_FOUNDATION
37 #endif
38 };
39
40 // These tests runs on real webcams and ensure WebRTC can acquire webcams
41 // correctly. They will do nothing if there are no webcams on the system.
42 // The webcam on the system must support up to 1080p, or the test will fail.
43 // This test is excellent for testing the various capture paths of WebRTC
44 // on all desktop platforms.
45 class WebRtcWebcamBrowserTest
46 : public WebRtcTestBase,
47 public testing::WithParamInterface<TargetVideoCaptureImplementation> {
48 public:
WebRtcWebcamBrowserTest()49 WebRtcWebcamBrowserTest() {
50 #if defined(OS_WIN)
51 if (GetParam() == TargetVideoCaptureImplementation::WIN_MEDIA_FOUNDATION) {
52 scoped_feature_list_.InitAndEnableFeature(
53 media::kMediaFoundationVideoCapture);
54 } else {
55 scoped_feature_list_.InitAndDisableFeature(
56 media::kMediaFoundationVideoCapture);
57 }
58 #endif
59 }
60
SetUpCommandLine(base::CommandLine * command_line)61 void SetUpCommandLine(base::CommandLine* command_line) override {
62 command_line->RemoveSwitch(switches::kUseFakeDeviceForMediaStream);
63 EXPECT_FALSE(command_line->HasSwitch(
64 switches::kUseFakeDeviceForMediaStream));
65 EXPECT_FALSE(command_line->HasSwitch(
66 switches::kUseFakeUIForMediaStream));
67 }
68
69 protected:
SetUpInProcessBrowserTestFixture()70 void SetUpInProcessBrowserTestFixture() override {
71 DetectErrorsInJavaScript(); // Look for errors in our rather complex js.
72 }
73
GetUserMediaAndGetStreamSize(content::WebContents * tab,const std::string & constraints)74 std::string GetUserMediaAndGetStreamSize(content::WebContents* tab,
75 const std::string& constraints) {
76 std::string actual_stream_size;
77 if (GetUserMediaWithSpecificConstraintsAndAcceptIfPrompted(tab,
78 constraints)) {
79 StartDetectingVideo(tab, "local-view");
80 if (WaitForVideoToPlay(tab))
81 actual_stream_size = GetStreamSize(tab, "local-view");
82 CloseLastLocalStream(tab);
83 }
84 return actual_stream_size;
85 }
86
87 private:
88 base::test::ScopedFeatureList scoped_feature_list_;
89 };
90
91 // This test is manual because the test results can vary heavily depending on
92 // which webcam or drivers you have on the system.
IN_PROC_BROWSER_TEST_P(WebRtcWebcamBrowserTest,MANUAL_TestAcquiringAndReacquiringWebcam)93 IN_PROC_BROWSER_TEST_P(WebRtcWebcamBrowserTest,
94 MANUAL_TestAcquiringAndReacquiringWebcam) {
95 ASSERT_TRUE(embedded_test_server()->Start());
96 GURL url(embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
97 ui_test_utils::NavigateToURL(browser(), url);
98 content::WebContents* tab =
99 browser()->tab_strip_model()->GetActiveWebContents();
100
101 if (!content::IsWebcamAvailableOnSystem(tab)) {
102 DVLOG(0) << "No webcam found on bot: skipping...";
103 return;
104 }
105
106 EXPECT_EQ("320x240",
107 GetUserMediaAndGetStreamSize(tab, kVideoCallConstraintsQVGA));
108 EXPECT_EQ("640x480",
109 GetUserMediaAndGetStreamSize(tab, kVideoCallConstraintsVGA));
110 EXPECT_EQ("640x360",
111 GetUserMediaAndGetStreamSize(tab, kVideoCallConstraints360p));
112 EXPECT_EQ("1280x720",
113 GetUserMediaAndGetStreamSize(tab, kVideoCallConstraints720p));
114 EXPECT_EQ("1920x1080",
115 GetUserMediaAndGetStreamSize(tab, kVideoCallConstraints1080p));
116 }
117
118 INSTANTIATE_TEST_SUITE_P(WebRtcWebcamBrowserTests,
119 WebRtcWebcamBrowserTest,
120 testing::ValuesIn(kTargetVideoCaptureImplementations));
121