chrome_webrtc_audio_quality_browsertest.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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. 4 5#include <ctime> 6 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" 27 28static const base::FilePath::CharType kReferenceFile[] = 29#if defined (OS_WIN) 30 FILE_PATH_LITERAL("pyauto_private/webrtc/human-voice-win.wav"); 31#else 32 FILE_PATH_LITERAL("pyauto_private/webrtc/human-voice-linux.wav"); 33#endif 34 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. 37static const char kReferenceFileRelativeUrl[] = 38#if defined (OS_WIN) 39 "../pyauto_private/webrtc/human-voice-win.wav"; 40#else 41 "../pyauto_private/webrtc/human-voice-linux.wav"; 42#endif 43 44static const base::FilePath::CharType kToolsPath[] = 45 FILE_PATH_LITERAL("pyauto_private/media/tools"); 46 47static const char kMainWebrtcTestHtmlPage[] = 48 "/webrtc/webrtc_audio_quality_test.html"; 49 50static base::FilePath GetTestDataDir() { 51 base::FilePath source_dir; 52 PathService::Get(chrome::DIR_TEST_DATA, &source_dir); 53 return source_dir; 54} 55 56// Test we can set up a WebRTC call and play audio through it. 57// 58// You must have the src-internal solution in your .gclient to put the required 59// pyauto_private directory into chrome/test/data/. 60// 61// This test will only work on machines that have been configured to record 62// their own input. 63// 64// On Linux: 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 74// from chrome). 75// 76// Note: the volume for ALL your input devices will be forced to 100% by 77// running this test on Linux. 78// 79// On Windows 7: 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 91// the recording. 92class WebRtcAudioQualityBrowserTest : public WebRtcTestBase, 93 public testing::WithParamInterface<bool> { 94 public: 95 WebRtcAudioQualityBrowserTest() {} 96 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 97 PeerConnectionServerRunner::KillAllPeerConnectionServersOnCurrentSystem(); 98 DetectErrorsInJavaScript(); // Look for errors in our rather complex js. 99 } 100 101 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 102 // This test expects real device handling and requires a real webcam / audio 103 // device; it will not work with fake devices. 104 EXPECT_FALSE(command_line->HasSwitch( 105 switches::kUseFakeDeviceForMediaStream)); 106 EXPECT_FALSE(command_line->HasSwitch( 107 switches::kUseFakeUIForMediaStream)); 108 109 bool enable_audio_track_processing = GetParam(); 110 if (enable_audio_track_processing) 111 command_line->AppendSwitch(switches::kEnableAudioTrackProcessing); 112 } 113 114 bool HasAllRequiredResources() { 115 base::FilePath reference_file = GetTestDataDir().Append(kReferenceFile); 116 if (!base::PathExists(reference_file)) { 117 LOG(ERROR) << "Cannot find the reference file to be used for audio " 118 << "quality comparison: " << reference_file.value(); 119 return false; 120 } 121 return true; 122 } 123 124 void AddAudioFile(const std::string& input_file_relative_url, 125 content::WebContents* tab_contents) { 126 EXPECT_EQ("ok-added", ExecuteJavascript( 127 "addAudioFile('" + input_file_relative_url + "')", tab_contents)); 128 } 129 130 void PlayAudioFile(content::WebContents* tab_contents) { 131 EXPECT_EQ("ok-playing", ExecuteJavascript("playAudioFile()", tab_contents)); 132 } 133 134 void EstablishCall(content::WebContents* from_tab, 135 content::WebContents* to_tab) { 136 EXPECT_EQ("ok-negotiating", 137 ExecuteJavascript("negotiateCall()", from_tab)); 138 139 // Ensure the call gets up on both sides. 140 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()", 141 "active", from_tab)); 142 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()", 143 "active", to_tab)); 144 } 145 146 base::FilePath CreateTemporaryWaveFile() { 147 base::FilePath filename; 148 EXPECT_TRUE(base::CreateTemporaryFile(&filename)); 149 base::FilePath wav_filename = 150 filename.AddExtension(FILE_PATH_LITERAL(".wav")); 151 EXPECT_TRUE(base::Move(filename, wav_filename)); 152 return wav_filename; 153 } 154 155 PeerConnectionServerRunner peerconnection_server_; 156}; 157 158class AudioRecorder { 159 public: 160 AudioRecorder(): recording_application_(base::kNullProcessHandle) {} 161 ~AudioRecorder() {} 162 163 // Starts the recording program for the specified duration. Returns true 164 // on success. 165 bool StartRecording(int duration_sec, const base::FilePath& output_file, 166 bool mono) { 167 EXPECT_EQ(base::kNullProcessHandle, recording_application_) 168 << "Tried to record, but is already recording."; 169 170 CommandLine command_line(CommandLine::NO_PROGRAM); 171#if defined(OS_WIN) 172 // This disable is required to run SoundRecorder.exe on 64-bit Windows 173 // from a 32-bit binary. We need to load the wow64 disable function from 174 // the DLL since it doesn't exist on Windows XP. 175 // TODO(phoglund): find some cleaner solution than using SoundRecorder.exe. 176 base::ScopedNativeLibrary kernel32_lib(base::FilePath(L"kernel32")); 177 if (kernel32_lib.is_valid()) { 178 typedef BOOL (WINAPI* Wow64DisableWow64FSRedirection)(PVOID*); 179 Wow64DisableWow64FSRedirection wow_64_disable_wow_64_fs_redirection; 180 wow_64_disable_wow_64_fs_redirection = 181 reinterpret_cast<Wow64DisableWow64FSRedirection>( 182 kernel32_lib.GetFunctionPointer( 183 "Wow64DisableWow64FsRedirection")); 184 if (wow_64_disable_wow_64_fs_redirection != NULL) { 185 PVOID* ignored = NULL; 186 wow_64_disable_wow_64_fs_redirection(ignored); 187 } 188 } 189 190 char duration_in_hms[128] = {0}; 191 struct tm duration_tm = {0}; 192 duration_tm.tm_sec = duration_sec; 193 EXPECT_NE(0u, strftime(duration_in_hms, arraysize(duration_in_hms), 194 "%H:%M:%S", &duration_tm)); 195 196 command_line.SetProgram( 197 base::FilePath(FILE_PATH_LITERAL("SoundRecorder.exe"))); 198 command_line.AppendArg("/FILE"); 199 command_line.AppendArgPath(output_file); 200 command_line.AppendArg("/DURATION"); 201 command_line.AppendArg(duration_in_hms); 202#else 203 int num_channels = mono ? 1 : 2; 204 command_line.SetProgram(base::FilePath("arecord")); 205 command_line.AppendArg("-d"); 206 command_line.AppendArg(base::StringPrintf("%d", duration_sec)); 207 command_line.AppendArg("-f"); 208 command_line.AppendArg("dat"); 209 command_line.AppendArg("-c"); 210 command_line.AppendArg(base::StringPrintf("%d", num_channels)); 211 command_line.AppendArgPath(output_file); 212#endif 213 214 VLOG(0) << "Running " << command_line.GetCommandLineString(); 215 return base::LaunchProcess(command_line, base::LaunchOptions(), 216 &recording_application_); 217 } 218 219 // Joins the recording program. Returns true on success. 220 bool WaitForRecordingToEnd() { 221 int exit_code = -1; 222 base::WaitForExitCode(recording_application_, &exit_code); 223 return exit_code == 0; 224 } 225 private: 226 base::ProcessHandle recording_application_; 227}; 228 229bool ForceMicrophoneVolumeTo100Percent() { 230#if defined(OS_WIN) 231 CommandLine command_line(GetTestDataDir().Append(kToolsPath).Append( 232 FILE_PATH_LITERAL("force_mic_volume_max.exe"))); 233 VLOG(0) << "Running " << command_line.GetCommandLineString(); 234 std::string result; 235 if (!base::GetAppOutput(command_line, &result)) { 236 LOG(ERROR) << "Failed to set source volume: output was " << result; 237 return false; 238 } 239#else 240 // Just force the volume of, say the first 5 devices. A machine will rarely 241 // have more input sources than that. This is way easier than finding the 242 // input device we happen to be using. 243 for (int device_index = 0; device_index < 5; ++device_index) { 244 std::string result; 245 const std::string kHundredPercentVolume = "65536"; 246 CommandLine command_line(base::FilePath(FILE_PATH_LITERAL("pacmd"))); 247 command_line.AppendArg("set-source-volume"); 248 command_line.AppendArg(base::StringPrintf("%d", device_index)); 249 command_line.AppendArg(kHundredPercentVolume); 250 VLOG(0) << "Running " << command_line.GetCommandLineString(); 251 if (!base::GetAppOutput(command_line, &result)) { 252 LOG(ERROR) << "Failed to set source volume: output was " << result; 253 return false; 254 } 255 } 256#endif 257 return true; 258} 259 260// Removes silence from beginning and end of the |input_audio_file| and writes 261// the result to the |output_audio_file|. Returns true on success. 262bool RemoveSilence(const base::FilePath& input_file, 263 const base::FilePath& output_file) { 264 // SOX documentation for silence command: http://sox.sourceforge.net/sox.html 265 // To remove the silence from both beginning and end of the audio file, we 266 // call sox silence command twice: once on normal file and again on its 267 // reverse, then we reverse the final output. 268 // Silence parameters are (in sequence): 269 // ABOVE_PERIODS: The period for which silence occurs. Value 1 is used for 270 // silence at beginning of audio. 271 // DURATION: the amount of time in seconds that non-silence must be detected 272 // before sox stops trimming audio. 273 // THRESHOLD: value used to indicate what sample value is treates as silence. 274 const char* kAbovePeriods = "1"; 275 const char* kDuration = "2"; 276 const char* kTreshold = "5%"; 277 278#if defined(OS_WIN) 279 CommandLine command_line(GetTestDataDir().Append(kToolsPath).Append( 280 FILE_PATH_LITERAL("sox.exe"))); 281#else 282 CommandLine command_line(base::FilePath(FILE_PATH_LITERAL("sox"))); 283#endif 284 command_line.AppendArgPath(input_file); 285 command_line.AppendArgPath(output_file); 286 command_line.AppendArg("silence"); 287 command_line.AppendArg(kAbovePeriods); 288 command_line.AppendArg(kDuration); 289 command_line.AppendArg(kTreshold); 290 command_line.AppendArg("reverse"); 291 command_line.AppendArg("silence"); 292 command_line.AppendArg(kAbovePeriods); 293 command_line.AppendArg(kDuration); 294 command_line.AppendArg(kTreshold); 295 command_line.AppendArg("reverse"); 296 297 VLOG(0) << "Running " << command_line.GetCommandLineString(); 298 std::string result; 299 bool ok = base::GetAppOutput(command_line, &result); 300 VLOG(0) << "Output was:\n\n" << result; 301 return ok; 302} 303 304bool CanParseAsFloat(const std::string& value) { 305 return atof(value.c_str()) != 0 || value == "0"; 306} 307 308// Runs PESQ to compare |reference_file| to a |actual_file|. The |sample_rate| 309// can be either 16000 or 8000. 310// 311// PESQ is only mono-aware, so the files should preferably be recorded in mono. 312// Furthermore it expects the file to be 16 rather than 32 bits, even though 313// 32 bits might work. The audio bandwidth of the two files should be the same 314// e.g. don't compare a 32 kHz file to a 8 kHz file. 315// 316// The raw score in MOS is written to |raw_mos|, whereas the MOS-LQO score is 317// written to mos_lqo. The scores are returned as floats in string form (e.g. 318// "3.145", etc). Returns true on success. 319bool RunPesq(const base::FilePath& reference_file, 320 const base::FilePath& actual_file, 321 int sample_rate, std::string* raw_mos, std::string* mos_lqo) { 322 // PESQ will break if the paths are too long (!). 323 EXPECT_LT(reference_file.value().length(), 128u); 324 EXPECT_LT(actual_file.value().length(), 128u); 325 326#if defined(OS_WIN) 327 base::FilePath pesq_path = 328 GetTestDataDir().Append(kToolsPath).Append(FILE_PATH_LITERAL("pesq.exe")); 329#else 330 base::FilePath pesq_path = 331 GetTestDataDir().Append(kToolsPath).Append(FILE_PATH_LITERAL("pesq")); 332#endif 333 334 if (!base::PathExists(pesq_path)) { 335 LOG(ERROR) << "Missing PESQ binary in " << pesq_path.value(); 336 return false; 337 } 338 339 CommandLine command_line(pesq_path); 340 command_line.AppendArg(base::StringPrintf("+%d", sample_rate)); 341 command_line.AppendArgPath(reference_file); 342 command_line.AppendArgPath(actual_file); 343 344 VLOG(0) << "Running " << command_line.GetCommandLineString(); 345 std::string result; 346 if (!base::GetAppOutput(command_line, &result)) { 347 LOG(ERROR) << "Failed to run PESQ."; 348 return false; 349 } 350 VLOG(0) << "Output was:\n\n" << result; 351 352 const std::string result_anchor = "Prediction (Raw MOS, MOS-LQO): = "; 353 std::size_t anchor_pos = result.find(result_anchor); 354 if (anchor_pos == std::string::npos) { 355 LOG(ERROR) << "PESQ was not able to compute a score; we probably recorded " 356 << "only silence."; 357 return false; 358 } 359 360 // There are two tab-separated numbers on the format x.xxx, e.g. 5 chars each. 361 std::size_t first_number_pos = anchor_pos + result_anchor.length(); 362 *raw_mos = result.substr(first_number_pos, 5); 363 EXPECT_TRUE(CanParseAsFloat(*raw_mos)) << "Failed to parse raw MOS number."; 364 *mos_lqo = result.substr(first_number_pos + 5 + 1, 5); 365 EXPECT_TRUE(CanParseAsFloat(*mos_lqo)) << "Failed to parse MOS LQO number."; 366 367 return true; 368} 369 370static const bool kRunTestsWithFlag[] = { false, true }; 371INSTANTIATE_TEST_CASE_P(WebRtcAudioQualityBrowserTests, 372 WebRtcAudioQualityBrowserTest, 373 testing::ValuesIn(kRunTestsWithFlag)); 374 375#if defined(OS_LINUX) || defined(OS_WIN) 376// Only implemented on Linux and Windows for now. 377#define MAYBE_MANUAL_TestAudioQuality MANUAL_TestAudioQuality 378#else 379#define MAYBE_MANUAL_TestAudioQuality DISABLED_MANUAL_TestAudioQuality 380#endif 381 382IN_PROC_BROWSER_TEST_P(WebRtcAudioQualityBrowserTest, 383 MAYBE_MANUAL_TestAudioQuality) { 384#if defined(OS_WIN) 385 if (base::win::GetVersion() < base::win::VERSION_VISTA) { 386 // It would take work to implement this on XP; not prioritized right now. 387 LOG(ERROR) << "This test is not implemented for Windows XP."; 388 return; 389 } 390#endif 391 ASSERT_TRUE(HasAllRequiredResources()); 392 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 393 ASSERT_TRUE(peerconnection_server_.Start()); 394 395 ASSERT_TRUE(ForceMicrophoneVolumeTo100Percent()); 396 397 ui_test_utils::NavigateToURL( 398 browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage)); 399 content::WebContents* left_tab = 400 browser()->tab_strip_model()->GetActiveWebContents(); 401 402 chrome::AddTabAt(browser(), GURL(), -1, true); 403 content::WebContents* right_tab = 404 browser()->tab_strip_model()->GetActiveWebContents(); 405 ui_test_utils::NavigateToURL( 406 browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage)); 407 408 ConnectToPeerConnectionServer("peer 1", left_tab); 409 ConnectToPeerConnectionServer("peer 2", right_tab); 410 411 EXPECT_EQ("ok-peerconnection-created", 412 ExecuteJavascript("preparePeerConnection()", left_tab)); 413 414 AddAudioFile(kReferenceFileRelativeUrl, left_tab); 415 416 EstablishCall(left_tab, right_tab); 417 418 // Note: the media flow isn't necessarily established on the connection just 419 // because the ready state is ok on both sides. We sleep a bit between call 420 // establishment and playing to avoid cutting of the beginning of the audio 421 // file. 422 SleepInJavascript(left_tab, 2000); 423 424 base::FilePath recording = CreateTemporaryWaveFile(); 425 426 // Note: the sound clip is about 10 seconds: record for 15 seconds to get some 427 // safety margins on each side. 428 AudioRecorder recorder; 429 static int kRecordingTimeSeconds = 15; 430 ASSERT_TRUE(recorder.StartRecording(kRecordingTimeSeconds, recording, true)); 431 432 PlayAudioFile(left_tab); 433 434 ASSERT_TRUE(recorder.WaitForRecordingToEnd()); 435 VLOG(0) << "Done recording to " << recording.value() << std::endl; 436 437 HangUp(left_tab); 438 WaitUntilHangupVerified(left_tab); 439 WaitUntilHangupVerified(right_tab); 440 441 base::FilePath trimmed_recording = CreateTemporaryWaveFile(); 442 443 ASSERT_TRUE(RemoveSilence(recording, trimmed_recording)); 444 VLOG(0) << "Trimmed silence: " << trimmed_recording.value() << std::endl; 445 446 std::string raw_mos; 447 std::string mos_lqo; 448 base::FilePath reference_file_in_test_dir = 449 GetTestDataDir().Append(kReferenceFile); 450 ASSERT_TRUE(RunPesq(reference_file_in_test_dir, trimmed_recording, 16000, 451 &raw_mos, &mos_lqo)); 452 453 perf_test::PrintResult("audio_pesq", "", "raw_mos", raw_mos, "score", true); 454 perf_test::PrintResult("audio_pesq", "", "mos_lqo", mos_lqo, "score", true); 455 456 EXPECT_TRUE(base::DeleteFile(recording, false)); 457 EXPECT_TRUE(base::DeleteFile(trimmed_recording, false)); 458 459 ASSERT_TRUE(peerconnection_server_.Stop()); 460} 461