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 audio_test_utils.check_audio_nodes(audio_facade, (['HEADPHONE'], None)) 69 70 logging.info('Setting playback data on Cros device') 71 source.set_playback_data(golden_file) 72 73 # Starts playing, waits for some time, and then starts recording. 74 # This is to avoid artifact caused by codec initialization. 75 logging.info('Start playing %s on Cros device', 76 golden_file.path) 77 source.start_playback() 78 79 time.sleep(self.DELAY_BEFORE_RECORD_SECONDS) 80 logging.info('Start recording from Chameleon.') 81 recorder.start_recording() 82 83 time.sleep(self.RECORD_SECONDS) 84 85 if check_quality: 86 # Add a silence while recording to detect audio 87 # spikes after playback. 88 time.sleep(self.SILENCE_WAIT) 89 90 recorder.stop_recording() 91 logging.info('Stopped recording from Chameleon.') 92 93 audio_test_utils.dump_cros_audio_logs( 94 host, audio_facade, self.resultsdir, 'after_recording') 95 96 recorder.read_recorded_binary() 97 logging.info('Read recorded binary from Chameleon.') 98 99 recorded_file = os.path.join(self.resultsdir, "recorded.raw") 100 logging.info('Saving recorded data to %s', recorded_file) 101 recorder.save_file(recorded_file) 102 103 # Removes the beginning of recorded data. This is to avoid artifact 104 # caused by Chameleon codec initialization in the beginning of 105 # recording. 106 recorder.remove_head(0.5) 107 108 recorded_file = os.path.join(self.resultsdir, "recorded_clipped.raw") 109 logging.info('Saving clipped data to %s', recorded_file) 110 recorder.save_file(recorded_file) 111 112 # Compares data by frequency. Headphone audio signal has gone through 113 # analog processing. This suffers from codec artifacts and noise on the 114 # path. Comparing data by frequency is more robust than comparing by 115 # correlation, which is suitable for fully-digital audio path like USB 116 # and HDMI. 117 audio_test_utils.check_recorded_frequency( 118 golden_file, recorder, check_artifacts=check_quality) 119