def run_once(self, host, suspend=False): golden_file = audio_test_data.SWEEP_TEST_FILE chameleon_board = host.chameleon factory = remote_facade_factory.RemoteFacadeFactory( host, results_dir=self.resultsdir) chameleon_board.setup_and_reset(self.outputdir) widget_factory = chameleon_audio_helper.AudioWidgetFactory( factory, host) source = widget_factory.create_widget( chameleon_audio_ids.CrosIds.USBOUT) recorder = widget_factory.create_widget( chameleon_audio_ids.ChameleonIds.USBIN) binder = widget_factory.create_binder(source, recorder) with chameleon_audio_helper.bind_widgets(binder): # Checks the node selected by cras is correct. audio_facade = factory.create_audio_facade() audio_test_utils.dump_cros_audio_logs(host, audio_facade, self.resultsdir, 'after_binding') audio_test_utils.check_and_set_chrome_active_node_types( audio_facade, 'USB', None) audio_test_utils.dump_cros_audio_logs(host, audio_facade, self.resultsdir, 'after_select') audio_test_utils.check_audio_nodes(audio_facade, (['USB'], None)) logging.info('Setting playback data on Cros device') audio_facade.set_selected_output_volume(70) source.set_playback_data(golden_file) if suspend: audio_test_utils.suspend_resume(host, self.SUSPEND_SECONDS) utils.poll_for_condition(condition=factory.ready, timeout=self.RPC_RECONNECT_TIMEOUT, desc='multimedia server reconnect') audio_test_utils.check_audio_nodes(audio_facade, (['USB'], None)) # Starts recording from Chameleon, waits for some time, and then # starts playing from Cros device. logging.info('Start recording from Chameleon.') recorder.start_recording() logging.info('Start playing %s on Cros device', golden_file.path) source.start_playback() time.sleep(self.RECORD_SECONDS) recorder.stop_recording() logging.info('Stopped recording from Chameleon.') audio_test_utils.dump_cros_audio_logs(host, audio_facade, self.resultsdir, 'after_recording') recorder.read_recorded_binary() logging.info('Read recorded binary from Chameleon.') recorded_file = os.path.join(self.resultsdir, "recorded.raw") logging.info('Saving recorded data to %s', recorded_file) recorder.save_file(recorded_file) audio_test_utils.compare_recorded_correlation(golden_file, recorder)
def run_once(self, host, suspend=False, disable=False, disconnect=False, check_quality=False): """Running Bluetooth basic audio tests @param host: device under test host @param suspend: suspend flag to enable suspend before play/record @param disable: disable flag to disable BT module before play/record @param disconnect: disconnect flag to disconnect BT module before play/record @param check_quality: flag to check audio quality. """ self.host = host golden_file = audio_test_data.FREQUENCY_TEST_FILE factory = self.create_remote_facade_factory(host) self.audio_facade = factory.create_audio_facade() chameleon_board = host.chameleon chameleon_board.reset() widget_factory = chameleon_audio_helper.AudioWidgetFactory( factory, host) source = widget_factory.create_widget( chameleon_audio_ids.CrosIds.BLUETOOTH_HEADPHONE) bluetooth_widget = widget_factory.create_widget( chameleon_audio_ids.PeripheralIds.BLUETOOTH_DATA_RX) recorder = widget_factory.create_widget( chameleon_audio_ids.ChameleonIds.LINEIN) binder = widget_factory.create_binder( source, bluetooth_widget, recorder) with chameleon_audio_helper.bind_widgets(binder): audio_test_utils.dump_cros_audio_logs( host, self.audio_facade, self.resultsdir, 'after_binding') # Checks the node selected by Cras is correct. audio_test_utils.check_audio_nodes(self.audio_facade, (['BLUETOOTH'], None)) self.audio_facade.set_selected_output_volume(80) # Starts playing, waits for some time, and then starts recording. # This is to avoid artifact caused by codec initialization. source.set_playback_data(golden_file) # Create link to control BT adapter. link = binder.get_binders()[0].get_link() if disable: self.disable_enable_bt(link) if disconnect: self.disconnect_connect_bt(link) if suspend: audio_test_utils.suspend_resume(host, self.SUSPEND_SECONDS) utils.poll_for_condition(condition=factory.ready, timeout=self.PRC_RECONNECT_TIMEOUT, desc='multimedia server reconnect') if disable or disconnect or suspend: audio_test_utils.dump_cros_audio_logs( host, self.audio_facade, self.resultsdir, 'after_action') # Gives DUT some time to auto-reconnect bluetooth after resume. if suspend: utils.poll_for_condition( condition=self.bluetooth_nodes_plugged, timeout=self.BLUETOOTH_RECONNECT_TIMEOUT_SECS, desc='bluetooth node auto-reconnect after suspend') # Delay some time for A2DP to be reconnected. # Normally this happens several seconds after HSP is # reconnected. However, there is no event what we can wait for, # so just wait some time. time.sleep(self.DELAY_FOR_A2DP_RECONNECT_AFTER_SUSPEND) with audio_test_utils.monitor_no_nodes_changed( self.audio_facade, self.dump_logs_after_nodes_changed): # Checks the node selected by Cras is correct again. audio_test_utils.check_audio_nodes(self.audio_facade, (['BLUETOOTH'], None)) logging.info('Start playing %s on Cros device', golden_file.path) source.start_playback() time.sleep(self.DELAY_BEFORE_RECORD_SECONDS) logging.info('Start recording from Chameleon.') recorder.start_recording() time.sleep(self.RECORD_SECONDS) recorder.stop_recording() logging.info('Stopped recording from Chameleon.') audio_test_utils.dump_cros_audio_logs( host, self.audio_facade, self.resultsdir, 'after_recording') recorder.read_recorded_binary() logging.info('Read recorded binary from Chameleon.') recorded_file = os.path.join(self.resultsdir, "recorded.raw") logging.info('Saving recorded data to %s', recorded_file) recorder.save_file(recorded_file) # Removes the beginning of recorded data. This is to avoid artifact # caused by Chameleon codec initialization in the beginning of # recording. recorder.remove_head(0.5) recorded_file = os.path.join(self.resultsdir, "recorded_clipped.raw") logging.info('Saving clipped data to %s', recorded_file) recorder.save_file(recorded_file) # Compares data by frequency. Audio signal recorded by microphone has # gone through analog processing and through the air. # This suffers from codec artifacts and noise on the path. # Comparing data by frequency is more robust than comparing by # correlation, which is suitable for fully-digital audio path like USB # and HDMI. audio_test_utils.check_recorded_frequency( golden_file, recorder, check_anomaly=check_quality)
def run_once(self, host, suspend=False): golden_file = audio_test_data.SWEEP_TEST_FILE chameleon_board = host.chameleon factory = self.create_remote_facade_factory(host) chameleon_board.reset() widget_factory = chameleon_audio_helper.AudioWidgetFactory( factory, host) playback_source = widget_factory.create_widget( chameleon_audio_ids.CrosIds.USBOUT) playback_recorder = widget_factory.create_widget( chameleon_audio_ids.ChameleonIds.USBIN) playback_binder = widget_factory.create_binder(playback_source, playback_recorder) record_source = widget_factory.create_widget( chameleon_audio_ids.ChameleonIds.USBOUT) record_recorder = widget_factory.create_widget( chameleon_audio_ids.CrosIds.USBIN) record_binder = widget_factory.create_binder(record_source, record_recorder) with chameleon_audio_helper.bind_widgets(playback_binder): with chameleon_audio_helper.bind_widgets(record_binder): # Checks the node selected by cras is correct. audio_facade = factory.create_audio_facade() audio_test_utils.dump_cros_audio_logs(host, audio_facade, self.resultsdir, 'after_binding') audio_test_utils.check_audio_nodes(audio_facade, (['USB'], ['USB'])) logging.info('Setting playback data on Cros device') audio_facade.set_selected_output_volume(70) playback_source.set_playback_data(golden_file) record_source.set_playback_data(golden_file) if suspend: audio_test_utils.suspend_resume(host, self.SUSPEND_SECONDS) utils.poll_for_condition( condition=factory.ready, timeout=self.RPC_RECONNECT_TIMEOUT, desc='multimedia server reconnect') audio_test_utils.check_audio_nodes(audio_facade, (['USB'], ['USB'])) logging.info('Start recording from Chameleon.') playback_recorder.start_recording() logging.info('Start recording from Cros device.') record_recorder.start_recording() logging.info('Start playing %s on Cros device', golden_file.path) playback_source.start_playback() logging.info('Start playing %s on Chameleon', golden_file.path) record_source.start_playback() time.sleep(self.RECORD_SECONDS) playback_recorder.stop_recording() logging.info('Stopped recording from Chameleon.') record_recorder.stop_recording() logging.info('Stopped recording from Cros device.') audio_test_utils.dump_cros_audio_logs(host, audio_facade, self.resultsdir, 'after_recording') playback_recorder.read_recorded_binary() logging.info('Read recorded binary from Chameleon.') record_recorder.read_recorded_binary() logging.info('Read recorded binary from Cros device.') playback_recorded_file = os.path.join(self.resultsdir, "playback_recorded.raw") logging.info('Saving Cros playback recorded data to %s', playback_recorded_file) playback_recorder.save_file(playback_recorded_file) record_recorded_file = os.path.join(self.resultsdir, "record_recorded.raw") logging.info('Saving Cros record recorded data to %s', record_recorded_file) record_recorder.save_file(record_recorded_file) error_messages = '' if not chameleon_audio_helper.compare_recorded_result( golden_file, playback_recorder, 'correlation'): error_messages += ('Record: Recorded file does not match' ' playback file.') if not chameleon_audio_helper.compare_recorded_result( golden_file, record_recorder, 'correlation'): error_messages += ('Playback: Recorded file does not match' ' playback file.') if error_messages: raise error.TestFail(error_messages)
def run_once(self, host, suspend=False, disable=False, disconnect=False, check_quality=False): """Running Bluetooth basic audio tests @param host: device under test host @param suspend: suspend flag to enable suspend before play/record @param disable: disable flag to disable BT module before play/record @param disconnect: disconnect flag to disconnect BT module before play/record @param check_quality: flag to check audio quality. """ self.host = host # Bluetooth HSP/HFP profile only supports one channel # playback/recording. So we should use simple frequency # test file which contains identical sine waves in two # channels. golden_file = audio_test_data.SIMPLE_FREQUENCY_TEST_FILE factory = self.create_remote_facade_factory(host) self.audio_facade = factory.create_audio_facade() chameleon_board = host.chameleon chameleon_board.reset() widget_factory = chameleon_audio_helper.AudioWidgetFactory( factory, host) playback_source = widget_factory.create_widget( chameleon_audio_ids.CrosIds.BLUETOOTH_HEADPHONE) playback_bluetooth_widget = widget_factory.create_widget( chameleon_audio_ids.PeripheralIds.BLUETOOTH_DATA_RX) playback_recorder = widget_factory.create_widget( chameleon_audio_ids.ChameleonIds.LINEIN) playback_binder = widget_factory.create_binder( playback_source, playback_bluetooth_widget, playback_recorder) record_source = widget_factory.create_widget( chameleon_audio_ids.ChameleonIds.LINEOUT) record_bluetooth_widget = widget_factory.create_widget( chameleon_audio_ids.PeripheralIds.BLUETOOTH_DATA_TX) record_recorder = widget_factory.create_widget( chameleon_audio_ids.CrosIds.BLUETOOTH_MIC) record_binder = widget_factory.create_binder( record_source, record_bluetooth_widget, record_recorder) with chameleon_audio_helper.bind_widgets(playback_binder): with chameleon_audio_helper.bind_widgets(record_binder): audio_test_utils.dump_cros_audio_logs( host, self.audio_facade, self.resultsdir, 'after_binding') # Checks the input node selected by Cras is internal microphone. # Checks crbug.com/495537 for the reason to lower bluetooth # microphone priority. if audio_test_utils.has_internal_microphone(host): audio_test_utils.check_audio_nodes(self.audio_facade, (None, ['INTERNAL_MIC'])) self.audio_facade.set_selected_output_volume(80) # Selecting input nodes needs to be after setting volume because # after setting volume, Cras notifies Chrome there is changes # in nodes, and Chrome selects the output/input nodes based # on its preference again. See crbug.com/535643. # Selects bluetooth mic to be the active input node. if audio_test_utils.has_internal_microphone(host): self.audio_facade.set_chrome_active_node_type( None, 'BLUETOOTH') # Checks the node selected by Cras is correct. audio_test_utils.check_audio_nodes(self.audio_facade, (['BLUETOOTH'], ['BLUETOOTH'])) # Setup the playback data. This step is time consuming. playback_source.set_playback_data(golden_file) record_source.set_playback_data(golden_file) # Create links to control disconnect and off/on BT adapter. link = playback_binder.get_binders()[0].get_link() if disable: self.disable_enable_bt(link) if disconnect: self.disconnect_connect_bt(link) if suspend: audio_test_utils.suspend_resume(host, self.SUSPEND_SECONDS) if disable or disconnect or suspend: audio_test_utils.dump_cros_audio_logs( host, self.audio_facade, self.resultsdir, 'after_action') utils.poll_for_condition(condition=factory.ready, timeout=self.PRC_RECONNECT_TIMEOUT, desc='multimedia server reconnect') # Gives DUT some time to auto-reconnect bluetooth after resume. if suspend: utils.poll_for_condition( condition=self.bluetooth_nodes_plugged, timeout=self.BLUETOOTH_RECONNECT_TIMEOUT_SECS, desc='bluetooth node auto-reconnect after suspend') if audio_test_utils.has_internal_microphone(host): # Select again BT input, as default input node is # INTERNAL_MIC. self.audio_facade.set_chrome_active_node_type( None, 'BLUETOOTH') with audio_test_utils.monitor_no_nodes_changed( self.audio_facade, self.dump_logs_after_nodes_changed): # Checks the node selected by Cras is correct again. audio_test_utils.check_audio_nodes(self.audio_facade, (['BLUETOOTH'], ['BLUETOOTH'])) # Starts playing, waits for some time, and then starts # recording. This is to avoid artifact caused by codec # initialization. logging.info('Start playing %s on Cros device', golden_file.path) playback_source.start_playback() logging.info('Start playing %s on Chameleon device', golden_file.path) record_source.start_playback() time.sleep(self.DELAY_BEFORE_RECORD_SECONDS) logging.info('Start recording from Chameleon.') playback_recorder.start_recording() logging.info('Start recording from Cros device.') record_recorder.start_recording() time.sleep(self.RECORD_SECONDS) playback_recorder.stop_recording() logging.info('Stopped recording from Chameleon.') record_recorder.stop_recording() logging.info('Stopped recording from Cros device.') audio_test_utils.dump_cros_audio_logs( host, self.audio_facade, self.resultsdir, 'after_recording') # Sleeps until playback data ends to prevent audio from # going to internal speaker. time.sleep(self.SLEEP_AFTER_RECORD_SECONDS) # Gets the recorded data. This step is time consuming. playback_recorder.read_recorded_binary() logging.info('Read recorded binary from Chameleon.') record_recorder.read_recorded_binary() logging.info('Read recorded binary from Chameleon.') recorded_file = os.path.join( self.resultsdir, "playback_recorded.raw") logging.info('Playback: Saving recorded data to %s', recorded_file) playback_recorder.save_file(recorded_file) recorded_file = os.path.join( self.resultsdir, "record_recorded.raw") logging.info('Record: Saving recorded data to %s', recorded_file) record_recorder.save_file(recorded_file) # Removes the beginning of recorded data. This is to avoid artifact # caused by Chameleon codec initialization in the beginning of # recording. playback_recorder.remove_head(0.5) # Removes the beginning of recorded data. This is to avoid artifact # caused by bluetooth module initialization in the beginning of # its playback. record_recorder.remove_head(0.5) recorded_file = os.path.join(self.resultsdir, "playback_clipped.raw") logging.info('Saving clipped data to %s', recorded_file) playback_recorder.save_file(recorded_file) recorded_file = os.path.join(self.resultsdir, "record_clipped.raw") logging.info('Saving clipped data to %s', recorded_file) record_recorder.save_file(recorded_file) # Compares data by frequency. Audio signal recorded by microphone has # gone through analog processing and through the air. # This suffers from codec artifacts and noise on the path. # Comparing data by frequency is more robust than comparing by # correlation, which is suitable for fully-digital audio path like USB # and HDMI. # Use a second peak ratio that can tolerate more noise because HSP # is low-quality. second_peak_ratio = audio_test_utils.HSP_SECOND_PEAK_RATIO error_messages = '' try: audio_test_utils.check_recorded_frequency( golden_file, playback_recorder, check_anomaly=check_quality, second_peak_ratio=second_peak_ratio) except error.TestFail, e: error_messages += str(e)
def run_once(self, host, suspend=False, disable=False, disconnect=False, check_quality=False): """Running Bluetooth basic audio tests @param host: device under test host @param suspend: suspend flag to enable suspend before play/record @param disable: disable flag to disable BT module before play/record @param disconnect: disconnect flag to disconnect BT module before play/record @param check_quality: flag to check audio quality. """ self.host = host golden_file = audio_test_data.SIMPLE_FREQUENCY_TEST_FILE factory = remote_facade_factory.RemoteFacadeFactory( host, results_dir=self.resultsdir) self.audio_facade = factory.create_audio_facade() chameleon_board = host.chameleon chameleon_board.setup_and_reset(self.outputdir) widget_factory = chameleon_audio_helper.AudioWidgetFactory( factory, host) source = widget_factory.create_widget( chameleon_audio_ids.ChameleonIds.LINEOUT) bluetooth_widget = widget_factory.create_widget( chameleon_audio_ids.PeripheralIds.BLUETOOTH_DATA_TX) recorder = widget_factory.create_widget( chameleon_audio_ids.CrosIds.BLUETOOTH_MIC) binder = widget_factory.create_binder(source, bluetooth_widget, recorder) second_peak_ratio = audio_test_utils.get_second_peak_ratio( source_id=source.port_id, recorder_id=recorder.port_id, is_hsp=True) with chameleon_audio_helper.bind_widgets(binder): audio_test_utils.dump_cros_audio_logs(host, self.audio_facade, self.resultsdir, 'after_binding') # For DUTs with permanently connected audio jack cable # Bluetooth output node should be selected explicitly. output_nodes, _ = self.audio_facade.get_plugged_node_types() audio_jack_plugged = False if 'HEADPHONE' in output_nodes or 'LINEOUT' in output_nodes: audio_jack_plugged = True audio_test_utils.check_audio_nodes(self.audio_facade, (None, ['MIC'])) elif audio_test_utils.has_internal_microphone(host): # Checks the input node selected by Cras is internal microphone. # Checks crbug.com/495537 for the reason to lower bluetooth # microphone priority. audio_test_utils.check_audio_nodes(self.audio_facade, (None, ['INTERNAL_MIC'])) # Selects bluetooth mic to be the active input node. if (audio_test_utils.has_internal_microphone(host) or audio_jack_plugged): self.audio_facade.set_chrome_active_node_type( None, 'BLUETOOTH') # Checks the node selected by Cras is correct again. audio_test_utils.check_audio_nodes(self.audio_facade, (None, ['BLUETOOTH'])) # Starts playing, waits for some time, and then starts recording. # This is to avoid artifact caused by codec initialization. source.set_playback_data(golden_file) # Create link to control BT adapter. link = binder.get_binders()[1].get_link() if disable: self.disable_enable_bt(link) if disconnect: self.disconnect_connect_bt(link) if suspend: audio_test_utils.suspend_resume(host, self.SUSPEND_SECONDS) utils.poll_for_condition(condition=factory.ready, timeout=self.PRC_RECONNECT_TIMEOUT, desc='multimedia server reconnect') if disable or disconnect or suspend: audio_test_utils.dump_cros_audio_logs(host, self.audio_facade, self.resultsdir, 'after_action') # Gives DUT some time to auto-reconnect bluetooth after resume. if suspend: utils.poll_for_condition( condition=self.bluetooth_nodes_plugged, timeout=self.BLUETOOTH_RECONNECT_TIMEOUT_SECS, desc='bluetooth node auto-reconnect after suspend') if audio_test_utils.has_internal_microphone(host): # Select again BT input as default input node is INTERNAL_MIC self.audio_facade.set_chrome_active_node_type( None, 'BLUETOOTH') with audio_test_utils.monitor_no_nodes_changed( self.audio_facade, self.dump_logs_after_nodes_changed): audio_test_utils.check_audio_nodes(self.audio_facade, (None, ['BLUETOOTH'])) logging.info('Start playing %s on Chameleon', golden_file.path) source.start_playback() time.sleep(self.DELAY_BEFORE_RECORD_SECONDS) logging.info('Start recording from Cros device.') recorder.start_recording() time.sleep(self.RECORD_SECONDS) recorder.stop_recording() logging.info('Stopped recording from Cros device.') audio_test_utils.dump_cros_audio_logs(host, self.audio_facade, self.resultsdir, 'after_recording') recorder.read_recorded_binary() logging.info('Read recorded binary from Chameleon.') recorded_file = os.path.join(self.resultsdir, "recorded.raw") logging.info('Saving recorded data to %s', recorded_file) recorder.save_file(recorded_file) # Removes the beginning of recorded data. This is to avoid artifact # caused by bluetooth module initialization in the beginning of # its playback. recorder.remove_head(0.5) recorded_file = os.path.join(self.resultsdir, "recorded_clipped.raw") logging.info('Saving clipped data to %s', recorded_file) recorder.save_file(recorded_file) # Compares data by frequency. Audio signal recorded by microphone has # gone through analog processing and through the air. # This suffers from codec artifacts and noise on the path. # Comparing data by frequency is more robust than comparing by # correlation, which is suitable for fully-digital audio path like USB # and HDMI. audio_test_utils.check_recorded_frequency( golden_file, recorder, check_anomaly=check_quality, second_peak_ratio=second_peak_ratio)