| 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 const char kBeamformingDeviceNameSuffix[] = " with beamforming"; |
| 42 const char kBeamformingDeviceIdSuffix[] = "-beamforming"; |
| 43 |
| 44 void AddDefaultDevice(AudioDeviceNames* device_names) { |
| 30 // Cras will route audio from a proper physical device automatically. | 45 // Cras will route audio from a proper physical device automatically. |
| 31 device_names->push_back( | 46 device_names->push_back( |
| 32 AudioDeviceName(AudioManagerBase::kDefaultDeviceName, | 47 AudioDeviceName(AudioManagerBase::kDefaultDeviceName, |
| 33 AudioManagerBase::kDefaultDeviceId)); | 48 AudioManagerBase::kDefaultDeviceId)); |
| 34 } | 49 } |
| 35 | 50 |
| 36 // Maximum number of output streams that can be open simultaneously. | 51 // Returns a mic positions string if the machine has a beamforming capable |
| 37 static const int kMaxOutputStreams = 50; | 52 // internal mic and otherwise an empty string. |
| 53 std::string MicPositions() { |
| 54 // Get the list of devices from CRAS. An internal mic with a non-empty |
| 55 // positions field indicates the machine has a beamforming capable mic array. |
| 56 chromeos::AudioDeviceList devices; |
| 57 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); |
| 58 for (const auto& device : devices) { |
| 59 if (device.type == chromeos::AUDIO_TYPE_INTERNAL_MIC) { |
| 60 // There should be only one internal mic device. |
| 61 return device.mic_positions; |
| 62 } |
| 63 } |
| 64 return ""; |
| 65 } |
| 38 | 66 |
| 39 // Default sample rate for input and output streams. | 67 // Returns the name of the virtual beamforming device. |
| 40 static const int kDefaultSampleRate = 48000; | 68 AudioDeviceName BeamformingDeviceName() { |
| 69 return AudioDeviceName(std::string(AudioManagerBase::kDefaultDeviceName) + |
| 70 kBeamformingDeviceNameSuffix, |
| 71 std::string(AudioManagerBase::kDefaultDeviceId) + |
| 72 kBeamformingDeviceIdSuffix); |
| 73 } |
| 41 | 74 |
| 42 // Define bounds for the output buffer size. | 75 } // namespace |
| 43 static const int kMinimumOutputBufferSize = 512; | |
| 44 static const int kMaximumOutputBufferSize = 8192; | |
| 45 | |
| 46 // Default input buffer size. | |
| 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 mic_positions_(ParsePointsFromString(MicPositions())) { |
| 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 // A non-empty mic_positions indicates we have a beamforming capable mic |
| 109 // array. Add the virtual beamforming device to the list. When this device is |
| 110 // queried through GetInputStreamParameters, provide the cached mic positions. |
| 111 if (!mic_positions_.empty()) { |
| 112 device_names->push_back(BeamformingDeviceName()); |
| 113 } |
| 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; |
| 133 // Return the cached mic positions in the case of the beamforming device. |
| 134 const std::vector<Point>& mic_positions = |
| 135 device_id == beamforming_device_name_.unique_id ? mic_positions_ |
| 136 : std::vector<Point>(); |
| 97 | 137 |
| 98 // TODO(hshi): Fine-tune audio parameters based on |device_id|. The optimal | 138 // TODO(hshi): Fine-tune audio parameters based on |device_id|. The optimal |
| 99 // parameters for the loopback stream may differ from the default. | 139 // parameters for the loopback stream may differ from the default. |
| 100 return AudioParameters( | 140 return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 101 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, | 141 CHANNEL_LAYOUT_STEREO, kDefaultSampleRate, 16, |
| 102 kDefaultSampleRate, 16, buffer_size, effects); | 142 buffer_size, mic_positions, effects); |
| 103 } | 143 } |
| 104 | 144 |
| 105 void AudioManagerCras::SetHasKeyboardMic() { | 145 void AudioManagerCras::SetHasKeyboardMic() { |
| 106 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 146 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 107 has_keyboard_mic_ = true; | 147 has_keyboard_mic_ = true; |
| 108 } | 148 } |
| 109 | 149 |
| 110 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( | 150 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( |
| 111 const AudioParameters& params) { | 151 const AudioParameters& params) { |
| 112 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | 152 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(); | 189 channel_layout = input_params.channel_layout(); |
| 150 buffer_size = | 190 buffer_size = |
| 151 std::min(kMaximumOutputBufferSize, | 191 std::min(kMaximumOutputBufferSize, |
| 152 std::max(buffer_size, input_params.frames_per_buffer())); | 192 std::max(buffer_size, input_params.frames_per_buffer())); |
| 153 } | 193 } |
| 154 | 194 |
| 155 int user_buffer_size = GetUserBufferSize(); | 195 int user_buffer_size = GetUserBufferSize(); |
| 156 if (user_buffer_size) | 196 if (user_buffer_size) |
| 157 buffer_size = user_buffer_size; | 197 buffer_size = user_buffer_size; |
| 158 | 198 |
| 159 return AudioParameters( | 199 return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, |
| 160 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, | 200 sample_rate, bits_per_sample, buffer_size); |
| 161 sample_rate, bits_per_sample, buffer_size, AudioParameters::NO_EFFECTS); | |
| 162 } | 201 } |
| 163 | 202 |
| 164 AudioOutputStream* AudioManagerCras::MakeOutputStream( | 203 AudioOutputStream* AudioManagerCras::MakeOutputStream( |
| 165 const AudioParameters& params) { | 204 const AudioParameters& params) { |
| 166 return new CrasUnifiedStream(params, this); | 205 return new CrasUnifiedStream(params, this); |
| 167 } | 206 } |
| 168 | 207 |
| 169 AudioInputStream* AudioManagerCras::MakeInputStream( | 208 AudioInputStream* AudioManagerCras::MakeInputStream( |
| 170 const AudioParameters& params, const std::string& device_id) { | 209 const AudioParameters& params, const std::string& device_id) { |
| 171 return new CrasInputStream(params, this, device_id); | 210 return new CrasInputStream(params, this, device_id); |
| 172 } | 211 } |
| 173 | 212 |
| 174 snd_pcm_format_t AudioManagerCras::BitsToFormat(int bits_per_sample) { | 213 snd_pcm_format_t AudioManagerCras::BitsToFormat(int bits_per_sample) { |
| 175 switch (bits_per_sample) { | 214 switch (bits_per_sample) { |
| 176 case 8: | 215 case 8: |
| 177 return SND_PCM_FORMAT_U8; | 216 return SND_PCM_FORMAT_U8; |
| 178 case 16: | 217 case 16: |
| 179 return SND_PCM_FORMAT_S16; | 218 return SND_PCM_FORMAT_S16; |
| 180 case 24: | 219 case 24: |
| 181 return SND_PCM_FORMAT_S24; | 220 return SND_PCM_FORMAT_S24; |
| 182 case 32: | 221 case 32: |
| 183 return SND_PCM_FORMAT_S32; | 222 return SND_PCM_FORMAT_S32; |
| 184 default: | 223 default: |
| 185 return SND_PCM_FORMAT_UNKNOWN; | 224 return SND_PCM_FORMAT_UNKNOWN; |
| 186 } | 225 } |
| 187 } | 226 } |
| 188 | 227 |
| 189 } // namespace media | 228 } // namespace media |
| OLD | NEW |