| 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 // 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 Loading... |
| 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 // Find if there exists |AUDIO_TYPE_INTERNAL_MIC| or |
| 173 // |AUDIO_TYPE_INTERNAL_SPEAKER|. If so, labeling the audio nodes that are |
| 174 // on internal devices using kInternalInputDevice or kInternalOutputDevice |
| 175 // and skip duplicates since pinning stream is pinned on device not node. |
| 176 // We only need to take care whenever there exists internal mic or internal |
| 177 // speaker since only internal device supports two nodes. |
| 178 int internal_input_dev_index = 0; |
| 179 int internal_output_dev_index = 0; |
| 180 for (const auto& device : devices) { |
| 181 if (device.type == chromeos::AUDIO_TYPE_INTERNAL_MIC) |
| 182 internal_input_dev_index = dev_index_of(device.id); |
| 183 else if (device.type == chromeos::AUDIO_TYPE_INTERNAL_SPEAKER) |
| 184 internal_output_dev_index = dev_index_of(device.id); |
| 185 } |
| 186 |
| 187 bool has_internal_input = false; |
| 188 bool has_internal_output = false; |
| 167 for (const auto& device : devices) { | 189 for (const auto& device : devices) { |
| 168 if (device.is_input == is_input && device.is_for_simple_usage()) { | 190 if (device.is_input == is_input && device.is_for_simple_usage()) { |
| 169 device_names->emplace_back(device.display_name, | 191 int dev_index = dev_index_of(device.id); |
| 170 base::Uint64ToString(device.id)); | 192 if (dev_index == internal_input_dev_index) { |
| 193 if (!has_internal_input) { |
| 194 device_names->emplace_back(kInternalInputDevice, |
| 195 base::Uint64ToString(device.id)); |
| 196 has_internal_input = true; |
| 197 } |
| 198 } else if (dev_index == internal_output_dev_index) { |
| 199 if (!has_internal_output) { |
| 200 device_names->emplace_back(kInternalOutputDevice, |
| 201 base::Uint64ToString(device.id)); |
| 202 has_internal_output = true; |
| 203 } |
| 204 } else { |
| 205 device_names->emplace_back(device.display_name, |
| 206 base::Uint64ToString(device.id)); |
| 207 } |
| 171 } | 208 } |
| 172 } | 209 } |
| 173 } | 210 } |
| 174 } | 211 } |
| 175 | 212 |
| 176 void AudioManagerCras::GetAudioInputDeviceNames( | 213 void AudioManagerCras::GetAudioInputDeviceNames( |
| 177 AudioDeviceNames* device_names) { | 214 AudioDeviceNames* device_names) { |
| 178 mic_positions_ = ParsePointsFromString(MicPositions()); | 215 mic_positions_ = ParsePointsFromString(MicPositions()); |
| 179 GetAudioDeviceNamesImpl(true, device_names); | 216 GetAudioDeviceNamesImpl(true, device_names); |
| 180 } | 217 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 RecordBeamformingDeviceState(BEAMFORMING_USER_DISABLED); | 262 RecordBeamformingDeviceState(BEAMFORMING_USER_DISABLED); |
| 226 } | 263 } |
| 227 } | 264 } |
| 228 return params; | 265 return params; |
| 229 } | 266 } |
| 230 | 267 |
| 231 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( | 268 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( |
| 232 const AudioParameters& params, | 269 const AudioParameters& params, |
| 233 const LogCallback& log_callback) { | 270 const LogCallback& log_callback) { |
| 234 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | 271 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
| 235 return MakeOutputStream(params); | 272 // (warx): pinning stream is not supported for MakeLinearOutputStream. |
| 273 return MakeOutputStream(params, AudioDeviceDescription::kDefaultDeviceId); |
| 236 } | 274 } |
| 237 | 275 |
| 238 AudioOutputStream* AudioManagerCras::MakeLowLatencyOutputStream( | 276 AudioOutputStream* AudioManagerCras::MakeLowLatencyOutputStream( |
| 239 const AudioParameters& params, | 277 const AudioParameters& params, |
| 240 const std::string& device_id, | 278 const std::string& device_id, |
| 241 const LogCallback& log_callback) { | 279 const LogCallback& log_callback) { |
| 242 DLOG_IF(ERROR, !device_id.empty()) << "Not implemented!"; | |
| 243 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); | 280 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); |
| 244 // TODO(dgreid): Open the correct input device for unified IO. | 281 // TODO(dgreid): Open the correct input device for unified IO. |
| 245 return MakeOutputStream(params); | 282 return MakeOutputStream(params, device_id); |
| 246 } | 283 } |
| 247 | 284 |
| 248 AudioInputStream* AudioManagerCras::MakeLinearInputStream( | 285 AudioInputStream* AudioManagerCras::MakeLinearInputStream( |
| 249 const AudioParameters& params, | 286 const AudioParameters& params, |
| 250 const std::string& device_id, | 287 const std::string& device_id, |
| 251 const LogCallback& log_callback) { | 288 const LogCallback& log_callback) { |
| 252 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | 289 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
| 253 return MakeInputStream(params, device_id); | 290 return MakeInputStream(params, device_id); |
| 254 } | 291 } |
| 255 | 292 |
| 256 AudioInputStream* AudioManagerCras::MakeLowLatencyInputStream( | 293 AudioInputStream* AudioManagerCras::MakeLowLatencyInputStream( |
| 257 const AudioParameters& params, | 294 const AudioParameters& params, |
| 258 const std::string& device_id, | 295 const std::string& device_id, |
| 259 const LogCallback& log_callback) { | 296 const LogCallback& log_callback) { |
| 260 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); | 297 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); |
| 261 return MakeInputStream(params, device_id); | 298 return MakeInputStream(params, device_id); |
| 262 } | 299 } |
| 263 | 300 |
| 264 AudioParameters AudioManagerCras::GetPreferredOutputStreamParameters( | 301 AudioParameters AudioManagerCras::GetPreferredOutputStreamParameters( |
| 265 const std::string& output_device_id, | 302 const std::string& output_device_id, |
| 266 const AudioParameters& input_params) { | 303 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; | 304 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; |
| 270 int sample_rate = kDefaultSampleRate; | 305 int sample_rate = kDefaultSampleRate; |
| 271 int buffer_size = kMinimumOutputBufferSize; | 306 int buffer_size = kMinimumOutputBufferSize; |
| 272 int bits_per_sample = 16; | 307 int bits_per_sample = 16; |
| 273 if (input_params.IsValid()) { | 308 if (input_params.IsValid()) { |
| 274 sample_rate = input_params.sample_rate(); | 309 sample_rate = input_params.sample_rate(); |
| 275 bits_per_sample = input_params.bits_per_sample(); | 310 bits_per_sample = input_params.bits_per_sample(); |
| 276 channel_layout = input_params.channel_layout(); | 311 channel_layout = input_params.channel_layout(); |
| 277 buffer_size = | 312 buffer_size = |
| 278 std::min(kMaximumOutputBufferSize, | 313 std::min(kMaximumOutputBufferSize, |
| 279 std::max(buffer_size, input_params.frames_per_buffer())); | 314 std::max(buffer_size, input_params.frames_per_buffer())); |
| 280 } | 315 } |
| 281 | 316 |
| 282 int user_buffer_size = GetUserBufferSize(); | 317 int user_buffer_size = GetUserBufferSize(); |
| 283 if (user_buffer_size) | 318 if (user_buffer_size) |
| 284 buffer_size = user_buffer_size; | 319 buffer_size = user_buffer_size; |
| 285 | 320 |
| 286 return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, | 321 return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, |
| 287 sample_rate, bits_per_sample, buffer_size); | 322 sample_rate, bits_per_sample, buffer_size); |
| 288 } | 323 } |
| 289 | 324 |
| 290 AudioOutputStream* AudioManagerCras::MakeOutputStream( | 325 AudioOutputStream* AudioManagerCras::MakeOutputStream( |
| 291 const AudioParameters& params) { | 326 const AudioParameters& params, |
| 292 return new CrasUnifiedStream(params, this); | 327 const std::string& device_id) { |
| 328 return new CrasUnifiedStream(params, this, device_id); |
| 293 } | 329 } |
| 294 | 330 |
| 295 AudioInputStream* AudioManagerCras::MakeInputStream( | 331 AudioInputStream* AudioManagerCras::MakeInputStream( |
| 296 const AudioParameters& params, const std::string& device_id) { | 332 const AudioParameters& params, const std::string& device_id) { |
| 297 return new CrasInputStream(params, this, device_id); | 333 return new CrasInputStream(params, this, device_id); |
| 298 } | 334 } |
| 299 | 335 |
| 300 snd_pcm_format_t AudioManagerCras::BitsToFormat(int bits_per_sample) { | 336 snd_pcm_format_t AudioManagerCras::BitsToFormat(int bits_per_sample) { |
| 301 switch (bits_per_sample) { | 337 switch (bits_per_sample) { |
| 302 case 8: | 338 case 8: |
| 303 return SND_PCM_FORMAT_U8; | 339 return SND_PCM_FORMAT_U8; |
| 304 case 16: | 340 case 16: |
| 305 return SND_PCM_FORMAT_S16; | 341 return SND_PCM_FORMAT_S16; |
| 306 case 24: | 342 case 24: |
| 307 return SND_PCM_FORMAT_S24; | 343 return SND_PCM_FORMAT_S24; |
| 308 case 32: | 344 case 32: |
| 309 return SND_PCM_FORMAT_S32; | 345 return SND_PCM_FORMAT_S32; |
| 310 default: | 346 default: |
| 311 return SND_PCM_FORMAT_UNKNOWN; | 347 return SND_PCM_FORMAT_UNKNOWN; |
| 312 } | 348 } |
| 313 } | 349 } |
| 314 | 350 |
| 351 bool AudioManagerCras::IsDefault(const std::string& device_id, bool is_input) { |
| 352 AudioDeviceNames device_names; |
| 353 GetAudioDeviceNamesImpl(is_input, &device_names); |
| 354 DCHECK(!device_names.empty()); |
| 355 AudioDeviceName device_name = device_names.front(); |
| 356 return device_name.unique_id == device_id; |
| 357 } |
| 358 |
| 315 } // namespace media | 359 } // namespace media |
| OLD | NEW |