1# Copyright (c) 2011 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, os, time
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.cros.audio import audio_helper
9from autotest_lib.client.cros.audio import alsa_utils
10from autotest_lib.client.cros.audio import cmd_utils
11
12
13TEST_DURATION = 1
14
15class audio_AlsaLoopback(audio_helper.alsa_rms_test):
16    """Verifies audio playback and capture function."""
17    version = 1
18
19    def run_once(self):
20        """Entry point of this test."""
21
22        # Multitone wav file lasts 10 seconds
23        wav_path = os.path.join(self.bindir, '10SEC.wav')
24
25        noise_file = os.path.join(self.resultsdir, 'hw_noise.wav')
26        recorded_file = os.path.join(self.resultsdir, 'hw_recorded.wav')
27
28        # Record a sample of "silence" to use as a noise profile.
29        alsa_utils.record(noise_file, duration=1)
30
31        p = cmd_utils.popen(alsa_utils.playback_cmd(wav_path))
32        try:
33            # Wait one second to make sure the playback has been started.
34            time.sleep(1)
35            alsa_utils.record(recorded_file, duration=TEST_DURATION)
36
37            # Make sure the audio is still playing.
38            if p.poll() != None:
39                raise error.TestError('playback stopped')
40        finally:
41            cmd_utils.kill_or_log_returncode(p)
42
43        rms_value = audio_helper.reduce_noise_and_get_rms(
44            recorded_file, noise_file)[0]
45
46        self.write_perf_keyval({'rms_value': rms_value})
47
48