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 time
10
11from autotest_lib.client.common_lib import error
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.client.cros.chameleon import chameleon_port_finder
17from autotest_lib.server.cros.audio import audio_test
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
31    def cleanup(self):
32        """Restore the CPU scaling governor mode."""
33        self._system_facade.set_scaling_governor_mode(0, self._original_mode)
34        logging.debug('Set CPU0 mode to %s', self._original_mode)
35
36
37    def set_high_performance_mode(self):
38        """Set the CPU scaling governor mode to performance mode."""
39        self._original_mode = self._system_facade.set_scaling_governor_mode(
40                0, 'performance')
41        logging.debug('Set CPU0 scaling governor mode to performance, '
42                      'original_mode: %s', self._original_mode)
43
44
45    def run_once(self, host):
46        edid_path = os.path.join(self.bindir,
47                                 'test_data/edids/HDMI_DELL_U2410.txt')
48        golden_file = audio_test_data.SWEEP_TEST_FILE
49
50        # Dump audio diagnostics data for debugging.
51        chameleon_board = host.chameleon
52        factory = self.create_remote_facade_factory(host)
53
54        self._system_facade = factory.create_system_facade()
55        self.set_high_performance_mode()
56
57        chameleon_board.reset()
58
59        widget_factory = chameleon_audio_helper.AudioWidgetFactory(
60                factory, host)
61
62        source = widget_factory.create_widget(
63            chameleon_audio_ids.CrosIds.HDMI)
64        recorder = widget_factory.create_widget(
65            chameleon_audio_ids.ChameleonIds.HDMI)
66        binder = widget_factory.create_binder(source, recorder)
67
68        display_facade = factory.create_display_facade()
69        finder = chameleon_port_finder.ChameleonVideoInputFinder(
70                chameleon_board, display_facade)
71        hdmi_port = finder.find_port('HDMI')
72        if not hdmi_port:
73            raise error.TestFail(
74                    'Can not find HDMI port, perhaps HDMI is not connected?')
75        with hdmi_port.use_edid_file(edid_path):
76
77            # TODO(cychiang) remove this when issue crbug.com/450101 is fixed.
78            audio_test_utils.correction_plug_unplug_for_audio(host, hdmi_port)
79
80            with chameleon_audio_helper.bind_widgets(binder):
81                audio_facade = factory.create_audio_facade()
82
83                audio_test_utils.dump_cros_audio_logs(
84                        host, audio_facade, self.resultsdir, 'after_binding')
85
86                output_nodes, _ = audio_facade.get_selected_node_types()
87                if output_nodes != ['HDMI']:
88                    raise error.TestFail(
89                            '%s rather than HDMI is selected on Cros device' %
90                                    output_nodes)
91
92                # Transfer the data to Cros device first because it takes
93                # several seconds.
94                source.set_playback_data(golden_file)
95
96                logging.info('Start recording from Chameleon.')
97                recorder.start_recording()
98
99                time.sleep(self.DELAY_BEFORE_PLAYBACK)
100
101                logging.info('Start playing %s on Cros device',
102                             golden_file.path)
103                source.start_playback(blocking=True)
104
105                logging.info('Stopped playing %s on Cros device',
106                             golden_file.path)
107                time.sleep(self.DELAY_AFTER_PLAYBACK)
108
109                audio_test_utils.dump_cros_audio_logs(
110                        host, audio_facade, self.resultsdir, 'after_recording')
111
112                recorder.stop_recording()
113                logging.info('Stopped recording from Chameleon.')
114                recorder.read_recorded_binary()
115
116            recorded_file = os.path.join(self.resultsdir, "recorded.raw")
117            logging.info('Saving recorded data to %s', recorded_file)
118            recorder.save_file(recorded_file)
119
120            if not chameleon_audio_helper.compare_recorded_result(
121                    golden_file, recorder, 'correlation'):
122                raise error.TestFail(
123                        'Recorded file does not match playback file')
124