blob: 9bc9cee85a46bdf5153d4f487f9566424a909df5 [file] [log] [blame]
Owen Linca365f82013-11-08 16:52:28 +08001# 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
5import logging
6import re
7
8from autotest_lib.client.bin import utils
Owen Lin9d19b272013-11-28 12:13:24 +08009from autotest_lib.client.cros.audio import cmd_utils
Owen Linca365f82013-11-08 16:52:28 +080010
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 Lin7ab45a22013-11-19 17:26:33 +080014_RE_NUM_ACTIVE_STREAM = re.compile('Num active streams: (.*)')
Owen Linca365f82013-11-08 16:52:28 +080015
16def playback(*args, **kargs):
17 """A helper function to execute the playback_cmd."""
Owen Lin9d19b272013-11-28 12:13:24 +080018 cmd_utils.execute(playback_cmd(*args, **kargs))
Owen Linca365f82013-11-08 16:52:28 +080019
20def capture(*args, **kargs):
21 """A helper function to execute the capture_cmd."""
Owen Lin9d19b272013-11-28 12:13:24 +080022 cmd_utils.execute(capture_cmd(*args, **kargs))
Owen Linca365f82013-11-08 16:52:28 +080023
Owen Lin2f7e7f42013-12-25 17:25:43 +080024def playback_cmd(playback_file, block_size=None, duration=None,
Owen Lin2013e462013-12-05 17:54:42 +080025 channels=2, rate=48000):
Owen Linca365f82013-11-08 16:52:28 +080026 """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 Lin2f7e7f42013-12-25 17:25:43 +080030 @param block_size: the number of frames per callback(dictates latency).
Owen Linca365f82013-11-08 16:52:28 +080031 @param duration: seconds to playback
32 @param rate: the sampling rate
33 """
34 args = [_CRAS_TEST_CLIENT]
35 args += ['--playback_file', playback_file]
Owen Lin2f7e7f42013-12-25 17:25:43 +080036 if block_size is not None:
37 args += ['--block_size', str(block_size)]
Owen Linca365f82013-11-08 16:52:28 +080038 if duration is not None:
39 args += ['--duration', str(duration)]
Owen Lin2013e462013-12-05 17:54:42 +080040 args += ['--num_channels', str(channels)]
Owen Linca365f82013-11-08 16:52:28 +080041 args += ['--rate', str(rate)]
42 return args
43
Owen Lina8129c62013-11-26 16:51:46 +080044def capture_cmd(
Owen Lin2f7e7f42013-12-25 17:25:43 +080045 capture_file, block_size=None, duration=10, channels=1, rate=48000):
Owen Linca365f82013-11-08 16:52:28 +080046 """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 Lin2f7e7f42013-12-25 17:25:43 +080049 @param block_size: the number of frames per callback(dictates latency).
Owen Linca365f82013-11-08 16:52:28 +080050 @param duration: seconds to record.
51 @param rate: the sampling rate.
52 """
53 args = [_CRAS_TEST_CLIENT]
54 args += ['--capture_file', capture_file]
Owen Lin2f7e7f42013-12-25 17:25:43 +080055 if block_size is not None:
56 args += ['--block_size', str(block_size)]
Owen Linca365f82013-11-08 16:52:28 +080057 args += ['--duration', str(duration)]
Owen Lina8129c62013-11-26 16:51:46 +080058 args += ['--num_channels', str(channels)]
Owen Linca365f82013-11-08 16:52:28 +080059 args += ['--rate', str(rate)]
60 return args
61
Owen Lindae7a0d2013-12-05 13:34:06 +080062
63def loopback(*args, **kargs):
64 """A helper function to execute loopback_cmd."""
65 cmd_utils.execute(loopback_cmd(*args, **kargs))
66
67
Owen Lin2013e462013-12-05 17:54:42 +080068def loopback_cmd(output_file, duration=10, channels=2, rate=48000):
Owen Lindae7a0d2013-12-05 13:34:06 +080069 """Gets a command to record the loopback.
70
71 @param output_file: The name of the file the loopback to be stored in.
72 @param channels: The number of channels of the recorded audio.
73 @param duration: seconds to record.
74 @param rate: the sampling rate.
75 """
76 args = [_CRAS_TEST_CLIENT]
77 args += ['--loopback_file', output_file]
78 args += ['--duration_seconds', str(duration)]
79 args += ['--num_channels', str(channels)]
80 args += ['--rate', str(rate)]
81 return args
82
83
Owen Linca365f82013-11-08 16:52:28 +080084def set_system_volume(volume):
85 """Set the system volume.
86
87 @param volume: the system output vlume to be set(0 - 100).
88 """
89 args = [_CRAS_TEST_CLIENT]
90 args += ['--volume', str(volume)]
Owen Lin9d19b272013-11-28 12:13:24 +080091 cmd_utils.execute(args)
Owen Linca365f82013-11-08 16:52:28 +080092
93def set_node_volume(node_id, volume):
94 """Set the volume of the given output node.
95
96 @param node_id: the id of the output node to be set the volume.
97 @param volume: the volume to be set(0-100).
98 """
99 args = [_CRAS_TEST_CLIENT]
100 args += ['--set_node_volume', '%s:%d' % (node_id, volume)]
Owen Lin9d19b272013-11-28 12:13:24 +0800101 cmd_utils.execute(args)
Owen Linca365f82013-11-08 16:52:28 +0800102
103def set_capture_gain(gain):
104 """Set the system capture gain.
105 @param gain the capture gain in db*100 (100 = 1dB)
106 """
107 args = [_CRAS_TEST_CLIENT]
108 args += ['--capture_gain', str(gain)]
Owen Lin9d19b272013-11-28 12:13:24 +0800109 cmd_utils.execute(args)
Owen Linca365f82013-11-08 16:52:28 +0800110
111def dump_server_info():
112 """Gets the CRAS's server information."""
Owen Lin0d65e8a2013-11-28 14:29:54 +0800113 args = [_CRAS_TEST_CLIENT, '--dump_server_info']
Owen Linad6610a2013-12-13 11:20:48 +0800114 return cmd_utils.execute(args, stdout=cmd_utils.PIPE)
Owen Linca365f82013-11-08 16:52:28 +0800115
116def get_selected_nodes():
117 """Returns the pair of active output node and input node."""
118 server_info = dump_server_info()
119 output_match = _RE_SELECTED_OUTPUT_NODE.search(server_info)
120 input_match = _RE_SELECTED_INPUT_NODE.search(server_info)
121 if not output_match or not input_match:
122 logging.error(server_info)
123 raise RuntimeError('No match for the pattern')
124
125 return (output_match.group(1).strip(), input_match.group(1).strip())
Owen Lin7ab45a22013-11-19 17:26:33 +0800126
127def get_active_stream_count():
128 """Gets the number of active streams."""
129 server_info = dump_server_info()
130 match = _RE_NUM_ACTIVE_STREAM.search(server_info)
131 if not match:
132 logging.error(server_info)
133 raise RuntimeException('Cannot find matched pattern')
134 return int(match.group(1))
Owen Lin56050862013-12-09 11:42:51 +0800135
136
137def set_system_mute(is_mute):
138 """Sets the system mute switch.
139
140 @param is_mute: Set True to mute the system playback.
141 """
142 args = [_CRAS_TEST_CLIENT, '--mute', '1' if is_mute else '0']
143 cmd_utils.execute(args)
144
145
146def set_capture_mute(is_mute):
147 """Sets the capture mute switch.
148
149 @param is_mute: Set True to mute the capture.
150 """
151 args = [_CRAS_TEST_CLIENT, '--capture_mute', '1' if is_mute else '0']
152 cmd_utils.execute(args)
Cheng-Yi Chiangf4104ff2014-12-23 19:39:01 +0800153
154
155def node_type_is_plugged(node_type, server_info=None):
156 """Determine if there is any node of node_type plugged.
157
158 Parses server info to get the plugged state of certain node type.
159 The server info of interest is in this format:
160
161 ...(other info)...
162
163 ID Vol Plugged L/R swapped Time Type Name
164 3:0 75 yes no 1419323058 HEADPHONE *Headphone
165 4:0 0 yes no 1419323059 MIC *Mic Jack
166
167 ...(other info)...
168
169
170 @param node_type: A str representing node type. e.g. 'HEADPHONE' or
171 'MIC'.
172 @param server_info: A str containing server info. None to call
173 dump_server_info in this function.
174
175 @returns: True if there is any node of node_type plugged. False otherwise.
176
177 @raises: ValueError: if cras server info format is not as expected.
178 """
179 # The label line
180 # ID Vol Plugged L/R swapped Time Type Name
181 _MIN_LEN_LABELS = 8
182 _INDEX_LABEL_PLUGGED = 2
183 _INDEX_LABEL_TYPE = 6
184
185 # The value line
186 # 3:0 75 yes no 1419323058 HEADPHONE *Headphone
187 _MIN_LEN_VALUES = 7
188 _INDEX_VALUE_PLUGGED = 2
189 _INDEX_VALUE_TYPE = 5
190
191 if not server_info:
192 server_info = dump_server_info()
193 state = False
194 for line in server_info.splitlines():
195 line_split = line.split()
196 # Checks if a label line follows format.
197 if 'Plugged' in line_split and 'Type' in line_split:
198 if (len(line_split) < _MIN_LEN_LABELS or
199 line_split[_INDEX_LABEL_PLUGGED] != 'Plugged' or
200 line_split[_INDEX_LABEL_TYPE] != 'Type'):
201 raise ValueError('cras server info format is not as '
202 'expected')
203 if len(line_split) < _MIN_LEN_VALUES:
204 continue
205 # Checks a value line of interest.
206 # There might be other nodes of node_type, so keep searching if
207 # this node is not plugged.
208 if (line_split[_INDEX_VALUE_TYPE] == node_type and
209 line_split[_INDEX_VALUE_PLUGGED] == 'yes'):
210 state = True
211 return state