desktopui_AudioFeedback.py revision f723ef2b04d72318ad5ca573524976b55f4dd2a7
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, tempfile
6
7from autotest_lib.client.bin import test
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros import cros_ui_test, httpd
10from autotest_lib.client.cros.audio import audio_helper
11
12# Names of mixer controls.
13_CONTROL_MASTER = "'Master Playback Volume'"
14_CONTROL_HEADPHONE = "'Headphone Playback Volume'"
15_CONTROL_SPEAKER = "'Speaker Playback Volume'"
16_CONTROL_MIC_BOOST = "'Mic Boost Volume'"
17_CONTROL_MIC_CAPTURE = "'Mic Capture Volume'"
18_CONTROL_CAPTURE = "'Capture Volume'"
19_CONTROL_PCM = "'PCM Playback Volume'"
20_CONTROL_DIGITAL = "'Digital Capture Volume'"
21_CONTROL_CAPTURE_SWITCH = "'Capture Switch'"
22
23# Default test configuration.
24_DEFAULT_CARD = '0'
25_DEFAULT_MIXER_SETTINGS = [{'name': _CONTROL_MASTER, 'value': "100%"},
26                           {'name': _CONTROL_HEADPHONE, 'value': "100%"},
27                           {'name': _CONTROL_SPEAKER, 'value': "0%"},
28                           {'name': _CONTROL_MIC_BOOST, 'value': "50%"},
29                           {'name': _CONTROL_MIC_CAPTURE, 'value': "50%"},
30                           {'name': _CONTROL_PCM, 'value': "100%"},
31                           {'name': _CONTROL_DIGITAL, 'value': "100%"},
32                           {'name': _CONTROL_CAPTURE, 'value': "100%"},
33                           {'name': _CONTROL_CAPTURE_SWITCH, 'value': "on"}]
34
35_DEFAULT_NUM_CHANNELS = 2
36_DEFAULT_RECORD_DURATION = 15
37# Minimum RMS value to consider a "pass".
38_DEFAULT_SOX_RMS_THRESHOLD = 0.30
39_DEFAULT_VOLUME_LEVEL = 100
40_DEFAULT_CAPTURE_GAIN = 2500
41
42
43class desktopui_AudioFeedback(cros_ui_test.UITest):
44    version = 1
45
46    def initialize(self,
47                   card=_DEFAULT_CARD,
48                   mixer_settings=_DEFAULT_MIXER_SETTINGS,
49                   num_channels=_DEFAULT_NUM_CHANNELS,
50                   record_duration=_DEFAULT_RECORD_DURATION,
51                   sox_min_rms=_DEFAULT_SOX_RMS_THRESHOLD,
52                   volume_level=_DEFAULT_VOLUME_LEVEL,
53                   capture_gain=_DEFAULT_CAPTURE_GAIN):
54        """Setup the deps for the test.
55
56        Args:
57            card: The index of the sound card to use.
58            mixer_settings: Alsa control settings to apply to the mixer before
59                starting the test.
60            num_channels: The number of channels on the device to test.
61            record_duration: How long of a sample to record.
62            sox_min_rms: The minimum RMS value to consider a pass.
63
64        Raises:
65            error.TestError if the deps can't be run.
66        """
67        self._card = card
68        self._mixer_settings = mixer_settings
69        self._volume_level = volume_level
70        self._capture_gain = capture_gain
71
72        cmd_rec = 'arecord -d %f -f dat' % record_duration
73        self._ah = audio_helper.AudioHelper(self,
74                record_command=cmd_rec,
75                sox_threshold=sox_min_rms,
76                num_channels=num_channels)
77        self._ah.setup_deps(['audioloop', 'sox'])
78
79        super(desktopui_AudioFeedback, self).initialize()
80        self._test_url = 'http://localhost:8000/youtube.html'
81        self._testServer = httpd.HTTPListener(8000, docroot=self.bindir)
82        self._testServer.run()
83
84    def run_once(self):
85        self._ah.set_volume_levels(self._volume_level, self._capture_gain)
86        if not self._ah.check_loopback_dongle():
87            raise error.TestError('Audio loopback dongle is in bad state.')
88
89        # Record a sample of "silence" to use as a noise profile.
90        with tempfile.NamedTemporaryFile(mode='w+t') as noise_file:
91            logging.info('Noise file: %s' % noise_file.name)
92            self._ah.record_sample(noise_file.name)
93
94            # Play the same video to test all channels.
95            self._ah.loopback_test_channels(noise_file,
96                    lambda channel: self.play_video())
97
98    def play_video(self):
99        """Plays a Youtube video to record audio samples.
100
101           Skipping initial 60 seconds so we can ignore initial silence
102           in the video.
103        """
104        logging.info('Playing back youtube media file %s.' % self._test_url)
105        self.pyauto.NavigateToURL(self._test_url)
106        if not self.pyauto.WaitUntil(lambda: self.pyauto.ExecuteJavascript("""
107                    player_status = document.getElementById('player_status');
108                    window.domAutomationController.send(player_status.innerHTML);
109               """), expect_retval='player ready'):
110            raise error.TestError('Failed to load the Youtube player')
111        self.pyauto.ExecuteJavascript("""
112            ytplayer.pauseVideo();
113            ytplayer.seekTo(60, true);
114            ytplayer.playVideo();
115            window.domAutomationController.send('');
116        """)
117