blob: dd8b2639d35812252527d260445a2b4ae1b84ee8 [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
25def playback_cmd(playback_file, buffer_frames=None, duration=None, rate=44100):
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.
30 @param buffer_frames: total number of frames to buffer.
31 @param duration: seconds to playback
32 @param rate: the sampling rate
33 """
34 args = [_CRAS_TEST_CLIENT]
35 args += ['--playback_file', playback_file]
36 if buffer_frames is not None:
37 args += ['--buffer_frames', str(buffer_frames)]
38 if duration is not None:
39 args += ['--duration', str(duration)]
40 args += ['--rate', str(rate)]
41 return args
42
43def capture_cmd(capture_file, buffer_frames=None, duration=10, rate=44100):
44 """Gets a command to capture the audio into the file with given settings.
45
46 @param capture_file: the name of file the audio to be stored in.
47 @param buffer_frames: total number of frames to bufffer.
48 @param duration: seconds to record.
49 @param rate: the sampling rate.
50 """
51 args = [_CRAS_TEST_CLIENT]
52 args += ['--capture_file', capture_file]
53 if buffer_frames is not None:
54 args += ['--buffer_frames', str(buffer_frames)]
55 args += ['--duration', str(duration)]
56 args += ['--rate', str(rate)]
57 return args
58
59def set_system_volume(volume):
60 """Set the system volume.
61
62 @param volume: the system output vlume to be set(0 - 100).
63 """
64 args = [_CRAS_TEST_CLIENT]
65 args += ['--volume', str(volume)]
Owen Lin9d19b272013-11-28 12:13:24 +080066 cmd_utils.execute(args)
Owen Linca365f82013-11-08 16:52:28 +080067
68def set_node_volume(node_id, volume):
69 """Set the volume of the given output node.
70
71 @param node_id: the id of the output node to be set the volume.
72 @param volume: the volume to be set(0-100).
73 """
74 args = [_CRAS_TEST_CLIENT]
75 args += ['--set_node_volume', '%s:%d' % (node_id, volume)]
Owen Lin9d19b272013-11-28 12:13:24 +080076 cmd_utils.execute(args)
Owen Linca365f82013-11-08 16:52:28 +080077
78def set_capture_gain(gain):
79 """Set the system capture gain.
80 @param gain the capture gain in db*100 (100 = 1dB)
81 """
82 args = [_CRAS_TEST_CLIENT]
83 args += ['--capture_gain', str(gain)]
Owen Lin9d19b272013-11-28 12:13:24 +080084 cmd_utils.execute(args)
Owen Linca365f82013-11-08 16:52:28 +080085
86def dump_server_info():
87 """Gets the CRAS's server information."""
Owen Lin0d65e8a2013-11-28 14:29:54 +080088 args = [_CRAS_TEST_CLIENT, '--dump_server_info']
89 return cmd_utils.execute(args, stdout=subprocess.PIPE)
Owen Linca365f82013-11-08 16:52:28 +080090
91def get_selected_nodes():
92 """Returns the pair of active output node and input node."""
93 server_info = dump_server_info()
94 output_match = _RE_SELECTED_OUTPUT_NODE.search(server_info)
95 input_match = _RE_SELECTED_INPUT_NODE.search(server_info)
96 if not output_match or not input_match:
97 logging.error(server_info)
98 raise RuntimeError('No match for the pattern')
99
100 return (output_match.group(1).strip(), input_match.group(1).strip())
Owen Lin7ab45a22013-11-19 17:26:33 +0800101
102def get_active_stream_count():
103 """Gets the number of active streams."""
104 server_info = dump_server_info()
105 match = _RE_NUM_ACTIVE_STREAM.search(server_info)
106 if not match:
107 logging.error(server_info)
108 raise RuntimeException('Cannot find matched pattern')
109 return int(match.group(1))