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.
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/path_service.h"
10 #include "base/process/launch.h"
11 #include "base/scoped_native_library.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/win/windows_version.h"
14 #include "chrome/browser/media/webrtc_browsertest_base.h"
15 #include "chrome/browser/media/webrtc_browsertest_common.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_tabstrip.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/test/base/ui_test_utils.h"
23 #include "chrome/test/ui/ui_test.h"
24 #include "content/public/test/browser_test_utils.h"
25 #include "net/test/embedded_test_server/embedded_test_server.h"
26 #include "testing/perf/perf_test.h"
28 static const base::FilePath::CharType kReferenceFile
[] =
30 FILE_PATH_LITERAL("pyauto_private/webrtc/human-voice-win.wav");
32 FILE_PATH_LITERAL("pyauto_private/webrtc/human-voice-linux.wav");
35 // The javascript will load the reference file relative to its location,
36 // which is in /webrtc on the web server. Therefore, prepend a '..' traversal.
37 static const char kReferenceFileRelativeUrl
[] =
39 "../pyauto_private/webrtc/human-voice-win.wav";
41 "../pyauto_private/webrtc/human-voice-linux.wav";
44 static const base::FilePath::CharType kToolsPath
[] =
45 FILE_PATH_LITERAL("pyauto_private/media/tools");
47 static const char kMainWebrtcTestHtmlPage
[] =
48 "/webrtc/webrtc_audio_quality_test.html";
50 static base::FilePath
GetTestDataDir() {
51 base::FilePath source_dir
;
52 PathService::Get(chrome::DIR_TEST_DATA
, &source_dir
);
56 // Test we can set up a WebRTC call and play audio through it.
58 // You must have the src-internal solution in your .gclient to put the required
59 // pyauto_private directory into chrome/test/data/.
61 // This test will only work on machines that have been configured to record
65 // 1. # sudo apt-get install pavucontrol
66 // 2. For the user who will run the test: # pavucontrol
67 // 3. In a separate terminal, # arecord dummy
68 // 4. In pavucontrol, go to the recording tab.
69 // 5. For the ALSA plug-in [aplay]: ALSA Capture from, change from <x> to
70 // <Monitor of x>, where x is whatever your primary sound device is called.
71 // 6. Try launching chrome as the target user on the target machine, try
72 // playing, say, a YouTube video, and record with # arecord -f dat tmp.dat.
73 // Verify the recording with aplay (should have recorded what you played
76 // Note: the volume for ALL your input devices will be forced to 100% by
77 // running this test on Linux.
80 // 1. Control panel > Sound > Manage audio devices.
81 // 2. In the recording tab, right-click in an empty space in the pane with the
82 // devices. Tick 'show disabled devices'.
83 // 3. You should see a 'stero mix' device - this is what your speakers output.
84 // Right click > Properties.
85 // 4. In the Listen tab for the mix device, check the 'listen to this device'
86 // checkbox. Ensure the mix device is the default recording device.
87 // 5. Launch chrome and try playing a video with sound. You should see
88 // in the volume meter for the mix device. Configure the mix device to have
89 // 50 / 100 in level. Also go into the playback tab, right-click Speakers,
90 // and set that level to 50 / 100. Otherwise you will get distortion in
92 class WebRtcAudioQualityBrowserTest
: public WebRtcTestBase
{
94 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE
{
95 PeerConnectionServerRunner::KillAllPeerConnectionServersOnCurrentSystem();
96 DetectErrorsInJavaScript(); // Look for errors in our rather complex js.
99 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
100 // This test expects real device handling and requires a real webcam / audio
101 // device; it will not work with fake devices.
102 EXPECT_FALSE(command_line
->HasSwitch(
103 switches::kUseFakeDeviceForMediaStream
));
104 EXPECT_FALSE(command_line
->HasSwitch(
105 switches::kUseFakeUIForMediaStream
));
108 bool HasAllRequiredResources() {
109 base::FilePath reference_file
= GetTestDataDir().Append(kReferenceFile
);
110 if (!base::PathExists(reference_file
)) {
111 LOG(ERROR
) << "Cannot find the reference file to be used for audio "
112 << "quality comparison: " << reference_file
.value();
118 void AddAudioFile(const std::string
& input_file_relative_url
,
119 content::WebContents
* tab_contents
) {
120 EXPECT_EQ("ok-added", ExecuteJavascript(
121 "addAudioFile('" + input_file_relative_url
+ "')", tab_contents
));
124 void PlayAudioFile(content::WebContents
* tab_contents
) {
125 EXPECT_EQ("ok-playing", ExecuteJavascript("playAudioFile()", tab_contents
));
128 void EstablishCall(content::WebContents
* from_tab
,
129 content::WebContents
* to_tab
) {
130 EXPECT_EQ("ok-negotiating",
131 ExecuteJavascript("negotiateCall()", from_tab
));
133 // Ensure the call gets up on both sides.
134 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
135 "active", from_tab
));
136 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
140 void HangUp(content::WebContents
* from_tab
) {
141 EXPECT_EQ("ok-call-hung-up", ExecuteJavascript("hangUp()", from_tab
));
144 void WaitUntilHangupVerified(content::WebContents
* tab_contents
) {
145 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
146 "no-peer-connection", tab_contents
));
149 base::FilePath
CreateTemporaryWaveFile() {
150 base::FilePath filename
;
151 EXPECT_TRUE(base::CreateTemporaryFile(&filename
));
152 base::FilePath wav_filename
=
153 filename
.AddExtension(FILE_PATH_LITERAL(".wav"));
154 EXPECT_TRUE(base::Move(filename
, wav_filename
));
158 PeerConnectionServerRunner peerconnection_server_
;
161 class AudioRecorder
{
163 AudioRecorder(): recording_application_(base::kNullProcessHandle
) {}
166 // Starts the recording program for the specified duration. Returns true
168 bool StartRecording(int duration_sec
, const base::FilePath
& output_file
,
170 EXPECT_EQ(base::kNullProcessHandle
, recording_application_
)
171 << "Tried to record, but is already recording.";
173 CommandLine
command_line(CommandLine::NO_PROGRAM
);
175 // This disable is required to run SoundRecorder.exe on 64-bit Windows
176 // from a 32-bit binary. We need to load the wow64 disable function from
177 // the DLL since it doesn't exist on Windows XP.
178 // TODO(phoglund): find some cleaner solution than using SoundRecorder.exe.
179 base::ScopedNativeLibrary
kernel32_lib(base::FilePath(L
"kernel32"));
180 if (kernel32_lib
.is_valid()) {
181 typedef BOOL (WINAPI
* Wow64DisableWow64FSRedirection
)(PVOID
*);
182 Wow64DisableWow64FSRedirection wow_64_disable_wow_64_fs_redirection
;
183 wow_64_disable_wow_64_fs_redirection
=
184 reinterpret_cast<Wow64DisableWow64FSRedirection
>(
185 kernel32_lib
.GetFunctionPointer(
186 "Wow64DisableWow64FsRedirection"));
187 if (wow_64_disable_wow_64_fs_redirection
!= NULL
) {
188 PVOID
* ignored
= NULL
;
189 wow_64_disable_wow_64_fs_redirection(ignored
);
193 char duration_in_hms
[128] = {0};
194 struct tm duration_tm
= {0};
195 duration_tm
.tm_sec
= duration_sec
;
196 EXPECT_NE(0u, strftime(duration_in_hms
, arraysize(duration_in_hms
),
197 "%H:%M:%S", &duration_tm
));
199 command_line
.SetProgram(
200 base::FilePath(FILE_PATH_LITERAL("SoundRecorder.exe")));
201 command_line
.AppendArg("/FILE");
202 command_line
.AppendArgPath(output_file
);
203 command_line
.AppendArg("/DURATION");
204 command_line
.AppendArg(duration_in_hms
);
206 int num_channels
= mono
? 1 : 2;
207 command_line
.SetProgram(base::FilePath("arecord"));
208 command_line
.AppendArg("-d");
209 command_line
.AppendArg(base::StringPrintf("%d", duration_sec
));
210 command_line
.AppendArg("-f");
211 command_line
.AppendArg("dat");
212 command_line
.AppendArg("-c");
213 command_line
.AppendArg(base::StringPrintf("%d", num_channels
));
214 command_line
.AppendArgPath(output_file
);
217 VLOG(0) << "Running " << command_line
.GetCommandLineString();
218 return base::LaunchProcess(command_line
, base::LaunchOptions(),
219 &recording_application_
);
222 // Joins the recording program. Returns true on success.
223 bool WaitForRecordingToEnd() {
225 base::WaitForExitCode(recording_application_
, &exit_code
);
226 return exit_code
== 0;
229 base::ProcessHandle recording_application_
;
232 bool ForceMicrophoneVolumeTo100Percent() {
234 CommandLine
command_line(GetTestDataDir().Append(kToolsPath
).Append(
235 FILE_PATH_LITERAL("force_mic_volume_max.exe")));
236 VLOG(0) << "Running " << command_line
.GetCommandLineString();
238 if (!base::GetAppOutput(command_line
, &result
)) {
239 LOG(ERROR
) << "Failed to set source volume: output was " << result
;
243 // Just force the volume of, say the first 5 devices. A machine will rarely
244 // have more input sources than that. This is way easier than finding the
245 // input device we happen to be using.
246 for (int device_index
= 0; device_index
< 5; ++device_index
) {
248 const std::string kHundredPercentVolume
= "65536";
249 CommandLine
command_line(base::FilePath(FILE_PATH_LITERAL("pacmd")));
250 command_line
.AppendArg("set-source-volume");
251 command_line
.AppendArg(base::StringPrintf("%d", device_index
));
252 command_line
.AppendArg(kHundredPercentVolume
);
253 VLOG(0) << "Running " << command_line
.GetCommandLineString();
254 if (!base::GetAppOutput(command_line
, &result
)) {
255 LOG(ERROR
) << "Failed to set source volume: output was " << result
;
263 // Removes silence from beginning and end of the |input_audio_file| and writes
264 // the result to the |output_audio_file|. Returns true on success.
265 bool RemoveSilence(const base::FilePath
& input_file
,
266 const base::FilePath
& output_file
) {
267 // SOX documentation for silence command: http://sox.sourceforge.net/sox.html
268 // To remove the silence from both beginning and end of the audio file, we
269 // call sox silence command twice: once on normal file and again on its
270 // reverse, then we reverse the final output.
271 // Silence parameters are (in sequence):
272 // ABOVE_PERIODS: The period for which silence occurs. Value 1 is used for
273 // silence at beginning of audio.
274 // DURATION: the amount of time in seconds that non-silence must be detected
275 // before sox stops trimming audio.
276 // THRESHOLD: value used to indicate what sample value is treates as silence.
277 const char* kAbovePeriods
= "1";
278 const char* kDuration
= "2";
279 const char* kTreshold
= "5%";
282 CommandLine
command_line(GetTestDataDir().Append(kToolsPath
).Append(
283 FILE_PATH_LITERAL("sox.exe")));
285 CommandLine
command_line(base::FilePath(FILE_PATH_LITERAL("sox")));
287 command_line
.AppendArgPath(input_file
);
288 command_line
.AppendArgPath(output_file
);
289 command_line
.AppendArg("silence");
290 command_line
.AppendArg(kAbovePeriods
);
291 command_line
.AppendArg(kDuration
);
292 command_line
.AppendArg(kTreshold
);
293 command_line
.AppendArg("reverse");
294 command_line
.AppendArg("silence");
295 command_line
.AppendArg(kAbovePeriods
);
296 command_line
.AppendArg(kDuration
);
297 command_line
.AppendArg(kTreshold
);
298 command_line
.AppendArg("reverse");
300 VLOG(0) << "Running " << command_line
.GetCommandLineString();
302 bool ok
= base::GetAppOutput(command_line
, &result
);
303 VLOG(0) << "Output was:\n\n" << result
;
307 bool CanParseAsFloat(const std::string
& value
) {
308 return atof(value
.c_str()) != 0 || value
== "0";
311 // Runs PESQ to compare |reference_file| to a |actual_file|. The |sample_rate|
312 // can be either 16000 or 8000.
314 // PESQ is only mono-aware, so the files should preferably be recorded in mono.
315 // Furthermore it expects the file to be 16 rather than 32 bits, even though
316 // 32 bits might work. The audio bandwidth of the two files should be the same
317 // e.g. don't compare a 32 kHz file to a 8 kHz file.
319 // The raw score in MOS is written to |raw_mos|, whereas the MOS-LQO score is
320 // written to mos_lqo. The scores are returned as floats in string form (e.g.
321 // "3.145", etc). Returns true on success.
322 bool RunPesq(const base::FilePath
& reference_file
,
323 const base::FilePath
& actual_file
,
324 int sample_rate
, std::string
* raw_mos
, std::string
* mos_lqo
) {
325 // PESQ will break if the paths are too long (!).
326 EXPECT_LT(reference_file
.value().length(), 128u);
327 EXPECT_LT(actual_file
.value().length(), 128u);
330 base::FilePath pesq_path
=
331 GetTestDataDir().Append(kToolsPath
).Append(FILE_PATH_LITERAL("pesq.exe"));
333 base::FilePath pesq_path
=
334 GetTestDataDir().Append(kToolsPath
).Append(FILE_PATH_LITERAL("pesq"));
337 if (!base::PathExists(pesq_path
)) {
338 LOG(ERROR
) << "Missing PESQ binary in " << pesq_path
.value();
342 CommandLine
command_line(pesq_path
);
343 command_line
.AppendArg(base::StringPrintf("+%d", sample_rate
));
344 command_line
.AppendArgPath(reference_file
);
345 command_line
.AppendArgPath(actual_file
);
347 VLOG(0) << "Running " << command_line
.GetCommandLineString();
349 if (!base::GetAppOutput(command_line
, &result
)) {
350 LOG(ERROR
) << "Failed to run PESQ.";
353 VLOG(0) << "Output was:\n\n" << result
;
355 const std::string result_anchor
= "Prediction (Raw MOS, MOS-LQO): = ";
356 std::size_t anchor_pos
= result
.find(result_anchor
);
357 if (anchor_pos
== std::string::npos
) {
358 LOG(ERROR
) << "PESQ was not able to compute a score; we probably recorded "
363 // There are two tab-separated numbers on the format x.xxx, e.g. 5 chars each.
364 std::size_t first_number_pos
= anchor_pos
+ result_anchor
.length();
365 *raw_mos
= result
.substr(first_number_pos
, 5);
366 EXPECT_TRUE(CanParseAsFloat(*raw_mos
)) << "Failed to parse raw MOS number.";
367 *mos_lqo
= result
.substr(first_number_pos
+ 5 + 1, 5);
368 EXPECT_TRUE(CanParseAsFloat(*mos_lqo
)) << "Failed to parse MOS LQO number.";
373 #if defined(OS_LINUX) || defined(OS_WIN)
374 // Only implemented on Linux and Windows for now.
375 #define MAYBE_MANUAL_TestAudioQuality MANUAL_TestAudioQuality
377 #define MAYBE_MANUAL_TestAudioQuality DISABLED_MANUAL_TestAudioQuality
380 IN_PROC_BROWSER_TEST_F(WebRtcAudioQualityBrowserTest
,
381 MAYBE_MANUAL_TestAudioQuality
) {
383 if (base::win::GetVersion() < base::win::VERSION_VISTA
) {
384 // It would take work to implement this on XP; not prioritized right now.
385 LOG(ERROR
) << "This test is not implemented for Windows XP.";
389 ASSERT_TRUE(HasAllRequiredResources());
390 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
391 ASSERT_TRUE(peerconnection_server_
.Start());
393 ASSERT_TRUE(ForceMicrophoneVolumeTo100Percent());
395 ui_test_utils::NavigateToURL(
396 browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage
));
397 content::WebContents
* left_tab
=
398 browser()->tab_strip_model()->GetActiveWebContents();
400 chrome::AddTabAt(browser(), GURL(), -1, true);
401 content::WebContents
* right_tab
=
402 browser()->tab_strip_model()->GetActiveWebContents();
403 ui_test_utils::NavigateToURL(
404 browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage
));
406 ConnectToPeerConnectionServer("peer 1", left_tab
);
407 ConnectToPeerConnectionServer("peer 2", right_tab
);
409 EXPECT_EQ("ok-peerconnection-created",
410 ExecuteJavascript("preparePeerConnection()", left_tab
));
412 AddAudioFile(kReferenceFileRelativeUrl
, left_tab
);
414 EstablishCall(left_tab
, right_tab
);
416 // Note: the media flow isn't necessarily established on the connection just
417 // because the ready state is ok on both sides. We sleep a bit between call
418 // establishment and playing to avoid cutting of the beginning of the audio
420 SleepInJavascript(left_tab
, 2000);
422 base::FilePath recording
= CreateTemporaryWaveFile();
424 // Note: the sound clip is about 10 seconds: record for 15 seconds to get some
425 // safety margins on each side.
426 AudioRecorder recorder
;
427 static int kRecordingTimeSeconds
= 15;
428 ASSERT_TRUE(recorder
.StartRecording(kRecordingTimeSeconds
, recording
, true));
430 PlayAudioFile(left_tab
);
432 ASSERT_TRUE(recorder
.WaitForRecordingToEnd());
433 VLOG(0) << "Done recording to " << recording
.value() << std::endl
;
436 WaitUntilHangupVerified(left_tab
);
437 WaitUntilHangupVerified(right_tab
);
439 base::FilePath trimmed_recording
= CreateTemporaryWaveFile();
441 ASSERT_TRUE(RemoveSilence(recording
, trimmed_recording
));
442 VLOG(0) << "Trimmed silence: " << trimmed_recording
.value() << std::endl
;
446 base::FilePath reference_file_in_test_dir
=
447 GetTestDataDir().Append(kReferenceFile
);
448 ASSERT_TRUE(RunPesq(reference_file_in_test_dir
, trimmed_recording
, 16000,
449 &raw_mos
, &mos_lqo
));
451 perf_test::PrintResult("audio_pesq", "", "raw_mos", raw_mos
, "score", true);
452 perf_test::PrintResult("audio_pesq", "", "mos_lqo", mos_lqo
, "score", true);
454 EXPECT_TRUE(base::DeleteFile(recording
, false));
455 EXPECT_TRUE(base::DeleteFile(trimmed_recording
, false));
457 ASSERT_TRUE(peerconnection_server_
.Stop());