OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/audio/cras/audio_manager_cras.h" | 5 #include "media/audio/cras/audio_manager_cras.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/environment.h" | 10 #include "base/environment.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/nix/xdg_util.h" | 12 #include "base/nix/xdg_util.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "chromeos/audio/audio_device.h" | 14 #include "chromeos/audio/audio_device.h" |
15 #include "chromeos/audio/cras_audio_handler.h" | 15 #include "chromeos/audio/cras_audio_handler.h" |
16 #include "media/audio/cras/cras_input.h" | 16 #include "media/audio/cras/cras_input.h" |
17 #include "media/audio/cras/cras_unified.h" | 17 #include "media/audio/cras/cras_unified.h" |
18 #include "media/base/channel_layout.h" | 18 #include "media/base/channel_layout.h" |
19 | 19 |
20 // cras_util.h headers pull in min/max macros... | 20 // cras_util.h headers pull in min/max macros... |
21 // TODO(dgreid): Fix headers such that these aren't imported. | 21 // TODO(dgreid): Fix headers such that these aren't imported. |
22 #undef min | 22 #undef min |
23 #undef max | 23 #undef max |
24 | 24 |
25 namespace media { | 25 namespace media { |
26 namespace { | |
26 | 27 |
27 static void AddDefaultDevice(AudioDeviceNames* device_names) { | 28 // Maximum number of output streams that can be open simultaneously. |
28 DCHECK(device_names->empty()); | 29 const int kMaxOutputStreams = 50; |
29 | 30 |
31 // Default sample rate for input and output streams. | |
32 const int kDefaultSampleRate = 48000; | |
33 | |
34 // Define bounds for the output buffer size. | |
35 const int kMinimumOutputBufferSize = 512; | |
36 const int kMaximumOutputBufferSize = 8192; | |
37 | |
38 // Default input buffer size. | |
39 const int kDefaultInputBufferSize = 1024; | |
40 | |
41 void AddDefaultDevice(AudioDeviceNames* device_names) { | |
30 // Cras will route audio from a proper physical device automatically. | 42 // Cras will route audio from a proper physical device automatically. |
31 device_names->push_back( | 43 device_names->push_back( |
32 AudioDeviceName(AudioManagerBase::kDefaultDeviceName, | 44 AudioDeviceName(AudioManagerBase::kDefaultDeviceName, |
33 AudioManagerBase::kDefaultDeviceId)); | 45 AudioManagerBase::kDefaultDeviceId)); |
34 } | 46 } |
35 | 47 |
36 // Maximum number of output streams that can be open simultaneously. | 48 // Returns a mic positions string if the machine has a beamforming capable |
37 static const int kMaxOutputStreams = 50; | 49 // internal mic and otherwise an empty string. |
50 std::string MicPositions() { | |
51 // Get the list of devices from CRAS. An internal mic with a non-empty | |
52 // positions field indicates the machine has a beamforming capable mic array. | |
53 chromeos::AudioDeviceList devices; | |
54 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); | |
55 for (const auto& device : devices) { | |
56 if (device.type == chromeos::AUDIO_TYPE_INTERNAL_MIC) { | |
57 // There should be only one internal mic device. | |
58 return device.mic_positions; | |
59 } | |
60 } | |
61 return ""; | |
62 } | |
38 | 63 |
39 // Default sample rate for input and output streams. | 64 // Returns the name and ID of the virtual beamforming device. |
40 static const int kDefaultSampleRate = 48000; | 65 AudioDeviceName BeamformingDeviceName() { |
66 static const char kBeamformingDeviceNameSuffix[] = " with beamforming"; | |
67 static const char kBeamformingDeviceIdSuffix[] = "-beamforming"; | |
41 | 68 |
42 // Define bounds for the output buffer size. | 69 return AudioDeviceName(std::string(AudioManagerBase::kDefaultDeviceName) + |
43 static const int kMinimumOutputBufferSize = 512; | 70 kBeamformingDeviceNameSuffix, |
44 static const int kMaximumOutputBufferSize = 8192; | 71 std::string(AudioManagerBase::kDefaultDeviceId) + |
72 kBeamformingDeviceIdSuffix); | |
73 } | |
45 | 74 |
46 // Default input buffer size. | 75 } // namespace |
47 static const int kDefaultInputBufferSize = 1024; | |
48 | 76 |
49 bool AudioManagerCras::HasAudioOutputDevices() { | 77 bool AudioManagerCras::HasAudioOutputDevices() { |
50 return true; | 78 return true; |
51 } | 79 } |
52 | 80 |
53 bool AudioManagerCras::HasAudioInputDevices() { | 81 bool AudioManagerCras::HasAudioInputDevices() { |
54 chromeos::AudioDeviceList devices; | 82 chromeos::AudioDeviceList devices; |
55 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); | 83 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); |
56 for (size_t i = 0; i < devices.size(); ++i) { | 84 for (size_t i = 0; i < devices.size(); ++i) { |
57 if (devices[i].is_input && devices[i].is_for_simple_usage()) | 85 if (devices[i].is_input && devices[i].is_for_simple_usage()) |
58 return true; | 86 return true; |
59 } | 87 } |
60 return false; | 88 return false; |
61 } | 89 } |
62 | 90 |
63 AudioManagerCras::AudioManagerCras(AudioLogFactory* audio_log_factory) | 91 AudioManagerCras::AudioManagerCras(AudioLogFactory* audio_log_factory) |
64 : AudioManagerBase(audio_log_factory), | 92 : AudioManagerBase(audio_log_factory), |
65 has_keyboard_mic_(false) { | 93 has_keyboard_mic_(false), |
94 beamforming_device_name_(BeamformingDeviceName()) { | |
ajm
2015/09/03 01:38:42
Alex, I reverted to storing this as a const member
| |
66 SetMaxOutputStreamsAllowed(kMaxOutputStreams); | 95 SetMaxOutputStreamsAllowed(kMaxOutputStreams); |
67 } | 96 } |
68 | 97 |
69 AudioManagerCras::~AudioManagerCras() { | 98 AudioManagerCras::~AudioManagerCras() { |
70 Shutdown(); | 99 Shutdown(); |
71 } | 100 } |
72 | 101 |
73 void AudioManagerCras::ShowAudioInputSettings() { | 102 void AudioManagerCras::ShowAudioInputSettings() { |
74 NOTIMPLEMENTED(); | 103 NOTIMPLEMENTED(); |
75 } | 104 } |
76 | 105 |
77 void AudioManagerCras::GetAudioInputDeviceNames( | 106 void AudioManagerCras::GetAudioInputDeviceNames( |
78 AudioDeviceNames* device_names) { | 107 AudioDeviceNames* device_names) { |
108 mic_positions_ = ParsePointsFromString(MicPositions()); | |
109 // At least two mic positions indicates we have a beamforming capable mic | |
110 // array. Add the virtual beamforming device to the list. When this device is | |
111 // queried through GetInputStreamParameters, provide the cached mic positions. | |
112 if (mic_positions_.size() > 1) | |
113 device_names->push_back(beamforming_device_name_); | |
114 | |
79 AddDefaultDevice(device_names); | 115 AddDefaultDevice(device_names); |
80 } | 116 } |
81 | 117 |
82 void AudioManagerCras::GetAudioOutputDeviceNames( | 118 void AudioManagerCras::GetAudioOutputDeviceNames( |
83 AudioDeviceNames* device_names) { | 119 AudioDeviceNames* device_names) { |
84 AddDefaultDevice(device_names); | 120 AddDefaultDevice(device_names); |
85 } | 121 } |
86 | 122 |
87 AudioParameters AudioManagerCras::GetInputStreamParameters( | 123 AudioParameters AudioManagerCras::GetInputStreamParameters( |
88 const std::string& device_id) { | 124 const std::string& device_id) { |
89 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 125 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
90 | 126 |
91 int user_buffer_size = GetUserBufferSize(); | 127 int user_buffer_size = GetUserBufferSize(); |
92 int buffer_size = user_buffer_size ? | 128 int buffer_size = user_buffer_size ? |
93 user_buffer_size : kDefaultInputBufferSize; | 129 user_buffer_size : kDefaultInputBufferSize; |
94 AudioParameters::PlatformEffectsMask effects = | 130 AudioParameters::PlatformEffectsMask effects = |
95 has_keyboard_mic_ ? AudioParameters::KEYBOARD_MIC | 131 has_keyboard_mic_ ? AudioParameters::KEYBOARD_MIC |
96 : AudioParameters::NO_EFFECTS; | 132 : AudioParameters::NO_EFFECTS; |
97 | 133 |
134 // Return the cached mic positions in the case of the beamforming device. | |
135 const std::vector<Point>& mic_positions = | |
136 device_id == beamforming_device_name_.unique_id ? mic_positions_ | |
137 : std::vector<Point>(); | |
138 | |
98 // TODO(hshi): Fine-tune audio parameters based on |device_id|. The optimal | 139 // TODO(hshi): Fine-tune audio parameters based on |device_id|. The optimal |
99 // parameters for the loopback stream may differ from the default. | 140 // parameters for the loopback stream may differ from the default. |
100 return AudioParameters( | 141 return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
101 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, | 142 CHANNEL_LAYOUT_STEREO, kDefaultSampleRate, 16, |
102 kDefaultSampleRate, 16, buffer_size, effects); | 143 buffer_size, mic_positions, effects); |
103 } | 144 } |
104 | 145 |
105 void AudioManagerCras::SetHasKeyboardMic() { | 146 void AudioManagerCras::SetHasKeyboardMic() { |
106 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 147 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
107 has_keyboard_mic_ = true; | 148 has_keyboard_mic_ = true; |
108 } | 149 } |
109 | 150 |
110 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( | 151 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( |
111 const AudioParameters& params) { | 152 const AudioParameters& params) { |
112 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | 153 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 channel_layout = input_params.channel_layout(); | 190 channel_layout = input_params.channel_layout(); |
150 buffer_size = | 191 buffer_size = |
151 std::min(kMaximumOutputBufferSize, | 192 std::min(kMaximumOutputBufferSize, |
152 std::max(buffer_size, input_params.frames_per_buffer())); | 193 std::max(buffer_size, input_params.frames_per_buffer())); |
153 } | 194 } |
154 | 195 |
155 int user_buffer_size = GetUserBufferSize(); | 196 int user_buffer_size = GetUserBufferSize(); |
156 if (user_buffer_size) | 197 if (user_buffer_size) |
157 buffer_size = user_buffer_size; | 198 buffer_size = user_buffer_size; |
158 | 199 |
159 return AudioParameters( | 200 return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, |
160 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, | 201 sample_rate, bits_per_sample, buffer_size); |
161 sample_rate, bits_per_sample, buffer_size, AudioParameters::NO_EFFECTS); | |
162 } | 202 } |
163 | 203 |
164 AudioOutputStream* AudioManagerCras::MakeOutputStream( | 204 AudioOutputStream* AudioManagerCras::MakeOutputStream( |
165 const AudioParameters& params) { | 205 const AudioParameters& params) { |
166 return new CrasUnifiedStream(params, this); | 206 return new CrasUnifiedStream(params, this); |
167 } | 207 } |
168 | 208 |
169 AudioInputStream* AudioManagerCras::MakeInputStream( | 209 AudioInputStream* AudioManagerCras::MakeInputStream( |
170 const AudioParameters& params, const std::string& device_id) { | 210 const AudioParameters& params, const std::string& device_id) { |
171 return new CrasInputStream(params, this, device_id); | 211 return new CrasInputStream(params, this, device_id); |
172 } | 212 } |
173 | 213 |
174 snd_pcm_format_t AudioManagerCras::BitsToFormat(int bits_per_sample) { | 214 snd_pcm_format_t AudioManagerCras::BitsToFormat(int bits_per_sample) { |
175 switch (bits_per_sample) { | 215 switch (bits_per_sample) { |
176 case 8: | 216 case 8: |
177 return SND_PCM_FORMAT_U8; | 217 return SND_PCM_FORMAT_U8; |
178 case 16: | 218 case 16: |
179 return SND_PCM_FORMAT_S16; | 219 return SND_PCM_FORMAT_S16; |
180 case 24: | 220 case 24: |
181 return SND_PCM_FORMAT_S24; | 221 return SND_PCM_FORMAT_S24; |
182 case 32: | 222 case 32: |
183 return SND_PCM_FORMAT_S32; | 223 return SND_PCM_FORMAT_S32; |
184 default: | 224 default: |
185 return SND_PCM_FORMAT_UNKNOWN; | 225 return SND_PCM_FORMAT_UNKNOWN; |
186 } | 226 } |
187 } | 227 } |
188 | 228 |
189 } // namespace media | 229 } // namespace media |
OLD | NEW |