Home | History | Annotate | Download | only in audio_AudioBasicBluetoothRecord
      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 record test using the Chameleon board."""
      6 
      7 import logging
      8 import os
      9 import time, threading
     10 
     11 from autotest_lib.client.bin import utils
     12 from autotest_lib.client.common_lib import error
     13 from autotest_lib.client.cros.audio import audio_test_data
     14 from autotest_lib.client.cros.chameleon import audio_test_utils
     15 from autotest_lib.client.cros.chameleon import chameleon_audio_helper
     16 from autotest_lib.client.cros.chameleon import chameleon_audio_ids
     17 from autotest_lib.server.cros.audio import audio_test
     18 
     19 
     20 class audio_AudioBasicBluetoothRecord(audio_test.AudioTest):
     21     """Server side bluetooth record audio test.
     22 
     23     This test talks to a Chameleon board and a Cros device to verify
     24     bluetooth record 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 
     39     def disconnect_connect_bt(self, link):
     40         """Performs disconnect and connect BT module
     41 
     42         @param link: binder link to control BT adapter
     43 
     44         """
     45 
     46         logging.info("Disconnecting BT module...")
     47         link.adapter_disconnect_module()
     48         time.sleep(self.DELAY_AFTER_DISCONNECT_SECONDS)
     49         if audio_test_utils.has_internal_microphone(self.host):
     50             audio_test_utils.check_audio_nodes(self.audio_facade,
     51                                                (None, ['INTERNAL_MIC']))
     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         if audio_test_utils.has_internal_microphone(self.host):
     68              audio_test_utils.check_audio_nodes(self.audio_facade,
     69                                                 (None, ['INTERNAL_MIC']))
     70         logging.info("Turning on BT module...")
     71         link.enable_bluetooth_module()
     72         time.sleep(self.DELAY_AFTER_ENABLING_MODULE_SECONDS)
     73         logging.info("Connecting BT module...")
     74         link.adapter_connect_module()
     75         time.sleep(self.DELAY_AFTER_RECONNECT_SECONDS)
     76 
     77 
     78     def bluetooth_nodes_plugged(self):
     79         """Checks if bluetooth nodes are plugged.
     80 
     81         @returns: True if bluetooth nodes are plugged. False otherwise.
     82 
     83         """
     84         return audio_test_utils.bluetooth_nodes_plugged(self.audio_facade)
     85 
     86 
     87     def dump_logs_after_nodes_changed(self):
     88         """Dumps the log after unexpected NodesChanged signal happens."""
     89         audio_test_utils.dump_cros_audio_logs(
     90                 self.host, self.audio_facade, self.resultsdir,
     91                 'after_nodes_changed')
     92 
     93 
     94     def run_once(self, host, suspend=False,
     95                  disable=False, disconnect=False, check_quality=False):
     96         """Running Bluetooth basic audio tests
     97 
     98         @param host: device under test host
     99         @param suspend: suspend flag to enable suspend before play/record
    100         @param disable: disable flag to disable BT module before play/record
    101         @param disconnect: disconnect flag to disconnect BT module
    102             before play/record
    103         @param check_quality: flag to check audio quality.
    104 
    105         """
    106 
    107         self.host = host
    108         golden_file = audio_test_data.SIMPLE_FREQUENCY_TEST_FILE
    109 
    110         factory = self.create_remote_facade_factory(host)
    111         self.audio_facade = factory.create_audio_facade()
    112 
    113         chameleon_board = host.chameleon
    114         chameleon_board.reset()
    115 
    116         widget_factory = chameleon_audio_helper.AudioWidgetFactory(
    117                 factory, host)
    118 
    119         source = widget_factory.create_widget(
    120             chameleon_audio_ids.ChameleonIds.LINEOUT)
    121         bluetooth_widget = widget_factory.create_widget(
    122             chameleon_audio_ids.PeripheralIds.BLUETOOTH_DATA_TX)
    123         recorder = widget_factory.create_widget(
    124             chameleon_audio_ids.CrosIds.BLUETOOTH_MIC)
    125 
    126         binder = widget_factory.create_binder(
    127                 source, bluetooth_widget, recorder)
    128 
    129         with chameleon_audio_helper.bind_widgets(binder):
    130 
    131             audio_test_utils.dump_cros_audio_logs(
    132                     host, self.audio_facade, self.resultsdir, 'after_binding')
    133 
    134             if audio_test_utils.has_internal_microphone(host):
    135                 # Checks the input node selected by Cras is internal microphone.
    136                 # Checks crbug.com/495537 for the reason to lower bluetooth
    137                 # microphone priority.
    138                 audio_test_utils.check_audio_nodes(self.audio_facade,
    139                                                    (None, ['INTERNAL_MIC']))
    140 
    141                 # Selects bluetooth mic to be the active input node.
    142                 self.audio_facade.set_chrome_active_node_type(None, 'BLUETOOTH')
    143 
    144             # Checks the node selected by Cras is correct again.
    145             audio_test_utils.check_audio_nodes(self.audio_facade,
    146                                                (None, ['BLUETOOTH']))
    147 
    148             # Starts playing, waits for some time, and then starts recording.
    149             # This is to avoid artifact caused by codec initialization.
    150             source.set_playback_data(golden_file)
    151 
    152             # Create link to control BT adapter.
    153             link = binder.get_binders()[1].get_link()
    154 
    155             if disable:
    156                 self.disable_enable_bt(link)
    157             if disconnect:
    158                 self.disconnect_connect_bt(link)
    159             if suspend:
    160                 audio_test_utils.suspend_resume(host, self.SUSPEND_SECONDS)
    161 
    162             utils.poll_for_condition(condition=factory.ready,
    163                                      timeout=self.PRC_RECONNECT_TIMEOUT,
    164                                      desc='multimedia server reconnect')
    165 
    166             if disable or disconnect or suspend:
    167                 audio_test_utils.dump_cros_audio_logs(
    168                         host, self.audio_facade, self.resultsdir,
    169                         'after_action')
    170 
    171             # Gives DUT some time to auto-reconnect bluetooth after resume.
    172             if suspend:
    173                 utils.poll_for_condition(
    174                         condition=self.bluetooth_nodes_plugged,
    175                         timeout=self.BLUETOOTH_RECONNECT_TIMEOUT_SECS,
    176                         desc='bluetooth node auto-reconnect after suspend')
    177 
    178             if audio_test_utils.has_internal_microphone(host):
    179                 # Select again BT input as default input node is INTERNAL_MIC
    180                 self.audio_facade.set_chrome_active_node_type(None, 'BLUETOOTH')
    181 
    182             with audio_test_utils.monitor_no_nodes_changed(
    183                     self.audio_facade, self.dump_logs_after_nodes_changed):
    184                 audio_test_utils.check_audio_nodes(self.audio_facade,
    185                                                    (None, ['BLUETOOTH']))
    186 
    187                 logging.info('Start playing %s on Chameleon',
    188                              golden_file.path)
    189                 source.start_playback()
    190 
    191                 time.sleep(self.DELAY_BEFORE_RECORD_SECONDS)
    192                 logging.info('Start recording from Cros device.')
    193                 recorder.start_recording()
    194 
    195                 time.sleep(self.RECORD_SECONDS)
    196 
    197                 recorder.stop_recording()
    198                 logging.info('Stopped recording from Cros device.')
    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 bluetooth module initialization in the beginning of
    213         # its playback.
    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                 second_peak_ratio=audio_test_utils.HSP_SECOND_PEAK_RATIO)
    229