1 # Copyright 2014 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 headphone audio test using the Chameleon board.""" 6 7 import logging 8 import os 9 import time 10 11 from autotest_lib.client.common_lib import error 12 from autotest_lib.client.cros.audio import audio_test_data 13 from autotest_lib.client.cros.chameleon import audio_test_utils 14 from autotest_lib.client.cros.chameleon import chameleon_audio_ids 15 from autotest_lib.client.cros.chameleon import chameleon_audio_helper 16 from autotest_lib.server.cros.audio import audio_test 17 from autotest_lib.server.cros.multimedia import remote_facade_factory 18 19 20 class audio_AudioBasicHeadphone(audio_test.AudioTest): 21 """Server side headphone audio test. 22 23 This test talks to a Chameleon board and a Cros device to verify 24 headphone audio function of the Cros device. 25 26 """ 27 version = 1 28 DELAY_BEFORE_RECORD_SECONDS = 0.5 29 RECORD_SECONDS = 5 30 DELAY_AFTER_BINDING = 0.5 31 SILENCE_WAIT = 5 32 33 def run_once(self, host, check_quality=False): 34 """Running basic headphone audio tests. 35 36 @param host: device under test host 37 @param check_quality: flag to check audio quality. 38 39 """ 40 if not audio_test_utils.has_headphone(host): 41 return 42 43 golden_file = audio_test_data.FREQUENCY_TEST_FILE 44 45 chameleon_board = host.chameleon 46 factory = remote_facade_factory.RemoteFacadeFactory( 47 host, results_dir=self.resultsdir) 48 49 chameleon_board.setup_and_reset(self.outputdir) 50 51 widget_factory = chameleon_audio_helper.AudioWidgetFactory( 52 factory, host) 53 54 source = widget_factory.create_widget( 55 chameleon_audio_ids.CrosIds.HEADPHONE) 56 recorder = widget_factory.create_widget( 57 chameleon_audio_ids.ChameleonIds.LINEIN) 58 binder = widget_factory.create_binder(source, recorder) 59 60 with chameleon_audio_helper.bind_widgets(binder): 61 # Checks the node selected by cras is correct. 62 time.sleep(self.DELAY_AFTER_BINDING) 63 audio_facade = factory.create_audio_facade() 64 65 audio_test_utils.dump_cros_audio_logs( 66 host, audio_facade, self.resultsdir, 'after_binding') 67 68 output_nodes, _ = audio_facade.get_selected_node_types() 69 if output_nodes != ['HEADPHONE']: 70 raise error.TestFail( 71 '%s rather than headphone is selected on Cros ' 72 'device' % output_nodes) 73 74 logging.info('Setting playback data on Cros device') 75 source.set_playback_data(golden_file) 76 77 # Starts playing, waits for some time, and then starts recording. 78 # This is to avoid artifact caused by codec initialization. 79 logging.info('Start playing %s on Cros device', 80 golden_file.path) 81 source.start_playback() 82 83 time.sleep(self.DELAY_BEFORE_RECORD_SECONDS) 84 logging.info('Start recording from Chameleon.') 85 recorder.start_recording() 86 87 time.sleep(self.RECORD_SECONDS) 88 89 if check_quality: 90 # Add a silence while recording to detect audio 91 # spikes after playback. 92 time.sleep(self.SILENCE_WAIT) 93 94 recorder.stop_recording() 95 logging.info('Stopped recording from Chameleon.') 96 97 audio_test_utils.dump_cros_audio_logs( 98 host, audio_facade, self.resultsdir, 'after_recording') 99 100 recorder.read_recorded_binary() 101 logging.info('Read recorded binary from Chameleon.') 102 103 recorded_file = os.path.join(self.resultsdir, "recorded.raw") 104 logging.info('Saving recorded data to %s', recorded_file) 105 recorder.save_file(recorded_file) 106 107 # Removes the beginning of recorded data. This is to avoid artifact 108 # caused by Chameleon codec initialization in the beginning of 109 # recording. 110 recorder.remove_head(0.5) 111 112 recorded_file = os.path.join(self.resultsdir, "recorded_clipped.raw") 113 logging.info('Saving clipped data to %s', recorded_file) 114 recorder.save_file(recorded_file) 115 116 # Compares data by frequency. Headphone audio signal has gone through 117 # analog processing. This suffers from codec artifacts and noise on the 118 # path. Comparing data by frequency is more robust than comparing by 119 # correlation, which is suitable for fully-digital audio path like USB 120 # and HDMI. 121 audio_test_utils.check_recorded_frequency( 122 golden_file, recorder, check_artifacts=check_quality) 123