| 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/clockless_audio_sink.h" | 5 #include "media/audio/clockless_audio_sink.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/threading/simple_thread.h" | 10 #include "base/threading/simple_thread.h" |
| 11 #include "media/base/audio_hash.h" |
| 11 | 12 |
| 12 namespace media { | 13 namespace media { |
| 13 | 14 |
| 14 // Internal to ClocklessAudioSink. Class is used to call Render() on a seperate | 15 // Internal to ClocklessAudioSink. Class is used to call Render() on a seperate |
| 15 // thread, running as fast as it can read the data. | 16 // thread, running as fast as it can read the data. |
| 16 class ClocklessAudioSinkThread : public base::DelegateSimpleThread::Delegate { | 17 class ClocklessAudioSinkThread : public base::DelegateSimpleThread::Delegate { |
| 17 public: | 18 public: |
| 18 explicit ClocklessAudioSinkThread(const AudioParameters& params, | 19 ClocklessAudioSinkThread(const AudioParameters& params, |
| 19 AudioRendererSink::RenderCallback* callback) | 20 AudioRendererSink::RenderCallback* callback, |
| 21 bool hashing) |
| 20 : callback_(callback), | 22 : callback_(callback), |
| 21 audio_bus_(AudioBus::Create(params)), | 23 audio_bus_(AudioBus::Create(params)), |
| 22 stop_event_(new base::WaitableEvent(false, false)) {} | 24 stop_event_(new base::WaitableEvent(false, false)) { |
| 25 if (hashing) |
| 26 audio_hash_.reset(new AudioHash()); |
| 27 } |
| 23 | 28 |
| 24 void Start() { | 29 void Start() { |
| 25 stop_event_->Reset(); | 30 stop_event_->Reset(); |
| 26 thread_.reset(new base::DelegateSimpleThread(this, "ClocklessAudioSink")); | 31 thread_.reset(new base::DelegateSimpleThread(this, "ClocklessAudioSink")); |
| 27 thread_->Start(); | 32 thread_->Start(); |
| 28 } | 33 } |
| 29 | 34 |
| 30 // Generate a signal to stop calling Render(). | 35 // Generate a signal to stop calling Render(). |
| 31 base::TimeDelta Stop() { | 36 base::TimeDelta Stop() { |
| 32 stop_event_->Signal(); | 37 stop_event_->Signal(); |
| 33 thread_->Join(); | 38 thread_->Join(); |
| 34 return playback_time_; | 39 return playback_time_; |
| 35 } | 40 } |
| 36 | 41 |
| 42 std::string GetAudioHash() { |
| 43 DCHECK(audio_hash_); |
| 44 return audio_hash_->ToString(); |
| 45 } |
| 46 |
| 37 private: | 47 private: |
| 38 // Call Render() repeatedly, keeping track of the rendering time. | 48 // Call Render() repeatedly, keeping track of the rendering time. |
| 39 void Run() override { | 49 void Run() override { |
| 40 base::TimeTicks start; | 50 base::TimeTicks start; |
| 41 while (!stop_event_->IsSignaled()) { | 51 while (!stop_event_->IsSignaled()) { |
| 42 int frames_received = callback_->Render(audio_bus_.get(), 0); | 52 const int frames_received = callback_->Render(audio_bus_.get(), 0); |
| 43 if (frames_received <= 0) { | 53 DCHECK_GE(frames_received, 0); |
| 54 if (audio_hash_) |
| 55 audio_hash_->Update(audio_bus_.get(), frames_received); |
| 56 if (!frames_received) { |
| 44 // No data received, so let other threads run to provide data. | 57 // No data received, so let other threads run to provide data. |
| 45 base::PlatformThread::YieldCurrentThread(); | 58 base::PlatformThread::YieldCurrentThread(); |
| 46 } else if (start.is_null()) { | 59 } else if (start.is_null()) { |
| 47 // First time we processed some audio, so record the starting time. | 60 // First time we processed some audio, so record the starting time. |
| 48 start = base::TimeTicks::Now(); | 61 start = base::TimeTicks::Now(); |
| 49 } else { | 62 } else { |
| 50 // Keep track of the last time data was rendered. | 63 // Keep track of the last time data was rendered. |
| 51 playback_time_ = base::TimeTicks::Now() - start; | 64 playback_time_ = base::TimeTicks::Now() - start; |
| 52 } | 65 } |
| 53 } | 66 } |
| 54 } | 67 } |
| 55 | 68 |
| 56 AudioRendererSink::RenderCallback* callback_; | 69 AudioRendererSink::RenderCallback* callback_; |
| 57 scoped_ptr<AudioBus> audio_bus_; | 70 scoped_ptr<AudioBus> audio_bus_; |
| 58 scoped_ptr<base::WaitableEvent> stop_event_; | 71 scoped_ptr<base::WaitableEvent> stop_event_; |
| 59 scoped_ptr<base::DelegateSimpleThread> thread_; | 72 scoped_ptr<base::DelegateSimpleThread> thread_; |
| 60 base::TimeDelta playback_time_; | 73 base::TimeDelta playback_time_; |
| 74 scoped_ptr<AudioHash> audio_hash_; |
| 61 }; | 75 }; |
| 62 | 76 |
| 63 ClocklessAudioSink::ClocklessAudioSink() | 77 ClocklessAudioSink::ClocklessAudioSink() |
| 64 : initialized_(false), | 78 : initialized_(false), playing_(false), hashing_(false) {} |
| 65 playing_(false) {} | |
| 66 | 79 |
| 67 ClocklessAudioSink::~ClocklessAudioSink() {} | 80 ClocklessAudioSink::~ClocklessAudioSink() {} |
| 68 | 81 |
| 69 void ClocklessAudioSink::Initialize(const AudioParameters& params, | 82 void ClocklessAudioSink::Initialize(const AudioParameters& params, |
| 70 RenderCallback* callback) { | 83 RenderCallback* callback) { |
| 71 DCHECK(!initialized_); | 84 DCHECK(!initialized_); |
| 72 thread_.reset(new ClocklessAudioSinkThread(params, callback)); | 85 thread_.reset(new ClocklessAudioSinkThread(params, callback, hashing_)); |
| 73 initialized_ = true; | 86 initialized_ = true; |
| 74 } | 87 } |
| 75 | 88 |
| 76 void ClocklessAudioSink::Start() { | 89 void ClocklessAudioSink::Start() { |
| 77 DCHECK(initialized_); | 90 DCHECK(initialized_); |
| 78 DCHECK(!playing_); | 91 DCHECK(!playing_); |
| 79 } | 92 } |
| 80 | 93 |
| 81 void ClocklessAudioSink::Stop() { | 94 void ClocklessAudioSink::Stop() { |
| 82 if (initialized_) | 95 if (initialized_) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 105 | 118 |
| 106 bool ClocklessAudioSink::SetVolume(double volume) { | 119 bool ClocklessAudioSink::SetVolume(double volume) { |
| 107 // Audio is always muted. | 120 // Audio is always muted. |
| 108 return volume == 0.0; | 121 return volume == 0.0; |
| 109 } | 122 } |
| 110 | 123 |
| 111 OutputDevice* ClocklessAudioSink::GetOutputDevice() { | 124 OutputDevice* ClocklessAudioSink::GetOutputDevice() { |
| 112 return nullptr; | 125 return nullptr; |
| 113 } | 126 } |
| 114 | 127 |
| 128 void ClocklessAudioSink::StartAudioHashForTesting() { |
| 129 DCHECK(!initialized_); |
| 130 hashing_ = true; |
| 131 } |
| 132 |
| 133 std::string ClocklessAudioSink::GetAudioHashForTesting() { |
| 134 return thread_ && hashing_ ? thread_->GetAudioHash() : std::string(); |
| 135 } |
| 136 |
| 115 } // namespace media | 137 } // namespace media |
| OLD | NEW |