xref: /aosp_15_r20/external/autotest/server/site_tests/audio_AudioAfterReboot/audio_AudioAfterReboot.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
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 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.chameleon import audio_test_utils
14from autotest_lib.client.cros.chameleon import audio_widget_link
15from autotest_lib.client.cros.chameleon import chameleon_audio_helper
16from autotest_lib.client.cros.chameleon import chameleon_audio_ids
17from autotest_lib.server.cros.audio import audio_test
18
19
20class audio_AudioAfterReboot(audio_test.AudioTest):
21    """Server side audio test.
22
23    This test talks to a Chameleon board and a Cros device to verify
24    audio function of the Cros device after reboot.
25
26    """
27    version = 1
28    DELAY_BEFORE_RECORD_SECONDS = 0.5
29    DELAY_AFTER_BINDING = 0.5
30    RECORD_SECONDS = 5
31    PRC_RECONNECT_TIMEOUT = 60
32
33    def play_and_record(self, source_widget, recorder_widget=None):
34        """Plays and records (if needed) audio.
35
36        @param source_widget: widget to do the playback
37        @param recorder_widget: widget to do the recording
38            None to skip recording.
39
40        """
41        self.check_correct_audio_node_selected()
42
43        # Play, wait for some time, and then start recording if needed.
44        source_widget.set_playback_data(self.golden_file)
45        logging.debug('Start playing %s', self.golden_file.path)
46        source_widget.start_playback()
47
48        if recorder_widget != None:
49            time.sleep(self.DELAY_BEFORE_RECORD_SECONDS)
50            logging.debug('Start recording.')
51            recorder_widget.start_recording()
52
53            time.sleep(self.RECORD_SECONDS)
54
55            recorder_widget.stop_recording()
56            logging.debug('Stopped recording.')
57
58            audio_test_utils.dump_cros_audio_logs(self.host, self.facade,
59                                                  self.resultsdir,
60                                                  'after_recording')
61
62            recorder_widget.read_recorded_binary()
63        else:
64            time.sleep(self.RECORD_SECONDS)
65
66    def save_and_check_data(self, recorder_widget):
67        """Saves and checks the data from the recorder.
68
69        @param recorder_widget: recorder widget to save data from
70
71        @raise error.TestFail: if comparison fails
72
73        """
74        recorded_file = os.path.join(self.resultsdir, "recorded.raw")
75        logging.debug('Saving recorded data to %s', recorded_file)
76        recorder_widget.save_file(recorded_file)
77
78        # Removes the beginning of recorded data. This is to avoid artifact
79        # caused by codec initialization in the beginning of recording.
80        recorder_widget.remove_head(2.0)
81
82        # Removes noise by a lowpass filter.
83        recorder_widget.lowpass_filter(self.low_pass_freq)
84        recorded_file = os.path.join(self.resultsdir, "recorded_filtered.raw")
85        logging.debug('Saving filtered data to %s', recorded_file)
86        recorder_widget.save_file(recorded_file)
87
88        # Compares data by frequency.
89        audio_test_utils.check_recorded_frequency(
90                self.golden_file,
91                recorder_widget,
92                second_peak_ratio=self.second_peak_ratio,
93                ignore_frequencies=self.ignore_frequencies)
94
95    def check_correct_audio_node_selected(self):
96        """Checks the node selected by Cras is correct."""
97        # Selects and checks the node selected by cras is correct.
98        audio_test_utils.check_and_set_chrome_active_node_types(
99                self.facade, self.audio_nodes[0][0], None)
100        audio_test_utils.check_audio_nodes(self.facade, self.audio_nodes)
101
102    def play_reboot_play_and_record(self, source_widget, recorder_widget):
103        """Play audio, then reboot, and play and record.
104
105        @param source_widget: source widget to play with
106        @param recorder_widget: recorder widget to record with
107
108        """
109        self.play_and_record(source_widget)
110
111        # Disconnecs audio bus so Cros device can detects plugger correctly
112        # when the test involes plugger.
113        # For case where audio bus is used but no plugger is used, it is no
114        # harm to disconnect audio bus and reconnect it.
115        if self.use_audio_bus:
116            logging.info('Disconnecting audio bus before reboot')
117            self.widget_link.disconnect_audio_bus()
118
119        self.host.reboot()
120        utils.poll_for_condition(
121                condition=self.factory.ready,
122                timeout=self.PRC_RECONNECT_TIMEOUT,
123        )
124        logging.debug('After reboot')
125
126        audio_test_utils.dump_cros_audio_logs(self.host, self.facade,
127                                              self.resultsdir, 'after_reboot')
128
129        self.check_correct_audio_node_selected()
130
131        if self.use_audio_bus:
132            logging.info('Reconnecting audio bus after reboot before playback')
133            self.widget_link.reconnect_audio_bus()
134
135        audio_test_utils.dump_cros_audio_logs(self.host, self.facade,
136                                              self.resultsdir,
137                                              'after_bus_reconnect')
138
139        self.play_and_record(source_widget, recorder_widget)
140
141    def run_once(self,
142                 host,
143                 golden_data,
144                 audio_nodes,
145                 bind_from=None,
146                 bind_to=None,
147                 source=None,
148                 recorder=None,
149                 is_internal=False,
150                 cfm_speaker=False,
151                 blocked_boards=[]):
152        """Runs the test main workflow.
153
154        @param host: A host object representing the DUT.
155        @param golden_data: audio file and low pass filter frequency
156           the audio file should be test data defined in audio_test_data
157        @param audio_nodes: audio nodes supposed to be selected
158        @param bind_from: audio originating entity to be binded
159            should be defined in chameleon_audio_ids
160        @param bind_to: audio directed_to entity to be binded
161            should be defined in chameleon_audio_ids
162        @param source: source widget entity
163            should be defined in chameleon_audio_ids
164        @param recorder: recorder widget entity
165            should be defined in chameleon_audio_ids
166        @param is_internal: whether internal audio is tested flag
167        @param cfm_speaker: whether cfm_speaker's audio is tested which is an
168            external USB speaker on CFM (ChromeBox For Meetings) devices.
169        @blocked_boards: boards to ignore and exit.
170
171        """
172        if self.host.get_board().split(':')[1] in blocked_boards:
173            raise error.TestNAError('Board NOT APPLICABLE to test!')
174        if ((bind_from == chameleon_audio_ids.CrosIds.HEADPHONE
175             or bind_to == chameleon_audio_ids.CrosIds.EXTERNAL_MIC)
176                    and not audio_test_utils.has_audio_jack(self.host)):
177            raise error.TestNAError(
178                    'No audio jack for the DUT.'
179                    ' Confirm swarming bot dimension and control file'
180                    ' dependency for audio jack is matching.'
181                    ' For new boards, has_audio_jack might need to be updated.'
182            )
183
184        if (recorder == chameleon_audio_ids.CrosIds.INTERNAL_MIC and
185            (not cfm_speaker
186             and not audio_test_utils.has_internal_microphone(host))):
187            return
188
189        if (source == chameleon_audio_ids.CrosIds.SPEAKER
190                    and (not cfm_speaker
191                         and not audio_test_utils.has_internal_speaker(host))):
192            return
193
194        self.audio_nodes = audio_nodes
195        self.golden_file, self.low_pass_freq = golden_data
196        widget_factory = chameleon_audio_helper.AudioWidgetFactory(
197                self.factory, host)
198        self.widget_link = None
199        self.use_audio_bus = False
200
201        self.second_peak_ratio = audio_test_utils.get_second_peak_ratio(
202                source_id=source, recorder_id=recorder)
203
204        self.ignore_frequencies = None
205        if (source == chameleon_audio_ids.CrosIds.SPEAKER
206                    or bind_to == chameleon_audio_ids.CrosIds.EXTERNAL_MIC):
207            self.ignore_frequencies = [50, 60]
208
209        # Two widgets are binded in the factory if necessary
210        binder_widget = None
211        bind_from_widget = None
212        bind_to_widget = None
213        if bind_from != None and bind_to != None:
214            bind_from_widget = widget_factory.create_widget(bind_from)
215            bind_to_widget = widget_factory.create_widget(bind_to)
216            binder_widget = widget_factory.create_binder(
217                    bind_from_widget, bind_to_widget)
218            self.widget_link = binder_widget.get_link()
219            if isinstance(self.widget_link, audio_widget_link.AudioBusLink):
220                self.use_audio_bus = True
221
222        # Additional widgets that could be part of the factory
223        if source == None:
224            source_widget = bind_from_widget
225        else:
226            source_widget = widget_factory.create_widget(source)
227        if recorder == None:
228            recorder_widget = bind_to_widget
229        else:
230            recorder_widget = widget_factory.create_widget(recorder)
231
232        # Selects and checks the node selected by cras is correct.
233        audio_test_utils.check_and_set_chrome_active_node_types(
234                self.facade, audio_nodes[0][0], None)
235
236        # Play only, reboot, then play and record.
237        if binder_widget != None:
238            with chameleon_audio_helper.bind_widgets(binder_widget):
239                time.sleep(self.DELAY_AFTER_BINDING)
240                self.play_reboot_play_and_record(source_widget,
241                                                 recorder_widget)
242        else:
243            self.play_reboot_play_and_record(source_widget, recorder_widget)
244
245        self.save_and_check_data(recorder_widget)
246