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 10import threading 11 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 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_AudioAfterSuspend(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. 25 26 """ 27 version = 1 28 DELAY_BEFORE_RECORD_SECONDS = 0.5 29 RECORD_SECONDS = 5 30 RESUME_TIMEOUT_SECS = 60 31 SHORT_WAIT = 2 32 SUSPEND_SECONDS = 40 33 34 35 def action_plug_jack(self, plug_state): 36 """Calls the audio interface API and plugs/unplugs. 37 38 @param plug_state: plug state to switch to 39 40 """ 41 logging.debug('Plugging' if plug_state else 'Unplugging') 42 jack_plugger = self.audio_board.get_jack_plugger() 43 44 # It is not required for the test to have jack plugger. 45 # We'll ignore the plug/unplug action and assume the target device 46 # is all time plugged/unplugged if there is no jack plugger. 47 if jack_plugger is None: 48 logging.debug('Jack plugger is NOT present!') 49 return 50 51 if plug_state: 52 jack_plugger.plug() 53 audio_test_utils.check_plugged_nodes_contain(self.audio_facade, 54 self.audio_nodes) 55 else: 56 jack_plugger.unplug() 57 time.sleep(self.SHORT_WAIT) 58 59 60 def action_suspend(self, suspend_time=SUSPEND_SECONDS): 61 """Calls the host method suspend. 62 63 @param suspend_time: time to suspend the device for. 64 65 """ 66 self.host.suspend(suspend_time=suspend_time) 67 68 69 def suspend_resume(self, plugged_before_suspend, plugged_after_suspend, 70 plugged_before_resume, test_case): 71 """Performs the mix of suspend/resume and plug/unplug 72 73 @param plugged_before_suspend: plug state before suspend 74 @param plugged_after_suspend: plug state after suspend 75 @param plugged_before_resume: plug state before resume 76 @param test_case: string identifying test case sequence 77 78 """ 79 80 # Suspend 81 boot_id = self.host.get_boot_id() 82 thread = threading.Thread(target=self.action_suspend) 83 thread.start() 84 try: 85 self.host.test_wait_for_sleep(3 * self.SUSPEND_SECONDS / 4) 86 except error.TestFail, ex: 87 self.errors.append("%s - %s" % (test_case, str(ex))) 88 89 # Plugged after suspended 90 self.action_plug_jack(plugged_after_suspend) 91 92 # Plugged before resumed 93 self.action_plug_jack(plugged_before_resume) 94 try: 95 self.host.test_wait_for_resume(boot_id, self.RESUME_TIMEOUT_SECS) 96 except error.TestFail, ex: 97 self.errors.append("%s - %s" % (test_case, str(ex))) 98 99 100 def check_correct_audio_node_selected(self): 101 """Checks the node selected by Cras is correct.""" 102 audio_test_utils.check_audio_nodes(self.audio_facade, self.audio_nodes) 103 104 105 def play_and_record(self, source_widget, recorder_widget): 106 """Plays and records audio 107 108 @param source_widget: widget to do the playback 109 @param recorder_widget: widget to do the recording 110 111 """ 112 audio_test_utils.dump_cros_audio_logs( 113 self.host, self.audio_facade, self.resultsdir, 114 'before_playback') 115 116 self.check_correct_audio_node_selected() 117 118 # Play, wait for some time, and then start recording. 119 # This is to avoid artifact caused by codec initialization. 120 source_widget.set_playback_data(self.golden_file) 121 logging.debug('Start playing %s', self.golden_file.path) 122 source_widget.start_playback() 123 124 time.sleep(self.DELAY_BEFORE_RECORD_SECONDS) 125 logging.debug('Start recording.') 126 recorder_widget.start_recording() 127 128 time.sleep(self.RECORD_SECONDS) 129 130 recorder_widget.stop_recording() 131 logging.debug('Stopped recording.') 132 133 audio_test_utils.dump_cros_audio_logs( 134 self.host, self.audio_facade, self.resultsdir, 135 'after_recording') 136 137 recorder_widget.read_recorded_binary() 138 139 140 def save_and_check_data(self, recorder_widget): 141 """Saves and checks the data from the recorder 142 143 @param recorder_widget: recorder widget to save data from 144 145 @returns (success, error_message): success is True if audio comparison 146 is successful, False otherwise. 147 error_message contains the error 148 message. 149 150 """ 151 recorded_file = os.path.join(self.resultsdir, "recorded.raw") 152 logging.debug('Saving recorded data to %s', recorded_file) 153 recorder_widget.save_file(recorded_file) 154 155 # Removes the beginning of recorded data. This is to avoid artifact 156 # caused by codec initialization in the beginning of 157 # recording. 158 recorder_widget.remove_head(2.0) 159 160 # Removes noise by a lowpass filter. 161 recorder_widget.lowpass_filter(self.low_pass_freq) 162 recorded_file = os.path.join(self.resultsdir, 163 "recorded_filtered.raw") 164 logging.debug('Saving filtered data to %s', recorded_file) 165 recorder_widget.save_file(recorded_file) 166 167 # Compares data by frequency and returns the result. 168 try: 169 audio_test_utils.check_recorded_frequency( 170 self.golden_file, recorder_widget, 171 second_peak_ratio=self.second_peak_ratio, 172 ignore_frequencies=self.ignore_frequencies) 173 except error.TestFail, e: 174 return (False, e) 175 176 return (True, None) 177 178 179 def run_once(self, host, audio_nodes, golden_data, 180 bind_from=None, bind_to=None, 181 source=None, recorder=None, plug_status=None): 182 """Runs the test main workflow 183 184 @param host: A host object representing the DUT. 185 @param audio_nodes: audio nodes supposed to be selected. 186 @param golden_data: audio file and low pass filter frequency 187 the audio file should be test data defined in audio_test_data 188 @param bind_from: audio originating entity to be binded 189 should be defined in chameleon_audio_ids 190 @param bind_to: audio directed_to entity to be binded 191 should be defined in chameleon_audio_ids 192 @param source: source widget entity 193 should be defined in chameleon_audio_ids 194 @param recorder: recorder widget entity 195 should be defined in chameleon_audio_ids 196 @param plug_status: audio channel plug unplug sequence 197 198 """ 199 if (recorder == chameleon_audio_ids.CrosIds.INTERNAL_MIC and 200 not audio_test_utils.has_internal_microphone(host)): 201 return 202 203 if (source == chameleon_audio_ids.CrosIds.SPEAKER and 204 not audio_test_utils.has_internal_speaker(host)): 205 return 206 207 self.host = host 208 self.audio_nodes = audio_nodes 209 210 self.second_peak_ratio = audio_test_utils.get_second_peak_ratio( 211 source_id=source, 212 recorder_id=recorder) 213 214 self.ignore_frequencies = None 215 if source == chameleon_audio_ids.CrosIds.SPEAKER: 216 self.ignore_frequencies = [50, 60] 217 218 self.errors = [] 219 self.golden_file, self.low_pass_freq = golden_data 220 chameleon_board = self.host.chameleon 221 self.factory = remote_facade_factory.RemoteFacadeFactory( 222 self.host, results_dir=self.resultsdir) 223 self.audio_facade = self.factory.create_audio_facade() 224 chameleon_board.setup_and_reset(self.outputdir) 225 widget_factory = chameleon_audio_helper.AudioWidgetFactory( 226 self.factory, host) 227 228 # Two widgets are binded in the factory if necessary 229 binder_widget = None 230 bind_from_widget = None 231 bind_to_widget = None 232 if bind_from != None and bind_to != None: 233 bind_from_widget = widget_factory.create_widget(bind_from) 234 bind_to_widget = widget_factory.create_widget(bind_to) 235 binder_widget = widget_factory.create_binder(bind_from_widget, 236 bind_to_widget) 237 238 # Additional widgets that could be part of the factory 239 if source == None: 240 source_widget = bind_from_widget 241 else: 242 source_widget = widget_factory.create_widget(source) 243 if recorder == None: 244 recorder_widget = bind_to_widget 245 else: 246 recorder_widget = widget_factory.create_widget(recorder) 247 248 self.audio_board = chameleon_board.get_audio_board() 249 250 test_index = 0 251 for (plugged_before_suspend, plugged_after_suspend, plugged_before_resume, 252 plugged_after_resume) in plug_status: 253 test_index += 1 254 255 test_case = ('TEST CASE %d: %s > suspend > %s > %s > resume > %s' % 256 (test_index, 'PLUG' if plugged_before_suspend else 'UNPLUG', 257 'PLUG' if plugged_after_suspend else 'UNPLUG', 258 'PLUG' if plugged_before_resume else 'UNPLUG', 259 'PLUG' if plugged_after_resume else 'UNPLUG')) 260 logging.info(test_case) 261 262 # Plugged status before suspended 263 self.action_plug_jack(plugged_before_suspend) 264 265 self.suspend_resume(plugged_before_suspend, 266 plugged_after_suspend, 267 plugged_before_resume, 268 test_case) 269 270 # Active (plugged for external) state after resume 271 self.action_plug_jack(plugged_after_resume) 272 273 # Explicitly select the node as there is a known issue 274 # that the selected node might change after a suspension. 275 # We should remove this after the issue is addressed(crbug:987529). 276 self.audio_facade.set_selected_node_types(self.audio_nodes[0], 277 self.audio_nodes[1]) 278 279 if binder_widget != None: 280 with chameleon_audio_helper.bind_widgets(binder_widget): 281 self.play_and_record(source_widget, recorder_widget) 282 else: 283 self.play_and_record(source_widget, recorder_widget) 284 285 success, error_message = self.save_and_check_data(recorder_widget) 286 if not success: 287 self.errors.append('%s: Comparison failed: %s' % 288 (test_case, error_message)) 289 290 if self.errors: 291 raise error.TestFail('; '.join(set(self.errors))) 292