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, bind_to=None, 161 source=None, recorder=None, is_internal=False): 162 """Runs the test main workflow. 163 164 @param host: A host object representing the DUT. 165 @param golden_data: audio file and low pass filter frequency 166 the audio file should be test data defined in audio_test_data 167 @param audio_nodes: audio nodes supposed to be selected 168 @param bind_from: audio originating entity to be binded 169 should be defined in chameleon_audio_ids 170 @param bind_to: audio directed_to entity to be binded 171 should be defined in chameleon_audio_ids 172 @param source: source widget entity 173 should be defined in chameleon_audio_ids 174 @param recorder: recorder widget entity 175 should be defined in chameleon_audio_ids 176 @param is_internal: whether internal audio is tested flag 177 178 """ 179 if (recorder == chameleon_audio_ids.CrosIds.INTERNAL_MIC and 180 not audio_test_utils.has_internal_microphone(host)): 181 return 182 183 if (source == chameleon_audio_ids.CrosIds.SPEAKER and 184 not audio_test_utils.has_internal_speaker(host)): 185 return 186 187 self.host = host 188 self.audio_nodes = audio_nodes 189 self.golden_file, self.low_pass_freq = golden_data 190 chameleon_board = self.host.chameleon 191 self.factory = remote_facade_factory.RemoteFacadeFactory( 192 self.host, results_dir=self.resultsdir) 193 self.audio_facade = self.factory.create_audio_facade() 194 chameleon_board.setup_and_reset(self.outputdir) 195 widget_factory = chameleon_audio_helper.AudioWidgetFactory( 196 self.factory, host) 197 self.audio_board = chameleon_board.get_audio_board() 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, 203 recorder_id=recorder) 204 205 self.ignore_frequencies = None 206 if source == chameleon_audio_ids.CrosIds.SPEAKER: 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(bind_from_widget, 217 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 # Plug for external audio 233 self.action_plug_jack(not is_internal) 234 235 # Play only, reboot, then play and record. 236 if binder_widget != None: 237 with chameleon_audio_helper.bind_widgets(binder_widget): 238 time.sleep(self.DELAY_AFTER_BINDING) 239 self.play_reboot_play_and_record(source_widget, recorder_widget) 240 else: 241 self.play_reboot_play_and_record(source_widget, recorder_widget) 242 243 self.save_and_check_data(recorder_widget) 244