blob: 0d73fa12797803b30379368595645dc8df646987 [file] [log] [blame]
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging
import os
import tempfile
from autotest_lib.client.bin import utils
from autotest_lib.client.common_lib import error
from import audio_helper
from import cmd_utils
from import cras_utils
from import sox_utils
class desktopui_MediaAudioFeedback(audio_helper.chrome_rms_test):
"""Verifies if media playback can be captured."""
version = 1
def run_once(self, test_files, test_duration):
self._rms_values = {}
noise_file = os.path.join(self.resultsdir, 'noise.wav')
noiseprof_file = tempfile.NamedTemporaryFile()
# Record a sample of "silence" to use as a noise profile.
cras_utils.capture(noise_file, duration=2)
# Open the test page
tab =[0]
os.path.join(self.bindir, 'play.html')))
# Test each media file for all channels.
for media_file in test_files:
self.rms_test(tab, media_file,, test_duration)
def rms_test(self, tab, media_file, noiseprof_file, test_duration):'rms test on media file %s.', media_file)
recorded_file = os.path.join(self.resultsdir, 'recorded.wav')
loopback_file = os.path.join(self.resultsdir, 'loopback.wav')
# Plays the media_file in the browser.
self.play_media(tab, media_file)
# Record the audio output and also the CRAS loopback output.
p1 = cmd_utils.popen(cras_utils.capture_cmd(
recorded_file, duration=test_duration))
p2 = cmd_utils.popen(cras_utils.loopback_cmd(
loopback_file, duration=test_duration))
cmd_utils.wait_and_check_returncode(p1, p2)
# See if we recorded something.
# We captured two channels of audio in the CRAS loopback.
# The RMS values are for debugging only.
loopback_stats = [audio_helper.get_channel_sox_stat(
loopback_file, i) for i in (1, 2)]'loopback stats: %s', [str(s) for s in loopback_stats])
reduced_file = tempfile.NamedTemporaryFile()
recorded_file,, noiseprof_file)
rms = audio_helper.get_rms([0]
self._rms_values['%s_rms_value' % media_file.replace('.', '_')] = rms
# Make sure the audio can be played to the end.
def wait_player_end(self, tab):
"""Wait for player ends playing."""
condition=lambda: tab.EvaluateJavaScript('player.ended'),
exception=error.TestError('Player never end until timeout.'))
def play_media(self, tab, media_file):
"""Plays a media file in Chromium.
@param media_file: Media file to test.
tab.EvaluateJavaScript('play("%s")' % media_file)
def get_current_time():
return tab.EvaluateJavaScript('player.currentTime')
# Make sure the audio is being played
old_time = get_current_time()
condition=lambda: get_current_time() > old_time,
exception=error.TestError('Player never start until timeout.'))