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