1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/audio/linux/audio_manager_linux.h"
#include "base/command_line.h"
#include "base/environment.h"
#include "base/logging.h"
#include "base/nix/xdg_util.h"
#include "base/process_util.h"
#include "base/stl_util.h"
#include "media/audio/audio_output_dispatcher.h"
#include "media/audio/fake_audio_input_stream.h"
#include "media/audio/fake_audio_output_stream.h"
#include "media/audio/linux/alsa_input.h"
#include "media/audio/linux/alsa_output.h"
#include "media/audio/linux/alsa_wrapper.h"
#if defined(USE_PULSEAUDIO)
#include "media/audio/linux/pulse_output.h"
#endif
#include "media/base/limits.h"
#include "media/base/media_switches.h"
// Maximum number of output streams that can be open simultaneously.
static const size_t kMaxOutputStreams = 50;
static const int kMaxInputChannels = 2;
// Since "default", "pulse" and "dmix" devices are virtual devices mapped to
// real devices, we remove them from the list to avoiding duplicate counting.
// In addition, note that we support no more than 2 channels for recording,
// hence surround devices are not stored in the list.
static const char* kInvalidAudioInputDevices[] = {
"default",
"null",
"pulse",
"dmix",
"surround",
};
static bool IsValidAudioInputDevice(const char* device_name) {
if (!device_name)
return false;
for (size_t i = 0; i < arraysize(kInvalidAudioInputDevices); ++i) {
if (strcmp(kInvalidAudioInputDevices[i], device_name) == 0)
return false;
}
return true;
}
// Implementation of AudioManager.
bool AudioManagerLinux::HasAudioOutputDevices() {
// TODO(ajwong): Make this actually query audio devices.
return true;
}
bool AudioManagerLinux::HasAudioInputDevices() {
if (!initialized()) {
return false;
}
// Constants specified by the ALSA API for device hints.
static const int kGetAllDevices = -1;
static const char kPcmInterfaceName[] = "pcm";
bool has_device = false;
void** hints = NULL;
// Use the same approach to find the devices as in
// AlsaPcmOutputStream::FindDeviceForChannels
// Get Alsa device hints.
int error = wrapper_->DeviceNameHint(kGetAllDevices,
kPcmInterfaceName,
&hints);
if (error == 0) {
has_device = HasAnyValidAudioInputDevice(hints);
} else {
LOG(ERROR) << "Unable to get device hints: " << wrapper_->StrError(error);
}
// Destroy the hint now that we're done with it.
wrapper_->DeviceNameFreeHint(hints);
return has_device;
}
AudioOutputStream* AudioManagerLinux::MakeAudioOutputStream(
const AudioParameters& params) {
// Early return for testing hook. Do this before checking for
// |initialized_|.
if (params.format == AudioParameters::AUDIO_MOCK) {
return FakeAudioOutputStream::MakeFakeStream(params);
}
if (!initialized()) {
return NULL;
}
// Don't allow opening more than |kMaxOutputStreams| streams.
if (active_streams_.size() >= kMaxOutputStreams) {
return NULL;
}
AudioOutputStream* stream;
#if defined(USE_PULSEAUDIO)
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUsePulseAudio)) {
stream = new PulseAudioOutputStream(params, this, GetMessageLoop());
} else {
#endif
std::string device_name = AlsaPcmOutputStream::kAutoSelectDevice;
if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kAlsaOutputDevice)) {
device_name = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kAlsaOutputDevice);
}
stream = new AlsaPcmOutputStream(device_name, params, wrapper_.get(), this,
GetMessageLoop());
#if defined(USE_PULSEAUDIO)
}
#endif
active_streams_.insert(stream);
return stream;
}
AudioInputStream* AudioManagerLinux::MakeAudioInputStream(
const AudioParameters& params) {
if (!params.IsValid() || params.channels > kMaxInputChannels)
return NULL;
if (params.format == AudioParameters::AUDIO_MOCK) {
return FakeAudioInputStream::MakeFakeStream(params);
} else if (params.format != AudioParameters::AUDIO_PCM_LINEAR) {
return NULL;
}
if (!initialized())
return NULL;
std::string device_name = AlsaPcmOutputStream::kAutoSelectDevice;
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAlsaInputDevice)) {
device_name = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kAlsaInputDevice);
}
AlsaPcmInputStream* stream = new AlsaPcmInputStream(
device_name, params, wrapper_.get());
return stream;
}
AudioManagerLinux::AudioManagerLinux() {
}
AudioManagerLinux::~AudioManagerLinux() {
// Make sure we stop the thread first. If we allow the default destructor to
// destroy the members, we may destroy audio streams before stopping the
// thread, resulting an unexpected behavior.
// This way we make sure activities of the audio streams are all stopped
// before we destroy them.
audio_thread_.Stop();
// Free output dispatchers, closing all remaining open streams.
output_dispatchers_.clear();
// Delete all the streams. Have to do it manually, we don't have ScopedSet<>,
// and we are not using ScopedVector<> because search there is slow.
STLDeleteElements(&active_streams_);
}
void AudioManagerLinux::Init() {
AudioManagerBase::Init();
wrapper_.reset(new AlsaWrapper());
}
void AudioManagerLinux::MuteAll() {
NOTIMPLEMENTED();
}
void AudioManagerLinux::UnMuteAll() {
NOTIMPLEMENTED();
}
void AudioManagerLinux::ReleaseOutputStream(AudioOutputStream* stream) {
if (stream) {
active_streams_.erase(stream);
delete stream;
}
}
bool AudioManagerLinux::CanShowAudioInputSettings() {
scoped_ptr<base::Environment> env(base::Environment::Create());
base::nix::DesktopEnvironment desktop = base::nix::GetDesktopEnvironment(
env.get());
return (desktop == base::nix::DESKTOP_ENVIRONMENT_GNOME ||
desktop == base::nix::DESKTOP_ENVIRONMENT_KDE3 ||
desktop == base::nix::DESKTOP_ENVIRONMENT_KDE4);
}
void AudioManagerLinux::ShowAudioInputSettings() {
scoped_ptr<base::Environment> env(base::Environment::Create());
base::nix::DesktopEnvironment desktop = base::nix::GetDesktopEnvironment(
env.get());
std::string command((desktop == base::nix::DESKTOP_ENVIRONMENT_GNOME) ?
"gnome-volume-control" : "kmix");
base::LaunchProcess(CommandLine(FilePath(command)), base::LaunchOptions(),
NULL);
}
void AudioManagerLinux::GetAudioInputDeviceNames(
media::AudioDeviceNames* device_names) {
// TODO(xians): query a full list of valid devices.
if (HasAudioInputDevices()) {
// Add the default device to the list.
// We use index 0 to make up the unique_id to identify the
// default devices.
media::AudioDeviceName name;
name.device_name = AudioManagerBase::kDefaultDeviceName;
name.unique_id = "0";
device_names->push_back(name);
}
}
bool AudioManagerLinux::HasAnyValidAudioInputDevice(void** hints) {
static const char kIoHintName[] = "IOID";
static const char kNameHintName[] = "NAME";
static const char kOutputDevice[] = "Output";
for (void** hint_iter = hints; *hint_iter != NULL; hint_iter++) {
// Only examine devices that are input capable. Valid values are
// "Input", "Output", and NULL which means both input and output.
scoped_ptr_malloc<char> io(wrapper_->DeviceNameGetHint(*hint_iter,
kIoHintName));
if (io != NULL && strcmp(kOutputDevice, io.get()) == 0)
continue;
scoped_ptr_malloc<char> hint_device_name(
wrapper_->DeviceNameGetHint(*hint_iter, kNameHintName));
if (IsValidAudioInputDevice(hint_device_name.get()))
return true;
}
return false;
}
// static
AudioManager* AudioManager::CreateAudioManager() {
return new AudioManagerLinux();
}
|