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