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_device.h" | 5 #include "media/audio/audio_output_device.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <cmath> | 10 #include <cmath> |
(...skipping 21 matching lines...) Expand all Loading... |
32 public: | 32 public: |
33 AudioThreadCallback(const AudioParameters& audio_parameters, | 33 AudioThreadCallback(const AudioParameters& audio_parameters, |
34 base::SharedMemoryHandle memory, | 34 base::SharedMemoryHandle memory, |
35 int memory_length, | 35 int memory_length, |
36 AudioRendererSink::RenderCallback* render_callback); | 36 AudioRendererSink::RenderCallback* render_callback); |
37 ~AudioThreadCallback() override; | 37 ~AudioThreadCallback() override; |
38 | 38 |
39 void MapSharedMemory() override; | 39 void MapSharedMemory() override; |
40 | 40 |
41 // Called whenever we receive notifications about pending data. | 41 // Called whenever we receive notifications about pending data. |
42 void Process(int64_t pending_data, base::TimeTicks delay_timestamp) override; | 42 void Process(uint32_t pending_data) override; |
43 | 43 |
44 // Returns whether the current thread is the audio device thread or not. | 44 // Returns whether the current thread is the audio device thread or not. |
45 // Will always return true if DCHECKs are not enabled. | 45 // Will always return true if DCHECKs are not enabled. |
46 bool CurrentThreadIsAudioDeviceThread(); | 46 bool CurrentThreadIsAudioDeviceThread(); |
47 | 47 |
48 private: | 48 private: |
49 const int frame_rate_; | 49 const int bytes_per_frame_; |
50 AudioRendererSink::RenderCallback* render_callback_; | 50 AudioRendererSink::RenderCallback* render_callback_; |
51 std::unique_ptr<AudioBus> output_bus_; | 51 std::unique_ptr<AudioBus> output_bus_; |
52 uint64_t callback_num_; | 52 uint64_t callback_num_; |
53 | 53 |
54 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); | 54 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); |
55 }; | 55 }; |
56 | 56 |
57 AudioOutputDevice::AudioOutputDevice( | 57 AudioOutputDevice::AudioOutputDevice( |
58 std::unique_ptr<AudioOutputIPC> ipc, | 58 std::unique_ptr<AudioOutputIPC> ipc, |
59 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 59 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 } | 424 } |
425 | 425 |
426 // AudioOutputDevice::AudioThreadCallback | 426 // AudioOutputDevice::AudioThreadCallback |
427 | 427 |
428 AudioOutputDevice::AudioThreadCallback::AudioThreadCallback( | 428 AudioOutputDevice::AudioThreadCallback::AudioThreadCallback( |
429 const AudioParameters& audio_parameters, | 429 const AudioParameters& audio_parameters, |
430 base::SharedMemoryHandle memory, | 430 base::SharedMemoryHandle memory, |
431 int memory_length, | 431 int memory_length, |
432 AudioRendererSink::RenderCallback* render_callback) | 432 AudioRendererSink::RenderCallback* render_callback) |
433 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length, 1), | 433 : AudioDeviceThread::Callback(audio_parameters, memory, memory_length, 1), |
434 frame_rate_(audio_parameters.sample_rate()), | 434 bytes_per_frame_(audio_parameters.GetBytesPerFrame()), |
435 render_callback_(render_callback), | 435 render_callback_(render_callback), |
436 callback_num_(0) {} | 436 callback_num_(0) {} |
437 | 437 |
438 AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() { | 438 AudioOutputDevice::AudioThreadCallback::~AudioThreadCallback() { |
439 } | 439 } |
440 | 440 |
441 void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() { | 441 void AudioOutputDevice::AudioThreadCallback::MapSharedMemory() { |
442 CHECK_EQ(total_segments_, 1); | 442 CHECK_EQ(total_segments_, 1); |
443 CHECK(shared_memory_.Map(memory_length_)); | 443 CHECK(shared_memory_.Map(memory_length_)); |
444 DCHECK_EQ(static_cast<size_t>(memory_length_), | 444 DCHECK_EQ(static_cast<size_t>(memory_length_), |
445 sizeof(AudioOutputBufferParameters) + | 445 sizeof(AudioOutputBufferParameters) + |
446 AudioBus::CalculateMemorySize(audio_parameters_)); | 446 AudioBus::CalculateMemorySize(audio_parameters_)); |
447 | 447 |
448 AudioOutputBuffer* buffer = | 448 AudioOutputBuffer* buffer = |
449 reinterpret_cast<AudioOutputBuffer*>(shared_memory_.memory()); | 449 reinterpret_cast<AudioOutputBuffer*>(shared_memory_.memory()); |
450 output_bus_ = AudioBus::WrapMemory(audio_parameters_, buffer->audio); | 450 output_bus_ = AudioBus::WrapMemory(audio_parameters_, buffer->audio); |
451 } | 451 } |
452 | 452 |
453 // Called whenever we receive notifications about pending data. | 453 // Called whenever we receive notifications about pending data. |
454 void AudioOutputDevice::AudioThreadCallback::Process( | 454 void AudioOutputDevice::AudioThreadCallback::Process(uint32_t pending_data) { |
455 int64_t pending_data, | 455 // Convert the number of pending bytes in the render buffer into frames. |
456 base::TimeTicks delay_timestamp) { | 456 double frames_delayed = static_cast<double>(pending_data) / bytes_per_frame_; |
457 base::TimeDelta delay = base::TimeDelta::FromInternalValue(pending_data); | |
458 | 457 |
459 callback_num_++; | 458 callback_num_++; |
460 TRACE_EVENT1("audio", "AudioOutputDevice::FireRenderCallback", | 459 TRACE_EVENT1("audio", "AudioOutputDevice::FireRenderCallback", |
461 "callback_num", callback_num_); | 460 "callback_num", callback_num_); |
462 | 461 |
463 // When playback starts, we get an immediate callback to Process to make sure | 462 // When playback starts, we get an immediate callback to Process to make sure |
464 // that we have some data, we'll get another one after the device is awake and | 463 // that we have some data, we'll get another one after the device is awake and |
465 // ingesting data, which is what we want to track with this trace. | 464 // ingesting data, which is what we want to track with this trace. |
466 if (callback_num_ == 2) { | 465 if (callback_num_ == 2) { |
467 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this); | 466 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this); |
468 } | 467 } |
469 | 468 |
470 // Read and reset the number of frames skipped. | 469 // Read and reset the number of frames skipped. |
471 AudioOutputBuffer* buffer = | 470 AudioOutputBuffer* buffer = |
472 reinterpret_cast<AudioOutputBuffer*>(shared_memory_.memory()); | 471 reinterpret_cast<AudioOutputBuffer*>(shared_memory_.memory()); |
473 uint32_t frames_skipped = buffer->params.frames_skipped; | 472 uint32_t frames_skipped = buffer->params.frames_skipped; |
474 buffer->params.frames_skipped = 0; | 473 buffer->params.frames_skipped = 0; |
475 | 474 |
476 DVLOG(4) << __func__ << " delay:" << delay | 475 DVLOG(4) << __func__ << " pending_data:" << pending_data |
| 476 << " frames_delayed(pre-round):" << frames_delayed |
477 << " frames_skipped:" << frames_skipped; | 477 << " frames_skipped:" << frames_skipped; |
478 | 478 |
479 // Update the audio-delay measurement, inform about the number of skipped | 479 // Update the audio-delay measurement, inform about the number of skipped |
480 // frames, and ask client to render audio. Since |output_bus_| is wrapping | 480 // frames, and ask client to render audio. Since |output_bus_| is wrapping |
481 // the shared memory the Render() call is writing directly into the shared | 481 // the shared memory the Render() call is writing directly into the shared |
482 // memory. | 482 // memory. |
483 render_callback_->Render(delay, delay_timestamp, frames_skipped, | 483 render_callback_->Render(output_bus_.get(), std::round(frames_delayed), |
484 output_bus_.get()); | 484 frames_skipped); |
485 } | 485 } |
486 | 486 |
487 bool AudioOutputDevice::AudioThreadCallback:: | 487 bool AudioOutputDevice::AudioThreadCallback:: |
488 CurrentThreadIsAudioDeviceThread() { | 488 CurrentThreadIsAudioDeviceThread() { |
489 return thread_checker_.CalledOnValidThread(); | 489 return thread_checker_.CalledOnValidThread(); |
490 } | 490 } |
491 | 491 |
492 } // namespace media | 492 } // namespace media |
OLD | NEW |