Home | History | Annotate | Download | only in audio_AlsaLoopback
      1 # Copyright (c) 2011 The Chromium 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 import logging, os, time
      6 
      7 from autotest_lib.client.common_lib import error
      8 from autotest_lib.client.cros.audio import audio_helper
      9 from autotest_lib.client.cros.audio import audio_test_data
     10 from autotest_lib.client.cros.audio import alsa_utils
     11 from autotest_lib.client.cros.audio import cmd_utils
     12 from autotest_lib.client.cros.audio import cras_utils
     13 
     14 
     15 TEST_DURATION = 1
     16 
     17 class audio_AlsaLoopback(audio_helper.alsa_rms_test):
     18     """Verifies audio playback and capture function."""
     19     version = 1
     20 
     21     def run_once(self):
     22         """Entry point of this test."""
     23 
     24         # Sine wav file lasts 5 seconds
     25         wav_path = os.path.join(self.bindir, '5SEC.wav')
     26         data_format = dict(file_type='wav', sample_format='S16_LE',
     27                 channel=2, rate=48000)
     28         wav_file = audio_test_data.GenerateAudioTestData(
     29             path=wav_path,
     30             data_format=data_format,
     31             duration_secs=5,
     32             frequencies=[440, 440],
     33             volume_scale=0.9)
     34 
     35         recorded_file = os.path.join(self.resultsdir, 'hw_recorded.wav')
     36 
     37         # Get selected input and output devices.
     38         cras_input = cras_utils.get_selected_input_device_name()
     39         cras_output = cras_utils.get_selected_output_device_name()
     40         logging.debug("Selected input=%s, output=%s", cras_input, cras_output)
     41         if cras_input is None:
     42             raise error.TestFail("Fail to get selected input device.")
     43         if cras_output is None:
     44             raise error.TestFail("Fail to get selected output device.")
     45         alsa_input = alsa_utils.convert_device_name(cras_input)
     46         alsa_output = alsa_utils.convert_device_name(cras_output)
     47 
     48         (output_type, input_type) = cras_utils.get_selected_node_types()
     49         if 'MIC' not in input_type:
     50             raise error.TestFail("Wrong input type=%s", input_type)
     51         if 'HEADPHONE' not in output_type:
     52             raise error.TestFail("Wrong output type=%s", output_type)
     53 
     54         p = cmd_utils.popen(alsa_utils.playback_cmd(wav_file.path, device=alsa_output))
     55         try:
     56             # Wait one second to make sure the playback has been started.
     57             time.sleep(1)
     58             alsa_utils.record(recorded_file, duration=TEST_DURATION,
     59                               device=alsa_input)
     60 
     61             # Make sure the audio is still playing.
     62             if p.poll() != None:
     63                 raise error.TestError('playback stopped')
     64         finally:
     65             cmd_utils.kill_or_log_returncode(p)
     66             wav_file.delete()
     67 
     68         rms_value = audio_helper.get_rms(recorded_file)[0]
     69 
     70         self.write_perf_keyval({'rms_value': rms_value})
     71 
     72