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