Chromium Code Reviews| 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 <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 Loading... | |
| 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 const char kInternal[] = "Internal"; | |
| 53 | |
| 52 enum CrosBeamformingDeviceState { | 54 enum CrosBeamformingDeviceState { |
| 53 BEAMFORMING_DEFAULT_ENABLED = 0, | 55 BEAMFORMING_DEFAULT_ENABLED = 0, |
| 54 BEAMFORMING_USER_ENABLED, | 56 BEAMFORMING_USER_ENABLED, |
| 55 BEAMFORMING_DEFAULT_DISABLED, | 57 BEAMFORMING_DEFAULT_DISABLED, |
| 56 BEAMFORMING_USER_DISABLED, | 58 BEAMFORMING_USER_DISABLED, |
| 57 BEAMFORMING_STATE_MAX = BEAMFORMING_USER_DISABLED | 59 BEAMFORMING_STATE_MAX = BEAMFORMING_USER_DISABLED |
| 58 }; | 60 }; |
| 59 | 61 |
| 60 void RecordBeamformingDeviceState(CrosBeamformingDeviceState state) { | 62 void RecordBeamformingDeviceState(CrosBeamformingDeviceState state) { |
| 61 UMA_HISTOGRAM_ENUMERATION("Media.CrosBeamformingDeviceState", state, | 63 UMA_HISTOGRAM_ENUMERATION("Media.CrosBeamformingDeviceState", state, |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 | 154 |
| 153 void AudioManagerCras::GetAudioDeviceNamesImpl(bool is_input, | 155 void AudioManagerCras::GetAudioDeviceNamesImpl(bool is_input, |
| 154 AudioDeviceNames* device_names) { | 156 AudioDeviceNames* device_names) { |
| 155 DCHECK(device_names->empty()); | 157 DCHECK(device_names->empty()); |
| 156 // At least two mic positions indicates we have a beamforming capable mic | 158 // 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 | 159 // array. Add the virtual beamforming device to the list. When this device is |
| 158 // queried through GetInputStreamParameters, provide the cached mic positions. | 160 // queried through GetInputStreamParameters, provide the cached mic positions. |
| 159 if (is_input && mic_positions_.size() > 1) | 161 if (is_input && mic_positions_.size() > 1) |
| 160 AddBeamformingDevices(device_names); | 162 AddBeamformingDevices(device_names); |
| 161 else | 163 else |
| 162 device_names->push_back(media::AudioDeviceName::CreateDefault()); | 164 device_names->push_back(AudioDeviceName::CreateDefault()); |
| 163 | 165 |
| 164 if (base::FeatureList::IsEnabled(features::kEnumerateAudioDevices)) { | 166 if (base::FeatureList::IsEnabled(features::kEnumerateAudioDevices)) { |
| 165 chromeos::AudioDeviceList devices; | 167 chromeos::AudioDeviceList devices; |
| 166 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); | 168 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); |
| 169 | |
| 170 int internal_input_dev_index = 0; | |
| 171 int internal_output_dev_index = 0; | |
| 172 for (const auto& device : devices) { | |
| 173 if (device.type == chromeos::AUDIO_TYPE_INTERNAL_MIC) | |
| 174 internal_input_dev_index = dev_index_of(device.id); | |
| 175 else if (device.type == chromeos::AUDIO_TYPE_INTERNAL_SPEAKER) | |
| 176 internal_output_dev_index = dev_index_of(device.id); | |
| 177 } | |
| 178 | |
| 179 bool has_internal_input = false; | |
| 180 bool has_internal_output = false; | |
| 167 for (const auto& device : devices) { | 181 for (const auto& device : devices) { |
| 168 if (device.is_input == is_input && device.is_for_simple_usage()) { | 182 if (device.is_input == is_input && device.is_for_simple_usage()) { |
| 169 device_names->emplace_back(device.display_name, | 183 int dev_index = dev_index_of(device.id); |
| 170 base::Uint64ToString(device.id)); | 184 if (dev_index == internal_input_dev_index) { |
| 185 if (!has_internal_input) { | |
| 186 device_names->emplace_back(kInternal, | |
| 187 base::Uint64ToString(device.id)); | |
| 188 has_internal_input = true; | |
| 189 } | |
| 190 } else if (dev_index == internal_output_dev_index) { | |
| 191 if (!has_internal_output) { | |
| 192 device_names->emplace_back(kInternal, | |
| 193 base::Uint64ToString(device.id)); | |
| 194 has_internal_output = true; | |
| 195 } | |
| 196 } else { | |
| 197 device_names->emplace_back(device.display_name, | |
| 198 base::Uint64ToString(device.id)); | |
| 199 } | |
| 171 } | 200 } |
| 172 } | 201 } |
| 173 } | 202 } |
| 174 } | 203 } |
| 175 | 204 |
| 176 void AudioManagerCras::GetAudioInputDeviceNames( | 205 void AudioManagerCras::GetAudioInputDeviceNames( |
| 177 AudioDeviceNames* device_names) { | 206 AudioDeviceNames* device_names) { |
| 178 mic_positions_ = ParsePointsFromString(MicPositions()); | 207 mic_positions_ = ParsePointsFromString(MicPositions()); |
| 179 GetAudioDeviceNamesImpl(true, device_names); | 208 GetAudioDeviceNamesImpl(true, device_names); |
| 180 } | 209 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 RecordBeamformingDeviceState(BEAMFORMING_USER_DISABLED); | 254 RecordBeamformingDeviceState(BEAMFORMING_USER_DISABLED); |
| 226 } | 255 } |
| 227 } | 256 } |
| 228 return params; | 257 return params; |
| 229 } | 258 } |
| 230 | 259 |
| 231 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( | 260 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( |
| 232 const AudioParameters& params, | 261 const AudioParameters& params, |
| 233 const LogCallback& log_callback) { | 262 const LogCallback& log_callback) { |
| 234 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | 263 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
| 235 return MakeOutputStream(params); | 264 // (warx): pinning stream is not supported for MakeLinearOutputStream. |
|
Qiang(Joe) Xu
2016/11/19 00:41:44
shall we open a new bug to track this? I don't hav
| |
| 265 return MakeOutputStream(params, AudioDeviceName::CreateDefault().unique_id); | |
| 236 } | 266 } |
| 237 | 267 |
| 238 AudioOutputStream* AudioManagerCras::MakeLowLatencyOutputStream( | 268 AudioOutputStream* AudioManagerCras::MakeLowLatencyOutputStream( |
| 239 const AudioParameters& params, | 269 const AudioParameters& params, |
| 240 const std::string& device_id, | 270 const std::string& device_id, |
| 241 const LogCallback& log_callback) { | 271 const LogCallback& log_callback) { |
| 242 DLOG_IF(ERROR, !device_id.empty()) << "Not implemented!"; | |
|
Qiang(Joe) Xu
2016/11/19 00:41:44
it is now supported
| |
| 243 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); | 272 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); |
| 244 // TODO(dgreid): Open the correct input device for unified IO. | 273 // TODO(dgreid): Open the correct input device for unified IO. |
| 245 return MakeOutputStream(params); | 274 return MakeOutputStream(params, device_id); |
| 246 } | 275 } |
| 247 | 276 |
| 248 AudioInputStream* AudioManagerCras::MakeLinearInputStream( | 277 AudioInputStream* AudioManagerCras::MakeLinearInputStream( |
| 249 const AudioParameters& params, | 278 const AudioParameters& params, |
| 250 const std::string& device_id, | 279 const std::string& device_id, |
| 251 const LogCallback& log_callback) { | 280 const LogCallback& log_callback) { |
| 252 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | 281 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
| 253 return MakeInputStream(params, device_id); | 282 return MakeInputStream(params, device_id); |
| 254 } | 283 } |
| 255 | 284 |
| 256 AudioInputStream* AudioManagerCras::MakeLowLatencyInputStream( | 285 AudioInputStream* AudioManagerCras::MakeLowLatencyInputStream( |
| 257 const AudioParameters& params, | 286 const AudioParameters& params, |
| 258 const std::string& device_id, | 287 const std::string& device_id, |
| 259 const LogCallback& log_callback) { | 288 const LogCallback& log_callback) { |
| 260 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); | 289 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); |
| 261 return MakeInputStream(params, device_id); | 290 return MakeInputStream(params, device_id); |
| 262 } | 291 } |
| 263 | 292 |
| 264 AudioParameters AudioManagerCras::GetPreferredOutputStreamParameters( | 293 AudioParameters AudioManagerCras::GetPreferredOutputStreamParameters( |
| 265 const std::string& output_device_id, | 294 const std::string& output_device_id, |
| 266 const AudioParameters& input_params) { | 295 const AudioParameters& input_params) { |
| 267 // TODO(tommi): Support |output_device_id|. | |
| 268 DLOG_IF(ERROR, !output_device_id.empty()) << "Not implemented!"; | |
|
Qiang(Joe) Xu
2016/11/19 00:41:44
it is now supported
| |
| 269 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; | 296 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; |
| 270 int sample_rate = kDefaultSampleRate; | 297 int sample_rate = kDefaultSampleRate; |
| 271 int buffer_size = kMinimumOutputBufferSize; | 298 int buffer_size = kMinimumOutputBufferSize; |
| 272 int bits_per_sample = 16; | 299 int bits_per_sample = 16; |
| 273 if (input_params.IsValid()) { | 300 if (input_params.IsValid()) { |
| 274 sample_rate = input_params.sample_rate(); | 301 sample_rate = input_params.sample_rate(); |
| 275 bits_per_sample = input_params.bits_per_sample(); | 302 bits_per_sample = input_params.bits_per_sample(); |
| 276 channel_layout = input_params.channel_layout(); | 303 channel_layout = input_params.channel_layout(); |
| 277 buffer_size = | 304 buffer_size = |
| 278 std::min(kMaximumOutputBufferSize, | 305 std::min(kMaximumOutputBufferSize, |
| 279 std::max(buffer_size, input_params.frames_per_buffer())); | 306 std::max(buffer_size, input_params.frames_per_buffer())); |
| 280 } | 307 } |
| 281 | 308 |
| 282 int user_buffer_size = GetUserBufferSize(); | 309 int user_buffer_size = GetUserBufferSize(); |
| 283 if (user_buffer_size) | 310 if (user_buffer_size) |
| 284 buffer_size = user_buffer_size; | 311 buffer_size = user_buffer_size; |
| 285 | 312 |
| 286 return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, | 313 return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, |
| 287 sample_rate, bits_per_sample, buffer_size); | 314 sample_rate, bits_per_sample, buffer_size); |
| 288 } | 315 } |
| 289 | 316 |
| 290 AudioOutputStream* AudioManagerCras::MakeOutputStream( | 317 AudioOutputStream* AudioManagerCras::MakeOutputStream( |
| 291 const AudioParameters& params) { | 318 const AudioParameters& params, |
| 292 return new CrasUnifiedStream(params, this); | 319 const std::string& device_id) { |
| 320 return new CrasUnifiedStream(params, this, device_id); | |
| 293 } | 321 } |
| 294 | 322 |
| 295 AudioInputStream* AudioManagerCras::MakeInputStream( | 323 AudioInputStream* AudioManagerCras::MakeInputStream( |
| 296 const AudioParameters& params, const std::string& device_id) { | 324 const AudioParameters& params, const std::string& device_id) { |
| 297 return new CrasInputStream(params, this, device_id); | 325 return new CrasInputStream(params, this, device_id); |
| 298 } | 326 } |
| 299 | 327 |
| 300 snd_pcm_format_t AudioManagerCras::BitsToFormat(int bits_per_sample) { | 328 snd_pcm_format_t AudioManagerCras::BitsToFormat(int bits_per_sample) { |
| 301 switch (bits_per_sample) { | 329 switch (bits_per_sample) { |
| 302 case 8: | 330 case 8: |
| 303 return SND_PCM_FORMAT_U8; | 331 return SND_PCM_FORMAT_U8; |
| 304 case 16: | 332 case 16: |
| 305 return SND_PCM_FORMAT_S16; | 333 return SND_PCM_FORMAT_S16; |
| 306 case 24: | 334 case 24: |
| 307 return SND_PCM_FORMAT_S24; | 335 return SND_PCM_FORMAT_S24; |
| 308 case 32: | 336 case 32: |
| 309 return SND_PCM_FORMAT_S32; | 337 return SND_PCM_FORMAT_S32; |
| 310 default: | 338 default: |
| 311 return SND_PCM_FORMAT_UNKNOWN; | 339 return SND_PCM_FORMAT_UNKNOWN; |
| 312 } | 340 } |
| 313 } | 341 } |
| 314 | 342 |
| 343 bool AudioManagerCras::IsDefault(const std::string& device_id, bool is_input) { | |
| 344 AudioDeviceNames device_names; | |
| 345 GetAudioDeviceNamesImpl(is_input, &device_names); | |
| 346 DCHECK(!device_names.empty()); | |
| 347 AudioDeviceName device_name = device_names.front(); | |
| 348 return device_name.unique_id == device_id; | |
| 349 } | |
| 350 | |
| 315 } // namespace media | 351 } // namespace media |
| OLD | NEW |