OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/audio/mac/audio_auhal_mac.h" | 5 #include "media/audio/mac/audio_auhal_mac.h" |
6 | 6 |
7 #include <CoreServices/CoreServices.h> | 7 #include <CoreServices/CoreServices.h> |
8 | 8 |
9 #include <string> | |
10 | |
9 #include "base/bind.h" | 11 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
11 #include "base/logging.h" | 13 #include "base/logging.h" |
12 #include "base/mac/mac_logging.h" | 14 #include "base/mac/mac_logging.h" |
13 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
14 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
15 #include "base/time/time.h" | |
16 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
17 #include "media/audio/mac/audio_manager_mac.h" | 18 #include "media/audio/mac/audio_manager_mac.h" |
18 #include "media/base/audio_pull_fifo.h" | 19 #include "media/base/audio_pull_fifo.h" |
19 | 20 |
20 namespace media { | 21 namespace media { |
21 | 22 |
22 static void WrapBufferList(AudioBufferList* buffer_list, | 23 static void WrapBufferList(AudioBufferList* buffer_list, |
23 AudioBus* bus, | 24 AudioBus* bus, |
24 int frames) { | 25 int frames) { |
25 DCHECK(buffer_list); | 26 DCHECK(buffer_list); |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 audio_fifo_.reset(new AudioPullFifo( | 230 audio_fifo_.reset(new AudioPullFifo( |
230 output_channels_, | 231 output_channels_, |
231 number_of_frames_, | 232 number_of_frames_, |
232 base::Bind(&AUHALStream::ProvideInput, base::Unretained(this)))); | 233 base::Bind(&AUHALStream::ProvideInput, base::Unretained(this)))); |
233 } | 234 } |
234 } | 235 } |
235 | 236 |
236 // Make |output_bus_| wrap the output AudioBufferList. | 237 // Make |output_bus_| wrap the output AudioBufferList. |
237 WrapBufferList(data, output_bus_.get(), number_of_frames); | 238 WrapBufferList(data, output_bus_.get(), number_of_frames); |
238 | 239 |
239 // Update the playout latency. | 240 // Update the playout time. |
240 const double playout_latency_frames = GetPlayoutLatency(output_time_stamp); | 241 const UInt64 output_time_ns = |
241 current_hardware_pending_bytes_ = static_cast<uint32_t>( | 242 AudioConvertHostTimeToNanos(output_time_stamp->mHostTime); |
242 (playout_latency_frames + 0.5) * params_.GetBytesPerFrame()); | 243 // TODO(jameswest): Find an alternative to FromInternalValue. |
244 current_target_playout_time_ = base::TimeTicks::FromInternalValue( | |
245 output_time_ns / base::Time::kNanosecondsPerMicrosecond); | |
243 | 246 |
244 if (audio_fifo_) | 247 if (audio_fifo_) |
245 audio_fifo_->Consume(output_bus_.get(), output_bus_->frames()); | 248 audio_fifo_->Consume(output_bus_.get(), output_bus_->frames()); |
246 else | 249 else |
247 ProvideInput(0, output_bus_.get()); | 250 ProvideInput(0, output_bus_.get()); |
248 | 251 |
249 last_number_of_frames_ = number_of_frames; | 252 last_number_of_frames_ = number_of_frames; |
250 | 253 |
251 return noErr; | 254 return noErr; |
252 } | 255 } |
253 | 256 |
254 void AUHALStream::ProvideInput(int frame_delay, AudioBus* dest) { | 257 void AUHALStream::ProvideInput(int frame_delay, AudioBus* dest) { |
255 base::AutoLock auto_lock(source_lock_); | 258 base::AutoLock auto_lock(source_lock_); |
256 if (!source_) { | 259 if (!source_) { |
257 dest->Zero(); | 260 dest->Zero(); |
258 return; | 261 return; |
259 } | 262 } |
260 | 263 |
264 const base::TimeDelta delay = base::TimeDelta::FromSecondsD( | |
265 static_cast<double>(frame_delay) / params_.sample_rate()); | |
266 const base::TimeTicks target_playout_time = | |
267 current_target_playout_time_ + delay; | |
268 | |
261 // Supply the input data and render the output data. | 269 // Supply the input data and render the output data. |
262 source_->OnMoreData(dest, current_hardware_pending_bytes_ + | 270 source_->OnMoreData(target_playout_time, current_lost_frames_, dest); |
263 frame_delay * params_.GetBytesPerFrame(), | |
264 current_lost_frames_); | |
265 dest->Scale(volume_); | 271 dest->Scale(volume_); |
266 current_lost_frames_ = 0; | 272 current_lost_frames_ = 0; |
267 } | 273 } |
268 | 274 |
269 // AUHAL callback. | 275 // AUHAL callback. |
270 OSStatus AUHALStream::InputProc( | 276 OSStatus AUHALStream::InputProc( |
271 void* user_data, | 277 void* user_data, |
272 AudioUnitRenderActionFlags* flags, | 278 AudioUnitRenderActionFlags* flags, |
273 const AudioTimeStamp* output_time_stamp, | 279 const AudioTimeStamp* output_time_stamp, |
274 UInt32 bus_number, | 280 UInt32 bus_number, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
327 &device_latency_frames); | 333 &device_latency_frames); |
328 if (result != noErr) { | 334 if (result != noErr) { |
329 OSSTATUS_DLOG(WARNING, result) << "Could not get audio device latency"; | 335 OSSTATUS_DLOG(WARNING, result) << "Could not get audio device latency"; |
330 return 0.0; | 336 return 0.0; |
331 } | 337 } |
332 | 338 |
333 return static_cast<double>((audio_unit_latency_sec * | 339 return static_cast<double>((audio_unit_latency_sec * |
334 output_format_.mSampleRate) + device_latency_frames); | 340 output_format_.mSampleRate) + device_latency_frames); |
335 } | 341 } |
336 | 342 |
337 double AUHALStream::GetPlayoutLatency( | |
338 const AudioTimeStamp* output_time_stamp) { | |
339 // Ensure mHostTime is valid. | |
340 if ((output_time_stamp->mFlags & kAudioTimeStampHostTimeValid) == 0) | |
chcunningham
2016/07/29 01:21:09
This method had checks for validity of host time..
jameswest
2016/08/26 02:08:47
Done.
| |
341 return 0; | |
342 | |
343 // Get the delay between the moment getting the callback and the scheduled | |
344 // time stamp that tells when the data is going to be played out. | |
345 UInt64 output_time_ns = AudioConvertHostTimeToNanos( | |
346 output_time_stamp->mHostTime); | |
347 UInt64 now_ns = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()); | |
348 | |
349 // Prevent overflow leading to huge delay information; occurs regularly on | |
350 // the bots, probably less so in the wild. | |
351 if (now_ns > output_time_ns) | |
chcunningham
2016/07/29 01:21:09
I think you need to still account for this
jameswest
2016/08/26 02:08:47
Done.
| |
352 return 0; | |
353 | |
354 double delay_frames = static_cast<double> | |
355 (1e-9 * (output_time_ns - now_ns) * output_format_.mSampleRate); | |
356 | |
357 return (delay_frames + hardware_latency_frames_); | |
358 } | |
359 | |
360 void AUHALStream::UpdatePlayoutTimestamp(const AudioTimeStamp* timestamp) { | 343 void AUHALStream::UpdatePlayoutTimestamp(const AudioTimeStamp* timestamp) { |
361 if ((timestamp->mFlags & kAudioTimeStampSampleTimeValid) == 0) | 344 if ((timestamp->mFlags & kAudioTimeStampSampleTimeValid) == 0) |
362 return; | 345 return; |
363 | 346 |
364 if (last_sample_time_) { | 347 if (last_sample_time_) { |
365 DCHECK_NE(0U, last_number_of_frames_); | 348 DCHECK_NE(0U, last_number_of_frames_); |
366 UInt32 diff = | 349 UInt32 diff = |
367 static_cast<UInt32>(timestamp->mSampleTime - last_sample_time_); | 350 static_cast<UInt32>(timestamp->mSampleTime - last_sample_time_); |
368 if (diff != last_number_of_frames_) { | 351 if (diff != last_number_of_frames_) { |
369 DCHECK_GT(diff, last_number_of_frames_); | 352 DCHECK_GT(diff, last_number_of_frames_); |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
555 OSStatus result = AudioUnitUninitialize(audio_unit_); | 538 OSStatus result = AudioUnitUninitialize(audio_unit_); |
556 OSSTATUS_DLOG_IF(ERROR, result != noErr, result) | 539 OSSTATUS_DLOG_IF(ERROR, result != noErr, result) |
557 << "AudioUnitUninitialize() failed."; | 540 << "AudioUnitUninitialize() failed."; |
558 result = AudioComponentInstanceDispose(audio_unit_); | 541 result = AudioComponentInstanceDispose(audio_unit_); |
559 OSSTATUS_DLOG_IF(ERROR, result != noErr, result) | 542 OSSTATUS_DLOG_IF(ERROR, result != noErr, result) |
560 << "AudioComponentInstanceDispose() failed."; | 543 << "AudioComponentInstanceDispose() failed."; |
561 audio_unit_ = 0; | 544 audio_unit_ = 0; |
562 } | 545 } |
563 | 546 |
564 } // namespace media | 547 } // namespace media |
OLD | NEW |