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