1# Lint as: python2, python3
2# Copyright 2015 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""This is a server side USB playback audio test using the Chameleon board."""
6
7import logging
8import os
9import time
10
11from autotest_lib.client.cros.audio import audio_test_data
12from autotest_lib.client.cros.chameleon import audio_test_utils
13from autotest_lib.client.cros.chameleon import chameleon_audio_ids
14from autotest_lib.client.cros.chameleon import chameleon_audio_helper
15from autotest_lib.server.cros.audio import audio_test
16from autotest_lib.client.common_lib import error
17
18
19class audio_AudioBasicUSBPlayback(audio_test.AudioTest):
20    """Server side USB playback audio test.
21
22    This test talks to a Chameleon board and a Cros device to verify
23    USB audio playback function of the Cros device.
24
25    """
26    version = 1
27    RECORD_SECONDS = 5
28
29    def run_once(self, suspend=False, blocked_boards=[]):
30        """Runs Basic Audio USB playback test.
31
32        @param suspend: True for suspend the device before playback.
33                        False for not suspend.
34
35        """
36        if self.host.get_board().split(':')[1] in blocked_boards:
37            raise error.TestNAError('Board pending fix for b/233962403!')
38
39        golden_file = audio_test_data.GenerateAudioTestData(
40                path=os.path.join(self.bindir, 'fix_1k_440_16.wav'),
41                duration_secs=6,
42                frequencies=[1000, 440])
43
44        source = self.widget_factory.create_widget(
45                chameleon_audio_ids.CrosIds.USBOUT)
46        recorder = self.widget_factory.create_widget(
47                chameleon_audio_ids.ChameleonIds.USBIN)
48        binder = self.widget_factory.create_binder(source, recorder)
49
50        with chameleon_audio_helper.bind_widgets(binder):
51            # Checks the node selected by cras is correct.
52            audio_test_utils.dump_cros_audio_logs(
53                    self.host, self.facade, self.resultsdir, 'after_binding')
54
55            audio_test_utils.check_and_set_chrome_active_node_types(
56                    self.facade, 'USB', None)
57            audio_test_utils.dump_cros_audio_logs(
58                    self.host, self.facade, self.resultsdir, 'after_select')
59
60            audio_test_utils.check_audio_nodes(self.facade, (['USB'], None))
61
62            logging.info('Setting playback data on Cros device')
63
64            self.facade.set_selected_output_volume(70)
65
66            source.set_playback_data(golden_file)
67
68            if suspend:
69                audio_test_utils.suspend_resume_and_verify(
70                        self.host, self.factory)
71                # Explicitly select the node as there is a known issue
72                # that the selected node might change after a suspension.
73                # We should remove this after the issue is addressed
74                # (crbug:987529).
75                self.facade.set_selected_node_types(['USB'], None)
76                audio_test_utils.check_audio_nodes(self.facade,
77                                                   (['USB'], None))
78
79            # Starts recording from Chameleon, waits for some time, and then
80            # starts playing from Cros device.
81            logging.info('Start recording from Chameleon.')
82            recorder.start_recording()
83
84            logging.info('Start playing %s on Cros device', golden_file.path)
85            source.start_playback()
86
87            time.sleep(self.RECORD_SECONDS)
88
89            recorder.stop_recording()
90            logging.info('Stopped recording from Chameleon.')
91
92            audio_test_utils.dump_cros_audio_logs(
93                    self.host, self.facade, self.resultsdir, 'after_recording')
94
95            recorder.read_recorded_binary()
96            logging.info('Read recorded binary from Chameleon.')
97
98        recorded_file = os.path.join(self.resultsdir, "recorded.raw")
99        logging.info('Saving recorded data to %s', recorded_file)
100        recorder.save_file(recorded_file)
101
102        audio_test_utils.check_recorded_frequency(golden_file, recorder)
103