1# Lint as: python2, python3
2# Copyright 2019 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 pinned stream 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
16
17
18class audio_AudioPinnedStream(audio_test.AudioTest):
19    """Server side pinned stream audio test.
20
21    This test talks to a Chameleon board and a Cros device to verify
22    pinned stream audio function of the Cros device.
23
24    """
25    version = 1
26    DELAY_BEFORE_RECORD_SECONDS = 0.5
27    PLAYBACK_RECORD_SECONDS = 3
28    RECORDING_RECORD_SECONDS = 6
29    DELAY_AFTER_BINDING = 0.5
30
31    def run_once(self, playback):
32        """Running basic pinned stream audio tests.
33
34        @param playback: True if testing for playback path
35        """
36        # [1330, 1330] sine wave
37        golden_file = audio_test_data.GenerateAudioTestData(
38                path=os.path.join(self.bindir, 'fix_1330_16.raw'),
39                duration_secs=10,
40                frequencies=[1330, 1330],
41                volume_scale=0.1)
42
43        # [2000, 1000] sine wave
44        usb_golden_file = audio_test_data.GenerateAudioTestData(
45                path=os.path.join(self.bindir, 'fix_2k_1k_16.raw'),
46                duration_secs=10,
47                frequencies=[2000, 1000])
48
49        if playback:
50            source = self.widget_factory.create_widget(
51                    chameleon_audio_ids.CrosIds.HEADPHONE)
52            recorder = self.widget_factory.create_widget(
53                    chameleon_audio_ids.ChameleonIds.LINEIN)
54            usb_source = self.widget_factory.create_widget(
55                    chameleon_audio_ids.CrosIds.USBOUT)
56            usb_recorder = self.widget_factory.create_widget(
57                    chameleon_audio_ids.ChameleonIds.USBIN)
58        else:
59            source = self.widget_factory.create_widget(
60                    chameleon_audio_ids.ChameleonIds.LINEOUT)
61            recorder = self.widget_factory.create_widget(
62                    chameleon_audio_ids.CrosIds.EXTERNAL_MIC)
63            usb_source = self.widget_factory.create_widget(
64                    chameleon_audio_ids.ChameleonIds.USBOUT)
65            usb_recorder = self.widget_factory.create_widget(
66                    chameleon_audio_ids.CrosIds.USBIN)
67
68        binder = self.widget_factory.create_binder(source, recorder)
69        usb_binder = self.widget_factory.create_binder(usb_source,
70                                                       usb_recorder)
71
72        with chameleon_audio_helper.bind_widgets(usb_binder):
73            with chameleon_audio_helper.bind_widgets(binder):
74                time.sleep(self.DELAY_AFTER_BINDING)
75
76                if playback:
77                    audio_test_utils.check_and_set_chrome_active_node_types(
78                            self.facade, 'HEADPHONE', None)
79                else:
80                    audio_test_utils.check_and_set_chrome_active_node_types(
81                            self.facade, None, 'MIC')
82
83                logging.info('Setting playback data')
84                source.set_playback_data(golden_file)
85                usb_source.set_playback_data(usb_golden_file)
86
87                logging.info('Start playing %s', golden_file.path)
88                source.start_playback()
89
90                logging.info('Start playing %s, pinned:%s',
91                             usb_golden_file.path, playback)
92                usb_source.start_playback(pinned=playback)
93
94                time.sleep(self.DELAY_BEFORE_RECORD_SECONDS)
95
96                logging.info('Start recording.')
97                recorder.start_recording()
98                if playback:
99                    # Not any two recorders on chameleon can record at the same
100                    # time. USB and LineIn can but we would keep them separate
101                    # here to keep things simple and change it when needed.
102                    # Should still record [2000, 1000] sine wave from USB as
103                    # it was set pinned on USB.
104                    time.sleep(self.PLAYBACK_RECORD_SECONDS)
105                    recorder.stop_recording()
106                    usb_recorder.start_recording()
107                    time.sleep(self.PLAYBACK_RECORD_SECONDS)
108                    usb_recorder.stop_recording()
109                else:
110                    usb_recorder.start_recording(pinned=True)
111                    time.sleep(self.RECORDING_RECORD_SECONDS)
112                    recorder.stop_recording()
113                    usb_recorder.stop_recording(pinned=True)
114
115                audio_test_utils.dump_cros_audio_logs(self.host, self.facade,
116                                                      self.resultsdir,
117                                                      'after_recording')
118
119        recorder.read_recorded_binary()
120        usb_recorder.read_recorded_binary()
121
122        recorded_file = os.path.join(self.resultsdir, "recorded.raw")
123        logging.info('Saving recorded data to %s', recorded_file)
124        recorder.save_file(recorded_file)
125
126        usb_recorded_file = os.path.join(self.resultsdir, "usb_recorded.raw")
127        logging.info('Saving recorded data to %s', usb_recorded_file)
128        usb_recorder.save_file(usb_recorded_file)
129
130        if playback:
131            audio_test_utils.check_recorded_frequency(golden_file, recorder)
132        else:
133            # The signal quality from chameleon will get affected when usb is
134            # also connected. We'll just lower the quality bar here, as it is
135            # not the main point of this test.
136            audio_test_utils.check_recorded_frequency(
137                    golden_file, recorder, second_peak_ratio=0.3)
138        audio_test_utils.check_recorded_frequency(usb_golden_file,
139                                                  usb_recorder)
140