Revert "Omit calls to set composing region when pasting image."
[chromium-blink-merge.git] / media / audio / cras / audio_manager_cras.cc
blob2b47de40cc9f7d04c9525000bdc3ba2a32efa2c7
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
3 // found in the LICENSE file.
5 #include "media/audio/cras/audio_manager_cras.h"
7 #include <algorithm>
9 #include "base/command_line.h"
10 #include "base/environment.h"
11 #include "base/logging.h"
12 #include "base/nix/xdg_util.h"
13 #include "base/stl_util.h"
14 #include "chromeos/audio/audio_device.h"
15 #include "chromeos/audio/cras_audio_handler.h"
16 #include "media/audio/cras/cras_input.h"
17 #include "media/audio/cras/cras_unified.h"
18 #include "media/base/channel_layout.h"
20 // cras_util.h headers pull in min/max macros...
21 // TODO(dgreid): Fix headers such that these aren't imported.
22 #undef min
23 #undef max
25 namespace media {
26 namespace {
28 // Maximum number of output streams that can be open simultaneously.
29 const int kMaxOutputStreams = 50;
31 // Default sample rate for input and output streams.
32 const int kDefaultSampleRate = 48000;
34 // Define bounds for the output buffer size.
35 const int kMinimumOutputBufferSize = 512;
36 const int kMaximumOutputBufferSize = 8192;
38 // Default input buffer size.
39 const int kDefaultInputBufferSize = 1024;
41 void AddDefaultDevice(AudioDeviceNames* device_names) {
42 // Cras will route audio from a proper physical device automatically.
43 device_names->push_back(
44 AudioDeviceName(AudioManagerBase::kDefaultDeviceName,
45 AudioManagerBase::kDefaultDeviceId));
48 // Returns the AudioDeviceName of the virtual device with beamforming on.
49 AudioDeviceName BeamformingOnDeviceName() {
50 // TODO(ajm): Replace these strings with properly localized ones.
51 // (crbug.com/497001)
52 static const char kBeamformingOnNameSuffix[] = " (pick up just one person)";
53 static const char kBeamformingOnIdSuffix[] = "-beamforming";
55 return AudioDeviceName(
56 std::string(AudioManagerBase::kDefaultDeviceName) +
57 kBeamformingOnNameSuffix,
58 std::string(AudioManagerBase::kDefaultDeviceId) + kBeamformingOnIdSuffix);
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);
69 // Returns a mic positions string if the machine has a beamforming capable
70 // internal mic and otherwise an empty string.
71 std::string MicPositions() {
72 // 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.
74 chromeos::AudioDeviceList devices;
75 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices);
76 for (const auto& device : devices) {
77 if (device.type == chromeos::AUDIO_TYPE_INTERNAL_MIC) {
78 // There should be only one internal mic device.
79 return device.mic_positions;
82 return "";
85 } // namespace
87 bool AudioManagerCras::HasAudioOutputDevices() {
88 return true;
91 bool AudioManagerCras::HasAudioInputDevices() {
92 chromeos::AudioDeviceList devices;
93 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices);
94 for (size_t i = 0; i < devices.size(); ++i) {
95 if (devices[i].is_input && devices[i].is_for_simple_usage())
96 return true;
98 return false;
101 AudioManagerCras::AudioManagerCras(AudioLogFactory* audio_log_factory)
102 : AudioManagerBase(audio_log_factory),
103 has_keyboard_mic_(false),
104 beamforming_on_device_name_(BeamformingOnDeviceName()),
105 beamforming_off_device_name_(BeamformingOffDeviceName()) {
106 SetMaxOutputStreamsAllowed(kMaxOutputStreams);
109 AudioManagerCras::~AudioManagerCras() {
110 Shutdown();
113 void AudioManagerCras::ShowAudioInputSettings() {
114 NOTIMPLEMENTED();
117 void AudioManagerCras::GetAudioInputDeviceNames(
118 AudioDeviceNames* device_names) {
119 DCHECK(device_names->empty());
121 mic_positions_ = ParsePointsFromString(MicPositions());
122 // 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
124 // queried through GetInputStreamParameters, provide the cached mic positions.
125 if (mic_positions_.size() > 1) {
126 device_names->push_back(beamforming_on_device_name_);
127 device_names->push_back(beamforming_off_device_name_);
128 } else {
129 AddDefaultDevice(device_names);
133 void AudioManagerCras::GetAudioOutputDeviceNames(
134 AudioDeviceNames* device_names) {
135 DCHECK(device_names->empty());
137 AddDefaultDevice(device_names);
140 AudioParameters AudioManagerCras::GetInputStreamParameters(
141 const std::string& device_id) {
142 DCHECK(GetTaskRunner()->BelongsToCurrentThread());
144 int user_buffer_size = GetUserBufferSize();
145 int buffer_size = user_buffer_size ?
146 user_buffer_size : kDefaultInputBufferSize;
148 // TODO(hshi): Fine-tune audio parameters based on |device_id|. The optimal
149 // parameters for the loopback stream may differ from the default.
150 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
151 CHANNEL_LAYOUT_STEREO, kDefaultSampleRate, 16,
152 buffer_size);
153 if (has_keyboard_mic_)
154 params.set_effects(AudioParameters::KEYBOARD_MIC);
155 if (device_id == beamforming_on_device_name_.unique_id)
156 params.set_mic_positions(mic_positions_);
157 return params;
160 void AudioManagerCras::SetHasKeyboardMic() {
161 DCHECK(GetTaskRunner()->BelongsToCurrentThread());
162 has_keyboard_mic_ = true;
165 AudioOutputStream* AudioManagerCras::MakeLinearOutputStream(
166 const AudioParameters& params) {
167 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
168 return MakeOutputStream(params);
171 AudioOutputStream* AudioManagerCras::MakeLowLatencyOutputStream(
172 const AudioParameters& params,
173 const std::string& device_id) {
174 DLOG_IF(ERROR, !device_id.empty()) << "Not implemented!";
175 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
176 // TODO(dgreid): Open the correct input device for unified IO.
177 return MakeOutputStream(params);
180 AudioInputStream* AudioManagerCras::MakeLinearInputStream(
181 const AudioParameters& params, const std::string& device_id) {
182 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
183 return MakeInputStream(params, device_id);
186 AudioInputStream* AudioManagerCras::MakeLowLatencyInputStream(
187 const AudioParameters& params, const std::string& device_id) {
188 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
189 return MakeInputStream(params, device_id);
192 AudioParameters AudioManagerCras::GetPreferredOutputStreamParameters(
193 const std::string& output_device_id,
194 const AudioParameters& input_params) {
195 // TODO(tommi): Support |output_device_id|.
196 DLOG_IF(ERROR, !output_device_id.empty()) << "Not implemented!";
197 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
198 int sample_rate = kDefaultSampleRate;
199 int buffer_size = kMinimumOutputBufferSize;
200 int bits_per_sample = 16;
201 if (input_params.IsValid()) {
202 sample_rate = input_params.sample_rate();
203 bits_per_sample = input_params.bits_per_sample();
204 channel_layout = input_params.channel_layout();
205 buffer_size =
206 std::min(kMaximumOutputBufferSize,
207 std::max(buffer_size, input_params.frames_per_buffer()));
210 int user_buffer_size = GetUserBufferSize();
211 if (user_buffer_size)
212 buffer_size = user_buffer_size;
214 return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout,
215 sample_rate, bits_per_sample, buffer_size);
218 AudioOutputStream* AudioManagerCras::MakeOutputStream(
219 const AudioParameters& params) {
220 return new CrasUnifiedStream(params, this);
223 AudioInputStream* AudioManagerCras::MakeInputStream(
224 const AudioParameters& params, const std::string& device_id) {
225 return new CrasInputStream(params, this, device_id);
228 snd_pcm_format_t AudioManagerCras::BitsToFormat(int bits_per_sample) {
229 switch (bits_per_sample) {
230 case 8:
231 return SND_PCM_FORMAT_U8;
232 case 16:
233 return SND_PCM_FORMAT_S16;
234 case 24:
235 return SND_PCM_FORMAT_S24;
236 case 32:
237 return SND_PCM_FORMAT_S32;
238 default:
239 return SND_PCM_FORMAT_UNKNOWN;
243 } // namespace media