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.bin import utils
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros.audio import audio_helper
10from autotest_lib.client.cros.audio import cmd_utils
11from autotest_lib.client.cros.audio import cras_utils
12
13
14TEST_DURATION = 1
15
16class audio_CrasLoopback(audio_helper.cras_rms_test):
17    """Verifies audio playback and capture function."""
18    version = 1
19
20    @staticmethod
21    def wait_for_active_stream_count(expected_count):
22        utils.poll_for_condition(
23            lambda: cras_utils.get_active_stream_count() == expected_count,
24            exception=error.TestError(
25                'Timeout waiting active stream count to become %d' %
26                 expected_count))
27
28
29    def run_once(self):
30        """Entry point of this test."""
31
32        # Multitone wav file lasts 10 seconds
33        wav_path = os.path.join(self.bindir, '10SEC.wav')
34
35        noise_file = os.path.join(self.resultsdir, 'cras_noise.wav')
36        recorded_file = os.path.join(self.resultsdir, 'cras_recorded.wav')
37
38        # Record a sample of "silence" to use as a noise profile.
39        cras_utils.capture(noise_file, duration=1)
40
41        self.wait_for_active_stream_count(0)
42        p = cmd_utils.popen(cras_utils.playback_cmd(wav_path))
43        try:
44            self.wait_for_active_stream_count(1)
45            cras_utils.capture(recorded_file, duration=TEST_DURATION)
46
47            # Make sure the audio is still playing.
48            if p.poll() != None:
49                raise error.TestError('playback stopped')
50        finally:
51            cmd_utils.kill_or_log_returncode(p)
52
53        rms_value = audio_helper.reduce_noise_and_get_rms(
54                recorded_file, noise_file)[0]
55        self.write_perf_keyval({'rms_value': rms_value})
56
57