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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
// Copyright (c) 2010 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 "chrome/browser/chromeos/audio_mixer_pulse.h"
#include <pulse/pulseaudio.h>
#include "base/logging.h"
#include "base/task.h"
namespace chromeos {
// Using asynchronous versions of the threaded PulseAudio API, as well
// as a worker thread so gets, sets, and the init sequence do not block the
// calling thread. GetVolume() and IsMute() can still be called synchronously
// if needed, but take a bit longer (~2ms vs ~0.3ms).
//
// Set calls just return without waiting. If you must guarantee the value has
// been set before continuing, immediately call the blocking Get version to
// synchronously get the value back.
//
// TODO(davej): Serialize volume/mute to preserve settings when restarting?
namespace {
const int kInvalidDeviceId = -1;
const double kMinVolumeDb = -90.0;
// Choosing 6.0dB here instead of 0dB to give user chance to amplify audio some
// in case sounds or their setup is too quiet for them.
const double kMaxVolumeDb = 6.0;
// Used for passing custom data to the PulseAudio callbacks.
struct CallbackWrapper {
AudioMixerPulse* instance;
bool done;
void* userdata;
};
} // namespace
// AudioInfo contains all the values we care about when getting info for a
// Sink (output device) used by GetAudioInfo().
struct AudioMixerPulse::AudioInfo {
pa_cvolume cvolume;
bool muted;
};
AudioMixerPulse::AudioMixerPulse()
: device_id_(kInvalidDeviceId),
last_channels_(0),
mainloop_lock_count_(0),
mixer_state_(UNINITIALIZED),
pa_context_(NULL),
pa_mainloop_(NULL) {
}
AudioMixerPulse::~AudioMixerPulse() {
PulseAudioFree();
if (thread_ != NULL) {
thread_->Stop();
thread_.reset();
}
}
void AudioMixerPulse::Init(InitDoneCallback* callback) {
DCHECK(callback);
if (!InitThread()) {
callback->Run(false);
delete callback;
return;
}
// Post the task of starting up, which can block for 200-500ms,
// so best not to do it on the caller's thread.
thread_->message_loop()->PostTask(FROM_HERE,
NewRunnableMethod(this, &AudioMixerPulse::DoInit, callback));
}
bool AudioMixerPulse::InitSync() {
if (!InitThread())
return false;
return PulseAudioInit();
}
double AudioMixerPulse::GetVolumeDb() const {
if (!MainloopLockIfReady())
return AudioMixer::kSilenceDb;
AudioInfo data;
GetAudioInfo(&data);
MainloopUnlock();
return pa_sw_volume_to_dB(data.cvolume.values[0]);
}
bool AudioMixerPulse::GetVolumeLimits(double* vol_min, double* vol_max) {
if (vol_min)
*vol_min = kMinVolumeDb;
if (vol_max)
*vol_max = kMaxVolumeDb;
return true;
}
void AudioMixerPulse::SetVolumeDb(double vol_db) {
if (!MainloopLockIfReady())
return;
// last_channels_ determines the number of channels on the main output device,
// and is used later to set the volume on all channels at once.
if (!last_channels_) {
AudioInfo data;
GetAudioInfo(&data);
last_channels_ = data.cvolume.channels;
}
pa_operation* pa_op;
pa_cvolume cvolume;
pa_cvolume_set(&cvolume, last_channels_, pa_sw_volume_from_dB(vol_db));
pa_op = pa_context_set_sink_volume_by_index(pa_context_, device_id_,
&cvolume, NULL, NULL);
pa_operation_unref(pa_op);
MainloopUnlock();
VLOG(1) << "Set volume to " << vol_db << " dB";
}
bool AudioMixerPulse::IsMute() const {
if (!MainloopLockIfReady())
return false;
AudioInfo data;
GetAudioInfo(&data);
MainloopUnlock();
return data.muted;
}
void AudioMixerPulse::SetMute(bool mute) {
if (!MainloopLockIfReady())
return;
pa_operation* pa_op;
pa_op = pa_context_set_sink_mute_by_index(pa_context_, device_id_,
mute ? 1 : 0, NULL, NULL);
pa_operation_unref(pa_op);
MainloopUnlock();
VLOG(1) << "Set mute to " << mute;
}
AudioMixer::State AudioMixerPulse::GetState() const {
AutoLock lock(mixer_state_lock_);
// If we think it's ready, verify it is actually so.
if ((mixer_state_ == READY) &&
(pa_context_get_state(pa_context_) != PA_CONTEXT_READY))
mixer_state_ = IN_ERROR;
return mixer_state_;
}
////////////////////////////////////////////////////////////////////////////////
// Private functions follow
void AudioMixerPulse::DoInit(InitDoneCallback* callback) {
bool success = PulseAudioInit();
callback->Run(success);
delete callback;
}
bool AudioMixerPulse::InitThread() {
AutoLock lock(mixer_state_lock_);
if (mixer_state_ != UNINITIALIZED)
return false;
if (thread_ == NULL) {
thread_.reset(new base::Thread("AudioMixerPulse"));
if (!thread_->Start()) {
thread_.reset();
return false;
}
}
mixer_state_ = INITIALIZING;
return true;
}
// static
void AudioMixerPulse::ConnectToPulseCallbackThunk(
pa_context* context, void* userdata) {
CallbackWrapper* data =
static_cast<CallbackWrapper*>(userdata);
data->instance->OnConnectToPulseCallback(context, &data->done);
}
void AudioMixerPulse::OnConnectToPulseCallback(
pa_context* context, bool* connect_done) {
pa_context_state_t state = pa_context_get_state(context);
if (state == PA_CONTEXT_READY ||
state == PA_CONTEXT_FAILED ||
state == PA_CONTEXT_TERMINATED) {
// Connection process has reached a terminal state. Wake PulseAudioInit().
*connect_done = true;
MainloopSignal();
}
}
bool AudioMixerPulse::PulseAudioInit() {
pa_context_state_t state = PA_CONTEXT_FAILED;
{
AutoLock lock(mixer_state_lock_);
if (mixer_state_ != INITIALIZING)
return false;
pa_mainloop_ = pa_threaded_mainloop_new();
if (!pa_mainloop_) {
LOG(ERROR) << "Can't create PulseAudio mainloop";
mixer_state_ = UNINITIALIZED;
return false;
}
if (pa_threaded_mainloop_start(pa_mainloop_) != 0) {
LOG(ERROR) << "Can't start PulseAudio mainloop";
pa_threaded_mainloop_free(pa_mainloop_);
mixer_state_ = UNINITIALIZED;
return false;
}
}
while (true) {
// Create connection to default server.
if (!MainloopSafeLock())
return false;
while (true) {
pa_mainloop_api* pa_mlapi = pa_threaded_mainloop_get_api(pa_mainloop_);
if (!pa_mlapi) {
LOG(ERROR) << "Can't get PulseAudio mainloop api";
break;
}
// This one takes the most time if run at app startup.
pa_context_ = pa_context_new(pa_mlapi, "ChromeAudio");
if (!pa_context_) {
LOG(ERROR) << "Can't create new PulseAudio context";
break;
}
MainloopUnlock();
if (!MainloopSafeLock())
return false;
CallbackWrapper data = {this, false, NULL};
pa_context_set_state_callback(pa_context_,
&ConnectToPulseCallbackThunk,
&data);
if (pa_context_connect(pa_context_, NULL,
PA_CONTEXT_NOAUTOSPAWN, NULL) != 0) {
LOG(ERROR) << "Can't start connection to PulseAudio sound server";
} else {
// Wait until we have a completed connection or fail.
do {
MainloopWait();
} while (!data.done);
state = pa_context_get_state(pa_context_);
if (state == PA_CONTEXT_FAILED) {
LOG(ERROR) << "PulseAudio connection failed (daemon not running?)";
} else if (state == PA_CONTEXT_TERMINATED) {
LOG(ERROR) << "PulseAudio connection terminated early";
} else if (state != PA_CONTEXT_READY) {
LOG(ERROR) << "Unknown problem connecting to PulseAudio";
}
}
pa_context_set_state_callback(pa_context_, NULL, NULL);
break;
}
MainloopUnlock();
if (state != PA_CONTEXT_READY)
break;
if (!MainloopSafeLock())
return false;
GetDefaultPlaybackDevice();
MainloopUnlock();
if (device_id_ == kInvalidDeviceId)
break;
{
AutoLock lock(mixer_state_lock_);
if (mixer_state_ == SHUTTING_DOWN)
return false;
mixer_state_ = READY;
}
return true;
}
// Failed startup sequence, clean up now.
PulseAudioFree();
return false;
}
void AudioMixerPulse::PulseAudioFree() {
{
AutoLock lock(mixer_state_lock_);
if (!pa_mainloop_)
mixer_state_ = UNINITIALIZED;
if ((mixer_state_ == UNINITIALIZED) || (mixer_state_ == SHUTTING_DOWN))
return;
// If still initializing on another thread, this will cause it to exit.
mixer_state_ = SHUTTING_DOWN;
}
DCHECK(pa_mainloop_);
MainloopLock();
if (pa_context_) {
pa_context_disconnect(pa_context_);
pa_context_unref(pa_context_);
pa_context_ = NULL;
}
MainloopUnlock();
pa_threaded_mainloop_stop(pa_mainloop_);
pa_threaded_mainloop_free(pa_mainloop_);
pa_mainloop_ = NULL;
{
AutoLock lock(mixer_state_lock_);
mixer_state_ = UNINITIALIZED;
}
}
void AudioMixerPulse::CompleteOperation(pa_operation* pa_op,
bool* done) const {
// After starting any operation, this helper checks if it started OK, then
// waits for it to complete by iterating through the mainloop until the
// operation is not running anymore.
CHECK(pa_op);
while (pa_operation_get_state(pa_op) == PA_OPERATION_RUNNING) {
// If operation still running, but we got what we needed, cancel it now.
if (*done) {
pa_operation_cancel(pa_op);
break;
}
MainloopWait();
}
pa_operation_unref(pa_op);
}
// Must be called with mainloop lock held
void AudioMixerPulse::GetDefaultPlaybackDevice() {
DCHECK_GT(mainloop_lock_count_, 0);
DCHECK(pa_context_);
DCHECK(pa_context_get_state(pa_context_) == PA_CONTEXT_READY);
CallbackWrapper data = {this, false, NULL};
pa_operation* pa_op = pa_context_get_sink_info_list(pa_context_,
EnumerateDevicesCallback,
&data);
CompleteOperation(pa_op, &data.done);
return;
}
void AudioMixerPulse::OnEnumerateDevices(const pa_sink_info* sink_info,
int eol, bool* done) {
if (device_id_ != kInvalidDeviceId)
return;
// TODO(davej): Should we handle cases of more than one output sink device?
// eol is < 0 for error, > 0 for end of list, ==0 while listing.
if (eol == 0) {
device_id_ = sink_info->index;
}
*done = true;
MainloopSignal();
}
// static
void AudioMixerPulse::EnumerateDevicesCallback(pa_context* unused,
const pa_sink_info* sink_info,
int eol,
void* userdata) {
CallbackWrapper* data =
static_cast<CallbackWrapper*>(userdata);
data->instance->OnEnumerateDevices(sink_info, eol, &data->done);
}
// Must be called with lock held
void AudioMixerPulse::GetAudioInfo(AudioInfo* info) const {
DCHECK_GT(mainloop_lock_count_, 0);
CallbackWrapper data = {const_cast<AudioMixerPulse*>(this), false, info};
pa_operation* pa_op = pa_context_get_sink_info_by_index(pa_context_,
device_id_,
GetAudioInfoCallback,
&data);
CompleteOperation(pa_op, &data.done);
}
// static
void AudioMixerPulse::GetAudioInfoCallback(pa_context* unused,
const pa_sink_info* sink_info,
int eol,
void* userdata) {
CallbackWrapper* data = static_cast<CallbackWrapper*>(userdata);
AudioInfo* info = static_cast<AudioInfo*>(data->userdata);
// Copy just the information we care about.
if (eol == 0) {
info->cvolume = sink_info->volume;
info->muted = sink_info->mute ? true : false;
data->done = true;
}
data->instance->MainloopSignal();
}
inline void AudioMixerPulse::MainloopLock() const {
pa_threaded_mainloop_lock(pa_mainloop_);
++mainloop_lock_count_;
}
inline void AudioMixerPulse::MainloopUnlock() const {
--mainloop_lock_count_;
pa_threaded_mainloop_unlock(pa_mainloop_);
}
// Must be called with the lock held.
inline void AudioMixerPulse::MainloopWait() const {
DCHECK_GT(mainloop_lock_count_, 0);
pa_threaded_mainloop_wait(pa_mainloop_);
}
// Must be called with the lock held.
inline void AudioMixerPulse::MainloopSignal() const {
DCHECK_GT(mainloop_lock_count_, 0);
pa_threaded_mainloop_signal(pa_mainloop_, 0);
}
inline bool AudioMixerPulse::MainloopSafeLock() const {
AutoLock lock(mixer_state_lock_);
if ((mixer_state_ == SHUTTING_DOWN) || (!pa_mainloop_))
return false;
pa_threaded_mainloop_lock(pa_mainloop_);
++mainloop_lock_count_;
return true;
}
inline bool AudioMixerPulse::MainloopLockIfReady() const {
AutoLock lock(mixer_state_lock_);
if (mixer_state_ != READY)
return false;
if (!pa_mainloop_)
return false;
pa_threaded_mainloop_lock(pa_mainloop_);
++mainloop_lock_count_;
return true;
}
} // namespace chromeos
|