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", base::IntToString(width
));
175 converter_command
.AppendSwitchASCII("--height", base::IntToString(height
));
176 converter_command
.AppendSwitchASCII("--delete_frames", "true");
178 // We produce an output file that will later be used as an input to the
179 // barcode decoder and frame analyzer tools.
180 DVLOG(0) << "Running " << converter_command
.GetCommandLineString();
182 bool ok
= base::GetAppOutput(converter_command
, &result
);
183 DVLOG(0) << "Output was:\n\n" << result
;
187 // Compares the |captured_video_filename| with the |reference_video_filename|.
189 // The barcode decoder decodes the captured video containing barcodes overlaid
190 // into every frame of the video (produced by rgba_to_i420_converter). It
191 // produces a set of PNG images and a |stats_file| that maps each captured
192 // frame to a frame in the reference video. The frames should be of size
193 // |width| x |height|.
194 // All measurements calculated are printed as perf parsable numbers to stdout.
195 bool CompareVideosAndPrintResult(
196 const char* test_label
,
199 const base::FilePath
& captured_video_filename
,
200 const base::FilePath
& reference_video_filename
,
201 const base::FilePath
& stats_file
) {
203 base::FilePath path_to_analyzer
= base::MakeAbsoluteFilePath(
204 GetBrowserDir().Append(kFrameAnalyzerExecutable
));
205 base::FilePath path_to_compare_script
= GetSourceDir().Append(
206 FILE_PATH_LITERAL("third_party/webrtc/tools/compare_videos.py"));
208 if (!base::PathExists(path_to_analyzer
)) {
209 LOG(ERROR
) << "Missing frame analyzer: should be in "
210 << path_to_analyzer
.value()
211 << ". Try building the chromium_builder_webrtc target.";
214 if (!base::PathExists(path_to_compare_script
)) {
215 LOG(ERROR
) << "Missing video compare script: should be in "
216 << path_to_compare_script
.value();
220 base::FilePath path_to_zxing
= test::GetToolForPlatform("zxing");
221 if (!base::PathExists(path_to_zxing
)) {
222 LOG(ERROR
) << "Missing zxing: should be in " << path_to_zxing
.value();
225 base::FilePath path_to_ffmpeg
= test::GetToolForPlatform("ffmpeg");
226 if (!base::PathExists(path_to_ffmpeg
)) {
227 LOG(ERROR
) << "Missing ffmpeg: should be in " << path_to_ffmpeg
.value();
231 // Note: don't append switches to this command since it will mess up the
232 // -u in the python invocation!
233 base::CommandLine
compare_command(base::CommandLine::NO_PROGRAM
);
234 EXPECT_TRUE(GetPythonCommand(&compare_command
));
236 compare_command
.AppendArgPath(path_to_compare_script
);
237 compare_command
.AppendArg(base::StringPrintf("--label=%s", test_label
));
238 compare_command
.AppendArg("--ref_video");
239 compare_command
.AppendArgPath(reference_video_filename
);
240 compare_command
.AppendArg("--test_video");
241 compare_command
.AppendArgPath(captured_video_filename
);
242 compare_command
.AppendArg("--frame_analyzer");
243 compare_command
.AppendArgPath(path_to_analyzer
);
244 compare_command
.AppendArg("--yuv_frame_width");
245 compare_command
.AppendArg(base::IntToString(width
));
246 compare_command
.AppendArg("--yuv_frame_height");
247 compare_command
.AppendArg(base::IntToString(height
));
248 compare_command
.AppendArg("--zxing_path");
249 compare_command
.AppendArgPath(path_to_zxing
);
250 compare_command
.AppendArg("--ffmpeg_path");
251 compare_command
.AppendArgPath(path_to_ffmpeg
);
252 compare_command
.AppendArg("--stats_file");
253 compare_command
.AppendArgPath(stats_file
);
255 DVLOG(0) << "Running " << compare_command
.GetCommandLineString();
257 bool ok
= base::GetAppOutput(compare_command
, &output
);
259 // Print to stdout to ensure the perf numbers are parsed properly by the
260 // buildbot step. The tool should print a handful RESULT lines.
261 printf("Output was:\n\n%s\n", output
.c_str());
262 bool has_result_lines
= output
.find("RESULT") != std::string::npos
;
263 if (!ok
|| !has_result_lines
) {
264 LOG(ERROR
) << "Failed to compare videos; see output above to see what "
272 VideoQualityTestConfig test_config_
;
274 base::FilePath
GetWorkingDir() {
275 return temp_working_dir_
.path();
279 base::FilePath
GetSourceDir() {
280 base::FilePath source_dir
;
281 PathService::Get(base::DIR_SOURCE_ROOT
, &source_dir
);
285 base::FilePath
GetBrowserDir() {
286 base::FilePath browser_dir
;
287 EXPECT_TRUE(PathService::Get(base::DIR_MODULE
, &browser_dir
));
291 scoped_ptr
<base::Environment
> environment_
;
292 base::FilePath webrtc_reference_video_y4m_
;
293 base::ScopedTempDir temp_working_dir_
;
296 INSTANTIATE_TEST_CASE_P(
297 WebRtcVideoQualityBrowserTests
,
298 WebRtcVideoQualityBrowserTest
,
299 testing::ValuesIn(kVideoConfigurations
));
301 IN_PROC_BROWSER_TEST_P(WebRtcVideoQualityBrowserTest
,
302 MANUAL_TestVideoQuality
) {
304 return; // Fails on XP. http://crbug.com/353078.
306 ASSERT_GE(TestTimeouts::action_max_timeout().InSeconds(), 150) <<
307 "This is a long-running test; you must specify "
308 "--ui-test-action-max-timeout to have a value of at least 150000.";
309 ASSERT_TRUE(test::HasReferenceFilesInCheckout());
310 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
312 content::WebContents
* left_tab
=
313 OpenPageAndGetUserMediaInNewTabWithConstraints(
314 embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage
),
315 test_config_
.constraints
);
316 content::WebContents
* right_tab
=
317 OpenPageAndGetUserMediaInNewTabWithConstraints(
318 embedded_test_server()->GetURL(kCapturingWebrtcHtmlPage
),
319 test_config_
.constraints
);
321 SetupPeerconnectionWithLocalStream(left_tab
);
322 SetupPeerconnectionWithLocalStream(right_tab
);
324 NegotiateCall(left_tab
, right_tab
);
326 // Poll slower here to avoid flooding the log with messages: capturing and
327 // sending frames take quite a bit of time.
328 int polling_interval_msec
= 1000;
330 EXPECT_TRUE(test::PollingWaitUntil(
331 "doneFrameCapturing()", "done-capturing", right_tab
,
332 polling_interval_msec
));
336 WriteCapturedFramesToWorkingDir(right_tab
);
338 // Shut everything down to avoid having the javascript race with the analysis
339 // tools. For instance, dont have console log printouts interleave with the
340 // RESULT lines from the analysis tools (crbug.com/323200).
341 chrome::CloseWebContents(browser(), left_tab
, false);
342 chrome::CloseWebContents(browser(), right_tab
, false);
344 ASSERT_TRUE(RunARGBtoI420Converter(
345 test_config_
.width
, test_config_
.height
,
346 GetWorkingDir().Append(kCapturedYuvFileName
)));
347 ASSERT_TRUE(CompareVideosAndPrintResult(
348 test_config_
.test_name
,
351 GetWorkingDir().Append(kCapturedYuvFileName
),
352 test::GetReferenceFilesDir()
353 .Append(test_config_
.reference_video
)
354 .AddExtension(test::kYuvFileExtension
),
355 GetWorkingDir().Append(kStatsFileName
)));