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"
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.
27 static void AddDefaultDevice(AudioDeviceNames
* device_names
) {
28 DCHECK(device_names
->empty());
30 // Cras will route audio from a proper physical device automatically.
31 device_names
->push_back(
32 AudioDeviceName(AudioManagerBase::kDefaultDeviceName
,
33 AudioManagerBase::kDefaultDeviceId
));
36 // Maximum number of output streams that can be open simultaneously.
37 static const int kMaxOutputStreams
= 50;
39 // Default sample rate for input and output streams.
40 static const int kDefaultSampleRate
= 48000;
42 // Define bounds for the output buffer size.
43 static const int kMinimumOutputBufferSize
= 512;
44 static const int kMaximumOutputBufferSize
= 8192;
46 // Default input buffer size.
47 static const int kDefaultInputBufferSize
= 1024;
49 bool AudioManagerCras::HasAudioOutputDevices() {
53 bool AudioManagerCras::HasAudioInputDevices() {
54 chromeos::AudioDeviceList devices
;
55 chromeos::CrasAudioHandler::Get()->GetAudioDevices(&devices
);
56 for (size_t i
= 0; i
< devices
.size(); ++i
) {
57 if (devices
[i
].is_input
&& devices
[i
].is_for_simple_usage())
63 AudioManagerCras::AudioManagerCras(AudioLogFactory
* audio_log_factory
)
64 : AudioManagerBase(audio_log_factory
),
65 has_keyboard_mic_(false) {
66 SetMaxOutputStreamsAllowed(kMaxOutputStreams
);
69 AudioManagerCras::~AudioManagerCras() {
73 void AudioManagerCras::ShowAudioInputSettings() {
77 void AudioManagerCras::GetAudioInputDeviceNames(
78 AudioDeviceNames
* device_names
) {
79 AddDefaultDevice(device_names
);
82 void AudioManagerCras::GetAudioOutputDeviceNames(
83 AudioDeviceNames
* device_names
) {
84 AddDefaultDevice(device_names
);
87 AudioParameters
AudioManagerCras::GetInputStreamParameters(
88 const std::string
& device_id
) {
89 DCHECK(GetTaskRunner()->BelongsToCurrentThread());
91 int user_buffer_size
= GetUserBufferSize();
92 int buffer_size
= user_buffer_size
?
93 user_buffer_size
: kDefaultInputBufferSize
;
94 AudioParameters::PlatformEffectsMask effects
=
95 has_keyboard_mic_
? AudioParameters::KEYBOARD_MIC
96 : AudioParameters::NO_EFFECTS
;
98 // TODO(hshi): Fine-tune audio parameters based on |device_id|. The optimal
99 // parameters for the loopback stream may differ from the default.
100 return AudioParameters(
101 AudioParameters::AUDIO_PCM_LOW_LATENCY
, CHANNEL_LAYOUT_STEREO
,
102 kDefaultSampleRate
, 16, buffer_size
, effects
);
105 void AudioManagerCras::SetHasKeyboardMic() {
106 DCHECK(GetTaskRunner()->BelongsToCurrentThread());
107 has_keyboard_mic_
= true;
110 AudioOutputStream
* AudioManagerCras::MakeLinearOutputStream(
111 const AudioParameters
& params
) {
112 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR
, params
.format());
113 return MakeOutputStream(params
);
116 AudioOutputStream
* AudioManagerCras::MakeLowLatencyOutputStream(
117 const AudioParameters
& params
,
118 const std::string
& device_id
) {
119 DLOG_IF(ERROR
, !device_id
.empty()) << "Not implemented!";
120 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY
, params
.format());
121 // TODO(dgreid): Open the correct input device for unified IO.
122 return MakeOutputStream(params
);
125 AudioInputStream
* AudioManagerCras::MakeLinearInputStream(
126 const AudioParameters
& params
, const std::string
& device_id
) {
127 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR
, params
.format());
128 return MakeInputStream(params
, device_id
);
131 AudioInputStream
* AudioManagerCras::MakeLowLatencyInputStream(
132 const AudioParameters
& params
, const std::string
& device_id
) {
133 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY
, params
.format());
134 return MakeInputStream(params
, device_id
);
137 AudioParameters
AudioManagerCras::GetPreferredOutputStreamParameters(
138 const std::string
& output_device_id
,
139 const AudioParameters
& input_params
) {
140 // TODO(tommi): Support |output_device_id|.
141 DLOG_IF(ERROR
, !output_device_id
.empty()) << "Not implemented!";
142 ChannelLayout channel_layout
= CHANNEL_LAYOUT_STEREO
;
143 int sample_rate
= kDefaultSampleRate
;
144 int buffer_size
= kMinimumOutputBufferSize
;
145 int bits_per_sample
= 16;
146 if (input_params
.IsValid()) {
147 sample_rate
= input_params
.sample_rate();
148 bits_per_sample
= input_params
.bits_per_sample();
149 channel_layout
= input_params
.channel_layout();
151 std::min(kMaximumOutputBufferSize
,
152 std::max(buffer_size
, input_params
.frames_per_buffer()));
155 int user_buffer_size
= GetUserBufferSize();
156 if (user_buffer_size
)
157 buffer_size
= user_buffer_size
;
159 return AudioParameters(
160 AudioParameters::AUDIO_PCM_LOW_LATENCY
, channel_layout
,
161 sample_rate
, bits_per_sample
, buffer_size
, AudioParameters::NO_EFFECTS
);
164 AudioOutputStream
* AudioManagerCras::MakeOutputStream(
165 const AudioParameters
& params
) {
166 return new CrasUnifiedStream(params
, this);
169 AudioInputStream
* AudioManagerCras::MakeInputStream(
170 const AudioParameters
& params
, const std::string
& device_id
) {
171 return new CrasInputStream(params
, this, device_id
);
174 snd_pcm_format_t
AudioManagerCras::BitsToFormat(int bits_per_sample
) {
175 switch (bits_per_sample
) {
177 return SND_PCM_FORMAT_U8
;
179 return SND_PCM_FORMAT_S16
;
181 return SND_PCM_FORMAT_S24
;
183 return SND_PCM_FORMAT_S32
;
185 return SND_PCM_FORMAT_UNKNOWN
;