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 the AudioDeviceName of the virtual device with beamforming on. |
37 static const int kMaxOutputStreams = 50; | 49 AudioDeviceName BeamformingOnDeviceName() { |
50 static const char kBeamformingOnNameSuffix[] = " (pick up just one person)"; | |
aluebs-chromium
2015/09/10 02:41:40
Add a TODO to remove these hardcoded strings?
ajm
2015/09/10 21:56:57
Done.
| |
51 static const char kBeamformingOnIdSuffix[] = "-beamforming"; | |
38 | 52 |
39 // Default sample rate for input and output streams. | 53 return AudioDeviceName( |
40 static const int kDefaultSampleRate = 48000; | 54 std::string(AudioManagerBase::kDefaultDeviceName) + |
55 kBeamformingOnNameSuffix, | |
56 std::string(AudioManagerBase::kDefaultDeviceId) + kBeamformingOnIdSuffix); | |
57 } | |
41 | 58 |
42 // Define bounds for the output buffer size. | 59 // Returns the AudioDeviceName of the virtual device with beamforming off. |
43 static const int kMinimumOutputBufferSize = 512; | 60 AudioDeviceName BeamformingOffDeviceName() { |
44 static const int kMaximumOutputBufferSize = 8192; | 61 static const char kBeamformingOffNameSuffix[] = " (pick up everything)"; |
62 return AudioDeviceName(std::string(AudioManagerBase::kDefaultDeviceName) + | |
63 kBeamformingOffNameSuffix, | |
64 AudioManagerBase::kDefaultDeviceId); | |
65 } | |
45 | 66 |
46 // Default input buffer size. | 67 // Returns a mic positions string if the machine has a beamforming capable |
47 static const int kDefaultInputBufferSize = 1024; | 68 // internal mic and otherwise an empty string. |
69 std::string MicPositions() { | |
70 // Get the list of devices from CRAS. An internal mic with a non-empty | |
71 // positions field indicates the machine has a beamforming capable mic array. | |
72 chromeos::AudioDeviceList devices; | |
73 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); | |
74 for (const auto& device : devices) { | |
75 if (device.type == chromeos::AUDIO_TYPE_INTERNAL_MIC) { | |
76 // There should be only one internal mic device. | |
77 return device.mic_positions; | |
78 } | |
79 } | |
80 return ""; | |
81 } | |
82 | |
83 } // namespace | |
48 | 84 |
49 bool AudioManagerCras::HasAudioOutputDevices() { | 85 bool AudioManagerCras::HasAudioOutputDevices() { |
50 return true; | 86 return true; |
51 } | 87 } |
52 | 88 |
53 bool AudioManagerCras::HasAudioInputDevices() { | 89 bool AudioManagerCras::HasAudioInputDevices() { |
54 chromeos::AudioDeviceList devices; | 90 chromeos::AudioDeviceList devices; |
55 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); | 91 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); |
56 for (size_t i = 0; i < devices.size(); ++i) { | 92 for (size_t i = 0; i < devices.size(); ++i) { |
57 if (devices[i].is_input && devices[i].is_for_simple_usage()) | 93 if (devices[i].is_input && devices[i].is_for_simple_usage()) |
58 return true; | 94 return true; |
59 } | 95 } |
60 return false; | 96 return false; |
61 } | 97 } |
62 | 98 |
63 AudioManagerCras::AudioManagerCras(AudioLogFactory* audio_log_factory) | 99 AudioManagerCras::AudioManagerCras(AudioLogFactory* audio_log_factory) |
64 : AudioManagerBase(audio_log_factory), | 100 : AudioManagerBase(audio_log_factory), |
65 has_keyboard_mic_(false) { | 101 has_keyboard_mic_(false), |
102 beamforming_on_device_name_(BeamformingOnDeviceName()), | |
103 beamforming_off_device_name_(BeamformingOffDeviceName()) { | |
66 SetMaxOutputStreamsAllowed(kMaxOutputStreams); | 104 SetMaxOutputStreamsAllowed(kMaxOutputStreams); |
67 } | 105 } |
68 | 106 |
69 AudioManagerCras::~AudioManagerCras() { | 107 AudioManagerCras::~AudioManagerCras() { |
70 Shutdown(); | 108 Shutdown(); |
71 } | 109 } |
72 | 110 |
73 void AudioManagerCras::ShowAudioInputSettings() { | 111 void AudioManagerCras::ShowAudioInputSettings() { |
74 NOTIMPLEMENTED(); | 112 NOTIMPLEMENTED(); |
75 } | 113 } |
76 | 114 |
77 void AudioManagerCras::GetAudioInputDeviceNames( | 115 void AudioManagerCras::GetAudioInputDeviceNames( |
78 AudioDeviceNames* device_names) { | 116 AudioDeviceNames* device_names) { |
79 AddDefaultDevice(device_names); | 117 DCHECK(device_names->empty()); |
118 | |
119 mic_positions_ = ParsePointsFromString(MicPositions()); | |
120 // At least two mic positions indicates we have a beamforming capable mic | |
121 // array. Add the virtual beamforming device to the list. When this device is | |
122 // queried through GetInputStreamParameters, provide the cached mic positions. | |
123 if (mic_positions_.size() > 1) { | |
124 device_names->push_back(beamforming_on_device_name_); | |
125 device_names->push_back(beamforming_off_device_name_); | |
126 } else { | |
127 AddDefaultDevice(device_names); | |
128 } | |
80 } | 129 } |
81 | 130 |
82 void AudioManagerCras::GetAudioOutputDeviceNames( | 131 void AudioManagerCras::GetAudioOutputDeviceNames( |
83 AudioDeviceNames* device_names) { | 132 AudioDeviceNames* device_names) { |
133 DCHECK(device_names->empty()); | |
134 | |
84 AddDefaultDevice(device_names); | 135 AddDefaultDevice(device_names); |
85 } | 136 } |
86 | 137 |
87 AudioParameters AudioManagerCras::GetInputStreamParameters( | 138 AudioParameters AudioManagerCras::GetInputStreamParameters( |
88 const std::string& device_id) { | 139 const std::string& device_id) { |
89 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 140 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
90 | 141 |
91 int user_buffer_size = GetUserBufferSize(); | 142 int user_buffer_size = GetUserBufferSize(); |
92 int buffer_size = user_buffer_size ? | 143 int buffer_size = user_buffer_size ? |
93 user_buffer_size : kDefaultInputBufferSize; | 144 user_buffer_size : kDefaultInputBufferSize; |
94 AudioParameters::PlatformEffectsMask effects = | |
95 has_keyboard_mic_ ? AudioParameters::KEYBOARD_MIC | |
96 : AudioParameters::NO_EFFECTS; | |
97 | 145 |
98 // TODO(hshi): Fine-tune audio parameters based on |device_id|. The optimal | 146 // TODO(hshi): Fine-tune audio parameters based on |device_id|. The optimal |
99 // parameters for the loopback stream may differ from the default. | 147 // parameters for the loopback stream may differ from the default. |
100 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, | 148 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
101 CHANNEL_LAYOUT_STEREO, kDefaultSampleRate, 16, | 149 CHANNEL_LAYOUT_STEREO, kDefaultSampleRate, 16, |
102 buffer_size); | 150 buffer_size); |
103 params.set_effects(effects); | 151 if (has_keyboard_mic_) |
152 params.set_effects(AudioParameters::KEYBOARD_MIC); | |
153 if (device_id == beamforming_on_device_name_.unique_id) | |
154 params.set_mic_positions(mic_positions_); | |
104 return params; | 155 return params; |
105 } | 156 } |
106 | 157 |
107 void AudioManagerCras::SetHasKeyboardMic() { | 158 void AudioManagerCras::SetHasKeyboardMic() { |
108 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 159 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
109 has_keyboard_mic_ = true; | 160 has_keyboard_mic_ = true; |
110 } | 161 } |
111 | 162 |
112 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( | 163 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( |
113 const AudioParameters& params) { | 164 const AudioParameters& params) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 case 24: | 232 case 24: |
182 return SND_PCM_FORMAT_S24; | 233 return SND_PCM_FORMAT_S24; |
183 case 32: | 234 case 32: |
184 return SND_PCM_FORMAT_S32; | 235 return SND_PCM_FORMAT_S32; |
185 default: | 236 default: |
186 return SND_PCM_FORMAT_UNKNOWN; | 237 return SND_PCM_FORMAT_UNKNOWN; |
187 } | 238 } |
188 } | 239 } |
189 | 240 |
190 } // namespace media | 241 } // namespace media |
OLD | NEW |