blob: a47a058fe5d5ecb0cc2bc6c315bdec0cb1415c71 [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
Owen Lin0d65e8a2013-11-28 14:29:54 +08007import subprocess
Owen Linca365f82013-11-08 16:52:28 +08008
9from autotest_lib.client.bin import utils
Owen Lin9d19b272013-11-28 12:13:24 +080010from autotest_lib.client.cros.audio import cmd_utils
Owen Linca365f82013-11-08 16:52:28 +080011
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: (.*)')
Owen Lin7ab45a22013-11-19 17:26:33 +080015_RE_NUM_ACTIVE_STREAM = re.compile('Num active streams: (.*)')
Owen Linca365f82013-11-08 16:52:28 +080016
17def playback(*args, **kargs):
18 """A helper function to execute the playback_cmd."""
Owen Lin9d19b272013-11-28 12:13:24 +080019 cmd_utils.execute(playback_cmd(*args, **kargs))
Owen Linca365f82013-11-08 16:52:28 +080020
21def capture(*args, **kargs):
22 """A helper function to execute the capture_cmd."""
Owen Lin9d19b272013-11-28 12:13:24 +080023 cmd_utils.execute(capture_cmd(*args, **kargs))
Owen Linca365f82013-11-08 16:52:28 +080024
Owen Lina8129c62013-11-26 16:51:46 +080025def playback_cmd(playback_file, buffer_frames=None, duration=None,
Owen Lin2013e462013-12-05 17:54:42 +080026 channels=2, rate=48000):
Owen Linca365f82013-11-08 16:52:28 +080027 """Gets a command to playback a file with given settings.
28
29 @param playback_file: the name of the file to play. '-' indicates to
30 playback raw audio from the stdin.
31 @param buffer_frames: total number of frames to buffer.
32 @param duration: seconds to playback
33 @param rate: the sampling rate
34 """
35 args = [_CRAS_TEST_CLIENT]
36 args += ['--playback_file', playback_file]
37 if buffer_frames is not None:
38 args += ['--buffer_frames', str(buffer_frames)]
39 if duration is not None:
40 args += ['--duration', str(duration)]
Owen Lin2013e462013-12-05 17:54:42 +080041 args += ['--num_channels', str(channels)]
Owen Linca365f82013-11-08 16:52:28 +080042 args += ['--rate', str(rate)]
43 return args
44
Owen Lina8129c62013-11-26 16:51:46 +080045def capture_cmd(
Owen Lin2013e462013-12-05 17:54:42 +080046 capture_file, buffer_frames=None, duration=10, channels=1, rate=48000):
Owen Linca365f82013-11-08 16:52:28 +080047 """Gets a command to capture the audio into the file with given settings.
48
49 @param capture_file: the name of file the audio to be stored in.
50 @param buffer_frames: total number of frames to bufffer.
51 @param duration: seconds to record.
52 @param rate: the sampling rate.
53 """
54 args = [_CRAS_TEST_CLIENT]
55 args += ['--capture_file', capture_file]
56 if buffer_frames is not None:
57 args += ['--buffer_frames', str(buffer_frames)]
58 args += ['--duration', str(duration)]
Owen Lina8129c62013-11-26 16:51:46 +080059 args += ['--num_channels', str(channels)]
Owen Linca365f82013-11-08 16:52:28 +080060 args += ['--rate', str(rate)]
61 return args
62
Owen Lindae7a0d2013-12-05 13:34:06 +080063
64def loopback(*args, **kargs):
65 """A helper function to execute loopback_cmd."""
66 cmd_utils.execute(loopback_cmd(*args, **kargs))
67
68
Owen Lin2013e462013-12-05 17:54:42 +080069def loopback_cmd(output_file, duration=10, channels=2, rate=48000):
Owen Lindae7a0d2013-12-05 13:34:06 +080070 """Gets a command to record the loopback.
71
72 @param output_file: The name of the file the loopback to be stored in.
73 @param channels: The number of channels of the recorded audio.
74 @param duration: seconds to record.
75 @param rate: the sampling rate.
76 """
77 args = [_CRAS_TEST_CLIENT]
78 args += ['--loopback_file', output_file]
79 args += ['--duration_seconds', str(duration)]
80 args += ['--num_channels', str(channels)]
81 args += ['--rate', str(rate)]
82 return args
83
84
Owen Linca365f82013-11-08 16:52:28 +080085def set_system_volume(volume):
86 """Set the system volume.
87
88 @param volume: the system output vlume to be set(0 - 100).
89 """
90 args = [_CRAS_TEST_CLIENT]
91 args += ['--volume', str(volume)]
Owen Lin9d19b272013-11-28 12:13:24 +080092 cmd_utils.execute(args)
Owen Linca365f82013-11-08 16:52:28 +080093
94def set_node_volume(node_id, volume):
95 """Set the volume of the given output node.
96
97 @param node_id: the id of the output node to be set the volume.
98 @param volume: the volume to be set(0-100).
99 """
100 args = [_CRAS_TEST_CLIENT]
101 args += ['--set_node_volume', '%s:%d' % (node_id, volume)]
Owen Lin9d19b272013-11-28 12:13:24 +0800102 cmd_utils.execute(args)
Owen Linca365f82013-11-08 16:52:28 +0800103
104def set_capture_gain(gain):
105 """Set the system capture gain.
106 @param gain the capture gain in db*100 (100 = 1dB)
107 """
108 args = [_CRAS_TEST_CLIENT]
109 args += ['--capture_gain', str(gain)]
Owen Lin9d19b272013-11-28 12:13:24 +0800110 cmd_utils.execute(args)
Owen Linca365f82013-11-08 16:52:28 +0800111
112def dump_server_info():
113 """Gets the CRAS's server information."""
Owen Lin0d65e8a2013-11-28 14:29:54 +0800114 args = [_CRAS_TEST_CLIENT, '--dump_server_info']
115 return cmd_utils.execute(args, stdout=subprocess.PIPE)
Owen Linca365f82013-11-08 16:52:28 +0800116
117def get_selected_nodes():
118 """Returns the pair of active output node and input node."""
119 server_info = dump_server_info()
120 output_match = _RE_SELECTED_OUTPUT_NODE.search(server_info)
121 input_match = _RE_SELECTED_INPUT_NODE.search(server_info)
122 if not output_match or not input_match:
123 logging.error(server_info)
124 raise RuntimeError('No match for the pattern')
125
126 return (output_match.group(1).strip(), input_match.group(1).strip())
Owen Lin7ab45a22013-11-19 17:26:33 +0800127
128def get_active_stream_count():
129 """Gets the number of active streams."""
130 server_info = dump_server_info()
131 match = _RE_NUM_ACTIVE_STREAM.search(server_info)
132 if not match:
133 logging.error(server_info)
134 raise RuntimeException('Cannot find matched pattern')
135 return int(match.group(1))
Owen Lin56050862013-12-09 11:42:51 +0800136
137
138def set_system_mute(is_mute):
139 """Sets the system mute switch.
140
141 @param is_mute: Set True to mute the system playback.
142 """
143 args = [_CRAS_TEST_CLIENT, '--mute', '1' if is_mute else '0']
144 cmd_utils.execute(args)
145
146
147def set_capture_mute(is_mute):
148 """Sets the capture mute switch.
149
150 @param is_mute: Set True to mute the capture.
151 """
152 args = [_CRAS_TEST_CLIENT, '--capture_mute', '1' if is_mute else '0']
153 cmd_utils.execute(args)