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/base64.h"
6 #include "base/command_line.h"
7 #include "base/environment.h"
8 #include "base/files/file.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/path_service.h"
12 #include "base/process/launch.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/test/test_timeouts.h"
17 #include "base/time/time.h"
18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/infobars/infobar_service.h"
20 #include "chrome/browser/media/media_stream_infobar_delegate.h"
21 #include "chrome/browser/media/webrtc_browsertest_base.h"
22 #include "chrome/browser/media/webrtc_browsertest_common.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/browser_tabstrip.h"
26 #include "chrome/browser/ui/tabs/tab_strip_model.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/test/base/in_process_browser_test.h"
29 #include "components/infobars/core/infobar.h"
30 #include "content/public/browser/notification_service.h"
31 #include "content/public/test/browser_test_utils.h"
32 #include "media/base/media_switches.h"
33 #include "net/test/embedded_test_server/embedded_test_server.h"
34 #include "net/test/python_utils.h"
35 #include "testing/perf/perf_test.h"
36 #include "ui/gl/gl_switches.h"
38 static const base::FilePath::CharType kFrameAnalyzerExecutable
[] =
40 FILE_PATH_LITERAL("frame_analyzer.exe");
42 FILE_PATH_LITERAL("frame_analyzer");
45 static const base::FilePath::CharType kArgbToI420ConverterExecutable
[] =
47 FILE_PATH_LITERAL("rgba_to_i420_converter.exe");
49 FILE_PATH_LITERAL("rgba_to_i420_converter");
52 static const base::FilePath::CharType kCapturedYuvFileName
[] =
53 FILE_PATH_LITERAL("captured_video.yuv");
54 static const base::FilePath::CharType kStatsFileName
[] =
55 FILE_PATH_LITERAL("stats.txt");
56 static const char kMainWebrtcTestHtmlPage
[] =
57 "/webrtc/webrtc_jsep01_test.html";
58 static const char kCapturingWebrtcHtmlPage
[] =
59 "/webrtc/webrtc_video_quality_test.html";
61 static const struct VideoQualityTestConfig
{
62 const char* test_name
;
65 const base::FilePath::CharType
* reference_video
;
66 const char* constraints
;
67 } kVideoConfigurations
[] = {
69 test::kReferenceFileName360p
,
70 WebRtcTestBase::kAudioVideoCallConstraints360p
},
72 test::kReferenceFileName720p
,
73 WebRtcTestBase::kAudioVideoCallConstraints720p
},
76 // Test the video quality of the WebRTC output.
78 // Prerequisites: This test case must run on a machine with a chrome playing
79 // the video from the reference files located in GetReferenceFilesDir().
80 // The file kReferenceY4mFileName.kY4mFileExtension is played using a
81 // FileVideoCaptureDevice and its sibling with kYuvFileExtension is used for
84 // You must also compile the chromium_builder_webrtc target before you run this
85 // test to get all the tools built.
87 // The external compare_videos.py script also depends on two external
88 // executables which must be located in the PATH when running this test.
89 // * zxing (see the CPP version at https://code.google.com/p/zxing)
90 // * ffmpeg 0.11.1 or compatible version (see http://www.ffmpeg.org)
92 // The test runs several 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
,
97 public testing::WithParamInterface
<VideoQualityTestConfig
> {
99 WebRtcVideoQualityBrowserTest()
100 : environment_(base::Environment::Create()) {
101 test_config_
= GetParam();
104 void SetUpInProcessBrowserTestFixture() override
{
105 DetectErrorsInJavaScript(); // Look for errors in our rather complex js.
107 ASSERT_TRUE(temp_working_dir_
.CreateUniqueTempDir());
110 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
111 // Set up the command line option with the expected file name. We will check
112 // its existence in HasAllRequiredResources().
113 webrtc_reference_video_y4m_
= test::GetReferenceFilesDir()
114 .Append(test_config_
.reference_video
)
115 .AddExtension(test::kY4mFileExtension
);
116 command_line
->AppendSwitchPath(switches::kUseFileForFakeVideoCapture
,
117 webrtc_reference_video_y4m_
);
118 command_line
->AppendSwitch(switches::kUseFakeDeviceForMediaStream
);
120 // The video playback will not work without a GPU, so force its use here.
121 command_line
->AppendSwitch(switches::kUseGpuInTests
);
124 // Writes all frames we've captured so far by grabbing them from the
125 // javascript and writing them to the temporary work directory.
126 void WriteCapturedFramesToWorkingDir(content::WebContents
* capturing_tab
) {
128 std::string response
=
129 ExecuteJavascript("getTotalNumberCapturedFrames()", capturing_tab
);
130 ASSERT_TRUE(base::StringToInt(response
, &num_frames
)) <<
131 "Failed to retrieve frame count: got " << response
;
132 ASSERT_NE(0, num_frames
) << "Failed to capture any frames.";
134 for (int i
= 0; i
< num_frames
; i
++) {
135 std::string base64_encoded_frame
=
136 ExecuteJavascript(base::StringPrintf("getOneCapturedFrame(%d)", i
),
138 std::string decoded_frame
;
139 ASSERT_TRUE(base::Base64Decode(base64_encoded_frame
, &decoded_frame
))
140 << "Failed to decode frame data '" << base64_encoded_frame
<< "'.";
142 std::string file_name
= base::StringPrintf("frame_%04d", i
);
143 base::File
frame_file(GetWorkingDir().AppendASCII(file_name
),
144 base::File::FLAG_CREATE
| base::File::FLAG_WRITE
);
145 size_t written
= frame_file
.Write(0, decoded_frame
.c_str(),
146 decoded_frame
.length());
147 ASSERT_EQ(decoded_frame
.length(), written
);
151 // Runs the RGBA to I420 converter on the video in |capture_video_filename|,
152 // which should contain frames of size |width| x |height|.
154 // The rgba_to_i420_converter is part of the webrtc_test_tools target which
155 // should be build prior to running this test. The resulting binary should
156 // live next to Chrome.
157 bool RunARGBtoI420Converter(int width
,
159 const base::FilePath
& captured_video_filename
) {
160 base::FilePath path_to_converter
=
161 GetBrowserDir().Append(kArgbToI420ConverterExecutable
);
163 if (!base::PathExists(path_to_converter
)) {
164 LOG(ERROR
) << "Missing ARGB->I420 converter: should be in "
165 << path_to_converter
.value()
166 << ". Try building the chromium_builder_webrtc target.";
170 base::CommandLine
converter_command(path_to_converter
);
171 converter_command
.AppendSwitchPath("--frames_dir", GetWorkingDir());
172 converter_command
.AppendSwitchPath("--output_file",
173 captured_video_filename
);
174 converter_command
.AppendSwitchASCII("--width",
175 base::StringPrintf("%d", width
));
176 converter_command
.AppendSwitchASCII("--height",
177 base::StringPrintf("%d", height
));
178 converter_command
.AppendSwitchASCII("--delete_frames", "true");
180 // We produce an output file that will later be used as an input to the
181 // barcode decoder and frame analyzer tools.
182 DVLOG(0) << "Running " << converter_command
.GetCommandLineString();
184 bool ok
= base::GetAppOutput(converter_command
, &result
);
185 DVLOG(0) << "Output was:\n\n" << result
;
189 // Compares the |captured_video_filename| with the |reference_video_filename|.
191 // The barcode decoder decodes the captured video containing barcodes overlaid
192 // into every frame of the video (produced by rgba_to_i420_converter). It
193 // produces a set of PNG images and a |stats_file| that maps each captured
194 // frame to a frame in the reference video. The frames should be of size
195 // |width| x |height|.
196 // All measurements calculated are printed as perf parsable numbers to stdout.
197 bool CompareVideosAndPrintResult(
198 const char* test_label
,
201 const base::FilePath
& captured_video_filename
,
202 const base::FilePath
& reference_video_filename
,
203 const base::FilePath
& stats_file
) {
205 base::FilePath path_to_analyzer
= base::MakeAbsoluteFilePath(
206 GetBrowserDir().Append(kFrameAnalyzerExecutable
));
207 base::FilePath path_to_compare_script
= GetSourceDir().Append(
208 FILE_PATH_LITERAL("third_party/webrtc/tools/compare_videos.py"));
210 if (!base::PathExists(path_to_analyzer
)) {
211 LOG(ERROR
) << "Missing frame analyzer: should be in "
212 << path_to_analyzer
.value()
213 << ". Try building the chromium_builder_webrtc target.";
216 if (!base::PathExists(path_to_compare_script
)) {
217 LOG(ERROR
) << "Missing video compare script: should be in "
218 << path_to_compare_script
.value();
222 base::FilePath path_to_zxing
= test::GetToolForPlatform("zxing");
223 if (!base::PathExists(path_to_zxing
)) {
224 LOG(ERROR
) << "Missing zxing: should be in " << path_to_zxing
.value();
227 base::FilePath path_to_ffmpeg
= test::GetToolForPlatform("ffmpeg");
228 if (!base::PathExists(path_to_ffmpeg
)) {
229 LOG(ERROR
) << "Missing ffmpeg: should be in " << path_to_ffmpeg
.value();
233 // Note: don't append switches to this command since it will mess up the
234 // -u in the python invocation!
235 base::CommandLine
compare_command(base::CommandLine::NO_PROGRAM
);
236 EXPECT_TRUE(GetPythonCommand(&compare_command
));
238 compare_command
.AppendArgPath(path_to_compare_script
);
239 compare_command
.AppendArg(base::StringPrintf("--label=%s", test_label
));
240 compare_command
.AppendArg("--ref_video");
241 compare_command
.AppendArgPath(reference_video_filename
);
242 compare_command
.AppendArg("--test_video");
243 compare_command
.AppendArgPath(captured_video_filename
);
244 compare_command
.AppendArg("--frame_analyzer");
245 compare_command
.AppendArgPath(path_to_analyzer
);
246 compare_command
.AppendArg("--yuv_frame_width");
247 compare_command
.AppendArg(base::StringPrintf("%d", width
));
248 compare_command
.AppendArg("--yuv_frame_height");
249 compare_command
.AppendArg(base::StringPrintf("%d", height
));
250 compare_command
.AppendArg("--zxing_path");
251 compare_command
.AppendArgPath(path_to_zxing
);
252 compare_command
.AppendArg("--ffmpeg_path");
253 compare_command
.AppendArgPath(path_to_ffmpeg
);
254 compare_command
.AppendArg("--stats_file");
255 compare_command
.AppendArgPath(stats_file
);
257 DVLOG(0) << "Running " << compare_command
.GetCommandLineString();
259 bool ok
= base::GetAppOutput(compare_command
, &output
);
261 // Print to stdout to ensure the perf numbers are parsed properly by the
262 // buildbot step. The tool should print a handful RESULT lines.
263 printf("Output was:\n\n%s\n", output
.c_str());
264 bool has_result_lines
= output
.find("RESULT") != std::string::npos
;
265 if (!ok
|| !has_result_lines
) {
266 LOG(ERROR
) << "Failed to compare videos; see output above to see what "
274 VideoQualityTestConfig test_config_
;
276 base::FilePath
GetWorkingDir() {
277 return temp_working_dir_
.path();
281 base::FilePath
GetSourceDir() {
282 base::FilePath source_dir
;
283 PathService::Get(base::DIR_SOURCE_ROOT
, &source_dir
);
287 base::FilePath
GetBrowserDir() {
288 base::FilePath browser_dir
;
289 EXPECT_TRUE(PathService::Get(base::DIR_MODULE
, &browser_dir
));
293 scoped_ptr
<base::Environment
> environment_
;
294 base::FilePath webrtc_reference_video_y4m_
;
295 base::ScopedTempDir temp_working_dir_
;
298 INSTANTIATE_TEST_CASE_P(
299 WebRtcVideoQualityBrowserTests
,
300 WebRtcVideoQualityBrowserTest
,
301 testing::ValuesIn(kVideoConfigurations
));
303 IN_PROC_BROWSER_TEST_P(WebRtcVideoQualityBrowserTest
,
304 MANUAL_TestVideoQuality
) {
306 return; // Fails on XP. http://crbug.com/353078.
308 ASSERT_GE(TestTimeouts::action_max_timeout().InSeconds(), 150) <<
309 "This is a long-running test; you must specify "
310 "--ui-test-action-max-timeout to have a value of at least 150000.";
311 ASSERT_TRUE(test::HasReferenceFilesInCheckout());
312 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
314 content::WebContents
* left_tab
=
315 OpenPageAndGetUserMediaInNewTabWithConstraints(
316 embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage
),
317 test_config_
.constraints
);
318 content::WebContents
* right_tab
=
319 OpenPageAndGetUserMediaInNewTabWithConstraints(
320 embedded_test_server()->GetURL(kCapturingWebrtcHtmlPage
),
321 test_config_
.constraints
);
323 SetupPeerconnectionWithLocalStream(left_tab
);
324 SetupPeerconnectionWithLocalStream(right_tab
);
326 NegotiateCall(left_tab
, right_tab
);
328 // Poll slower here to avoid flooding the log with messages: capturing and
329 // sending frames take quite a bit of time.
330 int polling_interval_msec
= 1000;
332 EXPECT_TRUE(test::PollingWaitUntil(
333 "doneFrameCapturing()", "done-capturing", right_tab
,
334 polling_interval_msec
));
338 WriteCapturedFramesToWorkingDir(right_tab
);
340 // Shut everything down to avoid having the javascript race with the analysis
341 // tools. For instance, dont have console log printouts interleave with the
342 // RESULT lines from the analysis tools (crbug.com/323200).
343 chrome::CloseWebContents(browser(), left_tab
, false);
344 chrome::CloseWebContents(browser(), right_tab
, false);
346 ASSERT_TRUE(RunARGBtoI420Converter(
347 test_config_
.width
, test_config_
.height
,
348 GetWorkingDir().Append(kCapturedYuvFileName
)));
349 ASSERT_TRUE(CompareVideosAndPrintResult(
350 test_config_
.test_name
,
353 GetWorkingDir().Append(kCapturedYuvFileName
),
354 test::GetReferenceFilesDir()
355 .Append(test_config_
.reference_video
)
356 .AddExtension(test::kYuvFileExtension
),
357 GetWorkingDir().Append(kStatsFileName
)));