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