// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "base/command_line.h" #include "base/debug/trace_event_impl.h" #include "base/json/json_reader.h" #include "base/strings/stringprintf.h" #include "base/strings/utf_string_conversions.h" #include "base/values.h" #include "content/browser/web_contents/web_contents_impl.h" #include "content/public/common/content_switches.h" #include "content/public/test/browser_test_utils.h" #include "content/shell/browser/shell.h" #include "content/test/content_browser_test.h" #include "content/test/content_browser_test_utils.h" #include "media/audio/audio_manager.h" #include "net/test/embedded_test_server/embedded_test_server.h" #include "testing/perf/perf_test.h" #if defined(OS_WIN) #include "base/win/windows_version.h" #endif const char kForceIsac16K[] = #ifdef OS_ANDROID // The default audio codec, Opus, doesn't work on Android. "true"; #else "false"; #endif namespace { static const char kGetUserMediaAndStop[] = "getUserMediaAndStop"; static const char kGetUserMediaAndWaitAndStop[] = "getUserMediaAndWaitAndStop"; static const char kGetUserMediaAndAnalyseAndStop[] = "getUserMediaAndAnalyseAndStop"; std::string GenerateGetUserMediaCall(const char* function_name, int min_width, int max_width, int min_height, int max_height, int min_frame_rate, int max_frame_rate) { return base::StringPrintf( "%s({video: {mandatory: {minWidth: %d, maxWidth: %d, " "minHeight: %d, maxHeight: %d, minFrameRate: %d, maxFrameRate: %d}, " "optional: []}});", function_name, min_width, max_width, min_height, max_height, min_frame_rate, max_frame_rate); } } namespace content { class WebrtcBrowserTest: public ContentBrowserTest { public: WebrtcBrowserTest() {} virtual ~WebrtcBrowserTest() {} virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { // We need fake devices in this test since we want to run on naked VMs. We // assume these switches are set by default in content_browsertests. ASSERT_TRUE(CommandLine::ForCurrentProcess()->HasSwitch( switches::kUseFakeDeviceForMediaStream)); ASSERT_TRUE(CommandLine::ForCurrentProcess()->HasSwitch( switches::kUseFakeUIForMediaStream)); // The video playback will not work without a GPU, so force its use here. // This may not be available on all VMs though. command_line->AppendSwitch(switches::kUseGpuInTests); } void DumpChromeTraceCallback( const scoped_refptr& events, bool has_more_events) { // Convert the dump output into a correct JSON List. std::string contents = "[" + events->data() + "]"; int error_code; std::string error_message; scoped_ptr value( base::JSONReader::ReadAndReturnError(contents, base::JSON_ALLOW_TRAILING_COMMAS, &error_code, &error_message)); ASSERT_TRUE(value.get() != NULL) << error_message; EXPECT_EQ(value->GetType(), base::Value::TYPE_LIST); base::ListValue* values; ASSERT_TRUE(value->GetAsList(&values)); int duration_ns = 0; std::string samples_duration; double timestamp_ns = 0.0; double previous_timestamp_ns = 0.0; std::string samples_interarrival_ns; for (ListValue::iterator it = values->begin(); it != values->end(); ++it) { const DictionaryValue* dict; EXPECT_TRUE((*it)->GetAsDictionary(&dict)); if (dict->GetInteger("dur", &duration_ns)) samples_duration.append(base::StringPrintf("%d,", duration_ns)); if (dict->GetDouble("ts", ×tamp_ns)) { if (previous_timestamp_ns) { samples_interarrival_ns.append( base::StringPrintf("%f,", timestamp_ns - previous_timestamp_ns)); } previous_timestamp_ns = timestamp_ns; } } ASSERT_GT(samples_duration.size(), 0u) << "Could not collect any samples during test, this is bad"; perf_test::PrintResultList("video_capture", "", "sample_duration", samples_duration, "ns", true); perf_test::PrintResultList("video_capture", "", "interarrival_time", samples_interarrival_ns, "ns", true); } protected: bool ExecuteJavascript(const std::string& javascript) { return ExecuteScript(shell()->web_contents(), javascript); } void ExpectTitle(const std::string& expected_title) const { base::string16 expected_title16(ASCIIToUTF16(expected_title)); TitleWatcher title_watcher(shell()->web_contents(), expected_title16); EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); } }; // These tests will all make a getUserMedia call with different constraints and // see that the success callback is called. If the error callback is called or // none of the callbacks are called the tests will simply time out and fail. IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, GetVideoStreamAndStop) { ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); GURL url(http://code.qt.io/embedded_test_server()->GetURL("/media/getusermedia.html")); NavigateToURL(shell(), url); EXPECT_TRUE(ExecuteJavascript( base::StringPrintf("%s({video: true});", kGetUserMediaAndStop))); ExpectTitle("OK"); } IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, GetAudioAndVideoStreamAndStop) { ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); GURL url(http://code.qt.io/embedded_test_server()->GetURL("/media/getusermedia.html")); NavigateToURL(shell(), url); EXPECT_TRUE(ExecuteJavascript(base::StringPrintf( "%s({video: true, audio: true});", kGetUserMediaAndStop))); ExpectTitle("OK"); } IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, GetAudioAndVideoStreamAndClone) { ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); GURL url(http://code.qt.io/embedded_test_server()->GetURL("/media/getusermedia.html")); NavigateToURL(shell(), url); EXPECT_TRUE(ExecuteJavascript("getUserMediaAndClone();")); ExpectTitle("OK"); } #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) // Timing out on ARM linux bot: http://crbug.com/238490 #define MAYBE_CanSetupVideoCall DISABLED_CanSetupVideoCall #else #define MAYBE_CanSetupVideoCall CanSetupVideoCall #endif // These tests will make a complete PeerConnection-based call and verify that // video is playing for the call. IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, MAYBE_CanSetupVideoCall) { ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); GURL url(http://code.qt.io/embedded_test_server()->GetURL("/media/peerconnection-call.html")); NavigateToURL(shell(), url); EXPECT_TRUE(ExecuteJavascript("call({video: true});")); ExpectTitle("OK"); } // This test will make a simple getUserMedia page, verify that video is playing // in a simple local