Home | History | Annotate | Download | only in desktopui_AudioFeedback
      1 # Copyright (c) 2012 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 import logging
      6 import os
      7 
      8 from autotest_lib.client.bin import test, utils
      9 from autotest_lib.client.common_lib import error
     10 from autotest_lib.client.common_lib.cros import chrome
     11 from autotest_lib.client.cros.audio import audio_helper
     12 from autotest_lib.client.cros.audio import cmd_utils
     13 from autotest_lib.client.cros.audio import cras_utils
     14 from autotest_lib.client.cros.audio import sox_utils
     15 
     16 TEST_DURATION = 15
     17 
     18 class desktopui_AudioFeedback(audio_helper.chrome_rms_test):
     19     """Verifies if youtube playback can be captured."""
     20     version = 1
     21 
     22     def play_video(self, tab, video_url):
     23         """Plays a Youtube video to record audio samples.
     24 
     25            @param tab: the tab to load and play the video.
     26         """
     27         tab.Navigate(video_url)
     28 
     29         def player_is_ready():
     30             return tab.EvaluateJavaScript('typeof player != "undefined"')
     31 
     32         utils.poll_for_condition(
     33             condition=player_is_ready,
     34             exception=error.TestError('Failed to load the Youtube player'))
     35 
     36         # Seek to 60 seconds to skip the silence in the beginning.
     37         tab.ExecuteJavaScript('player.seekTo(60, true)')
     38         tab.ExecuteJavaScript('player.playVideo()')
     39 
     40         # Make sure the video is playing
     41         def get_current_time():
     42             return tab.EvaluateJavaScript('player.getCurrentTime()')
     43 
     44         old_time = get_current_time()
     45         utils.poll_for_condition(
     46             condition=lambda: get_current_time() > old_time,
     47             exception=error.TestError('Video is not played until timeout'))
     48 
     49 
     50     def run_once(self):
     51         """Entry point of this test."""
     52         self.chrome.browser.platform.SetHTTPServerDirectories(self.bindir)
     53 
     54         video_url = self.chrome.browser.platform.http_server.UrlOf(
     55                 os.path.join(self.bindir, 'youtube.html'))
     56         logging.info('Playing back youtube media file %s.', video_url)
     57         noise_file = os.path.join(self.resultsdir, "noise.wav")
     58         recorded_file = os.path.join(self.resultsdir, "recorded.wav")
     59         loopback_file = os.path.join(self.resultsdir, "loopback.wav")
     60 
     61         # Record a sample of "silence" to use as a noise profile.
     62         cras_utils.capture(noise_file, duration=3)
     63 
     64         # Play a video and record the audio output
     65         self.play_video(self.chrome.browser.tabs[0], video_url)
     66 
     67         p1 = cmd_utils.popen(cras_utils.capture_cmd(
     68                 recorded_file, duration=TEST_DURATION))
     69         p2 = cmd_utils.popen(cras_utils.loopback_cmd(
     70                 loopback_file, duration=TEST_DURATION))
     71 
     72         cmd_utils.wait_and_check_returncode(p1, p2)
     73 
     74         # See if we recorded something
     75         loopback_stats = [audio_helper.get_channel_sox_stat(
     76                 loopback_file, i) for i in (1, 2)]
     77         logging.info('loopback stats: %s', [str(s) for s in loopback_stats])
     78         rms_value = audio_helper.reduce_noise_and_get_rms(
     79             recorded_file, noise_file)[0]
     80 
     81         self.write_perf_keyval({'rms_value': rms_value})
     82