1# Copyright 2015 The Chromium OS 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
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.bin import utils
12from autotest_lib.client.common_lib import error
13from autotest_lib.client.cros.audio import audio_test_data
14from autotest_lib.client.cros.chameleon import audio_test_utils
15from autotest_lib.client.cros.chameleon import chameleon_audio_ids
16from autotest_lib.client.cros.chameleon import chameleon_audio_helper
17from autotest_lib.server.cros.audio import audio_test
18
19
20class audio_AudioBasicUSBPlayback(audio_test.AudioTest):
21    """Server side USB playback audio test.
22
23    This test talks to a Chameleon board and a Cros device to verify
24    USB audio playback function of the Cros device.
25
26    """
27    version = 1
28    RECORD_SECONDS = 5
29    SUSPEND_SECONDS = 30
30    RPC_RECONNECT_TIMEOUT = 60
31
32    def run_once(self, host, suspend=False):
33        golden_file = audio_test_data.SWEEP_TEST_FILE
34
35        chameleon_board = host.chameleon
36        factory = self.create_remote_facade_factory(host)
37
38        chameleon_board.reset()
39
40        widget_factory = chameleon_audio_helper.AudioWidgetFactory(
41                factory, host)
42
43        source = widget_factory.create_widget(
44            chameleon_audio_ids.CrosIds.USBOUT)
45        recorder = widget_factory.create_widget(
46            chameleon_audio_ids.ChameleonIds.USBIN)
47        binder = widget_factory.create_binder(source, recorder)
48
49        with chameleon_audio_helper.bind_widgets(binder):
50            # Checks the node selected by cras is correct.
51            audio_facade = factory.create_audio_facade()
52
53            audio_test_utils.dump_cros_audio_logs(
54                    host, audio_facade, self.resultsdir, 'after_binding')
55
56            audio_test_utils.check_audio_nodes(audio_facade, (['USB'], None))
57
58            logging.info('Setting playback data on Cros device')
59
60            audio_facade.set_selected_output_volume(70)
61
62            source.set_playback_data(golden_file)
63
64            if suspend:
65                audio_test_utils.suspend_resume(host, self.SUSPEND_SECONDS)
66                utils.poll_for_condition(condition=factory.ready,
67                                         timeout=self.RPC_RECONNECT_TIMEOUT,
68                                         desc='multimedia server reconnect')
69                audio_test_utils.check_audio_nodes(audio_facade,
70                                                   (['USB'], None))
71
72            # Starts recording from Chameleon, waits for some time, and then
73            # starts playing from Cros device.
74            logging.info('Start recording from Chameleon.')
75            recorder.start_recording()
76
77            logging.info('Start playing %s on Cros device',
78                         golden_file.path)
79            source.start_playback()
80
81            time.sleep(self.RECORD_SECONDS)
82
83            recorder.stop_recording()
84            logging.info('Stopped recording from Chameleon.')
85
86            audio_test_utils.dump_cros_audio_logs(
87                    host, audio_facade, self.resultsdir, 'after_recording')
88
89            recorder.read_recorded_binary()
90            logging.info('Read recorded binary from Chameleon.')
91
92        recorded_file = os.path.join(self.resultsdir, "recorded.raw")
93        logging.info('Saving recorded data to %s', recorded_file)
94        recorder.save_file(recorded_file)
95
96        if not chameleon_audio_helper.compare_recorded_result(
97                golden_file, recorder, 'correlation'):
98            raise error.TestFail(
99                    'Recorded file does not match playback file')
100