1# Lint as: python2, python3 2# Copyright 2014 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5"""This is a server side HDMI audio test using the Chameleon board.""" 6 7import logging 8import os 9import time 10 11from autotest_lib.client.cros.audio import audio_test_data 12from autotest_lib.client.cros.chameleon import audio_test_utils 13from autotest_lib.client.cros.chameleon import chameleon_audio_helper 14from autotest_lib.client.cros.chameleon import chameleon_audio_ids 15from autotest_lib.server.cros.audio import audio_test 16 17 18class audio_AudioBasicHDMI(audio_test.AudioTest): 19 """Server side HDMI audio test. 20 21 This test talks to a Chameleon board and a Cros device to verify 22 HDMI audio function of the Cros device. 23 24 """ 25 version = 2 26 RECORDING_DURATION = 6 27 WEB_PLAYBACK_SEC = 15 28 29 def cleanup(self): 30 """Restore the CPU scaling governor mode.""" 31 self._system_facade.set_scaling_governor_mode(0, self._original_mode) 32 logging.debug('Set CPU0 mode to %s', self._original_mode) 33 34 def set_high_performance_mode(self): 35 """Set the CPU scaling governor mode to performance mode.""" 36 self._original_mode = self._system_facade.set_scaling_governor_mode( 37 0, 'performance') 38 logging.debug( 39 'Set CPU0 scaling governor mode to performance, ' 40 'original_mode: %s', self._original_mode) 41 42 def playback_and_suspend(self, audio_facade, while_playback): 43 """ Does playback and suspend-resume. 44 45 @param audio_facade: audio facade to check nodes. 46 @param while_playback: whether to play when suspending. 47 """ 48 if while_playback: 49 logging.info('Playing audio served on the web...') 50 web_file = audio_test_data.HEADPHONE_10MIN_TEST_FILE 51 file_url = getattr(web_file, 'url', None) 52 browser_facade = self.factory.create_browser_facade() 53 tab_descriptor = browser_facade.new_tab(file_url) 54 time.sleep(self.WEB_PLAYBACK_SEC) 55 audio_test_utils.suspend_resume_and_verify(self.host, self.factory) 56 57 # Stop audio playback by closing the browser tab. 58 if while_playback: 59 browser_facade.close_tab(tab_descriptor) 60 61 # Explicitly select the node as there is a known issue 62 # that the selected node might change after a suspension. 63 # We should remove this after the issue is addressed(crbug:987529). 64 audio_facade.set_selected_node_types(['HDMI'], None) 65 audio_test_utils.check_audio_nodes(audio_facade, (['HDMI'], None)) 66 67 def run_once(self, 68 suspend=False, 69 while_playback=False, 70 check_quality=False): 71 """Running basic HDMI audio tests. 72 73 @param host: device under test host 74 @param suspend: whether to suspend 75 @param while_playback: whether to suspend while audio playback 76 @param check_quality: True to check quality. 77 78 """ 79 golden_file = audio_test_data.GenerateAudioTestData( 80 path=os.path.join(self.bindir, 'fix_2k_1k_16.raw'), 81 duration_secs=10, 82 frequencies=[2000, 1000]) 83 84 # For DUTs with permanently connected audio jack cable 85 # connecting HDMI won't switch automatically the node. Adding 86 # audio_jack_plugged flag to select HDMI node after binding. 87 output_nodes, _ = self.facade.get_selected_node_types() 88 audio_jack_plugged = False 89 if output_nodes == ['HEADPHONE'] or output_nodes == ['LINEOUT']: 90 audio_jack_plugged = True 91 logging.debug('Found audio jack plugged!') 92 93 self._system_facade = self.factory.create_system_facade() 94 self.set_high_performance_mode() 95 96 source = self.widget_factory.create_widget( 97 chameleon_audio_ids.CrosIds.HDMI) 98 recorder = self.widget_factory.create_widget( 99 chameleon_audio_ids.ChameleonIds.HDMI) 100 binder = self.widget_factory.create_binder(source, recorder) 101 102 with chameleon_audio_helper.bind_widgets(binder): 103 audio_test_utils.dump_cros_audio_logs( 104 self.host, self.facade, self.resultsdir, 'after_binding') 105 106 # HDMI node needs to be selected, when audio jack is plugged 107 if audio_jack_plugged: 108 self.facade.set_chrome_active_node_type('HDMI', None) 109 audio_test_utils.check_audio_nodes(self.facade, (['HDMI'], None)) 110 111 # Suspend after playing audio (if directed) and resume 112 # before the HDMI audio test. 113 if suspend: 114 self.playback_and_suspend(self.facade, while_playback) 115 116 source.set_playback_data(golden_file) 117 118 # There is a known issue that if we reopen the HDMI device, 119 # chameleon will interrupt recording (crbug.com/1027040). However, 120 # if there is another stream playing earlier, CRAS may need to 121 # reopen the device during recording. To fix it, we should move 122 # playback before the recording of chameleon. 123 logging.info('Start playing %s on Cros device', golden_file.path) 124 source.start_playback() 125 126 logging.info('Start recording from Chameleon.') 127 recorder.start_recording() 128 129 time.sleep(self.RECORDING_DURATION) 130 131 audio_test_utils.dump_cros_audio_logs( 132 self.host, self.facade, self.resultsdir, 'after_recording') 133 134 recorder.stop_recording() 135 logging.info('Stopped recording from Chameleon.') 136 recorder.read_recorded_binary() 137 138 recorded_file = os.path.join(self.resultsdir, "recorded.raw") 139 logging.info('Saving recorded data to %s', recorded_file) 140 recorder.save_file(recorded_file) 141 142 audio_test_utils.check_recorded_frequency( 143 golden_file, recorder, check_artifacts=check_quality) 144