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/files/file_util.h"
9 #include "base/process/launch.h"
10 #include "base/scoped_native_library.h"
11 #include "base/strings/stringprintf.h"
12 #include "chrome/browser/media/webrtc_browsertest_base.h"
13 #include "chrome/browser/media/webrtc_browsertest_common.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_tabstrip.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "content/public/test/browser_test_utils.h"
22 #include "media/base/media_switches.h"
23 #include "net/test/embedded_test_server/embedded_test_server.h"
24 #include "testing/perf/perf_test.h"
26 // These are relative to the reference file dir defined by
27 // webrtc_browsertest_common.h (i.e. chrome/test/data/webrtc/resources).
28 static const base::FilePath::CharType kReferenceFile
[] =
30 FILE_PATH_LITERAL("human-voice-win.wav");
31 #elif defined (OS_MACOSX)
32 FILE_PATH_LITERAL("human-voice-mac.wav");
34 FILE_PATH_LITERAL("human-voice-linux.wav");
37 static const base::FilePath::CharType kAgcTestReferenceFile
[] =
38 FILE_PATH_LITERAL("speech_44kHz_16bit_stereo.wav");
40 // The javascript will load the reference file relative to its location,
41 // which is in /webrtc on the web server. The files we are looking for are in
42 // webrtc/resources in the chrome/test/data folder.
43 static const char kReferenceFileRelativeUrl
[] =
45 "resources/human-voice-win.wav";
46 #elif defined (OS_MACOSX)
47 "resources/human-voice-mac.wav";
49 "resources/human-voice-linux.wav";
52 static const char kWebRtcAudioTestHtmlPage
[] =
53 "/webrtc/webrtc_audio_quality_test.html";
55 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_MACOSX)
56 #define MAYBE_WebRtcAudioQualityBrowserTest WebRtcAudioQualityBrowserTest
58 // Not implemented on Android, ChromeOS etc.
59 #define MAYBE_WebRtcAudioQualityBrowserTest DISABLED_WebRtcAudioQualityBrowserTest
62 // Test we can set up a WebRTC call and play audio through it.
64 // If you're not a googler and want to run this test, you need to provide a
65 // pesq binary for your platform (and sox.exe on windows). Read more on how
66 // resources are managed in chrome/test/data/webrtc/resources/README.
68 // This test will only work on machines that have been configured to record
72 // 1. # sudo apt-get install pavucontrol sox
73 // 2. For the user who will run the test: # pavucontrol
74 // 3. In a separate terminal, # arecord dummy
75 // 4. In pavucontrol, go to the recording tab.
76 // 5. For the ALSA plug-in [aplay]: ALSA Capture from, change from <x> to
77 // <Monitor of x>, where x is whatever your primary sound device is called.
78 // 6. Try launching chrome as the target user on the target machine, try
79 // playing, say, a YouTube video, and record with # arecord -f dat tmp.dat.
80 // Verify the recording with aplay (should have recorded what you played
83 // Note: the volume for ALL your input devices will be forced to 100% by
84 // running this test on Linux.
87 // 1. Get SoundFlower: http://rogueamoeba.com/freebies/soundflower/download.php
88 // 2. Install it + reboot.
89 // 3. Install MacPorts (http://www.macports.org/).
90 // 4. Install sox: sudo port install sox.
91 // 5. (For Chrome bots) Ensure sox and rec are reachable from the env the test
92 // executes in (sox and rec tends to install in /opt/, which generally isn't
93 // in the Chrome bots' env). For instance, run
94 // sudo ln -s /opt/local/bin/rec /usr/local/bin/rec
95 // sudo ln -s /opt/local/bin/sox /usr/local/bin/sox
96 // 6. In Sound Preferences, set both input and output to Soundflower (2ch).
97 // Note: You will no longer hear audio on this machine, and it will no
98 // longer use any built-in mics.
99 // 7. Try launching chrome as the target user on the target machine, try
100 // playing, say, a YouTube video, and record with 'rec test.wav trim 0 5'.
101 // Stop the video in chrome and try playing back the file; you should hear
102 // a recording of the video (note; if you play back on the target machine
103 // you must revert the changes in step 3 first).
106 // 1. Control panel > Sound > Manage audio devices.
107 // 2. In the recording tab, right-click in an empty space in the pane with the
108 // devices. Tick 'show disabled devices'.
109 // 3. You should see a 'stero mix' device - this is what your speakers output.
110 // Right click > Properties.
111 // 4. In the Listen tab for the mix device, check the 'listen to this device'
112 // checkbox. Ensure the mix device is the default recording device.
113 // 5. Launch chrome and try playing a video with sound. You should see
114 // in the volume meter for the mix device. Configure the mix device to have
115 // 50 / 100 in level. Also go into the playback tab, right-click Speakers,
116 // and set that level to 50 / 100. Otherwise you will get distortion in
118 class MAYBE_WebRtcAudioQualityBrowserTest
: public WebRtcTestBase
{
120 MAYBE_WebRtcAudioQualityBrowserTest() {}
121 void SetUpInProcessBrowserTestFixture() override
{
122 DetectErrorsInJavaScript(); // Look for errors in our rather complex js.
125 void SetUpCommandLine(CommandLine
* command_line
) override
{
126 EXPECT_FALSE(command_line
->HasSwitch(
127 switches::kUseFakeUIForMediaStream
));
129 // The WebAudio-based tests don't care what devices are available to
130 // getUserMedia, and the getUserMedia-based tests will play back a file
131 // through the fake device using using --use-file-for-fake-audio-capture.
132 command_line
->AppendSwitch(switches::kUseFakeDeviceForMediaStream
);
135 void ConfigureFakeDeviceToPlayFile(const base::FilePath
& wav_file_path
) {
136 CommandLine::ForCurrentProcess()->AppendSwitchPath(
137 switches::kUseFileForFakeAudioCapture
, wav_file_path
);
140 void AddAudioFileToWebAudio(const std::string
& input_file_relative_url
,
141 content::WebContents
* tab_contents
) {
142 // This calls into webaudio.js.
143 EXPECT_EQ("ok-added", ExecuteJavascript(
144 "addAudioFile('" + input_file_relative_url
+ "')", tab_contents
));
147 void PlayAudioFileThroughWebAudio(content::WebContents
* tab_contents
) {
148 EXPECT_EQ("ok-playing", ExecuteJavascript("playAudioFile()", tab_contents
));
151 base::FilePath
CreateTemporaryWaveFile() {
152 base::FilePath filename
;
153 EXPECT_TRUE(base::CreateTemporaryFile(&filename
));
154 base::FilePath wav_filename
=
155 filename
.AddExtension(FILE_PATH_LITERAL(".wav"));
156 EXPECT_TRUE(base::Move(filename
, wav_filename
));
160 content::WebContents
* OpenPageWithoutGetUserMedia(const char* url
) {
161 chrome::AddTabAt(browser(), GURL(), -1, true);
162 ui_test_utils::NavigateToURL(
163 browser(), embedded_test_server()->GetURL(url
));
164 content::WebContents
* tab
=
165 browser()->tab_strip_model()->GetActiveWebContents();
167 // Prepare the peer connections manually in this test since we don't add
168 // getUserMedia-derived media streams in this test like the other tests.
169 EXPECT_EQ("ok-peerconnection-created",
170 ExecuteJavascript("preparePeerConnection()", tab
));
175 class AudioRecorder
{
177 AudioRecorder(): recording_application_(base::kNullProcessHandle
) {}
180 // Starts the recording program for the specified duration. Returns true
182 bool StartRecording(int duration_sec
, const base::FilePath
& output_file
,
184 EXPECT_EQ(base::kNullProcessHandle
, recording_application_
)
185 << "Tried to record, but is already recording.";
187 CommandLine
command_line(CommandLine::NO_PROGRAM
);
189 // This disable is required to run SoundRecorder.exe on 64-bit Windows
190 // from a 32-bit binary. We need to load the wow64 disable function from
191 // the DLL since it doesn't exist on Windows XP.
192 // TODO(phoglund): find some cleaner solution than using SoundRecorder.exe.
193 base::ScopedNativeLibrary
kernel32_lib(base::FilePath(L
"kernel32"));
194 if (kernel32_lib
.is_valid()) {
195 typedef BOOL (WINAPI
* Wow64DisableWow64FSRedirection
)(PVOID
*);
196 Wow64DisableWow64FSRedirection wow_64_disable_wow_64_fs_redirection
;
197 wow_64_disable_wow_64_fs_redirection
=
198 reinterpret_cast<Wow64DisableWow64FSRedirection
>(
199 kernel32_lib
.GetFunctionPointer(
200 "Wow64DisableWow64FsRedirection"));
201 if (wow_64_disable_wow_64_fs_redirection
!= NULL
) {
202 PVOID
* ignored
= NULL
;
203 wow_64_disable_wow_64_fs_redirection(ignored
);
207 char duration_in_hms
[128] = {0};
208 struct tm duration_tm
= {0};
209 duration_tm
.tm_sec
= duration_sec
;
210 EXPECT_NE(0u, strftime(duration_in_hms
, arraysize(duration_in_hms
),
211 "%H:%M:%S", &duration_tm
));
213 command_line
.SetProgram(
214 base::FilePath(FILE_PATH_LITERAL("SoundRecorder.exe")));
215 command_line
.AppendArg("/FILE");
216 command_line
.AppendArgPath(output_file
);
217 command_line
.AppendArg("/DURATION");
218 command_line
.AppendArg(duration_in_hms
);
219 #elif defined(OS_MACOSX)
220 command_line
.SetProgram(base::FilePath("rec"));
221 command_line
.AppendArg("-b");
222 command_line
.AppendArg("16");
223 command_line
.AppendArg("-q");
224 command_line
.AppendArgPath(output_file
);
225 command_line
.AppendArg("trim");
226 command_line
.AppendArg("0");
227 command_line
.AppendArg(base::StringPrintf("%d", duration_sec
));
228 command_line
.AppendArg("rate");
229 command_line
.AppendArg("16k");
231 command_line
.AppendArg("remix");
232 command_line
.AppendArg("-");
235 int num_channels
= mono
? 1 : 2;
236 command_line
.SetProgram(base::FilePath("arecord"));
237 command_line
.AppendArg("-d");
238 command_line
.AppendArg(base::StringPrintf("%d", duration_sec
));
239 command_line
.AppendArg("-f");
240 command_line
.AppendArg("dat");
241 command_line
.AppendArg("-c");
242 command_line
.AppendArg(base::StringPrintf("%d", num_channels
));
243 command_line
.AppendArgPath(output_file
);
246 DVLOG(0) << "Running " << command_line
.GetCommandLineString();
247 return base::LaunchProcess(command_line
, base::LaunchOptions(),
248 &recording_application_
);
251 // Joins the recording program. Returns true on success.
252 bool WaitForRecordingToEnd() {
254 base::WaitForExitCode(recording_application_
, &exit_code
);
255 return exit_code
== 0;
258 base::ProcessHandle recording_application_
;
261 bool ForceMicrophoneVolumeTo100Percent() {
263 // Note: the force binary isn't in tools since it's one of our own.
264 CommandLine
command_line(test::GetReferenceFilesDir().Append(
265 FILE_PATH_LITERAL("force_mic_volume_max.exe")));
266 DVLOG(0) << "Running " << command_line
.GetCommandLineString();
268 if (!base::GetAppOutput(command_line
, &result
)) {
269 LOG(ERROR
) << "Failed to set source volume: output was " << result
;
272 #elif defined(OS_MACOSX)
273 CommandLine
command_line(base::FilePath(FILE_PATH_LITERAL("osascript")));
274 command_line
.AppendArg("-e");
275 command_line
.AppendArg("set volume input volume 100");
276 command_line
.AppendArg("-e");
277 command_line
.AppendArg("set volume output volume 100");
280 if (!base::GetAppOutput(command_line
, &result
)) {
281 LOG(ERROR
) << "Failed to set source volume: output was " << result
;
285 // Just force the volume of, say the first 5 devices. A machine will rarely
286 // have more input sources than that. This is way easier than finding the
287 // input device we happen to be using.
288 for (int device_index
= 0; device_index
< 5; ++device_index
) {
290 const std::string kHundredPercentVolume
= "65536";
291 CommandLine
command_line(base::FilePath(FILE_PATH_LITERAL("pacmd")));
292 command_line
.AppendArg("set-source-volume");
293 command_line
.AppendArg(base::StringPrintf("%d", device_index
));
294 command_line
.AppendArg(kHundredPercentVolume
);
295 DVLOG(0) << "Running " << command_line
.GetCommandLineString();
296 if (!base::GetAppOutput(command_line
, &result
)) {
297 LOG(ERROR
) << "Failed to set source volume: output was " << result
;
305 // Removes silence from beginning and end of the |input_audio_file| and writes
306 // the result to the |output_audio_file|. Returns true on success.
307 bool RemoveSilence(const base::FilePath
& input_file
,
308 const base::FilePath
& output_file
) {
309 // SOX documentation for silence command: http://sox.sourceforge.net/sox.html
310 // To remove the silence from both beginning and end of the audio file, we
311 // call sox silence command twice: once on normal file and again on its
312 // reverse, then we reverse the final output.
313 // Silence parameters are (in sequence):
314 // ABOVE_PERIODS: The period for which silence occurs. Value 1 is used for
315 // silence at beginning of audio.
316 // DURATION: the amount of time in seconds that non-silence must be detected
317 // before sox stops trimming audio.
318 // THRESHOLD: value used to indicate what sample value is treates as silence.
319 const char* kAbovePeriods
= "1";
320 const char* kDuration
= "2";
321 const char* kTreshold
= "5%";
324 base::FilePath sox_path
= test::GetReferenceFilesDir().Append(
325 FILE_PATH_LITERAL("tools/sox.exe"));
326 if (!base::PathExists(sox_path
)) {
327 LOG(ERROR
) << "Missing sox.exe binary in " << sox_path
.value()
328 << "; you may have to provide this binary yourself.";
331 CommandLine
command_line(sox_path
);
333 CommandLine
command_line(base::FilePath(FILE_PATH_LITERAL("sox")));
335 command_line
.AppendArgPath(input_file
);
336 command_line
.AppendArgPath(output_file
);
337 command_line
.AppendArg("silence");
338 command_line
.AppendArg(kAbovePeriods
);
339 command_line
.AppendArg(kDuration
);
340 command_line
.AppendArg(kTreshold
);
341 command_line
.AppendArg("reverse");
342 command_line
.AppendArg("silence");
343 command_line
.AppendArg(kAbovePeriods
);
344 command_line
.AppendArg(kDuration
);
345 command_line
.AppendArg(kTreshold
);
346 command_line
.AppendArg("reverse");
348 DVLOG(0) << "Running " << command_line
.GetCommandLineString();
350 bool ok
= base::GetAppOutput(command_line
, &result
);
351 DVLOG(0) << "Output was:\n\n" << result
;
355 bool CanParseAsFloat(const std::string
& value
) {
356 return atof(value
.c_str()) != 0 || value
== "0";
359 // Runs PESQ to compare |reference_file| to a |actual_file|. The |sample_rate|
360 // can be either 16000 or 8000.
362 // PESQ is only mono-aware, so the files should preferably be recorded in mono.
363 // Furthermore it expects the file to be 16 rather than 32 bits, even though
364 // 32 bits might work. The audio bandwidth of the two files should be the same
365 // e.g. don't compare a 32 kHz file to a 8 kHz file.
367 // The raw score in MOS is written to |raw_mos|, whereas the MOS-LQO score is
368 // written to mos_lqo. The scores are returned as floats in string form (e.g.
369 // "3.145", etc). Returns true on success.
370 bool RunPesq(const base::FilePath
& reference_file
,
371 const base::FilePath
& actual_file
,
372 int sample_rate
, std::string
* raw_mos
, std::string
* mos_lqo
) {
373 // PESQ will break if the paths are too long (!).
374 EXPECT_LT(reference_file
.value().length(), 128u);
375 EXPECT_LT(actual_file
.value().length(), 128u);
378 base::FilePath pesq_path
=
379 test::GetReferenceFilesDir().Append(FILE_PATH_LITERAL("tools/pesq.exe"));
380 #elif defined(OS_MACOSX)
381 base::FilePath pesq_path
=
382 test::GetReferenceFilesDir().Append(FILE_PATH_LITERAL("tools/pesq_mac"));
384 base::FilePath pesq_path
=
385 test::GetReferenceFilesDir().Append(FILE_PATH_LITERAL("tools/pesq"));
388 if (!base::PathExists(pesq_path
)) {
389 LOG(ERROR
) << "Missing PESQ binary in " << pesq_path
.value()
390 << "; you may have to provide this binary yourself.";
394 CommandLine
command_line(pesq_path
);
395 command_line
.AppendArg(base::StringPrintf("+%d", sample_rate
));
396 command_line
.AppendArgPath(reference_file
);
397 command_line
.AppendArgPath(actual_file
);
399 DVLOG(0) << "Running " << command_line
.GetCommandLineString();
401 if (!base::GetAppOutput(command_line
, &result
)) {
402 LOG(ERROR
) << "Failed to run PESQ.";
405 DVLOG(0) << "Output was:\n\n" << result
;
407 const std::string result_anchor
= "Prediction (Raw MOS, MOS-LQO): = ";
408 std::size_t anchor_pos
= result
.find(result_anchor
);
409 if (anchor_pos
== std::string::npos
) {
410 LOG(ERROR
) << "PESQ was not able to compute a score; we probably recorded "
411 << "only silence. Please check the output/input volume levels.";
415 // There are two tab-separated numbers on the format x.xxx, e.g. 5 chars each.
416 std::size_t first_number_pos
= anchor_pos
+ result_anchor
.length();
417 *raw_mos
= result
.substr(first_number_pos
, 5);
418 EXPECT_TRUE(CanParseAsFloat(*raw_mos
)) << "Failed to parse raw MOS number.";
419 *mos_lqo
= result
.substr(first_number_pos
+ 5 + 1, 5);
420 EXPECT_TRUE(CanParseAsFloat(*mos_lqo
)) << "Failed to parse MOS LQO number.";
425 IN_PROC_BROWSER_TEST_F(MAYBE_WebRtcAudioQualityBrowserTest
,
426 MANUAL_TestAudioQuality
) {
428 LOG(ERROR
) << "This test is not implemented for Windows XP.";
432 // http://crbug.com/379798.
433 LOG(ERROR
) << "Temporarily disabled for Win 8.";
436 ASSERT_TRUE(test::HasReferenceFilesInCheckout());
437 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
439 ASSERT_TRUE(ForceMicrophoneVolumeTo100Percent());
441 content::WebContents
* left_tab
=
442 OpenPageWithoutGetUserMedia(kWebRtcAudioTestHtmlPage
);
443 content::WebContents
* right_tab
=
444 OpenPageWithoutGetUserMedia(kWebRtcAudioTestHtmlPage
);
446 AddAudioFileToWebAudio(kReferenceFileRelativeUrl
, left_tab
);
448 NegotiateCall(left_tab
, right_tab
);
450 // Note: the media flow isn't necessarily established on the connection just
451 // because the ready state is ok on both sides. We sleep a bit between call
452 // establishment and playing to avoid cutting off the beginning of the stream.
453 test::SleepInJavascript(left_tab
, 2000);
455 base::FilePath recording
= CreateTemporaryWaveFile();
457 // Note: the sound clip is about 10 seconds: record for 15 seconds to get some
458 // safety margins on each side.
459 AudioRecorder recorder
;
460 static int kRecordingTimeSeconds
= 15;
461 ASSERT_TRUE(recorder
.StartRecording(kRecordingTimeSeconds
, recording
, true));
463 PlayAudioFileThroughWebAudio(left_tab
);
465 ASSERT_TRUE(recorder
.WaitForRecordingToEnd());
466 DVLOG(0) << "Done recording to " << recording
.value() << std::endl
;
470 base::FilePath trimmed_recording
= CreateTemporaryWaveFile();
472 ASSERT_TRUE(RemoveSilence(recording
, trimmed_recording
));
473 DVLOG(0) << "Trimmed silence: " << trimmed_recording
.value() << std::endl
;
477 base::FilePath reference_file_in_test_dir
=
478 test::GetReferenceFilesDir().Append(kReferenceFile
);
479 ASSERT_TRUE(RunPesq(reference_file_in_test_dir
, trimmed_recording
, 16000,
480 &raw_mos
, &mos_lqo
));
482 perf_test::PrintResult("audio_pesq", "", "raw_mos", raw_mos
, "score", true);
483 perf_test::PrintResult("audio_pesq", "", "mos_lqo", mos_lqo
, "score", true);
485 EXPECT_TRUE(base::DeleteFile(recording
, false));
486 EXPECT_TRUE(base::DeleteFile(trimmed_recording
, false));
489 IN_PROC_BROWSER_TEST_F(MAYBE_WebRtcAudioQualityBrowserTest
,
490 MANUAL_TestAutoGainControlIncreasesEnergyForLowAudio
) {
492 LOG(ERROR
) << "This test is not implemented for Windows XP.";
496 // http://crbug.com/379798.
497 LOG(ERROR
) << "Temporarily disabled for Win 8.";
500 ASSERT_TRUE(test::HasReferenceFilesInCheckout());
501 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
503 ASSERT_TRUE(ForceMicrophoneVolumeTo100Percent());
505 ConfigureFakeDeviceToPlayFile(
506 test::GetReferenceFilesDir().Append(kAgcTestReferenceFile
));
508 // Create a one-way call.
509 GURL test_page
= embedded_test_server()->GetURL(kWebRtcAudioTestHtmlPage
);
510 content::WebContents
* left_tab
=
511 OpenPageAndGetUserMediaInNewTabWithConstraints(test_page
,
512 kAudioOnlyCallConstraints
);
513 SetupPeerconnectionWithLocalStream(left_tab
);
515 content::WebContents
* right_tab
=
516 OpenPageWithoutGetUserMedia(kWebRtcAudioTestHtmlPage
);
518 NegotiateCall(left_tab
, right_tab
);
520 base::FilePath recording
= CreateTemporaryWaveFile();
522 AudioRecorder recorder
;
523 static int kRecordingTimeSeconds
= 10;
524 ASSERT_TRUE(recorder
.StartRecording(kRecordingTimeSeconds
, recording
, true));
526 ASSERT_TRUE(recorder
.WaitForRecordingToEnd());
527 DVLOG(0) << "Done recording to " << recording
.value() << std::endl
;
531 base::FilePath trimmed_recording
= CreateTemporaryWaveFile();
533 ASSERT_TRUE(RemoveSilence(recording
, trimmed_recording
));
534 DVLOG(0) << "Trimmed silence: " << trimmed_recording
.value() << std::endl
;
536 // TODO(phoglund): invoke bjornv's audio energy analysis tool on the trimmed
537 // recording and log the result.
539 EXPECT_TRUE(base::DeleteFile(recording
, false));
540 EXPECT_TRUE(base::DeleteFile(trimmed_recording
, false));