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 28 matching lines...) Expand all Loading... | |
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(uint32_t pending_data) 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 bytes_per_frame_; | 49 const int frame_rate_; |
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 bytes_per_frame_(audio_parameters.GetBytesPerFrame()), | 434 frame_rate_(audio_parameters.sample_rate()), |
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(uint32_t pending_data) { | 454 void AudioOutputDevice::AudioThreadCallback::Process(uint32_t pending_data) { |
miu
2016/11/21 20:30:53
Did you mean to check |pending_data| here and retu
Mikhail
2016/11/22 12:10:40
That is done at 'AudioDeviceThread::ThreadMain()'
| |
455 // Convert the number of pending bytes in the render buffer into frames. | |
456 double frames_delayed = static_cast<double>(pending_data) / bytes_per_frame_; | |
457 | |
458 callback_num_++; | 455 callback_num_++; |
459 TRACE_EVENT1("audio", "AudioOutputDevice::FireRenderCallback", | 456 TRACE_EVENT1("audio", "AudioOutputDevice::FireRenderCallback", |
460 "callback_num", callback_num_); | 457 "callback_num", callback_num_); |
461 | 458 |
462 // When playback starts, we get an immediate callback to Process to make sure | 459 // When playback starts, we get an immediate callback to Process to make sure |
463 // that we have some data, we'll get another one after the device is awake and | 460 // that we have some data, we'll get another one after the device is awake and |
464 // ingesting data, which is what we want to track with this trace. | 461 // ingesting data, which is what we want to track with this trace. |
465 if (callback_num_ == 2) { | 462 if (callback_num_ == 2) { |
466 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this); | 463 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this); |
467 } | 464 } |
468 | 465 |
469 // Read and reset the number of frames skipped. | 466 // Read and reset the number of frames skipped. |
470 AudioOutputBuffer* buffer = | 467 AudioOutputBuffer* buffer = |
471 reinterpret_cast<AudioOutputBuffer*>(shared_memory_.memory()); | 468 reinterpret_cast<AudioOutputBuffer*>(shared_memory_.memory()); |
472 uint32_t frames_skipped = buffer->params.frames_skipped; | 469 uint32_t frames_skipped = buffer->params.frames_skipped; |
473 buffer->params.frames_skipped = 0; | 470 buffer->params.frames_skipped = 0; |
474 | 471 |
475 DVLOG(4) << __func__ << " pending_data:" << pending_data | 472 base::TimeDelta delay = |
476 << " frames_delayed(pre-round):" << frames_delayed | 473 base::TimeDelta::FromMicroseconds(buffer->params.delay); |
474 buffer->params.delay = 0; | |
475 | |
476 base::TimeTicks delay_timestamp = | |
477 base::TimeTicks() + | |
478 base::TimeDelta::FromMicroseconds(buffer->params.delay_timestamp); | |
479 buffer->params.delay_timestamp = 0; | |
480 | |
481 DVLOG(4) << __func__ << " delay:" << delay << " delay_timestamp:" << delay | |
477 << " frames_skipped:" << frames_skipped; | 482 << " frames_skipped:" << frames_skipped; |
478 | 483 |
479 // Update the audio-delay measurement, inform about the number of skipped | 484 // Update the audio-delay measurement, inform about the number of skipped |
480 // frames, and ask client to render audio. Since |output_bus_| is wrapping | 485 // 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 | 486 // the shared memory the Render() call is writing directly into the shared |
482 // memory. | 487 // memory. |
483 render_callback_->Render(output_bus_.get(), std::round(frames_delayed), | 488 render_callback_->Render(delay, delay_timestamp, frames_skipped, |
484 frames_skipped); | 489 output_bus_.get()); |
485 } | 490 } |
486 | 491 |
487 bool AudioOutputDevice::AudioThreadCallback:: | 492 bool AudioOutputDevice::AudioThreadCallback:: |
488 CurrentThreadIsAudioDeviceThread() { | 493 CurrentThreadIsAudioDeviceThread() { |
489 return thread_checker_.CalledOnValidThread(); | 494 return thread_checker_.CalledOnValidThread(); |
490 } | 495 } |
491 | 496 |
492 } // namespace media | 497 } // namespace media |
OLD | NEW |