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: (.*)') |
Owen Lin | 7ab45a2 | 2013-11-19 17:26:33 +0800 | [diff] [blame] | 15 | _RE_NUM_ACTIVE_STREAM = re.compile('Num active streams: (.*)') |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 16 | |
| 17 | def playback(*args, **kargs): |
| 18 | """A helper function to execute the playback_cmd.""" |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 19 | cmd_utils.execute(playback_cmd(*args, **kargs)) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 20 | |
| 21 | def capture(*args, **kargs): |
| 22 | """A helper function to execute the capture_cmd.""" |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 23 | cmd_utils.execute(capture_cmd(*args, **kargs)) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 24 | |
Owen Lin | a8129c6 | 2013-11-26 16:51:46 +0800 | [diff] [blame^] | 25 | def playback_cmd(playback_file, buffer_frames=None, duration=None, |
| 26 | rate=44100): |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 27 | """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)] |
| 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( |
| 45 | capture_file, buffer_frames=None, duration=10, channels=2, rate=44100): |
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. |
| 49 | @param buffer_frames: total number of frames to bufffer. |
| 50 | @param duration: seconds to record. |
| 51 | @param rate: the sampling rate. |
| 52 | """ |
| 53 | args = [_CRAS_TEST_CLIENT] |
| 54 | args += ['--capture_file', capture_file] |
| 55 | if buffer_frames is not None: |
| 56 | args += ['--buffer_frames', str(buffer_frames)] |
| 57 | args += ['--duration', str(duration)] |
Owen Lin | a8129c6 | 2013-11-26 16:51:46 +0800 | [diff] [blame^] | 58 | args += ['--num_channels', str(channels)] |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 59 | args += ['--rate', str(rate)] |
| 60 | return args |
| 61 | |
| 62 | def set_system_volume(volume): |
| 63 | """Set the system volume. |
| 64 | |
| 65 | @param volume: the system output vlume to be set(0 - 100). |
| 66 | """ |
| 67 | args = [_CRAS_TEST_CLIENT] |
| 68 | args += ['--volume', str(volume)] |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 69 | cmd_utils.execute(args) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 70 | |
| 71 | def set_node_volume(node_id, volume): |
| 72 | """Set the volume of the given output node. |
| 73 | |
| 74 | @param node_id: the id of the output node to be set the volume. |
| 75 | @param volume: the volume to be set(0-100). |
| 76 | """ |
| 77 | args = [_CRAS_TEST_CLIENT] |
| 78 | args += ['--set_node_volume', '%s:%d' % (node_id, volume)] |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 79 | cmd_utils.execute(args) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 80 | |
| 81 | def set_capture_gain(gain): |
| 82 | """Set the system capture gain. |
| 83 | @param gain the capture gain in db*100 (100 = 1dB) |
| 84 | """ |
| 85 | args = [_CRAS_TEST_CLIENT] |
| 86 | args += ['--capture_gain', str(gain)] |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame] | 87 | cmd_utils.execute(args) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 88 | |
| 89 | def dump_server_info(): |
| 90 | """Gets the CRAS's server information.""" |
Owen Lin | 0d65e8a | 2013-11-28 14:29:54 +0800 | [diff] [blame] | 91 | args = [_CRAS_TEST_CLIENT, '--dump_server_info'] |
| 92 | return cmd_utils.execute(args, stdout=subprocess.PIPE) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 93 | |
| 94 | def get_selected_nodes(): |
| 95 | """Returns the pair of active output node and input node.""" |
| 96 | server_info = dump_server_info() |
| 97 | output_match = _RE_SELECTED_OUTPUT_NODE.search(server_info) |
| 98 | input_match = _RE_SELECTED_INPUT_NODE.search(server_info) |
| 99 | if not output_match or not input_match: |
| 100 | logging.error(server_info) |
| 101 | raise RuntimeError('No match for the pattern') |
| 102 | |
| 103 | return (output_match.group(1).strip(), input_match.group(1).strip()) |
Owen Lin | 7ab45a2 | 2013-11-19 17:26:33 +0800 | [diff] [blame] | 104 | |
| 105 | def get_active_stream_count(): |
| 106 | """Gets the number of active streams.""" |
| 107 | server_info = dump_server_info() |
| 108 | match = _RE_NUM_ACTIVE_STREAM.search(server_info) |
| 109 | if not match: |
| 110 | logging.error(server_info) |
| 111 | raise RuntimeException('Cannot find matched pattern') |
| 112 | return int(match.group(1)) |