OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/audio_output_controller.h" | 5 #include "media/audio/audio_output_controller.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
| 9 #include <algorithm> |
9 #include <limits> | 10 #include <limits> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
13 #include "base/numerics/safe_conversions.h" | 14 #include "base/numerics/safe_conversions.h" |
14 #include "base/task_runner_util.h" | 15 #include "base/task_runner_util.h" |
15 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 #include "base/trace_event/trace_event.h" | 18 #include "base/trace_event/trace_event.h" |
18 | 19 |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 if (stream_ != diverting_to_stream_) | 286 if (stream_ != diverting_to_stream_) |
286 OnDeviceChange(); | 287 OnDeviceChange(); |
287 } | 288 } |
288 | 289 |
289 void AudioOutputController::DoReportError() { | 290 void AudioOutputController::DoReportError() { |
290 DCHECK(message_loop_->BelongsToCurrentThread()); | 291 DCHECK(message_loop_->BelongsToCurrentThread()); |
291 if (state_ != kClosed) | 292 if (state_ != kClosed) |
292 handler_->OnError(); | 293 handler_->OnError(); |
293 } | 294 } |
294 | 295 |
295 int AudioOutputController::OnMoreData(AudioBus* dest, | 296 int AudioOutputController::OnMoreData(base::TimeTicks target_playout_time, |
296 uint32_t total_bytes_delay, | 297 int prior_frames_skipped, |
297 uint32_t frames_skipped) { | 298 AudioBus* dest) { |
298 TRACE_EVENT0("audio", "AudioOutputController::OnMoreData"); | 299 TRACE_EVENT0("audio", "AudioOutputController::OnMoreData"); |
299 | 300 |
300 // Indicate that we haven't wedged (at least not indefinitely, WedgeCheck() | 301 // Indicate that we haven't wedged (at least not indefinitely, WedgeCheck() |
301 // may have already fired if OnMoreData() took an abnormal amount of time). | 302 // may have already fired if OnMoreData() took an abnormal amount of time). |
302 // Since this thread is the only writer of |on_more_io_data_called_| once the | 303 // Since this thread is the only writer of |on_more_io_data_called_| once the |
303 // thread starts, its safe to compare and then increment. | 304 // thread starts, its safe to compare and then increment. |
304 if (base::AtomicRefCountIsZero(&on_more_io_data_called_)) | 305 if (base::AtomicRefCountIsZero(&on_more_io_data_called_)) |
305 base::AtomicRefCountInc(&on_more_io_data_called_); | 306 base::AtomicRefCountInc(&on_more_io_data_called_); |
306 | 307 |
307 sync_reader_->Read(dest); | 308 sync_reader_->Read(dest); |
308 | 309 |
| 310 // If |target_playout_time| is prior to base::TimeTicks::Now(), use a delay of |
| 311 // zero. This can happen if base::TimeTicks::Now() was passed as |
| 312 // |target_playout_time| and the clock has advanced. |
| 313 // TODO(jameswest): Change AudioRendererSink::RenderCallback::Render to use |
| 314 // the same signature as AudioOutputStream::AudioSourceCallback::OnMoreData |
| 315 // and pass |target_playout_time| to SyncReader. |
| 316 const base::TimeDelta delay = |
| 317 std::max(target_playout_time - base::TimeTicks::Now(), base::TimeDelta()); |
| 318 const int total_bytes_delay = |
| 319 delay.InSecondsF() * params_.GetBytesPerSecond(); |
309 const int frames = dest->frames(); | 320 const int frames = dest->frames(); |
310 sync_reader_->UpdatePendingBytes( | 321 sync_reader_->UpdatePendingBytes( |
311 total_bytes_delay + frames * params_.GetBytesPerFrame(), frames_skipped); | 322 total_bytes_delay + frames * params_.GetBytesPerFrame(), |
| 323 prior_frames_skipped); |
312 | 324 |
313 bool need_to_duplicate = false; | 325 bool need_to_duplicate = false; |
314 { | 326 { |
315 base::AutoLock lock(duplication_targets_lock_); | 327 base::AutoLock lock(duplication_targets_lock_); |
316 need_to_duplicate = !duplication_targets_.empty(); | 328 need_to_duplicate = !duplication_targets_.empty(); |
317 } | 329 } |
318 if (need_to_duplicate) { | 330 if (need_to_duplicate) { |
319 const base::TimeTicks reference_time = | |
320 base::TimeTicks::Now() + | |
321 base::TimeDelta::FromMicroseconds(base::Time::kMicrosecondsPerSecond * | |
322 total_bytes_delay / | |
323 params_.GetBytesPerSecond()); | |
324 std::unique_ptr<AudioBus> copy(AudioBus::Create(params_)); | 331 std::unique_ptr<AudioBus> copy(AudioBus::Create(params_)); |
325 dest->CopyTo(copy.get()); | 332 dest->CopyTo(copy.get()); |
326 message_loop_->PostTask( | 333 message_loop_->PostTask( |
327 FROM_HERE, | 334 FROM_HERE, |
328 base::Bind(&AudioOutputController::BroadcastDataToDuplicationTargets, | 335 base::Bind(&AudioOutputController::BroadcastDataToDuplicationTargets, |
329 this, base::Passed(©), reference_time)); | 336 this, base::Passed(©), target_playout_time)); |
330 } | 337 } |
331 | 338 |
332 if (will_monitor_audio_levels()) | 339 if (will_monitor_audio_levels()) |
333 power_monitor_.Scan(*dest, frames); | 340 power_monitor_.Scan(*dest, frames); |
334 | 341 |
335 return frames; | 342 return frames; |
336 } | 343 } |
337 | 344 |
338 void AudioOutputController::BroadcastDataToDuplicationTargets( | 345 void AudioOutputController::BroadcastDataToDuplicationTargets( |
339 std::unique_ptr<AudioBus> audio_bus, | 346 std::unique_ptr<AudioBus> audio_bus, |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 DCHECK(message_loop_->BelongsToCurrentThread()); | 512 DCHECK(message_loop_->BelongsToCurrentThread()); |
506 | 513 |
507 // If we should be playing and we haven't, that's a wedge. | 514 // If we should be playing and we haven't, that's a wedge. |
508 if (state_ == kPlaying) { | 515 if (state_ == kPlaying) { |
509 UMA_HISTOGRAM_BOOLEAN("Media.AudioOutputControllerPlaybackStartupSuccess", | 516 UMA_HISTOGRAM_BOOLEAN("Media.AudioOutputControllerPlaybackStartupSuccess", |
510 base::AtomicRefCountIsOne(&on_more_io_data_called_)); | 517 base::AtomicRefCountIsOne(&on_more_io_data_called_)); |
511 } | 518 } |
512 } | 519 } |
513 | 520 |
514 } // namespace media | 521 } // namespace media |
OLD | NEW |