1# Copyright 2015 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 an audio quality test over headphone 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_ids
15from autotest_lib.client.cros.chameleon import chameleon_audio_helper
16from autotest_lib.server.cros.audio import audio_test
17from autotest_lib.server.cros.multimedia import remote_facade_factory
18
19
20class audio_MediaBasicVerification(audio_test.AudioTest):
21    """Server side audio quality test over 3.5 headphones.
22
23    This test talks to a Chameleon board and a Cros device to verify
24    headphone audio quality of the Cros device.
25
26    """
27    version = 1
28    DELAY_BEFORE_RECORD_SECONDS = 0.5
29    RECORD_SECONDS = 10
30    DELAY_AFTER_BINDING = 0.5
31    UNSUPPORTED_BOARD_TYPES = ['CHROMEBIT']
32
33    def run_once(self, host, audio_test_file):
34
35        if host.get_board_type() in self.UNSUPPORTED_BOARD_TYPES:
36            raise error.TestNAError(
37                    'DUT is not supported for this scenario. Skipping test.')
38
39        chameleon_board = host.chameleon
40        factory = remote_facade_factory.RemoteFacadeFactory(
41                host, results_dir=self.resultsdir)
42        chameleon_board.setup_and_reset(self.outputdir)
43
44        widget_factory = chameleon_audio_helper.AudioWidgetFactory(
45                factory, host)
46
47        source = widget_factory.create_widget(
48            chameleon_audio_ids.CrosIds.HEADPHONE)
49        recorder = widget_factory.create_widget(
50            chameleon_audio_ids.ChameleonIds.LINEIN)
51        binder = widget_factory.create_binder(source, recorder)
52
53        with chameleon_audio_helper.bind_widgets(binder):
54            # Checks the node selected by cras is correct.
55            time.sleep(self.DELAY_AFTER_BINDING)
56            audio_facade = factory.create_audio_facade()
57
58            audio_test_utils.dump_cros_audio_logs(
59                    host, audio_facade, self.resultsdir, 'after_binding')
60
61            audio_test_utils.check_audio_nodes(audio_facade, (['HEADPHONE'], None))
62
63            # Starts playing, waits for some time, and then starts recording.
64            # This is to avoid artifact caused by codec initialization.
65            logging.info('Start playing %s on Cros device',
66                         audio_test_file)
67            browser_facade = factory.create_browser_facade()
68            tab_descriptor = browser_facade.new_tab(audio_test_file)
69
70            time.sleep(self.DELAY_BEFORE_RECORD_SECONDS)
71            logging.info('Start recording from Chameleon.')
72            recorder.start_recording()
73
74            time.sleep(self.RECORD_SECONDS)
75
76            recorder.stop_recording()
77            logging.info('Stopped recording from Chameleon.')
78            browser_facade.close_tab(tab_descriptor)
79
80            audio_test_utils.dump_cros_audio_logs(
81                    host, audio_facade, self.resultsdir, 'after_recording')
82
83            recorder.read_recorded_binary()
84            logging.info('Read recorded binary from Chameleon.')
85
86        recorded_file = os.path.join(self.resultsdir, 'recorded.raw')
87        logging.info('Saving recorded data to %s', recorded_file)
88        recorder.save_file(recorded_file)
89
90        # Removes the beginning of recorded data. This is to avoid artifact
91        # caused by Chameleon codec initialization in the beginning of
92        # recording.
93        recorder.remove_head(0.5)
94
95        # Removes noise by a lowpass filter.
96        recorder.lowpass_filter(4000)
97        recorded_file = os.path.join(self.resultsdir, 'recorded_filtered.raw')
98        logging.info('Saving filtered data to %s', recorded_file)
99        recorder.save_file(recorded_file)
100
101        # Compares data by frequency. Headphone audio signal has gone through
102        # analog processing. This suffers from codec artifacts and noise on the
103        # path. Comparing data by frequency is more robust than comparing by
104        # correlation, which is suitable for fully-digital audio path like USB
105        # and HDMI.
106        audio_test_utils.check_recorded_frequency(
107                audio_test_data.MEDIA_HEADPHONE_TEST_FILE,
108                recorder, check_anomaly=True)
109