Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 1 | # Copyright (c) 2013 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 |
| 6 | import re |
Owen Lin | 0d65e8a | 2013-11-28 14:29:54 +0800 | [diff] [blame^] | 7 | import subprocess |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 8 | |
| 9 | from autotest_lib.client.bin import utils |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 10 | from autotest_lib.client.cros.audio import cmd_utils |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 11 | |
| 12 | _CRAS_TEST_CLIENT = '/usr/bin/cras_test_client' |
| 13 | _RE_SELECTED_OUTPUT_NODE = re.compile('Selected Output Node: (.*)') |
| 14 | _RE_SELECTED_INPUT_NODE = re.compile('Selected Input Node: (.*)') |
| 15 | |
| 16 | def playback(*args, **kargs): |
| 17 | """A helper function to execute the playback_cmd.""" |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 18 | cmd_utils.execute(playback_cmd(*args, **kargs)) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 19 | |
| 20 | def capture(*args, **kargs): |
| 21 | """A helper function to execute the capture_cmd.""" |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 22 | cmd_utils.execute(capture_cmd(*args, **kargs)) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 23 | |
| 24 | def playback_cmd(playback_file, buffer_frames=None, duration=None, rate=44100): |
| 25 | """Gets a command to playback a file with given settings. |
| 26 | |
| 27 | @param playback_file: the name of the file to play. '-' indicates to |
| 28 | playback raw audio from the stdin. |
| 29 | @param buffer_frames: total number of frames to buffer. |
| 30 | @param duration: seconds to playback |
| 31 | @param rate: the sampling rate |
| 32 | """ |
| 33 | args = [_CRAS_TEST_CLIENT] |
| 34 | args += ['--playback_file', playback_file] |
| 35 | if buffer_frames is not None: |
| 36 | args += ['--buffer_frames', str(buffer_frames)] |
| 37 | if duration is not None: |
| 38 | args += ['--duration', str(duration)] |
| 39 | args += ['--rate', str(rate)] |
| 40 | return args |
| 41 | |
| 42 | def capture_cmd(capture_file, buffer_frames=None, duration=10, rate=44100): |
| 43 | """Gets a command to capture the audio into the file with given settings. |
| 44 | |
| 45 | @param capture_file: the name of file the audio to be stored in. |
| 46 | @param buffer_frames: total number of frames to bufffer. |
| 47 | @param duration: seconds to record. |
| 48 | @param rate: the sampling rate. |
| 49 | """ |
| 50 | args = [_CRAS_TEST_CLIENT] |
| 51 | args += ['--capture_file', capture_file] |
| 52 | if buffer_frames is not None: |
| 53 | args += ['--buffer_frames', str(buffer_frames)] |
| 54 | args += ['--duration', str(duration)] |
| 55 | args += ['--rate', str(rate)] |
| 56 | return args |
| 57 | |
| 58 | def set_system_volume(volume): |
| 59 | """Set the system volume. |
| 60 | |
| 61 | @param volume: the system output vlume to be set(0 - 100). |
| 62 | """ |
| 63 | args = [_CRAS_TEST_CLIENT] |
| 64 | args += ['--volume', str(volume)] |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 65 | cmd_utils.execute(args) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 66 | |
| 67 | def set_node_volume(node_id, volume): |
| 68 | """Set the volume of the given output node. |
| 69 | |
| 70 | @param node_id: the id of the output node to be set the volume. |
| 71 | @param volume: the volume to be set(0-100). |
| 72 | """ |
| 73 | args = [_CRAS_TEST_CLIENT] |
| 74 | args += ['--set_node_volume', '%s:%d' % (node_id, volume)] |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 75 | cmd_utils.execute(args) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 76 | |
| 77 | def set_capture_gain(gain): |
| 78 | """Set the system capture gain. |
| 79 | @param gain the capture gain in db*100 (100 = 1dB) |
| 80 | """ |
| 81 | args = [_CRAS_TEST_CLIENT] |
| 82 | args += ['--capture_gain', str(gain)] |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 83 | cmd_utils.execute(args) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 84 | |
| 85 | def dump_server_info(): |
| 86 | """Gets the CRAS's server information.""" |
Owen Lin | 0d65e8a | 2013-11-28 14:29:54 +0800 | [diff] [blame^] | 87 | args = [_CRAS_TEST_CLIENT, '--dump_server_info'] |
| 88 | return cmd_utils.execute(args, stdout=subprocess.PIPE) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 89 | |
| 90 | def get_selected_nodes(): |
| 91 | """Returns the pair of active output node and input node.""" |
| 92 | server_info = dump_server_info() |
| 93 | output_match = _RE_SELECTED_OUTPUT_NODE.search(server_info) |
| 94 | input_match = _RE_SELECTED_INPUT_NODE.search(server_info) |
| 95 | if not output_match or not input_match: |
| 96 | logging.error(server_info) |
| 97 | raise RuntimeError('No match for the pattern') |
| 98 | |
| 99 | return (output_match.group(1).strip(), input_match.group(1).strip()) |