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