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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
// Copyright 2013 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/mac/audio_auhal_mac.h"
#include <CoreServices/CoreServices.h>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/mac/mac_logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "media/audio/mac/audio_manager_mac.h"
#include "media/base/audio_pull_fifo.h"
namespace media {
static void WrapBufferList(AudioBufferList* buffer_list,
AudioBus* bus,
int frames) {
DCHECK(buffer_list);
DCHECK(bus);
const int channels = bus->channels();
const int buffer_list_channels = buffer_list->mNumberBuffers;
CHECK_EQ(channels, buffer_list_channels);
// Copy pointers from AudioBufferList.
for (int i = 0; i < channels; ++i) {
bus->SetChannelData(
i, static_cast<float*>(buffer_list->mBuffers[i].mData));
}
// Finally set the actual length.
bus->set_frames(frames);
}
AUHALStream::AUHALStream(AudioManagerMac* manager,
const AudioParameters& params,
AudioDeviceID device)
: manager_(manager),
params_(params),
output_channels_(params_.channels()),
number_of_frames_(params_.frames_per_buffer()),
number_of_frames_requested_(0),
source_(NULL),
device_(device),
audio_unit_(0),
volume_(1),
hardware_latency_frames_(0),
stopped_(true),
current_hardware_pending_bytes_(0),
current_lost_frames_(0),
last_sample_time_(0.0),
last_number_of_frames_(0),
total_lost_frames_(0),
largest_glitch_frames_(0),
glitches_detected_(0) {
// We must have a manager.
DCHECK(manager_);
DVLOG(1) << "AUHALStream::AUHALStream()";
DVLOG(1) << "Device: " << device;
DVLOG(1) << "Output channels: " << output_channels_;
DVLOG(1) << "Sample rate: " << params_.sample_rate();
DVLOG(1) << "Buffer size: " << number_of_frames_;
}
AUHALStream::~AUHALStream() {
DCHECK(thread_checker_.CalledOnValidThread());
CHECK(!audio_unit_);
ReportAndResetStats();
}
bool AUHALStream::Open() {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!output_bus_.get());
DCHECK(!audio_unit_);
// Get the total number of output channels that the
// hardware supports.
int device_output_channels;
bool got_output_channels = AudioManagerMac::GetDeviceChannels(
device_,
kAudioDevicePropertyScopeOutput,
&device_output_channels);
// Sanity check the requested output channels.
if (!got_output_channels ||
output_channels_ <= 0 || output_channels_ > device_output_channels) {
LOG(ERROR) << "AudioDevice does not support requested output channels.";
return false;
}
// The requested sample-rate must match the hardware sample-rate.
int sample_rate = AudioManagerMac::HardwareSampleRateForDevice(device_);
if (sample_rate != params_.sample_rate()) {
LOG(ERROR) << "Requested sample-rate: " << params_.sample_rate()
<< " must match the hardware sample-rate: " << sample_rate;
return false;
}
// The output bus will wrap the AudioBufferList given to us in
// the Render() callback.
DCHECK_GT(output_channels_, 0);
output_bus_ = AudioBus::CreateWrapper(output_channels_);
// Try to set the I/O buffer size for the HAL to |number_of_frames_| and set
// |size_was_changed| to true if the size was changed. The status of other
// active audio input and output streams are involved in the final setting.
// NOTE: always do this setting before configuring the audio unit since there
// is a risk of deadlocking in Core Audio otherwise.
bool size_was_changed = false;
const bool is_input = false;
if (!manager_->MaybeChangeBufferSize(device_, is_input, number_of_frames_,
&size_was_changed)) {
return false;
}
bool configured = ConfigureAUHAL();
if (configured)
hardware_latency_frames_ = GetHardwareLatency();
return configured;
}
void AUHALStream::Close() {
DCHECK(thread_checker_.CalledOnValidThread());
CloseAudioUnit();
// Inform the audio manager that we have been closed. This will cause our
// destruction.
manager_->ReleaseOutputStream(this);
}
void AUHALStream::Start(AudioSourceCallback* callback) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(callback);
if (!audio_unit_) {
DLOG(ERROR) << "Open() has not been called successfully";
return;
}
if (!stopped_) {
CHECK_EQ(source_, callback);
return;
}
// Check if we should defer Start() for http://crbug.com/160920.
if (manager_->ShouldDeferStreamStart()) {
// Use a cancellable closure so that if Stop() is called before Start()
// actually runs, we can cancel the pending start.
deferred_start_cb_.Reset(
base::Bind(&AUHALStream::Start, base::Unretained(this), callback));
manager_->GetTaskRunner()->PostDelayedTask(
FROM_HERE, deferred_start_cb_.callback(), base::TimeDelta::FromSeconds(
AudioManagerMac::kStartDelayInSecsForPowerEvents));
return;
}
stopped_ = false;
audio_fifo_.reset();
{
base::AutoLock auto_lock(source_lock_);
source_ = callback;
}
OSStatus result = AudioOutputUnitStart(audio_unit_);
if (result == noErr)
return;
Stop();
OSSTATUS_DLOG(ERROR, result) << "AudioOutputUnitStart() failed.";
callback->OnError(this);
}
void AUHALStream::Stop() {
DCHECK(thread_checker_.CalledOnValidThread());
deferred_start_cb_.Cancel();
if (stopped_)
return;
OSStatus result = AudioOutputUnitStop(audio_unit_);
OSSTATUS_DLOG_IF(ERROR, result != noErr, result)
<< "AudioOutputUnitStop() failed.";
if (result != noErr)
source_->OnError(this);
ReportAndResetStats();
base::AutoLock auto_lock(source_lock_);
source_ = NULL;
stopped_ = true;
}
void AUHALStream::SetVolume(double volume) {
volume_ = static_cast<float>(volume);
}
void AUHALStream::GetVolume(double* volume) {
*volume = volume_;
}
// Pulls on our provider to get rendered audio stream.
// Note to future hackers of this function: Do not add locks which can
// be contended in the middle of stream processing here (starting and stopping
// the stream are ok) because this is running on a real-time thread.
OSStatus AUHALStream::Render(
AudioUnitRenderActionFlags* flags,
const AudioTimeStamp* output_time_stamp,
UInt32 bus_number,
UInt32 number_of_frames,
AudioBufferList* data) {
TRACE_EVENT0("audio", "AUHALStream::Render");
UpdatePlayoutTimestamp(output_time_stamp);
// If the stream parameters change for any reason, we need to insert a FIFO
// since the OnMoreData() pipeline can't handle frame size changes.
if (number_of_frames != number_of_frames_) {
// Create a FIFO on the fly to handle any discrepancies in callback rates.
if (!audio_fifo_) {
number_of_frames_requested_ = number_of_frames;
DVLOG(1) << "Audio frame size changed from " << number_of_frames_
<< " to " << number_of_frames
<< "; adding FIFO to compensate.";
audio_fifo_.reset(new AudioPullFifo(
output_channels_,
number_of_frames_,
base::Bind(&AUHALStream::ProvideInput, base::Unretained(this))));
}
}
// Make |output_bus_| wrap the output AudioBufferList.
WrapBufferList(data, output_bus_.get(), number_of_frames);
// Update the playout latency.
const double playout_latency_frames = GetPlayoutLatency(output_time_stamp);
current_hardware_pending_bytes_ = static_cast<uint32_t>(
(playout_latency_frames + 0.5) * params_.GetBytesPerFrame());
if (audio_fifo_)
audio_fifo_->Consume(output_bus_.get(), output_bus_->frames());
else
ProvideInput(0, output_bus_.get());
last_number_of_frames_ = number_of_frames;
return noErr;
}
void AUHALStream::ProvideInput(int frame_delay, AudioBus* dest) {
base::AutoLock auto_lock(source_lock_);
if (!source_) {
dest->Zero();
return;
}
// Supply the input data and render the output data.
source_->OnMoreData(dest, current_hardware_pending_bytes_ +
frame_delay * params_.GetBytesPerFrame(),
current_lost_frames_);
dest->Scale(volume_);
current_lost_frames_ = 0;
}
// AUHAL callback.
OSStatus AUHALStream::InputProc(
void* user_data,
AudioUnitRenderActionFlags* flags,
const AudioTimeStamp* output_time_stamp,
UInt32 bus_number,
UInt32 number_of_frames,
AudioBufferList* io_data) {
// Dispatch to our class method.
AUHALStream* audio_output =
static_cast<AUHALStream*>(user_data);
if (!audio_output)
return -1;
return audio_output->Render(
flags,
output_time_stamp,
bus_number,
number_of_frames,
io_data);
}
double AUHALStream::GetHardwareLatency() {
if (!audio_unit_ || device_ == kAudioObjectUnknown) {
DLOG(WARNING) << "AudioUnit is NULL or device ID is unknown";
return 0.0;
}
// Get audio unit latency.
Float64 audio_unit_latency_sec = 0.0;
UInt32 size = sizeof(audio_unit_latency_sec);
OSStatus result = AudioUnitGetProperty(
audio_unit_,
kAudioUnitProperty_Latency,
kAudioUnitScope_Global,
0,
&audio_unit_latency_sec,
&size);
if (result != noErr) {
OSSTATUS_DLOG(WARNING, result) << "Could not get AudioUnit latency";
return 0.0;
}
// Get output audio device latency.
static const AudioObjectPropertyAddress property_address = {
kAudioDevicePropertyLatency,
kAudioDevicePropertyScopeOutput,
kAudioObjectPropertyElementMaster
};
UInt32 device_latency_frames = 0;
size = sizeof(device_latency_frames);
result = AudioObjectGetPropertyData(
device_,
&property_address,
0,
NULL,
&size,
&device_latency_frames);
if (result != noErr) {
OSSTATUS_DLOG(WARNING, result) << "Could not get audio device latency";
return 0.0;
}
return static_cast<double>((audio_unit_latency_sec *
output_format_.mSampleRate) + device_latency_frames);
}
double AUHALStream::GetPlayoutLatency(
const AudioTimeStamp* output_time_stamp) {
// Ensure mHostTime is valid.
if ((output_time_stamp->mFlags & kAudioTimeStampHostTimeValid) == 0)
return 0;
// Get the delay between the moment getting the callback and the scheduled
// time stamp that tells when the data is going to be played out.
UInt64 output_time_ns = AudioConvertHostTimeToNanos(
output_time_stamp->mHostTime);
UInt64 now_ns = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime());
// Prevent overflow leading to huge delay information; occurs regularly on
// the bots, probably less so in the wild.
if (now_ns > output_time_ns)
return 0;
double delay_frames = static_cast<double>
(1e-9 * (output_time_ns - now_ns) * output_format_.mSampleRate);
return (delay_frames + hardware_latency_frames_);
}
void AUHALStream::UpdatePlayoutTimestamp(const AudioTimeStamp* timestamp) {
if ((timestamp->mFlags & kAudioTimeStampSampleTimeValid) == 0)
return;
if (last_sample_time_) {
DCHECK_NE(0U, last_number_of_frames_);
UInt32 diff =
static_cast<UInt32>(timestamp->mSampleTime - last_sample_time_);
if (diff != last_number_of_frames_) {
DCHECK_GT(diff, last_number_of_frames_);
// We're being asked to render samples post what we expected. Update the
// glitch count etc and keep a record of the largest glitch.
auto lost_frames = diff - last_number_of_frames_;
total_lost_frames_ += lost_frames;
current_lost_frames_ += lost_frames;
if (lost_frames > largest_glitch_frames_)
largest_glitch_frames_ = lost_frames;
++glitches_detected_;
}
}
// Store the last sample time for use next time we get called back.
last_sample_time_ = timestamp->mSampleTime;
}
void AUHALStream::ReportAndResetStats() {
if (!last_sample_time_)
return; // No stats gathered to report.
// A value of 0 indicates that we got the buffer size we asked for.
UMA_HISTOGRAM_COUNTS("Media.Audio.Render.FramesRequested",
number_of_frames_requested_);
// Even if there aren't any glitches, we want to record it to get a feel for
// how often we get no glitches vs the alternative.
UMA_HISTOGRAM_CUSTOM_COUNTS("Media.Audio.Render.Glitches", glitches_detected_,
0, 999999, 100);
if (glitches_detected_ != 0) {
auto lost_frames_ms = (total_lost_frames_ * 1000) / params_.sample_rate();
UMA_HISTOGRAM_COUNTS("Media.Audio.Render.LostFramesInMs", lost_frames_ms);
auto largest_glitch_ms =
(largest_glitch_frames_ * 1000) / params_.sample_rate();
UMA_HISTOGRAM_COUNTS("Media.Audio.Render.LargestGlitchMs",
largest_glitch_ms);
DLOG(WARNING) << "Total glitches=" << glitches_detected_
<< ". Total frames lost=" << total_lost_frames_ << " ("
<< lost_frames_ms;
}
number_of_frames_requested_ = 0;
glitches_detected_ = 0;
last_sample_time_ = 0;
last_number_of_frames_ = 0;
total_lost_frames_ = 0;
largest_glitch_frames_ = 0;
}
bool AUHALStream::SetStreamFormat(
AudioStreamBasicDescription* desc,
int channels,
UInt32 scope,
UInt32 element) {
DCHECK(desc);
AudioStreamBasicDescription& format = *desc;
format.mSampleRate = params_.sample_rate();
format.mFormatID = kAudioFormatLinearPCM;
format.mFormatFlags = kAudioFormatFlagsNativeFloatPacked |
kLinearPCMFormatFlagIsNonInterleaved;
format.mBytesPerPacket = sizeof(Float32);
format.mFramesPerPacket = 1;
format.mBytesPerFrame = sizeof(Float32);
format.mChannelsPerFrame = channels;
format.mBitsPerChannel = 32;
format.mReserved = 0;
OSStatus result = AudioUnitSetProperty(
audio_unit_,
kAudioUnitProperty_StreamFormat,
scope,
element,
&format,
sizeof(format));
return (result == noErr);
}
bool AUHALStream::ConfigureAUHAL() {
DCHECK(thread_checker_.CalledOnValidThread());
if (device_ == kAudioObjectUnknown || output_channels_ == 0)
return false;
AudioComponentDescription desc = {
kAudioUnitType_Output,
kAudioUnitSubType_HALOutput,
kAudioUnitManufacturer_Apple,
0,
0
};
AudioComponent comp = AudioComponentFindNext(0, &desc);
if (!comp)
return false;
OSStatus result = AudioComponentInstanceNew(comp, &audio_unit_);
if (result != noErr) {
OSSTATUS_DLOG(ERROR, result) << "AudioComponentInstanceNew() failed.";
return false;
}
// Enable output as appropriate.
// See Apple technote for details about the EnableIO property.
// Note that we use bus 1 for input and bus 0 for output:
// http://developer.apple.com/library/mac/#technotes/tn2091/_index.html
UInt32 enable_IO = 1;
result = AudioUnitSetProperty(
audio_unit_,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Output,
0,
&enable_IO,
sizeof(enable_IO));
if (result != noErr) {
CloseAudioUnit();
return false;
}
// Set the device to be used with the AUHAL AudioUnit.
result = AudioUnitSetProperty(
audio_unit_,
kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global,
0,
&device_,
sizeof(AudioDeviceID));
if (result != noErr) {
CloseAudioUnit();
return false;
}
// Set stream formats.
// See Apple's tech note for details on the peculiar way that
// inputs and outputs are handled in the AUHAL concerning scope and bus
// (element) numbers:
// http://developer.apple.com/library/mac/#technotes/tn2091/_index.html
if (!SetStreamFormat(&output_format_,
output_channels_,
kAudioUnitScope_Input,
0)) {
CloseAudioUnit();
return false;
}
// Setup callback.
AURenderCallbackStruct callback;
callback.inputProc = InputProc;
callback.inputProcRefCon = this;
result = AudioUnitSetProperty(
audio_unit_,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
0,
&callback,
sizeof(callback));
if (result != noErr) {
CloseAudioUnit();
return false;
}
result = AudioUnitInitialize(audio_unit_);
if (result != noErr) {
OSSTATUS_DLOG(ERROR, result) << "AudioUnitInitialize() failed.";
CloseAudioUnit();
return false;
}
return true;
}
void AUHALStream::CloseAudioUnit() {
DCHECK(thread_checker_.CalledOnValidThread());
if (!audio_unit_)
return;
OSStatus result = AudioUnitUninitialize(audio_unit_);
OSSTATUS_DLOG_IF(ERROR, result != noErr, result)
<< "AudioUnitUninitialize() failed.";
result = AudioComponentInstanceDispose(audio_unit_);
OSSTATUS_DLOG_IF(ERROR, result != noErr, result)
<< "AudioComponentInstanceDispose() failed.";
audio_unit_ = 0;
}
} // namespace media
|