Home | History | Annotate | Download | only in audio_AudioBasicBluetoothPlayback
      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 a server side bluetooth playback test using the Chameleon board."""
      6 
      7 import logging
      8 import os
      9 import time
     10 
     11 from autotest_lib.client.bin import utils
     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_helper
     15 from autotest_lib.client.cros.chameleon import chameleon_audio_ids
     16 from autotest_lib.server.cros.audio import audio_test
     17 from autotest_lib.server.cros.multimedia import remote_facade_factory
     18 
     19 
     20 class audio_AudioBasicBluetoothPlayback(audio_test.AudioTest):
     21     """Server side bluetooth playback audio test.
     22 
     23     This test talks to a Chameleon board and a Cros device to verify
     24     bluetooth playback audio function of the Cros device.
     25 
     26     """
     27     version = 1
     28     DELAY_AFTER_DISABLING_MODULE_SECONDS = 30
     29     DELAY_AFTER_DISCONNECT_SECONDS = 5
     30     DELAY_AFTER_ENABLING_MODULE_SECONDS = 10
     31     DELAY_AFTER_RECONNECT_SECONDS = 5
     32     DELAY_BEFORE_RECORD_SECONDS = 0.5
     33     RECORD_SECONDS = 5
     34     SUSPEND_SECONDS = 30
     35     RESUME_TIMEOUT_SECS = 60
     36     PRC_RECONNECT_TIMEOUT = 60
     37     BLUETOOTH_RECONNECT_TIMEOUT_SECS = 30
     38     DELAY_FOR_A2DP_RECONNECT_AFTER_SUSPEND = 10
     39 
     40     def disconnect_connect_bt(self, link):
     41         """Performs disconnect and connect BT module
     42 
     43         @param link: binder link to control BT adapter
     44 
     45         """
     46 
     47         logging.info("Disconnecting BT module...")
     48         link.adapter_disconnect_module()
     49         time.sleep(self.DELAY_AFTER_DISCONNECT_SECONDS)
     50         audio_test_utils.check_audio_nodes(self.audio_facade,
     51                                            (['INTERNAL_SPEAKER'], None))
     52         logging.info("Connecting BT module...")
     53         link.adapter_connect_module()
     54         time.sleep(self.DELAY_AFTER_RECONNECT_SECONDS)
     55 
     56 
     57     def disable_enable_bt(self, link):
     58         """Performs turn off and then on BT module
     59 
     60         @param link: binder link to control BT adapter
     61 
     62         """
     63 
     64         logging.info("Turning off BT module...")
     65         link.disable_bluetooth_module()
     66         time.sleep(self.DELAY_AFTER_DISABLING_MODULE_SECONDS)
     67         audio_test_utils.check_audio_nodes(self.audio_facade,
     68                                            (['INTERNAL_SPEAKER'], None))
     69         logging.info("Turning on BT module...")
     70         link.enable_bluetooth_module()
     71         time.sleep(self.DELAY_AFTER_ENABLING_MODULE_SECONDS)
     72         logging.info("Connecting BT module...")
     73         link.adapter_connect_module()
     74         time.sleep(self.DELAY_AFTER_RECONNECT_SECONDS)
     75 
     76 
     77     def bluetooth_nodes_plugged(self):
     78         """Checks if bluetooth nodes are plugged.
     79 
     80         @returns: True if bluetooth nodes are plugged. False otherwise.
     81 
     82         """
     83         return audio_test_utils.bluetooth_nodes_plugged(self.audio_facade)
     84 
     85 
     86     def dump_logs_after_nodes_changed(self):
     87         """Dumps the log after unexpected NodesChanged signal happens."""
     88         audio_test_utils.dump_cros_audio_logs(
     89                 self.host, self.audio_facade, self.resultsdir,
     90                 'after_nodes_changed')
     91 
     92 
     93     def run_once(self, host, suspend=False,
     94                  disable=False, disconnect=False, check_quality=False):
     95         """Running Bluetooth basic audio tests
     96 
     97         @param host: device under test host
     98         @param suspend: suspend flag to enable suspend before play/record
     99         @param disable: disable flag to disable BT module before play/record
    100         @param disconnect: disconnect flag to disconnect BT module
    101             before play/record
    102         @param check_quality: flag to check audio quality.
    103 
    104         """
    105         self.host = host
    106         golden_file = audio_test_data.FREQUENCY_TEST_FILE
    107 
    108         factory = remote_facade_factory.RemoteFacadeFactory(
    109                 host, results_dir=self.resultsdir)
    110         self.audio_facade = factory.create_audio_facade()
    111 
    112         chameleon_board = host.chameleon
    113         chameleon_board.setup_and_reset(self.outputdir)
    114 
    115         widget_factory = chameleon_audio_helper.AudioWidgetFactory(
    116                 factory, host)
    117 
    118         source = widget_factory.create_widget(
    119             chameleon_audio_ids.CrosIds.BLUETOOTH_HEADPHONE)
    120         bluetooth_widget = widget_factory.create_widget(
    121             chameleon_audio_ids.PeripheralIds.BLUETOOTH_DATA_RX)
    122         recorder = widget_factory.create_widget(
    123             chameleon_audio_ids.ChameleonIds.LINEIN)
    124 
    125         binder = widget_factory.create_binder(
    126                 source, bluetooth_widget, recorder)
    127 
    128         with chameleon_audio_helper.bind_widgets(binder):
    129 
    130             audio_test_utils.dump_cros_audio_logs(
    131                     host, self.audio_facade, self.resultsdir, 'after_binding')
    132 
    133             # For DUTs with permanently connected audio jack cable
    134             # Bluetooth output node should be selected explicitly.
    135             output_nodes, _ = self.audio_facade.get_plugged_node_types()
    136             if 'HEADPHONE' in output_nodes:
    137                 self.audio_facade.set_chrome_active_node_type('BLUETOOTH',
    138                                                               None)
    139             # Checks the node selected by Cras is correct.
    140             audio_test_utils.check_audio_nodes(self.audio_facade,
    141                                                (['BLUETOOTH'], None))
    142 
    143             self.audio_facade.set_selected_output_volume(80)
    144 
    145             # Starts playing, waits for some time, and then starts recording.
    146             # This is to avoid artifact caused by codec initialization.
    147             source.set_playback_data(golden_file)
    148 
    149             # Create link to control BT adapter.
    150             link = binder.get_binders()[0].get_link()
    151 
    152             if disable:
    153                 self.disable_enable_bt(link)
    154             if disconnect:
    155                 self.disconnect_connect_bt(link)
    156             if suspend:
    157                 audio_test_utils.suspend_resume(host, self.SUSPEND_SECONDS)
    158 
    159             utils.poll_for_condition(condition=factory.ready,
    160                                      timeout=self.PRC_RECONNECT_TIMEOUT,
    161                                      desc='multimedia server reconnect')
    162 
    163             if disable or disconnect or suspend:
    164                 audio_test_utils.dump_cros_audio_logs(
    165                         host, self.audio_facade, self.resultsdir,
    166                         'after_action')
    167 
    168             # Gives DUT some time to auto-reconnect bluetooth after resume.
    169             if suspend:
    170                 utils.poll_for_condition(
    171                         condition=self.bluetooth_nodes_plugged,
    172                         timeout=self.BLUETOOTH_RECONNECT_TIMEOUT_SECS,
    173                         desc='bluetooth node auto-reconnect after suspend')
    174                 # Delay some time for A2DP to be reconnected.
    175                 # Normally this happens several seconds after HSP is
    176                 # reconnected. However, there is no event what we can wait for,
    177                 # so just wait some time.
    178                 time.sleep(self.DELAY_FOR_A2DP_RECONNECT_AFTER_SUSPEND)
    179 
    180             with audio_test_utils.monitor_no_nodes_changed(
    181                     self.audio_facade, self.dump_logs_after_nodes_changed):
    182                 # Checks the node selected by Cras is correct again.
    183                 audio_test_utils.check_audio_nodes(self.audio_facade,
    184                                                    (['BLUETOOTH'], None))
    185 
    186                 logging.info('Start playing %s on Cros device',
    187                              golden_file.path)
    188 
    189                 source.start_playback()
    190 
    191                 time.sleep(self.DELAY_BEFORE_RECORD_SECONDS)
    192                 logging.info('Start recording from Chameleon.')
    193                 recorder.start_recording()
    194 
    195                 time.sleep(self.RECORD_SECONDS)
    196 
    197                 recorder.stop_recording()
    198                 logging.info('Stopped recording from Chameleon.')
    199 
    200                 audio_test_utils.dump_cros_audio_logs(
    201                         host, self.audio_facade, self.resultsdir,
    202                         'after_recording')
    203 
    204                 recorder.read_recorded_binary()
    205                 logging.info('Read recorded binary from Chameleon.')
    206 
    207                 recorded_file = os.path.join(self.resultsdir, "recorded.raw")
    208                 logging.info('Saving recorded data to %s', recorded_file)
    209                 recorder.save_file(recorded_file)
    210 
    211         # Removes the beginning of recorded data. This is to avoid artifact
    212         # caused by Chameleon codec initialization in the beginning of
    213         # recording.
    214         recorder.remove_head(0.5)
    215 
    216         recorded_file = os.path.join(self.resultsdir, "recorded_clipped.raw")
    217         logging.info('Saving clipped data to %s', recorded_file)
    218         recorder.save_file(recorded_file)
    219 
    220         # Compares data by frequency. Audio signal recorded by microphone has
    221         # gone through analog processing and through the air.
    222         # This suffers from codec artifacts and noise on the path.
    223         # Comparing data by frequency is more robust than comparing by
    224         # correlation, which is suitable for fully-digital audio path like USB
    225         # and HDMI.
    226         audio_test_utils.check_recorded_frequency(
    227                 golden_file, recorder, check_anomaly=check_quality)
    228