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 "base/command_line.h"
8 #include "base/environment.h"
9 #include "base/logging.h"
10 #include "base/nix/xdg_util.h"
11 #include "base/process_util.h"
12 #include "base/stl_util.h"
13 #include "media/audio/audio_util.h"
14 #include "media/audio/cras/cras_input.h"
15 #include "media/audio/cras/cras_unified.h"
16 #include "media/base/channel_layout.h"
20 // Maximum number of output streams that can be open simultaneously.
21 static const int kMaxOutputStreams
= 50;
23 // Default sample rate for input and output streams.
24 static const int kDefaultSampleRate
= 48000;
26 bool AudioManagerCras::HasAudioOutputDevices() {
30 bool AudioManagerCras::HasAudioInputDevices() {
34 AudioManagerCras::AudioManagerCras() {
35 SetMaxOutputStreamsAllowed(kMaxOutputStreams
);
38 AudioManagerCras::~AudioManagerCras() {
42 void AudioManagerCras::ShowAudioInputSettings() {
46 void AudioManagerCras::GetAudioInputDeviceNames(
47 media::AudioDeviceNames
* device_names
) {
48 DCHECK(device_names
->empty());
49 GetCrasAudioInputDevices(device_names
);
53 AudioParameters
AudioManagerCras::GetInputStreamParameters(
54 const std::string
& device_id
) {
55 static const int kDefaultInputBufferSize
= 1024;
57 return AudioParameters(
58 AudioParameters::AUDIO_PCM_LOW_LATENCY
, CHANNEL_LAYOUT_STEREO
,
59 kDefaultSampleRate
, 16, kDefaultInputBufferSize
);
62 void AudioManagerCras::GetCrasAudioInputDevices(
63 media::AudioDeviceNames
* device_names
) {
64 // Cras will route audio from a proper physical device automatically.
65 device_names
->push_back(
66 AudioDeviceName(AudioManagerBase::kDefaultDeviceName
,
67 AudioManagerBase::kDefaultDeviceId
));
70 AudioOutputStream
* AudioManagerCras::MakeLinearOutputStream(
71 const AudioParameters
& params
) {
72 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR
, params
.format());
73 return MakeOutputStream(params
);
76 AudioOutputStream
* AudioManagerCras::MakeLowLatencyOutputStream(
77 const AudioParameters
& params
) {
78 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY
, params
.format());
79 return MakeOutputStream(params
);
82 AudioInputStream
* AudioManagerCras::MakeLinearInputStream(
83 const AudioParameters
& params
, const std::string
& device_id
) {
84 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR
, params
.format());
85 return MakeInputStream(params
, device_id
);
88 AudioInputStream
* AudioManagerCras::MakeLowLatencyInputStream(
89 const AudioParameters
& params
, const std::string
& device_id
) {
90 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY
, params
.format());
91 return MakeInputStream(params
, device_id
);
94 AudioParameters
AudioManagerCras::GetPreferredOutputStreamParameters(
95 const AudioParameters
& input_params
) {
96 static const int kDefaultOutputBufferSize
= 512;
98 ChannelLayout channel_layout
= CHANNEL_LAYOUT_STEREO
;
99 int sample_rate
= kDefaultSampleRate
;
100 int buffer_size
= kDefaultOutputBufferSize
;
101 int bits_per_sample
= 16;
102 int input_channels
= 0;
103 if (input_params
.IsValid()) {
104 sample_rate
= input_params
.sample_rate();
105 bits_per_sample
= input_params
.bits_per_sample();
106 channel_layout
= input_params
.channel_layout();
107 input_channels
= input_params
.input_channels();
108 buffer_size
= input_params
.frames_per_buffer();
111 int user_buffer_size
= GetUserBufferSize();
112 if (user_buffer_size
)
113 buffer_size
= user_buffer_size
;
115 return AudioParameters(
116 AudioParameters::AUDIO_PCM_LOW_LATENCY
, channel_layout
, input_channels
,
117 sample_rate
, bits_per_sample
, buffer_size
);
120 AudioOutputStream
* AudioManagerCras::MakeOutputStream(
121 const AudioParameters
& params
) {
122 return new CrasUnifiedStream(params
, this);
125 AudioInputStream
* AudioManagerCras::MakeInputStream(
126 const AudioParameters
& params
, const std::string
& device_id
) {
127 return new CrasInputStream(params
, this);