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 |
| 7 | |
| 8 | from autotest_lib.client.bin import utils |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 9 | from autotest_lib.client.cros.audio import cmd_utils |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 10 | |
| 11 | _CRAS_TEST_CLIENT = '/usr/bin/cras_test_client' |
| 12 | _RE_SELECTED_OUTPUT_NODE = re.compile('Selected Output Node: (.*)') |
| 13 | _RE_SELECTED_INPUT_NODE = re.compile('Selected Input Node: (.*)') |
Owen Lin | 7ab45a2 | 2013-11-19 17:26:33 +0800 | [diff] [blame] | 14 | _RE_NUM_ACTIVE_STREAM = re.compile('Num active streams: (.*)') |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 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 | |
Owen Lin | 2f7e7f4 | 2013-12-25 17:25:43 +0800 | [diff] [blame] | 24 | def playback_cmd(playback_file, block_size=None, duration=None, |
Owen Lin | 2013e46 | 2013-12-05 17:54:42 +0800 | [diff] [blame] | 25 | channels=2, rate=48000): |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 26 | """Gets a command to playback a file with given settings. |
| 27 | |
| 28 | @param playback_file: the name of the file to play. '-' indicates to |
| 29 | playback raw audio from the stdin. |
Owen Lin | 2f7e7f4 | 2013-12-25 17:25:43 +0800 | [diff] [blame] | 30 | @param block_size: the number of frames per callback(dictates latency). |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 31 | @param duration: seconds to playback |
| 32 | @param rate: the sampling rate |
| 33 | """ |
| 34 | args = [_CRAS_TEST_CLIENT] |
| 35 | args += ['--playback_file', playback_file] |
Owen Lin | 2f7e7f4 | 2013-12-25 17:25:43 +0800 | [diff] [blame] | 36 | if block_size is not None: |
| 37 | args += ['--block_size', str(block_size)] |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 38 | if duration is not None: |
| 39 | args += ['--duration', str(duration)] |
Owen Lin | 2013e46 | 2013-12-05 17:54:42 +0800 | [diff] [blame] | 40 | args += ['--num_channels', str(channels)] |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 41 | args += ['--rate', str(rate)] |
| 42 | return args |
| 43 | |
Owen Lin | a8129c6 | 2013-11-26 16:51:46 +0800 | [diff] [blame] | 44 | def capture_cmd( |
Owen Lin | 2f7e7f4 | 2013-12-25 17:25:43 +0800 | [diff] [blame] | 45 | capture_file, block_size=None, duration=10, channels=1, rate=48000): |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 46 | """Gets a command to capture the audio into the file with given settings. |
| 47 | |
| 48 | @param capture_file: the name of file the audio to be stored in. |
Owen Lin | 2f7e7f4 | 2013-12-25 17:25:43 +0800 | [diff] [blame] | 49 | @param block_size: the number of frames per callback(dictates latency). |
Cheng-Yi Chiang | eaf5329 | 2015-01-12 20:53:56 +0800 | [diff] [blame] | 50 | @param duration: seconds to record. If it is None, duration is not set, |
| 51 | and command will keep capturing audio until it is |
| 52 | terminated. |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 53 | @param rate: the sampling rate. |
| 54 | """ |
| 55 | args = [_CRAS_TEST_CLIENT] |
| 56 | args += ['--capture_file', capture_file] |
Owen Lin | 2f7e7f4 | 2013-12-25 17:25:43 +0800 | [diff] [blame] | 57 | if block_size is not None: |
| 58 | args += ['--block_size', str(block_size)] |
Cheng-Yi Chiang | eaf5329 | 2015-01-12 20:53:56 +0800 | [diff] [blame] | 59 | if duration is not None: |
| 60 | args += ['--duration', str(duration)] |
Owen Lin | a8129c6 | 2013-11-26 16:51:46 +0800 | [diff] [blame] | 61 | args += ['--num_channels', str(channels)] |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 62 | args += ['--rate', str(rate)] |
| 63 | return args |
| 64 | |
Owen Lin | dae7a0d | 2013-12-05 13:34:06 +0800 | [diff] [blame] | 65 | |
| 66 | def loopback(*args, **kargs): |
| 67 | """A helper function to execute loopback_cmd.""" |
| 68 | cmd_utils.execute(loopback_cmd(*args, **kargs)) |
| 69 | |
| 70 | |
Owen Lin | 2013e46 | 2013-12-05 17:54:42 +0800 | [diff] [blame] | 71 | def loopback_cmd(output_file, duration=10, channels=2, rate=48000): |
Owen Lin | dae7a0d | 2013-12-05 13:34:06 +0800 | [diff] [blame] | 72 | """Gets a command to record the loopback. |
| 73 | |
| 74 | @param output_file: The name of the file the loopback to be stored in. |
| 75 | @param channels: The number of channels of the recorded audio. |
| 76 | @param duration: seconds to record. |
| 77 | @param rate: the sampling rate. |
| 78 | """ |
| 79 | args = [_CRAS_TEST_CLIENT] |
| 80 | args += ['--loopback_file', output_file] |
| 81 | args += ['--duration_seconds', str(duration)] |
| 82 | args += ['--num_channels', str(channels)] |
| 83 | args += ['--rate', str(rate)] |
| 84 | return args |
| 85 | |
| 86 | |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 87 | def set_system_volume(volume): |
| 88 | """Set the system volume. |
| 89 | |
| 90 | @param volume: the system output vlume to be set(0 - 100). |
| 91 | """ |
| 92 | args = [_CRAS_TEST_CLIENT] |
| 93 | args += ['--volume', str(volume)] |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 94 | cmd_utils.execute(args) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 95 | |
| 96 | def set_node_volume(node_id, volume): |
| 97 | """Set the volume of the given output node. |
| 98 | |
| 99 | @param node_id: the id of the output node to be set the volume. |
| 100 | @param volume: the volume to be set(0-100). |
| 101 | """ |
| 102 | args = [_CRAS_TEST_CLIENT] |
| 103 | args += ['--set_node_volume', '%s:%d' % (node_id, volume)] |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 104 | cmd_utils.execute(args) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 105 | |
| 106 | def set_capture_gain(gain): |
| 107 | """Set the system capture gain. |
| 108 | @param gain the capture gain in db*100 (100 = 1dB) |
| 109 | """ |
| 110 | args = [_CRAS_TEST_CLIENT] |
| 111 | args += ['--capture_gain', str(gain)] |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 112 | cmd_utils.execute(args) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 113 | |
| 114 | def dump_server_info(): |
| 115 | """Gets the CRAS's server information.""" |
Owen Lin | 0d65e8a | 2013-11-28 14:29:54 +0800 | [diff] [blame] | 116 | args = [_CRAS_TEST_CLIENT, '--dump_server_info'] |
Owen Lin | ad6610a | 2013-12-13 11:20:48 +0800 | [diff] [blame] | 117 | return cmd_utils.execute(args, stdout=cmd_utils.PIPE) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 118 | |
| 119 | def get_selected_nodes(): |
| 120 | """Returns the pair of active output node and input node.""" |
| 121 | server_info = dump_server_info() |
| 122 | output_match = _RE_SELECTED_OUTPUT_NODE.search(server_info) |
| 123 | input_match = _RE_SELECTED_INPUT_NODE.search(server_info) |
| 124 | if not output_match or not input_match: |
| 125 | logging.error(server_info) |
| 126 | raise RuntimeError('No match for the pattern') |
| 127 | |
| 128 | return (output_match.group(1).strip(), input_match.group(1).strip()) |
Owen Lin | 7ab45a2 | 2013-11-19 17:26:33 +0800 | [diff] [blame] | 129 | |
Cheng-Yi Chiang | c902f2e | 2015-03-09 10:45:44 +0800 | [diff] [blame^] | 130 | |
| 131 | def set_selected_output_node_volume(volume): |
| 132 | """Sets the selected output node volume. |
| 133 | |
| 134 | @param volume: the volume to be set (0-100). |
| 135 | """ |
| 136 | selected_output_node_id, _ = get_selected_nodes() |
| 137 | set_node_volume(selected_output_node_id, volume) |
| 138 | |
| 139 | |
Owen Lin | 7ab45a2 | 2013-11-19 17:26:33 +0800 | [diff] [blame] | 140 | def get_active_stream_count(): |
| 141 | """Gets the number of active streams.""" |
| 142 | server_info = dump_server_info() |
| 143 | match = _RE_NUM_ACTIVE_STREAM.search(server_info) |
| 144 | if not match: |
| 145 | logging.error(server_info) |
| 146 | raise RuntimeException('Cannot find matched pattern') |
| 147 | return int(match.group(1)) |
Owen Lin | 5605086 | 2013-12-09 11:42:51 +0800 | [diff] [blame] | 148 | |
| 149 | |
| 150 | def set_system_mute(is_mute): |
| 151 | """Sets the system mute switch. |
| 152 | |
| 153 | @param is_mute: Set True to mute the system playback. |
| 154 | """ |
| 155 | args = [_CRAS_TEST_CLIENT, '--mute', '1' if is_mute else '0'] |
| 156 | cmd_utils.execute(args) |
| 157 | |
| 158 | |
| 159 | def set_capture_mute(is_mute): |
| 160 | """Sets the capture mute switch. |
| 161 | |
| 162 | @param is_mute: Set True to mute the capture. |
| 163 | """ |
| 164 | args = [_CRAS_TEST_CLIENT, '--capture_mute', '1' if is_mute else '0'] |
| 165 | cmd_utils.execute(args) |
Cheng-Yi Chiang | f4104ff | 2014-12-23 19:39:01 +0800 | [diff] [blame] | 166 | |
| 167 | |
| 168 | def node_type_is_plugged(node_type, server_info=None): |
| 169 | """Determine if there is any node of node_type plugged. |
| 170 | |
| 171 | Parses server info to get the plugged state of certain node type. |
| 172 | The server info of interest is in this format: |
| 173 | |
| 174 | ...(other info)... |
| 175 | |
| 176 | ID Vol Plugged L/R swapped Time Type Name |
| 177 | 3:0 75 yes no 1419323058 HEADPHONE *Headphone |
| 178 | 4:0 0 yes no 1419323059 MIC *Mic Jack |
| 179 | |
| 180 | ...(other info)... |
| 181 | |
| 182 | |
| 183 | @param node_type: A str representing node type. e.g. 'HEADPHONE' or |
| 184 | 'MIC'. |
| 185 | @param server_info: A str containing server info. None to call |
| 186 | dump_server_info in this function. |
| 187 | |
| 188 | @returns: True if there is any node of node_type plugged. False otherwise. |
| 189 | |
| 190 | @raises: ValueError: if cras server info format is not as expected. |
| 191 | """ |
| 192 | # The label line |
| 193 | # ID Vol Plugged L/R swapped Time Type Name |
| 194 | _MIN_LEN_LABELS = 8 |
| 195 | _INDEX_LABEL_PLUGGED = 2 |
| 196 | _INDEX_LABEL_TYPE = 6 |
| 197 | |
| 198 | # The value line |
| 199 | # 3:0 75 yes no 1419323058 HEADPHONE *Headphone |
| 200 | _MIN_LEN_VALUES = 7 |
| 201 | _INDEX_VALUE_PLUGGED = 2 |
| 202 | _INDEX_VALUE_TYPE = 5 |
| 203 | |
| 204 | if not server_info: |
| 205 | server_info = dump_server_info() |
| 206 | state = False |
| 207 | for line in server_info.splitlines(): |
| 208 | line_split = line.split() |
| 209 | # Checks if a label line follows format. |
| 210 | if 'Plugged' in line_split and 'Type' in line_split: |
| 211 | if (len(line_split) < _MIN_LEN_LABELS or |
| 212 | line_split[_INDEX_LABEL_PLUGGED] != 'Plugged' or |
| 213 | line_split[_INDEX_LABEL_TYPE] != 'Type'): |
| 214 | raise ValueError('cras server info format is not as ' |
| 215 | 'expected') |
| 216 | if len(line_split) < _MIN_LEN_VALUES: |
| 217 | continue |
| 218 | # Checks a value line of interest. |
| 219 | # There might be other nodes of node_type, so keep searching if |
| 220 | # this node is not plugged. |
| 221 | if (line_split[_INDEX_VALUE_TYPE] == node_type and |
| 222 | line_split[_INDEX_VALUE_PLUGGED] == 'yes'): |
| 223 | state = True |
| 224 | return state |