chrome_webrtc_audio_quality_browsertest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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 "chrome/browser/media/webrtc_browsertest_base.h"
14#include "chrome/browser/media/webrtc_browsertest_common.h"
15#include "chrome/browser/profiles/profile.h"
16#include "chrome/browser/ui/browser.h"
17#include "chrome/browser/ui/browser_tabstrip.h"
18#include "chrome/browser/ui/tabs/tab_strip_model.h"
19#include "chrome/common/chrome_paths.h"
20#include "chrome/common/chrome_switches.h"
21#include "chrome/test/base/ui_test_utils.h"
22#include "content/public/test/browser_test_utils.h"
23#include "media/base/media_switches.h"
24#include "net/test/embedded_test_server/embedded_test_server.h"
25#include "testing/perf/perf_test.h"
26
27// These are relative to the reference file dir defined by
28// webrtc_browsertest_common.h (i.e. chrome/test/data/webrtc/resources).
29static const base::FilePath::CharType kReferenceFile[] =
30#if defined (OS_WIN)
31    FILE_PATH_LITERAL("human-voice-win.wav");
32#else
33    FILE_PATH_LITERAL("human-voice-linux.wav");
34#endif
35
36// The javascript will load the reference file relative to its location,
37// which is in /webrtc on the web server. The files we are looking for are in
38// webrtc/resources in the chrome/test/data folder.
39static const char kReferenceFileRelativeUrl[] =
40#if defined (OS_WIN)
41    "resources/human-voice-win.wav";
42#else
43    "resources/human-voice-linux.wav";
44#endif
45
46static const char kMainWebrtcTestHtmlPage[] =
47    "/webrtc/webrtc_audio_quality_test.html";
48
49// Test we can set up a WebRTC call and play audio through it.
50//
51// You must have the src-internal solution in your .gclient to put the required
52// pyauto_private directory into chrome/test/data/.
53//
54// This test will only work on machines that have been configured to record
55// their own input.
56//
57// On Linux:
58// 1. # sudo apt-get install pavucontrol
59// 2. For the user who will run the test: # pavucontrol
60// 3. In a separate terminal, # arecord dummy
61// 4. In pavucontrol, go to the recording tab.
62// 5. For the ALSA plug-in [aplay]: ALSA Capture from, change from <x> to
63//    <Monitor of x>, where x is whatever your primary sound device is called.
64// 6. Try launching chrome as the target user on the target machine, try
65//    playing, say, a YouTube video, and record with # arecord -f dat tmp.dat.
66//    Verify the recording with aplay (should have recorded what you played
67//    from chrome).
68//
69// Note: the volume for ALL your input devices will be forced to 100% by
70//       running this test on Linux.
71//
72// On Windows 7:
73// 1. Control panel > Sound > Manage audio devices.
74// 2. In the recording tab, right-click in an empty space in the pane with the
75//    devices. Tick 'show disabled devices'.
76// 3. You should see a 'stero mix' device - this is what your speakers output.
77//    Right click > Properties.
78// 4. In the Listen tab for the mix device, check the 'listen to this device'
79//    checkbox. Ensure the mix device is the default recording device.
80// 5. Launch chrome and try playing a video with sound. You should see
81//    in the volume meter for the mix device. Configure the mix device to have
82//    50 / 100 in level. Also go into the playback tab, right-click Speakers,
83//    and set that level to 50 / 100. Otherwise you will get distortion in
84//    the recording.
85class WebRtcAudioQualityBrowserTest : public WebRtcTestBase {
86 public:
87  WebRtcAudioQualityBrowserTest() {}
88  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
89    DetectErrorsInJavaScript();  // Look for errors in our rather complex js.
90  }
91
92  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
93    // This test expects real device handling and requires a real webcam / audio
94    // device; it will not work with fake devices.
95    EXPECT_FALSE(command_line->HasSwitch(
96        switches::kUseFakeDeviceForMediaStream));
97    EXPECT_FALSE(command_line->HasSwitch(
98        switches::kUseFakeUIForMediaStream));
99  }
100
101  void AddAudioFile(const std::string& input_file_relative_url,
102                    content::WebContents* tab_contents) {
103    EXPECT_EQ("ok-added", ExecuteJavascript(
104        "addAudioFile('" + input_file_relative_url + "')", tab_contents));
105  }
106
107  void PlayAudioFile(content::WebContents* tab_contents) {
108    EXPECT_EQ("ok-playing", ExecuteJavascript("playAudioFile()", tab_contents));
109  }
110
111  base::FilePath CreateTemporaryWaveFile() {
112    base::FilePath filename;
113    EXPECT_TRUE(base::CreateTemporaryFile(&filename));
114    base::FilePath wav_filename =
115        filename.AddExtension(FILE_PATH_LITERAL(".wav"));
116    EXPECT_TRUE(base::Move(filename, wav_filename));
117    return wav_filename;
118  }
119};
120
121class AudioRecorder {
122 public:
123  AudioRecorder(): recording_application_(base::kNullProcessHandle) {}
124  ~AudioRecorder() {}
125
126  // Starts the recording program for the specified duration. Returns true
127  // on success.
128  bool StartRecording(int duration_sec, const base::FilePath& output_file,
129                      bool mono) {
130    EXPECT_EQ(base::kNullProcessHandle, recording_application_)
131        << "Tried to record, but is already recording.";
132
133    CommandLine command_line(CommandLine::NO_PROGRAM);
134#if defined(OS_WIN)
135    // This disable is required to run SoundRecorder.exe on 64-bit Windows
136    // from a 32-bit binary. We need to load the wow64 disable function from
137    // the DLL since it doesn't exist on Windows XP.
138    // TODO(phoglund): find some cleaner solution than using SoundRecorder.exe.
139    base::ScopedNativeLibrary kernel32_lib(base::FilePath(L"kernel32"));
140    if (kernel32_lib.is_valid()) {
141      typedef BOOL (WINAPI* Wow64DisableWow64FSRedirection)(PVOID*);
142      Wow64DisableWow64FSRedirection wow_64_disable_wow_64_fs_redirection;
143      wow_64_disable_wow_64_fs_redirection =
144          reinterpret_cast<Wow64DisableWow64FSRedirection>(
145              kernel32_lib.GetFunctionPointer(
146                  "Wow64DisableWow64FsRedirection"));
147      if (wow_64_disable_wow_64_fs_redirection != NULL) {
148        PVOID* ignored = NULL;
149        wow_64_disable_wow_64_fs_redirection(ignored);
150      }
151    }
152
153    char duration_in_hms[128] = {0};
154    struct tm duration_tm = {0};
155    duration_tm.tm_sec = duration_sec;
156    EXPECT_NE(0u, strftime(duration_in_hms, arraysize(duration_in_hms),
157                           "%H:%M:%S", &duration_tm));
158
159    command_line.SetProgram(
160        base::FilePath(FILE_PATH_LITERAL("SoundRecorder.exe")));
161    command_line.AppendArg("/FILE");
162    command_line.AppendArgPath(output_file);
163    command_line.AppendArg("/DURATION");
164    command_line.AppendArg(duration_in_hms);
165#else
166    int num_channels = mono ? 1 : 2;
167    command_line.SetProgram(base::FilePath("arecord"));
168    command_line.AppendArg("-d");
169    command_line.AppendArg(base::StringPrintf("%d", duration_sec));
170    command_line.AppendArg("-f");
171    command_line.AppendArg("dat");
172    command_line.AppendArg("-c");
173    command_line.AppendArg(base::StringPrintf("%d", num_channels));
174    command_line.AppendArgPath(output_file);
175#endif
176
177    VLOG(0) << "Running " << command_line.GetCommandLineString();
178    return base::LaunchProcess(command_line, base::LaunchOptions(),
179                               &recording_application_);
180  }
181
182  // Joins the recording program. Returns true on success.
183  bool WaitForRecordingToEnd() {
184    int exit_code = -1;
185    base::WaitForExitCode(recording_application_, &exit_code);
186    return exit_code == 0;
187  }
188 private:
189  base::ProcessHandle recording_application_;
190};
191
192bool ForceMicrophoneVolumeTo100Percent() {
193#if defined(OS_WIN)
194  CommandLine command_line(test::GetReferenceFilesDir().Append(
195      FILE_PATH_LITERAL("force_mic_volume_max.exe")));
196  VLOG(0) << "Running " << command_line.GetCommandLineString();
197  std::string result;
198  if (!base::GetAppOutput(command_line, &result)) {
199    LOG(ERROR) << "Failed to set source volume: output was " << result;
200    return false;
201  }
202#else
203  // Just force the volume of, say the first 5 devices. A machine will rarely
204  // have more input sources than that. This is way easier than finding the
205  // input device we happen to be using.
206  for (int device_index = 0; device_index < 5; ++device_index) {
207    std::string result;
208    const std::string kHundredPercentVolume = "65536";
209    CommandLine command_line(base::FilePath(FILE_PATH_LITERAL("pacmd")));
210    command_line.AppendArg("set-source-volume");
211    command_line.AppendArg(base::StringPrintf("%d", device_index));
212    command_line.AppendArg(kHundredPercentVolume);
213    VLOG(0) << "Running " << command_line.GetCommandLineString();
214    if (!base::GetAppOutput(command_line, &result)) {
215      LOG(ERROR) << "Failed to set source volume: output was " << result;
216      return false;
217    }
218  }
219#endif
220  return true;
221}
222
223// Removes silence from beginning and end of the |input_audio_file| and writes
224// the result to the |output_audio_file|. Returns true on success.
225bool RemoveSilence(const base::FilePath& input_file,
226                   const base::FilePath& output_file) {
227  // SOX documentation for silence command: http://sox.sourceforge.net/sox.html
228  // To remove the silence from both beginning and end of the audio file, we
229  // call sox silence command twice: once on normal file and again on its
230  // reverse, then we reverse the final output.
231  // Silence parameters are (in sequence):
232  // ABOVE_PERIODS: The period for which silence occurs. Value 1 is used for
233  //                 silence at beginning of audio.
234  // DURATION: the amount of time in seconds that non-silence must be detected
235  //           before sox stops trimming audio.
236  // THRESHOLD: value used to indicate what sample value is treates as silence.
237  const char* kAbovePeriods = "1";
238  const char* kDuration = "2";
239  const char* kTreshold = "5%";
240
241#if defined(OS_WIN)
242  CommandLine command_line(test::GetReferenceFilesDir().Append(
243      FILE_PATH_LITERAL("sox.exe")));
244#else
245  CommandLine command_line(base::FilePath(FILE_PATH_LITERAL("sox")));
246#endif
247  command_line.AppendArgPath(input_file);
248  command_line.AppendArgPath(output_file);
249  command_line.AppendArg("silence");
250  command_line.AppendArg(kAbovePeriods);
251  command_line.AppendArg(kDuration);
252  command_line.AppendArg(kTreshold);
253  command_line.AppendArg("reverse");
254  command_line.AppendArg("silence");
255  command_line.AppendArg(kAbovePeriods);
256  command_line.AppendArg(kDuration);
257  command_line.AppendArg(kTreshold);
258  command_line.AppendArg("reverse");
259
260  VLOG(0) << "Running " << command_line.GetCommandLineString();
261  std::string result;
262  bool ok = base::GetAppOutput(command_line, &result);
263  VLOG(0) << "Output was:\n\n" << result;
264  return ok;
265}
266
267bool CanParseAsFloat(const std::string& value) {
268  return atof(value.c_str()) != 0 || value == "0";
269}
270
271// Runs PESQ to compare |reference_file| to a |actual_file|. The |sample_rate|
272// can be either 16000 or 8000.
273//
274// PESQ is only mono-aware, so the files should preferably be recorded in mono.
275// Furthermore it expects the file to be 16 rather than 32 bits, even though
276// 32 bits might work. The audio bandwidth of the two files should be the same
277// e.g. don't compare a 32 kHz file to a 8 kHz file.
278//
279// The raw score in MOS is written to |raw_mos|, whereas the MOS-LQO score is
280// written to mos_lqo. The scores are returned as floats in string form (e.g.
281// "3.145", etc). Returns true on success.
282bool RunPesq(const base::FilePath& reference_file,
283             const base::FilePath& actual_file,
284             int sample_rate, std::string* raw_mos, std::string* mos_lqo) {
285  // PESQ will break if the paths are too long (!).
286  EXPECT_LT(reference_file.value().length(), 128u);
287  EXPECT_LT(actual_file.value().length(), 128u);
288
289#if defined(OS_WIN)
290  base::FilePath pesq_path =
291      test::GetReferenceFilesDir().Append(FILE_PATH_LITERAL("pesq.exe"));
292#else
293  base::FilePath pesq_path =
294      test::GetReferenceFilesDir().Append(FILE_PATH_LITERAL("pesq"));
295#endif
296
297  if (!base::PathExists(pesq_path)) {
298    LOG(ERROR) << "Missing PESQ binary in " << pesq_path.value();
299    return false;
300  }
301
302  CommandLine command_line(pesq_path);
303  command_line.AppendArg(base::StringPrintf("+%d", sample_rate));
304  command_line.AppendArgPath(reference_file);
305  command_line.AppendArgPath(actual_file);
306
307  VLOG(0) << "Running " << command_line.GetCommandLineString();
308  std::string result;
309  if (!base::GetAppOutput(command_line, &result)) {
310    LOG(ERROR) << "Failed to run PESQ.";
311    return false;
312  }
313  VLOG(0) << "Output was:\n\n" << result;
314
315  const std::string result_anchor = "Prediction (Raw MOS, MOS-LQO):  = ";
316  std::size_t anchor_pos = result.find(result_anchor);
317  if (anchor_pos == std::string::npos) {
318    LOG(ERROR) << "PESQ was not able to compute a score; we probably recorded "
319        << "only silence.";
320    return false;
321  }
322
323  // There are two tab-separated numbers on the format x.xxx, e.g. 5 chars each.
324  std::size_t first_number_pos = anchor_pos + result_anchor.length();
325  *raw_mos = result.substr(first_number_pos, 5);
326  EXPECT_TRUE(CanParseAsFloat(*raw_mos)) << "Failed to parse raw MOS number.";
327  *mos_lqo = result.substr(first_number_pos + 5 + 1, 5);
328  EXPECT_TRUE(CanParseAsFloat(*mos_lqo)) << "Failed to parse MOS LQO number.";
329
330  return true;
331}
332
333#if defined(OS_LINUX) || defined(OS_WIN)
334// Only implemented on Linux and Windows for now.
335#define MAYBE_MANUAL_TestAudioQuality MANUAL_TestAudioQuality
336#else
337#define MAYBE_MANUAL_TestAudioQuality DISABLED_MANUAL_TestAudioQuality
338#endif
339
340IN_PROC_BROWSER_TEST_F(WebRtcAudioQualityBrowserTest,
341                       MAYBE_MANUAL_TestAudioQuality) {
342  if (OnWinXp()) {
343    LOG(ERROR) << "This test is not implemented for Windows XP.";
344    return;
345  }
346  if (OnWin8()) {
347    // http://crbug.com/379798.
348    LOG(ERROR) << "Temporarily disabled for Win 8.";
349    return;
350  }
351  ASSERT_TRUE(test::HasReferenceFilesInCheckout());
352  ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
353
354  ASSERT_TRUE(ForceMicrophoneVolumeTo100Percent());
355
356  ui_test_utils::NavigateToURL(
357      browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
358  content::WebContents* left_tab =
359      browser()->tab_strip_model()->GetActiveWebContents();
360
361  chrome::AddTabAt(browser(), GURL(), -1, true);
362  content::WebContents* right_tab =
363      browser()->tab_strip_model()->GetActiveWebContents();
364  ui_test_utils::NavigateToURL(
365      browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
366
367  // Prepare the peer connections manually in this test since we don't add
368  // getUserMedia-derived media streams in this test like the other tests.
369  EXPECT_EQ("ok-peerconnection-created",
370            ExecuteJavascript("preparePeerConnection()", left_tab));
371  EXPECT_EQ("ok-peerconnection-created",
372            ExecuteJavascript("preparePeerConnection()", right_tab));
373
374  AddAudioFile(kReferenceFileRelativeUrl, left_tab);
375
376  NegotiateCall(left_tab, right_tab);
377
378  // Note: the media flow isn't necessarily established on the connection just
379  // because the ready state is ok on both sides. We sleep a bit between call
380  // establishment and playing to avoid cutting of the beginning of the audio
381  // file.
382  test::SleepInJavascript(left_tab, 2000);
383
384  base::FilePath recording = CreateTemporaryWaveFile();
385
386  // Note: the sound clip is about 10 seconds: record for 15 seconds to get some
387  // safety margins on each side.
388  AudioRecorder recorder;
389  static int kRecordingTimeSeconds = 15;
390  ASSERT_TRUE(recorder.StartRecording(kRecordingTimeSeconds, recording, true));
391
392  PlayAudioFile(left_tab);
393
394  ASSERT_TRUE(recorder.WaitForRecordingToEnd());
395  VLOG(0) << "Done recording to " << recording.value() << std::endl;
396
397  HangUp(left_tab);
398
399  base::FilePath trimmed_recording = CreateTemporaryWaveFile();
400
401  ASSERT_TRUE(RemoveSilence(recording, trimmed_recording));
402  VLOG(0) << "Trimmed silence: " << trimmed_recording.value() << std::endl;
403
404  std::string raw_mos;
405  std::string mos_lqo;
406  base::FilePath reference_file_in_test_dir =
407      test::GetReferenceFilesDir().Append(kReferenceFile);
408  ASSERT_TRUE(RunPesq(reference_file_in_test_dir, trimmed_recording, 16000,
409                      &raw_mos, &mos_lqo));
410
411  perf_test::PrintResult("audio_pesq", "", "raw_mos", raw_mos, "score", true);
412  perf_test::PrintResult("audio_pesq", "", "mos_lqo", mos_lqo, "score", true);
413
414  EXPECT_TRUE(base::DeleteFile(recording, false));
415  EXPECT_TRUE(base::DeleteFile(trimmed_recording, false));
416}
417