Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: media/audio/cras/audio_manager_cras.cc

Issue 2516813002: Enable pinning stream output routing for webapp request on ChromeOS (Closed)
Patch Set: new labels instead of Internal Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/audio/cras/audio_manager_cras.h ('k') | media/audio/cras/cras_input.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 // Define bounds for the output buffer size. 42 // Define bounds for the output buffer size.
43 const int kMinimumOutputBufferSize = 512; 43 const int kMinimumOutputBufferSize = 512;
44 const int kMaximumOutputBufferSize = 8192; 44 const int kMaximumOutputBufferSize = 8192;
45 45
46 // Default input buffer size. 46 // Default input buffer size.
47 const int kDefaultInputBufferSize = 1024; 47 const int kDefaultInputBufferSize = 1024;
48 48
49 const char kBeamformingOnDeviceId[] = "default-beamforming-on"; 49 const char kBeamformingOnDeviceId[] = "default-beamforming-on";
50 const char kBeamformingOffDeviceId[] = "default-beamforming-off"; 50 const char kBeamformingOffDeviceId[] = "default-beamforming-off";
51 51
52 // Labels for showing audio devices with internal nodes.
53 const char kInternalInputDevice[] = "Headset/Front Mic";
54 const char kInternalOutputDevice[] = "HP/Speaker";
55
52 enum CrosBeamformingDeviceState { 56 enum CrosBeamformingDeviceState {
53 BEAMFORMING_DEFAULT_ENABLED = 0, 57 BEAMFORMING_DEFAULT_ENABLED = 0,
54 BEAMFORMING_USER_ENABLED, 58 BEAMFORMING_USER_ENABLED,
55 BEAMFORMING_DEFAULT_DISABLED, 59 BEAMFORMING_DEFAULT_DISABLED,
56 BEAMFORMING_USER_DISABLED, 60 BEAMFORMING_USER_DISABLED,
57 BEAMFORMING_STATE_MAX = BEAMFORMING_USER_DISABLED 61 BEAMFORMING_STATE_MAX = BEAMFORMING_USER_DISABLED
58 }; 62 };
59 63
60 void RecordBeamformingDeviceState(CrosBeamformingDeviceState state) { 64 void RecordBeamformingDeviceState(CrosBeamformingDeviceState state) {
61 UMA_HISTOGRAM_ENUMERATION("Media.CrosBeamformingDeviceState", state, 65 UMA_HISTOGRAM_ENUMERATION("Media.CrosBeamformingDeviceState", state,
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 156
153 void AudioManagerCras::GetAudioDeviceNamesImpl(bool is_input, 157 void AudioManagerCras::GetAudioDeviceNamesImpl(bool is_input,
154 AudioDeviceNames* device_names) { 158 AudioDeviceNames* device_names) {
155 DCHECK(device_names->empty()); 159 DCHECK(device_names->empty());
156 // At least two mic positions indicates we have a beamforming capable mic 160 // At least two mic positions indicates we have a beamforming capable mic
157 // array. Add the virtual beamforming device to the list. When this device is 161 // array. Add the virtual beamforming device to the list. When this device is
158 // queried through GetInputStreamParameters, provide the cached mic positions. 162 // queried through GetInputStreamParameters, provide the cached mic positions.
159 if (is_input && mic_positions_.size() > 1) 163 if (is_input && mic_positions_.size() > 1)
160 AddBeamformingDevices(device_names); 164 AddBeamformingDevices(device_names);
161 else 165 else
162 device_names->push_back(media::AudioDeviceName::CreateDefault()); 166 device_names->push_back(AudioDeviceName::CreateDefault());
163 167
164 if (base::FeatureList::IsEnabled(features::kEnumerateAudioDevices)) { 168 if (base::FeatureList::IsEnabled(features::kEnumerateAudioDevices)) {
165 chromeos::AudioDeviceList devices; 169 chromeos::AudioDeviceList devices;
166 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); 170 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices);
171
172 int internal_input_dev_index = 0;
173 int internal_output_dev_index = 0;
174 for (const auto& device : devices) {
175 if (device.type == chromeos::AUDIO_TYPE_INTERNAL_MIC)
176 internal_input_dev_index = dev_index_of(device.id);
177 else if (device.type == chromeos::AUDIO_TYPE_INTERNAL_SPEAKER)
dgreid 2016/11/29 19:38:44 How does this work on devices without a speaker no
Qiang(Joe) Xu 2016/11/29 21:57:47 For devices without a speaker node or internal mic
dgreid 2016/11/29 23:23:18 Got it. Yes, I hadn't fully understood the code h
178 internal_output_dev_index = dev_index_of(device.id);
179 }
180
181 bool has_internal_input = false;
182 bool has_internal_output = false;
167 for (const auto& device : devices) { 183 for (const auto& device : devices) {
168 if (device.is_input == is_input && device.is_for_simple_usage()) { 184 if (device.is_input == is_input && device.is_for_simple_usage()) {
169 device_names->emplace_back(device.display_name, 185 int dev_index = dev_index_of(device.id);
170 base::Uint64ToString(device.id)); 186 if (dev_index == internal_input_dev_index) {
187 if (!has_internal_input) {
188 device_names->emplace_back(kInternalInputDevice,
189 base::Uint64ToString(device.id));
190 has_internal_input = true;
191 }
192 } else if (dev_index == internal_output_dev_index) {
193 if (!has_internal_output) {
194 device_names->emplace_back(kInternalOutputDevice,
195 base::Uint64ToString(device.id));
196 has_internal_output = true;
197 }
198 } else {
199 device_names->emplace_back(device.display_name,
200 base::Uint64ToString(device.id));
201 }
dgreid 2016/11/29 19:38:44 Can you summarize what this code does? Is "Replac
Qiang(Joe) Xu 2016/11/29 21:57:47 "Replace the internal and external device names wi
dgreid 2016/11/29 23:23:18 OK, can you add a comment with a summary? It was
Qiang(Joe) Xu 2016/11/30 01:01:17 Done.
171 } 202 }
172 } 203 }
173 } 204 }
174 } 205 }
175 206
176 void AudioManagerCras::GetAudioInputDeviceNames( 207 void AudioManagerCras::GetAudioInputDeviceNames(
177 AudioDeviceNames* device_names) { 208 AudioDeviceNames* device_names) {
178 mic_positions_ = ParsePointsFromString(MicPositions()); 209 mic_positions_ = ParsePointsFromString(MicPositions());
179 GetAudioDeviceNamesImpl(true, device_names); 210 GetAudioDeviceNamesImpl(true, device_names);
180 } 211 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 RecordBeamformingDeviceState(BEAMFORMING_USER_DISABLED); 256 RecordBeamformingDeviceState(BEAMFORMING_USER_DISABLED);
226 } 257 }
227 } 258 }
228 return params; 259 return params;
229 } 260 }
230 261
231 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( 262 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream(
232 const AudioParameters& params, 263 const AudioParameters& params,
233 const LogCallback& log_callback) { 264 const LogCallback& log_callback) {
234 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); 265 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
235 return MakeOutputStream(params); 266 // (warx): pinning stream is not supported for MakeLinearOutputStream.
267 return MakeOutputStream(params, AudioDeviceDescription::kDefaultDeviceId);
236 } 268 }
237 269
238 AudioOutputStream* AudioManagerCras::MakeLowLatencyOutputStream( 270 AudioOutputStream* AudioManagerCras::MakeLowLatencyOutputStream(
239 const AudioParameters& params, 271 const AudioParameters& params,
240 const std::string& device_id, 272 const std::string& device_id,
241 const LogCallback& log_callback) { 273 const LogCallback& log_callback) {
242 DLOG_IF(ERROR, !device_id.empty()) << "Not implemented!";
243 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); 274 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
244 // TODO(dgreid): Open the correct input device for unified IO. 275 // TODO(dgreid): Open the correct input device for unified IO.
245 return MakeOutputStream(params); 276 return MakeOutputStream(params, device_id);
246 } 277 }
247 278
248 AudioInputStream* AudioManagerCras::MakeLinearInputStream( 279 AudioInputStream* AudioManagerCras::MakeLinearInputStream(
249 const AudioParameters& params, 280 const AudioParameters& params,
250 const std::string& device_id, 281 const std::string& device_id,
251 const LogCallback& log_callback) { 282 const LogCallback& log_callback) {
252 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); 283 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
253 return MakeInputStream(params, device_id); 284 return MakeInputStream(params, device_id);
254 } 285 }
255 286
256 AudioInputStream* AudioManagerCras::MakeLowLatencyInputStream( 287 AudioInputStream* AudioManagerCras::MakeLowLatencyInputStream(
257 const AudioParameters& params, 288 const AudioParameters& params,
258 const std::string& device_id, 289 const std::string& device_id,
259 const LogCallback& log_callback) { 290 const LogCallback& log_callback) {
260 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); 291 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
261 return MakeInputStream(params, device_id); 292 return MakeInputStream(params, device_id);
262 } 293 }
263 294
264 AudioParameters AudioManagerCras::GetPreferredOutputStreamParameters( 295 AudioParameters AudioManagerCras::GetPreferredOutputStreamParameters(
265 const std::string& output_device_id, 296 const std::string& output_device_id,
266 const AudioParameters& input_params) { 297 const AudioParameters& input_params) {
267 // TODO(tommi): Support |output_device_id|.
268 DLOG_IF(ERROR, !output_device_id.empty()) << "Not implemented!";
269 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; 298 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
270 int sample_rate = kDefaultSampleRate; 299 int sample_rate = kDefaultSampleRate;
271 int buffer_size = kMinimumOutputBufferSize; 300 int buffer_size = kMinimumOutputBufferSize;
272 int bits_per_sample = 16; 301 int bits_per_sample = 16;
273 if (input_params.IsValid()) { 302 if (input_params.IsValid()) {
274 sample_rate = input_params.sample_rate(); 303 sample_rate = input_params.sample_rate();
275 bits_per_sample = input_params.bits_per_sample(); 304 bits_per_sample = input_params.bits_per_sample();
276 channel_layout = input_params.channel_layout(); 305 channel_layout = input_params.channel_layout();
277 buffer_size = 306 buffer_size =
278 std::min(kMaximumOutputBufferSize, 307 std::min(kMaximumOutputBufferSize,
279 std::max(buffer_size, input_params.frames_per_buffer())); 308 std::max(buffer_size, input_params.frames_per_buffer()));
280 } 309 }
281 310
282 int user_buffer_size = GetUserBufferSize(); 311 int user_buffer_size = GetUserBufferSize();
283 if (user_buffer_size) 312 if (user_buffer_size)
284 buffer_size = user_buffer_size; 313 buffer_size = user_buffer_size;
285 314
286 return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, 315 return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout,
287 sample_rate, bits_per_sample, buffer_size); 316 sample_rate, bits_per_sample, buffer_size);
288 } 317 }
289 318
290 AudioOutputStream* AudioManagerCras::MakeOutputStream( 319 AudioOutputStream* AudioManagerCras::MakeOutputStream(
291 const AudioParameters& params) { 320 const AudioParameters& params,
292 return new CrasUnifiedStream(params, this); 321 const std::string& device_id) {
322 return new CrasUnifiedStream(params, this, device_id);
293 } 323 }
294 324
295 AudioInputStream* AudioManagerCras::MakeInputStream( 325 AudioInputStream* AudioManagerCras::MakeInputStream(
296 const AudioParameters& params, const std::string& device_id) { 326 const AudioParameters& params, const std::string& device_id) {
297 return new CrasInputStream(params, this, device_id); 327 return new CrasInputStream(params, this, device_id);
298 } 328 }
299 329
300 snd_pcm_format_t AudioManagerCras::BitsToFormat(int bits_per_sample) { 330 snd_pcm_format_t AudioManagerCras::BitsToFormat(int bits_per_sample) {
301 switch (bits_per_sample) { 331 switch (bits_per_sample) {
302 case 8: 332 case 8:
303 return SND_PCM_FORMAT_U8; 333 return SND_PCM_FORMAT_U8;
304 case 16: 334 case 16:
305 return SND_PCM_FORMAT_S16; 335 return SND_PCM_FORMAT_S16;
306 case 24: 336 case 24:
307 return SND_PCM_FORMAT_S24; 337 return SND_PCM_FORMAT_S24;
308 case 32: 338 case 32:
309 return SND_PCM_FORMAT_S32; 339 return SND_PCM_FORMAT_S32;
310 default: 340 default:
311 return SND_PCM_FORMAT_UNKNOWN; 341 return SND_PCM_FORMAT_UNKNOWN;
312 } 342 }
313 } 343 }
314 344
345 bool AudioManagerCras::IsDefault(const std::string& device_id, bool is_input) {
346 AudioDeviceNames device_names;
347 GetAudioDeviceNamesImpl(is_input, &device_names);
348 DCHECK(!device_names.empty());
349 AudioDeviceName device_name = device_names.front();
350 return device_name.unique_id == device_id;
351 }
352
315 } // namespace media 353 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/cras/audio_manager_cras.h ('k') | media/audio/cras/cras_input.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698