Home | History | Annotate | Download | only in audio_AudioBasicHeadphone
      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 headphone audio test using the Chameleon board."""
      6 
      7 import logging
      8 import os
      9 import time
     10 
     11 from autotest_lib.client.common_lib import error
     12 from autotest_lib.client.cros.audio import audio_test_data
     13 from autotest_lib.client.cros.chameleon import audio_test_utils
     14 from autotest_lib.client.cros.chameleon import chameleon_audio_ids
     15 from autotest_lib.client.cros.chameleon import chameleon_audio_helper
     16 from autotest_lib.server.cros.audio import audio_test
     17 
     18 
     19 class audio_AudioBasicHeadphone(audio_test.AudioTest):
     20     """Server side headphone audio test.
     21 
     22     This test talks to a Chameleon board and a Cros device to verify
     23     headphone audio function of the Cros device.
     24 
     25     """
     26     version = 1
     27     DELAY_BEFORE_RECORD_SECONDS = 0.5
     28     RECORD_SECONDS = 5
     29     DELAY_AFTER_BINDING = 0.5
     30 
     31     def run_once(self, host, check_quality=False):
     32         """Running basic headphone audio tests.
     33 
     34         @param host: device under test host
     35         @param check_quality: flag to check audio quality.
     36 
     37         """
     38         golden_file = audio_test_data.FREQUENCY_TEST_FILE
     39 
     40         chameleon_board = host.chameleon
     41         factory = self.create_remote_facade_factory(host)
     42 
     43         chameleon_board.reset()
     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             output_nodes, _ = audio_facade.get_selected_node_types()
     63             if output_nodes != ['HEADPHONE']:
     64                 raise error.TestFail(
     65                         '%s rather than headphone is selected on Cros '
     66                         'device' % output_nodes)
     67 
     68             logging.info('Setting playback data on Cros device')
     69             source.set_playback_data(golden_file)
     70 
     71             # Starts playing, waits for some time, and then starts recording.
     72             # This is to avoid artifact caused by codec initialization.
     73             logging.info('Start playing %s on Cros device',
     74                          golden_file.path)
     75             source.start_playback()
     76 
     77             time.sleep(self.DELAY_BEFORE_RECORD_SECONDS)
     78             logging.info('Start recording from Chameleon.')
     79             recorder.start_recording()
     80 
     81             time.sleep(self.RECORD_SECONDS)
     82 
     83             recorder.stop_recording()
     84             logging.info('Stopped recording from Chameleon.')
     85 
     86             audio_test_utils.dump_cros_audio_logs(
     87                     host, audio_facade, self.resultsdir, 'after_recording')
     88 
     89             recorder.read_recorded_binary()
     90             logging.info('Read recorded binary from Chameleon.')
     91 
     92         recorded_file = os.path.join(self.resultsdir, "recorded.raw")
     93         logging.info('Saving recorded data to %s', recorded_file)
     94         recorder.save_file(recorded_file)
     95 
     96         # Removes the beginning of recorded data. This is to avoid artifact
     97         # caused by Chameleon codec initialization in the beginning of
     98         # recording.
     99         recorder.remove_head(0.5)
    100 
    101         recorded_file = os.path.join(self.resultsdir, "recorded_clipped.raw")
    102         logging.info('Saving clipped data to %s', recorded_file)
    103         recorder.save_file(recorded_file)
    104 
    105         # Compares data by frequency. Headphone audio signal has gone through
    106         # analog processing. This suffers from codec artifacts and noise on the
    107         # path. Comparing data by frequency is more robust than comparing by
    108         # correlation, which is suitable for fully-digital audio path like USB
    109         # and HDMI.
    110         audio_test_utils.check_recorded_frequency(
    111                 golden_file, recorder, check_anomaly=check_quality)
    112