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 #include "media/base/media_resources.h" |
19 | 20 |
20 // cras_util.h headers pull in min/max macros... | 21 // cras_util.h headers pull in min/max macros... |
21 // TODO(dgreid): Fix headers such that these aren't imported. | 22 // TODO(dgreid): Fix headers such that these aren't imported. |
22 #undef min | 23 #undef min |
23 #undef max | 24 #undef max |
24 | 25 |
25 namespace media { | 26 namespace media { |
26 namespace { | 27 namespace { |
27 | 28 |
28 // Maximum number of output streams that can be open simultaneously. | 29 // Maximum number of output streams that can be open simultaneously. |
29 const int kMaxOutputStreams = 50; | 30 const int kMaxOutputStreams = 50; |
30 | 31 |
31 // Default sample rate for input and output streams. | 32 // Default sample rate for input and output streams. |
32 const int kDefaultSampleRate = 48000; | 33 const int kDefaultSampleRate = 48000; |
33 | 34 |
34 // Define bounds for the output buffer size. | 35 // Define bounds for the output buffer size. |
35 const int kMinimumOutputBufferSize = 512; | 36 const int kMinimumOutputBufferSize = 512; |
36 const int kMaximumOutputBufferSize = 8192; | 37 const int kMaximumOutputBufferSize = 8192; |
37 | 38 |
38 // Default input buffer size. | 39 // Default input buffer size. |
39 const int kDefaultInputBufferSize = 1024; | 40 const int kDefaultInputBufferSize = 1024; |
40 | 41 |
| 42 const char kBeamformingOnUniqueId[] = "beamforming-on"; |
| 43 const char kBeamformingOffUniqueId[] = "beamforming-off"; |
| 44 |
41 void AddDefaultDevice(AudioDeviceNames* device_names) { | 45 void AddDefaultDevice(AudioDeviceNames* device_names) { |
| 46 DCHECK(device_names->empty()); |
| 47 |
42 // Cras will route audio from a proper physical device automatically. | 48 // Cras will route audio from a proper physical device automatically. |
43 device_names->push_back( | 49 device_names->push_back(AudioDeviceName(AudioManager::GetDefaultDeviceName(), |
44 AudioDeviceName(AudioManagerBase::kDefaultDeviceName, | 50 AudioManagerBase::kDefaultDeviceId)); |
45 AudioManagerBase::kDefaultDeviceId)); | |
46 } | 51 } |
47 | 52 |
48 // Returns the AudioDeviceName of the virtual device with beamforming on. | 53 // Adds the beamforming on and off devices to |device_names|. |
49 AudioDeviceName BeamformingOnDeviceName() { | 54 void AddBeamformingDevices(AudioDeviceNames* device_names) { |
50 // TODO(ajm): Replace these strings with properly localized ones. | 55 DCHECK(device_names->empty()); |
51 // (crbug.com/497001) | |
52 static const char kBeamformingOnNameSuffix[] = " (pick up just one person)"; | |
53 static const char kBeamformingOnIdSuffix[] = "-beamforming"; | |
54 | 56 |
55 return AudioDeviceName( | 57 device_names->push_back(AudioDeviceName( |
56 std::string(AudioManagerBase::kDefaultDeviceName) + | 58 GetLocalizedStringUTF8(BEAMFORMING_ON_DEFAULT_AUDIO_INPUT_DEVICE_NAME), |
57 kBeamformingOnNameSuffix, | 59 kBeamformingOnUniqueId)); |
58 std::string(AudioManagerBase::kDefaultDeviceId) + kBeamformingOnIdSuffix); | 60 device_names->push_back(AudioDeviceName( |
59 } | 61 GetLocalizedStringUTF8(BEAMFORMING_OFF_DEFAULT_AUDIO_INPUT_DEVICE_NAME), |
60 | 62 kBeamformingOffUniqueId)); |
61 // Returns the AudioDeviceName of the virtual device with beamforming off. | |
62 AudioDeviceName BeamformingOffDeviceName() { | |
63 static const char kBeamformingOffNameSuffix[] = " (pick up everything)"; | |
64 return AudioDeviceName(std::string(AudioManagerBase::kDefaultDeviceName) + | |
65 kBeamformingOffNameSuffix, | |
66 AudioManagerBase::kDefaultDeviceId); | |
67 } | 63 } |
68 | 64 |
69 // Returns a mic positions string if the machine has a beamforming capable | 65 // Returns a mic positions string if the machine has a beamforming capable |
70 // internal mic and otherwise an empty string. | 66 // internal mic and otherwise an empty string. |
71 std::string MicPositions() { | 67 std::string MicPositions() { |
72 // Get the list of devices from CRAS. An internal mic with a non-empty | 68 // Get the list of devices from CRAS. An internal mic with a non-empty |
73 // positions field indicates the machine has a beamforming capable mic array. | 69 // positions field indicates the machine has a beamforming capable mic array. |
74 chromeos::AudioDeviceList devices; | 70 chromeos::AudioDeviceList devices; |
75 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); | 71 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); |
76 for (const auto& device : devices) { | 72 for (const auto& device : devices) { |
(...skipping 15 matching lines...) Expand all Loading... |
92 chromeos::AudioDeviceList devices; | 88 chromeos::AudioDeviceList devices; |
93 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); | 89 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); |
94 for (size_t i = 0; i < devices.size(); ++i) { | 90 for (size_t i = 0; i < devices.size(); ++i) { |
95 if (devices[i].is_input && devices[i].is_for_simple_usage()) | 91 if (devices[i].is_input && devices[i].is_for_simple_usage()) |
96 return true; | 92 return true; |
97 } | 93 } |
98 return false; | 94 return false; |
99 } | 95 } |
100 | 96 |
101 AudioManagerCras::AudioManagerCras(AudioLogFactory* audio_log_factory) | 97 AudioManagerCras::AudioManagerCras(AudioLogFactory* audio_log_factory) |
102 : AudioManagerBase(audio_log_factory), | 98 : AudioManagerBase(audio_log_factory), has_keyboard_mic_(false) { |
103 has_keyboard_mic_(false), | |
104 beamforming_on_device_name_(BeamformingOnDeviceName()), | |
105 beamforming_off_device_name_(BeamformingOffDeviceName()) { | |
106 SetMaxOutputStreamsAllowed(kMaxOutputStreams); | 99 SetMaxOutputStreamsAllowed(kMaxOutputStreams); |
107 } | 100 } |
108 | 101 |
109 AudioManagerCras::~AudioManagerCras() { | 102 AudioManagerCras::~AudioManagerCras() { |
110 Shutdown(); | 103 Shutdown(); |
111 } | 104 } |
112 | 105 |
113 void AudioManagerCras::ShowAudioInputSettings() { | 106 void AudioManagerCras::ShowAudioInputSettings() { |
114 NOTIMPLEMENTED(); | 107 NOTIMPLEMENTED(); |
115 } | 108 } |
116 | 109 |
117 void AudioManagerCras::GetAudioInputDeviceNames( | 110 void AudioManagerCras::GetAudioInputDeviceNames( |
118 AudioDeviceNames* device_names) { | 111 AudioDeviceNames* device_names) { |
119 DCHECK(device_names->empty()); | 112 DCHECK(device_names->empty()); |
120 | 113 |
121 mic_positions_ = ParsePointsFromString(MicPositions()); | 114 mic_positions_ = ParsePointsFromString(MicPositions()); |
122 // At least two mic positions indicates we have a beamforming capable mic | 115 // At least two mic positions indicates we have a beamforming capable mic |
123 // array. Add the virtual beamforming device to the list. When this device is | 116 // array. Add the virtual beamforming device to the list. When this device is |
124 // queried through GetInputStreamParameters, provide the cached mic positions. | 117 // queried through GetInputStreamParameters, provide the cached mic positions. |
125 if (mic_positions_.size() > 1) { | 118 if (mic_positions_.size() > 1) |
126 device_names->push_back(beamforming_on_device_name_); | 119 AddBeamformingDevices(device_names); |
127 device_names->push_back(beamforming_off_device_name_); | 120 else |
128 } else { | |
129 AddDefaultDevice(device_names); | 121 AddDefaultDevice(device_names); |
130 } | |
131 } | 122 } |
132 | 123 |
133 void AudioManagerCras::GetAudioOutputDeviceNames( | 124 void AudioManagerCras::GetAudioOutputDeviceNames( |
134 AudioDeviceNames* device_names) { | 125 AudioDeviceNames* device_names) { |
135 DCHECK(device_names->empty()); | |
136 | |
137 AddDefaultDevice(device_names); | 126 AddDefaultDevice(device_names); |
138 } | 127 } |
139 | 128 |
140 AudioParameters AudioManagerCras::GetInputStreamParameters( | 129 AudioParameters AudioManagerCras::GetInputStreamParameters( |
141 const std::string& device_id) { | 130 const std::string& device_id) { |
142 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 131 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
143 | 132 |
144 int user_buffer_size = GetUserBufferSize(); | 133 int user_buffer_size = GetUserBufferSize(); |
145 int buffer_size = user_buffer_size ? | 134 int buffer_size = user_buffer_size ? |
146 user_buffer_size : kDefaultInputBufferSize; | 135 user_buffer_size : kDefaultInputBufferSize; |
147 | 136 |
148 // TODO(hshi): Fine-tune audio parameters based on |device_id|. The optimal | 137 // TODO(hshi): Fine-tune audio parameters based on |device_id|. The optimal |
149 // parameters for the loopback stream may differ from the default. | 138 // parameters for the loopback stream may differ from the default. |
150 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, | 139 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
151 CHANNEL_LAYOUT_STEREO, kDefaultSampleRate, 16, | 140 CHANNEL_LAYOUT_STEREO, kDefaultSampleRate, 16, |
152 buffer_size); | 141 buffer_size); |
153 if (has_keyboard_mic_) | 142 if (has_keyboard_mic_) |
154 params.set_effects(AudioParameters::KEYBOARD_MIC); | 143 params.set_effects(AudioParameters::KEYBOARD_MIC); |
155 if (device_id == beamforming_on_device_name_.unique_id) | 144 if (device_id == kBeamformingOnUniqueId) |
156 params.set_mic_positions(mic_positions_); | 145 params.set_mic_positions(mic_positions_); |
157 return params; | 146 return params; |
158 } | 147 } |
159 | 148 |
160 void AudioManagerCras::SetHasKeyboardMic() { | 149 void AudioManagerCras::SetHasKeyboardMic() { |
161 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 150 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
162 has_keyboard_mic_ = true; | 151 has_keyboard_mic_ = true; |
163 } | 152 } |
164 | 153 |
165 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( | 154 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 case 24: | 223 case 24: |
235 return SND_PCM_FORMAT_S24; | 224 return SND_PCM_FORMAT_S24; |
236 case 32: | 225 case 32: |
237 return SND_PCM_FORMAT_S32; | 226 return SND_PCM_FORMAT_S32; |
238 default: | 227 default: |
239 return SND_PCM_FORMAT_UNKNOWN; | 228 return SND_PCM_FORMAT_UNKNOWN; |
240 } | 229 } |
241 } | 230 } |
242 | 231 |
243 } // namespace media | 232 } // namespace media |
OLD | NEW |