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 a mic positions string if the machine has a beamforming capable |
37 static const int kMaxOutputStreams = 50; | 49 // internal mic and otherwise an empty string. |
| 50 std::string MicPositions() { |
| 51 // Get the list of devices from CRAS. An internal mic with a non-empty |
| 52 // positions field indicates the machine has a beamforming capable mic array. |
| 53 chromeos::AudioDeviceList devices; |
| 54 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices); |
| 55 for (const auto& device : devices) { |
| 56 if (device.type == chromeos::AUDIO_TYPE_INTERNAL_MIC) { |
| 57 // There should be only one internal mic device. |
| 58 return device.mic_positions; |
| 59 } |
| 60 } |
| 61 return ""; |
| 62 } |
38 | 63 |
39 // Default sample rate for input and output streams. | 64 // Returns the name and ID of the virtual beamforming device. |
40 static const int kDefaultSampleRate = 48000; | 65 AudioDeviceName BeamformingDeviceName() { |
| 66 static const char kBeamformingDeviceNameSuffix[] = " with beamforming"; |
| 67 static const char kBeamformingDeviceIdSuffix[] = "-beamforming"; |
41 | 68 |
42 // Define bounds for the output buffer size. | 69 return AudioDeviceName(std::string(AudioManagerBase::kDefaultDeviceName) + |
43 static const int kMinimumOutputBufferSize = 512; | 70 kBeamformingDeviceNameSuffix, |
44 static const int kMaximumOutputBufferSize = 8192; | 71 std::string(AudioManagerBase::kDefaultDeviceId) + |
| 72 kBeamformingDeviceIdSuffix); |
| 73 } |
45 | 74 |
46 // Default input buffer size. | 75 } // namespace |
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 beamforming_device_name_(BeamformingDeviceName()) { |
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 mic_positions_ = ParsePointsFromString(MicPositions()); |
| 109 // At least two mic positions indicates we have a beamforming capable mic |
| 110 // array. Add the virtual beamforming device to the list. When this device is |
| 111 // queried through GetInputStreamParameters, provide the cached mic positions. |
| 112 if (mic_positions_.size() > 1) |
| 113 device_names->push_back(beamforming_device_name_); |
| 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; |
97 | 133 |
| 134 // Return the cached mic positions in the case of the beamforming device. |
| 135 const std::vector<Point>& mic_positions = |
| 136 device_id == beamforming_device_name_.unique_id ? mic_positions_ |
| 137 : std::vector<Point>(); |
| 138 |
98 // TODO(hshi): Fine-tune audio parameters based on |device_id|. The optimal | 139 // TODO(hshi): Fine-tune audio parameters based on |device_id|. The optimal |
99 // parameters for the loopback stream may differ from the default. | 140 // parameters for the loopback stream may differ from the default. |
100 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, | 141 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
101 CHANNEL_LAYOUT_STEREO, kDefaultSampleRate, 16, | 142 CHANNEL_LAYOUT_STEREO, kDefaultSampleRate, 16, |
102 buffer_size); | 143 buffer_size); |
103 params.set_effects(effects); | 144 params.set_effects(effects); |
| 145 params.set_mic_positions(mic_positions); |
104 return params; | 146 return params; |
105 } | 147 } |
106 | 148 |
107 void AudioManagerCras::SetHasKeyboardMic() { | 149 void AudioManagerCras::SetHasKeyboardMic() { |
108 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 150 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
109 has_keyboard_mic_ = true; | 151 has_keyboard_mic_ = true; |
110 } | 152 } |
111 | 153 |
112 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( | 154 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream( |
113 const AudioParameters& params) { | 155 const AudioParameters& params) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 case 24: | 223 case 24: |
182 return SND_PCM_FORMAT_S24; | 224 return SND_PCM_FORMAT_S24; |
183 case 32: | 225 case 32: |
184 return SND_PCM_FORMAT_S32; | 226 return SND_PCM_FORMAT_S32; |
185 default: | 227 default: |
186 return SND_PCM_FORMAT_UNKNOWN; | 228 return SND_PCM_FORMAT_UNKNOWN; |
187 } | 229 } |
188 } | 230 } |
189 | 231 |
190 } // namespace media | 232 } // namespace media |
OLD | NEW |