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.cros.audio import audio_test_data
13from autotest_lib.client.cros.chameleon import audio_test_utils
14from autotest_lib.client.cros.chameleon import chameleon_audio_helper
15from autotest_lib.client.cros.chameleon import chameleon_audio_ids
16from autotest_lib.server.cros.audio import audio_test
17from autotest_lib.server.cros.multimedia import remote_facade_factory
18
19
20class audio_AudioBasicUSBRecord(audio_test.AudioTest):
21    """Server side USB capture audio test.
22
23    This test talks to a Chameleon board and a Cros device to verify
24    USB audio record 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 = remote_facade_factory.RemoteFacadeFactory(
37                host, results_dir=self.resultsdir)
38
39        chameleon_board.setup_and_reset(self.outputdir)
40
41        widget_factory = chameleon_audio_helper.AudioWidgetFactory(
42                factory, host)
43
44        source = widget_factory.create_widget(
45            chameleon_audio_ids.ChameleonIds.USBOUT)
46        recorder = widget_factory.create_widget(
47            chameleon_audio_ids.CrosIds.USBIN)
48        binder = 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_facade = factory.create_audio_facade()
53
54            audio_test_utils.dump_cros_audio_logs(
55                    host, audio_facade, self.resultsdir, 'after_binding')
56
57            audio_test_utils.check_audio_nodes(audio_facade,
58                                               (None, ['USB']))
59            logging.info('Setting playback data on Cros device')
60
61            audio_facade.set_selected_output_volume(70)
62
63            source.set_playback_data(golden_file)
64
65            if suspend:
66                audio_test_utils.suspend_resume(host, self.SUSPEND_SECONDS)
67                utils.poll_for_condition(condition=factory.ready,
68                                         timeout=self.RPC_RECONNECT_TIMEOUT,
69                                         desc='multimedia server reconnect')
70                audio_test_utils.check_audio_nodes(audio_facade,
71                                                   (None, ['USB']))
72
73            # Starts playing from Chameleon (which waits for Cros device),
74            # waits for some time, and then starts recording from Cros device.
75            logging.info('Start playing %s on Chameleon device',
76                         golden_file.path)
77            source.start_playback()
78
79            logging.info('Start recording from Cros.')
80            recorder.start_recording()
81
82            time.sleep(self.RECORD_SECONDS)
83
84            recorder.stop_recording()
85            logging.info('Stopped recording from Cros.')
86
87            audio_test_utils.dump_cros_audio_logs(
88                    host, audio_facade, self.resultsdir, 'after_recording')
89
90            recorder.read_recorded_binary()
91            logging.info('Read recorded binary from Cros.')
92
93        recorded_file = os.path.join(self.resultsdir, "recorded.raw")
94        logging.info('Saving recorded data to %s', recorded_file)
95        recorder.save_file(recorded_file)
96
97        audio_test_utils.compare_recorded_correlation(golden_file, recorder)
98