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 HDMI audio test using the Chameleon board.""" 6 7import logging 8import os 9import threading 10import time 11 12from autotest_lib.client.cros.audio import audio_test_data 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_AudioBasicHDMI(audio_test.AudioTest): 21 """Server side HDMI audio test. 22 23 This test talks to a Chameleon board and a Cros device to verify 24 HDMI audio function of the Cros device. 25 26 """ 27 version = 2 28 DELAY_BEFORE_PLAYBACK = 2 29 DELAY_AFTER_PLAYBACK = 2 30 CONNECT_TIMEOUT_SEC = 30 31 SUSPEND_SEC = 15 32 WAIT_TO_REBIND_SEC = 15 33 WEB_PLAYBACK_SEC = 15 34 35 def cleanup(self): 36 """Restore the CPU scaling governor mode.""" 37 self._system_facade.set_scaling_governor_mode(0, self._original_mode) 38 logging.debug('Set CPU0 mode to %s', self._original_mode) 39 40 41 def set_high_performance_mode(self): 42 """Set the CPU scaling governor mode to performance mode.""" 43 self._original_mode = self._system_facade.set_scaling_governor_mode( 44 0, 'performance') 45 logging.debug('Set CPU0 scaling governor mode to performance, ' 46 'original_mode: %s', self._original_mode) 47 48 49 def playback_and_suspend(self, audio_facade, while_playback): 50 """ Does playback and suspend-resume. 51 52 @param audio_facade: audio facade to check nodes. 53 @param while_playback: whether to play when suspending. 54 """ 55 if while_playback: 56 logging.info('Playing audio served on the web...') 57 web_file = audio_test_data.HEADPHONE_10MIN_TEST_FILE 58 file_url = getattr(web_file, 'url', None) 59 browser_facade = self.factory.create_browser_facade() 60 tab_descriptor = browser_facade.new_tab(file_url) 61 time.sleep(self.WEB_PLAYBACK_SEC) 62 logging.info('Suspending...') 63 boot_id = self.host.get_boot_id() 64 self.host.suspend(suspend_time=self.SUSPEND_SEC) 65 self.host.test_wait_for_resume(boot_id, self.CONNECT_TIMEOUT_SEC) 66 logging.info('Resumed and back online.') 67 time.sleep(self.WAIT_TO_REBIND_SEC) 68 69 # Stop audio playback by closing the browser tab. 70 if while_playback: 71 browser_facade.close_tab(tab_descriptor) 72 audio_test_utils.check_audio_nodes(audio_facade, 73 (['HDMI'], None)) 74 75 76 def run_once(self, host, suspend=False, while_playback=False, 77 check_quality=False): 78 """Running basic HDMI audio tests. 79 80 @param host: device under test host 81 @param suspend: whether to suspend 82 @param while_playback: whether to suspend while audio playback 83 @param check_quality: True to check quality. 84 85 """ 86 golden_file = audio_test_data.FREQUENCY_TEST_FILE 87 self.host = host 88 89 # Dump audio diagnostics data for debugging. 90 chameleon_board = host.chameleon 91 self.factory = remote_facade_factory.RemoteFacadeFactory( 92 host, results_dir=self.resultsdir) 93 94 # For DUTs with permanently connected audio jack cable 95 # connecting HDMI won't switch automatically the node. Adding 96 # audio_jack_plugged flag to select HDMI node after binding. 97 audio_facade = self.factory.create_audio_facade() 98 output_nodes, _ = audio_facade.get_selected_node_types() 99 audio_jack_plugged = False 100 if output_nodes == ['HEADPHONE'] or output_nodes == ['LINEOUT']: 101 audio_jack_plugged = True 102 logging.debug('Found audio jack plugged!') 103 104 self._system_facade = self.factory.create_system_facade() 105 self.set_high_performance_mode() 106 107 chameleon_board.setup_and_reset(self.outputdir) 108 109 widget_factory = chameleon_audio_helper.AudioWidgetFactory( 110 self.factory, host) 111 112 source = widget_factory.create_widget( 113 chameleon_audio_ids.CrosIds.HDMI) 114 recorder = widget_factory.create_widget( 115 chameleon_audio_ids.ChameleonIds.HDMI) 116 binder = widget_factory.create_binder(source, recorder) 117 118 with chameleon_audio_helper.bind_widgets(binder): 119 audio_test_utils.dump_cros_audio_logs( 120 host, audio_facade, self.resultsdir, 'after_binding') 121 122 # HDMI node needs to be selected, when audio jack is plugged 123 if audio_jack_plugged: 124 audio_facade.set_chrome_active_node_type('HDMI', None) 125 audio_test_utils.check_audio_nodes(audio_facade, 126 (['HDMI'], None)) 127 128 # Suspend after playing audio (if directed) and resume 129 # before the HDMI audio test. 130 if suspend: 131 self.playback_and_suspend(audio_facade, while_playback) 132 133 source.set_playback_data(golden_file) 134 135 logging.info('Start recording from Chameleon.') 136 recorder.start_recording() 137 138 time.sleep(self.DELAY_BEFORE_PLAYBACK) 139 140 logging.info('Start playing %s on Cros device', 141 golden_file.path) 142 source.start_playback(blocking=True) 143 144 logging.info('Stopped playing %s on Cros device', 145 golden_file.path) 146 time.sleep(self.DELAY_AFTER_PLAYBACK) 147 148 audio_test_utils.dump_cros_audio_logs( 149 host, audio_facade, self.resultsdir, 'after_recording') 150 151 recorder.stop_recording() 152 logging.info('Stopped recording from Chameleon.') 153 recorder.read_recorded_binary() 154 155 recorded_file = os.path.join(self.resultsdir, "recorded.raw") 156 logging.info('Saving recorded data to %s', recorded_file) 157 recorder.save_file(recorded_file) 158 159 audio_test_utils.check_recorded_frequency( 160 golden_file, recorder, check_artifacts=check_quality) 161