1 // 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.
5 #include "base/environment.h"
6 #include "base/file_util.h"
7 #include "base/path_service.h"
8 #include "base/process/launch.h"
9 #include "base/strings/string_split.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/test/test_timeouts.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/infobars/infobar.h"
15 #include "chrome/browser/infobars/infobar_service.h"
16 #include "chrome/browser/media/media_stream_infobar_delegate.h"
17 #include "chrome/browser/media/webrtc_browsertest_base.h"
18 #include "chrome/browser/media/webrtc_browsertest_common.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_tabstrip.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/test/base/in_process_browser_test.h"
25 #include "chrome/test/base/ui_test_utils.h"
26 #include "chrome/test/ui/ui_test.h"
27 #include "content/public/browser/notification_service.h"
28 #include "content/public/test/browser_test_utils.h"
29 #include "media/base/media_switches.h"
30 #include "net/test/embedded_test_server/embedded_test_server.h"
31 #include "net/test/python_utils.h"
32 #include "testing/perf/perf_test.h"
34 static const base::FilePath::CharType kFrameAnalyzerExecutable
[] =
36 FILE_PATH_LITERAL("frame_analyzer.exe");
38 FILE_PATH_LITERAL("frame_analyzer");
41 static const base::FilePath::CharType kArgbToI420ConverterExecutable
[] =
43 FILE_PATH_LITERAL("rgba_to_i420_converter.exe");
45 FILE_PATH_LITERAL("rgba_to_i420_converter");
48 static const char kHomeEnvName
[] =
55 // The working dir should be in the user's home folder.
56 static const base::FilePath::CharType kWorkingDirName
[] =
57 FILE_PATH_LITERAL("webrtc_video_quality");
58 static const base::FilePath::CharType kReferenceYuvFileName
[] =
59 FILE_PATH_LITERAL("reference_video.yuv");
60 static const base::FilePath::CharType kCapturedYuvFileName
[] =
61 FILE_PATH_LITERAL("captured_video.yuv");
62 static const base::FilePath::CharType kStatsFileName
[] =
63 FILE_PATH_LITERAL("stats.txt");
64 static const char kMainWebrtcTestHtmlPage
[] =
65 "/webrtc/webrtc_jsep01_test.html";
66 static const char kCapturingWebrtcHtmlPage
[] =
67 "/webrtc/webrtc_video_quality_test.html";
68 static const int kVgaWidth
= 640;
69 static const int kVgaHeight
= 480;
71 // If you change the port number, don't forget to modify video_extraction.js
73 static const char kPyWebSocketPortNumber
[] = "12221";
75 // Test the video quality of the WebRTC output.
77 // Prerequisites: This test case must run on a machine with a virtual webcam
78 // that plays video from the reference file located in <the running user's home
79 // folder>/kWorkingDirName/kReferenceYuvFileName.
81 // You must also compile the chromium_builder_webrtc target before you run this
82 // test to get all the tools built.
84 // The external compare_videos.py script also depends on two external
85 // executables which must be located in the PATH when running this test.
86 // * zxing (see the CPP version at https://code.google.com/p/zxing)
87 // * ffmpeg 0.11.1 or compatible version (see http://www.ffmpeg.org)
89 // The test case will launch a custom binary (peerconnection_server) which will
90 // allow two WebRTC clients to find each other.
92 // The test also runs several other custom binaries - rgba_to_i420 converter and
93 // frame_analyzer. Both tools can be found under third_party/webrtc/tools. The
94 // test also runs a stand alone Python implementation of a WebSocket server
95 // (pywebsocket) and a barcode_decoder script.
96 class WebRtcVideoQualityBrowserTest
: public WebRtcTestBase
{
98 WebRtcVideoQualityBrowserTest()
99 : pywebsocket_server_(0),
100 environment_(base::Environment::Create()) {}
102 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE
{
103 PeerConnectionServerRunner::KillAllPeerConnectionServersOnCurrentSystem();
104 DetectErrorsInJavaScript(); // Look for errors in our rather complex js.
107 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
108 // This test expects real device handling and requires a real webcam / audio
109 // device; it will not work with fake devices.
111 command_line
->HasSwitch(switches::kUseFakeDeviceForMediaStream
))
112 << "You cannot run this test with fake devices.";
114 // The video playback will not work without a GPU, so force its use here.
115 command_line
->AppendSwitch(switches::kUseGpuInTests
);
118 bool HasAllRequiredResources() {
119 if (!base::PathExists(GetWorkingDir())) {
120 LOG(ERROR
) << "Cannot find the working directory for the reference video "
121 "and the temporary files:" << GetWorkingDir().value();
124 base::FilePath reference_file
=
125 GetWorkingDir().Append(kReferenceYuvFileName
);
126 if (!base::PathExists(reference_file
)) {
127 LOG(ERROR
) << "Cannot find the reference file to be used for video "
128 << "quality comparison: " << reference_file
.value();
134 bool StartPyWebSocketServer() {
135 base::FilePath path_pywebsocket_dir
=
136 GetSourceDir().Append(FILE_PATH_LITERAL("third_party/pywebsocket/src"));
137 base::FilePath pywebsocket_server
= path_pywebsocket_dir
.Append(
138 FILE_PATH_LITERAL("mod_pywebsocket/standalone.py"));
139 base::FilePath path_to_data_handler
=
140 GetSourceDir().Append(FILE_PATH_LITERAL("chrome/test/functional"));
142 if (!base::PathExists(pywebsocket_server
)) {
143 LOG(ERROR
) << "Missing pywebsocket server.";
146 if (!base::PathExists(path_to_data_handler
)) {
147 LOG(ERROR
) << "Missing data handler for pywebsocket server.";
151 AppendToPythonPath(path_pywebsocket_dir
);
153 // Note: don't append switches to this command since it will mess up the
154 // -u in the python invocation!
155 CommandLine
pywebsocket_command(CommandLine::NO_PROGRAM
);
156 EXPECT_TRUE(GetPythonCommand(&pywebsocket_command
));
158 pywebsocket_command
.AppendArgPath(pywebsocket_server
);
159 pywebsocket_command
.AppendArg("-p");
160 pywebsocket_command
.AppendArg(kPyWebSocketPortNumber
);
161 pywebsocket_command
.AppendArg("-d");
162 pywebsocket_command
.AppendArgPath(path_to_data_handler
);
164 VLOG(0) << "Running " << pywebsocket_command
.GetCommandLineString();
165 return base::LaunchProcess(pywebsocket_command
, base::LaunchOptions(),
166 &pywebsocket_server_
);
169 bool ShutdownPyWebSocketServer() {
170 return base::KillProcess(pywebsocket_server_
, 0, false);
173 void EstablishCall(content::WebContents
* from_tab
,
174 content::WebContents
* to_tab
) {
175 EXPECT_EQ("ok-peerconnection-created",
176 ExecuteJavascript("preparePeerConnection()", from_tab
));
177 EXPECT_EQ("ok-added", ExecuteJavascript("addLocalStream()", from_tab
));
178 EXPECT_EQ("ok-negotiating", ExecuteJavascript("negotiateCall()", from_tab
));
180 // Ensure the call gets up on both sides.
181 EXPECT_TRUE(PollingWaitUntil(
182 "getPeerConnectionReadyState()", "active", from_tab
));
183 EXPECT_TRUE(PollingWaitUntil(
184 "getPeerConnectionReadyState()", "active", to_tab
));
187 void HangUp(content::WebContents
* from_tab
) {
188 EXPECT_EQ("ok-call-hung-up", ExecuteJavascript("hangUp()", from_tab
));
191 void WaitUntilHangupVerified(content::WebContents
* tab_contents
) {
192 EXPECT_TRUE(PollingWaitUntil(
193 "getPeerConnectionReadyState()", "no-peer-connection", tab_contents
));
196 // Runs the RGBA to I420 converter on the video in |capture_video_filename|,
197 // which should contain frames of size |width| x |height|.
199 // The rgba_to_i420_converter is part of the webrtc_test_tools target which
200 // should be build prior to running this test. The resulting binary should
201 // live next to Chrome.
202 bool RunARGBtoI420Converter(int width
,
204 const base::FilePath
& captured_video_filename
) {
205 base::FilePath path_to_converter
= base::MakeAbsoluteFilePath(
206 GetBrowserDir().Append(kArgbToI420ConverterExecutable
));
208 if (!base::PathExists(path_to_converter
)) {
209 LOG(ERROR
) << "Missing ARGB->I420 converter: should be in "
210 << path_to_converter
.value();
214 CommandLine
converter_command(path_to_converter
);
215 converter_command
.AppendSwitchPath("--frames_dir", GetWorkingDir());
216 converter_command
.AppendSwitchPath("--output_file",
217 captured_video_filename
);
218 converter_command
.AppendSwitchASCII("--width",
219 base::StringPrintf("%d", width
));
220 converter_command
.AppendSwitchASCII("--height",
221 base::StringPrintf("%d", height
));
223 // We produce an output file that will later be used as an input to the
224 // barcode decoder and frame analyzer tools.
225 VLOG(0) << "Running " << converter_command
.GetCommandLineString();
227 bool ok
= base::GetAppOutput(converter_command
, &result
);
228 VLOG(0) << "Output was:\n\n" << result
;
232 // Compares the |captured_video_filename| with the |reference_video_filename|.
234 // The barcode decoder decodes the captured video containing barcodes overlaid
235 // into every frame of the video (produced by rgba_to_i420_converter). It
236 // produces a set of PNG images and a |stats_file| that maps each captured
237 // frame to a frame in the reference video. The frames should be of size
238 // |width| x |height|.
239 // All measurements calculated are printed as perf parsable numbers to stdout.
240 bool CompareVideosAndPrintResult(
243 const base::FilePath
& captured_video_filename
,
244 const base::FilePath
& reference_video_filename
,
245 const base::FilePath
& stats_file
) {
247 base::FilePath path_to_analyzer
= base::MakeAbsoluteFilePath(
248 GetBrowserDir().Append(kFrameAnalyzerExecutable
));
249 base::FilePath path_to_compare_script
= GetSourceDir().Append(
250 FILE_PATH_LITERAL("third_party/webrtc/tools/compare_videos.py"));
252 if (!base::PathExists(path_to_analyzer
)) {
253 LOG(ERROR
) << "Missing frame analyzer: should be in "
254 << path_to_analyzer
.value();
257 if (!base::PathExists(path_to_compare_script
)) {
258 LOG(ERROR
) << "Missing video compare script: should be in "
259 << path_to_compare_script
.value();
263 // Note: don't append switches to this command since it will mess up the
264 // -u in the python invocation!
265 CommandLine
compare_command(CommandLine::NO_PROGRAM
);
266 EXPECT_TRUE(GetPythonCommand(&compare_command
));
268 compare_command
.AppendArgPath(path_to_compare_script
);
269 compare_command
.AppendArg("--label=VGA");
270 compare_command
.AppendArg("--ref_video");
271 compare_command
.AppendArgPath(reference_video_filename
);
272 compare_command
.AppendArg("--test_video");
273 compare_command
.AppendArgPath(captured_video_filename
);
274 compare_command
.AppendArg("--frame_analyzer");
275 compare_command
.AppendArgPath(path_to_analyzer
);
276 compare_command
.AppendArg("--yuv_frame_width");
277 compare_command
.AppendArg(base::StringPrintf("%d", width
));
278 compare_command
.AppendArg("--yuv_frame_height");
279 compare_command
.AppendArg(base::StringPrintf("%d", height
));
280 compare_command
.AppendArg("--stats_file");
281 compare_command
.AppendArgPath(stats_file
);
283 VLOG(0) << "Running " << compare_command
.GetCommandLineString();
285 bool ok
= base::GetAppOutput(compare_command
, &output
);
286 // Print to stdout to ensure the perf numbers are parsed properly by the
288 printf("Output was:\n\n%s\n", output
.c_str());
292 base::FilePath
GetWorkingDir() {
293 std::string home_dir
;
294 environment_
->GetVar(kHomeEnvName
, &home_dir
);
295 base::FilePath::StringType
native_home_dir(home_dir
.begin(),
297 return base::FilePath(native_home_dir
).Append(kWorkingDirName
);
300 PeerConnectionServerRunner peerconnection_server_
;
303 base::FilePath
GetSourceDir() {
304 base::FilePath source_dir
;
305 PathService::Get(base::DIR_SOURCE_ROOT
, &source_dir
);
309 base::FilePath
GetBrowserDir() {
310 base::FilePath browser_dir
;
311 EXPECT_TRUE(PathService::Get(base::DIR_MODULE
, &browser_dir
));
315 base::ProcessHandle pywebsocket_server_
;
316 scoped_ptr
<base::Environment
> environment_
;
319 IN_PROC_BROWSER_TEST_F(WebRtcVideoQualityBrowserTest
,
320 MANUAL_TestVGAVideoQuality
) {
321 ASSERT_GE(TestTimeouts::action_max_timeout().InSeconds(), 150) <<
322 "This is a long-running test; you must specify "
323 "--ui-test-action-max-timeout to have a value of at least 150000.";
325 ASSERT_TRUE(HasAllRequiredResources());
326 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
327 ASSERT_TRUE(StartPyWebSocketServer());
328 ASSERT_TRUE(peerconnection_server_
.Start());
330 content::WebContents
* left_tab
=
331 OpenPageAndGetUserMediaInNewTab(
332 embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage
));
333 content::WebContents
* right_tab
=
334 OpenPageAndGetUserMediaInNewTab(
335 embedded_test_server()->GetURL(kCapturingWebrtcHtmlPage
));
337 ConnectToPeerConnectionServer("peer 1", left_tab
);
338 ConnectToPeerConnectionServer("peer 2", right_tab
);
340 EstablishCall(left_tab
, right_tab
);
342 // Poll slower here to avoid flooding the log with messages: capturing and
343 // sending frames take quite a bit of time.
344 int polling_interval_msec
= 1000;
346 EXPECT_TRUE(PollingWaitUntil(
347 "doneFrameCapturing()", "done-capturing", right_tab
,
348 polling_interval_msec
));
351 WaitUntilHangupVerified(left_tab
);
352 WaitUntilHangupVerified(right_tab
);
354 EXPECT_TRUE(PollingWaitUntil(
355 "haveMoreFramesToSend()", "no-more-frames", right_tab
,
356 polling_interval_msec
));
358 // Shut everything down to avoid having the javascript race with the analysis
359 // tools. For instance, dont have console log printouts interleave with the
360 // RESULT lines from the analysis tools (crbug.com/323200).
361 ASSERT_TRUE(peerconnection_server_
.Stop());
362 ASSERT_TRUE(ShutdownPyWebSocketServer());
364 chrome::CloseWebContents(browser(), left_tab
, false);
365 chrome::CloseWebContents(browser(), right_tab
, false);
367 RunARGBtoI420Converter(
368 kVgaWidth
, kVgaHeight
, GetWorkingDir().Append(kCapturedYuvFileName
));
370 CompareVideosAndPrintResult(kVgaWidth
,
372 GetWorkingDir().Append(kCapturedYuvFileName
),
373 GetWorkingDir().Append(kReferenceYuvFileName
),
374 GetWorkingDir().Append(kStatsFileName
)));