Chromium Code Reviews| 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 return audio_hash_->ToString(); | |
|
wolenetz
2015/07/29 21:57:27
nit: Is this for test only (I think so)? If not, t
DaleCurtis
2015/07/30 01:28:14
It is only for testing, but I can't add ForTesting
wolenetz
2015/08/06 22:21:27
Acknowledged.
| |
| 44 } | |
| 45 | |
| 37 private: | 46 private: |
| 38 // Call Render() repeatedly, keeping track of the rendering time. | 47 // Call Render() repeatedly, keeping track of the rendering time. |
| 39 void Run() override { | 48 void Run() override { |
| 40 base::TimeTicks start; | 49 base::TimeTicks start; |
| 41 while (!stop_event_->IsSignaled()) { | 50 while (!stop_event_->IsSignaled()) { |
| 42 int frames_received = callback_->Render(audio_bus_.get(), 0); | 51 int frames_received = callback_->Render(audio_bus_.get(), 0); |
| 52 if (audio_hash_) | |
| 53 audio_hash_->Update(audio_bus_.get(), frames_received); | |
|
wolenetz
2015/07/29 21:57:27
hmm. Could frames_received indeed be negative (see
DaleCurtis
2015/07/30 01:28:14
No, it's always >= 0. I've added a DCHECK and fixe
wolenetz
2015/08/06 22:21:27
Acknowledged.
| |
| 43 if (frames_received <= 0) { | 54 if (frames_received <= 0) { |
| 44 // No data received, so let other threads run to provide data. | 55 // No data received, so let other threads run to provide data. |
| 45 base::PlatformThread::YieldCurrentThread(); | 56 base::PlatformThread::YieldCurrentThread(); |
| 46 } else if (start.is_null()) { | 57 } else if (start.is_null()) { |
| 47 // First time we processed some audio, so record the starting time. | 58 // First time we processed some audio, so record the starting time. |
| 48 start = base::TimeTicks::Now(); | 59 start = base::TimeTicks::Now(); |
| 49 } else { | 60 } else { |
| 50 // Keep track of the last time data was rendered. | 61 // Keep track of the last time data was rendered. |
| 51 playback_time_ = base::TimeTicks::Now() - start; | 62 playback_time_ = base::TimeTicks::Now() - start; |
| 52 } | 63 } |
| 53 } | 64 } |
| 54 } | 65 } |
| 55 | 66 |
| 56 AudioRendererSink::RenderCallback* callback_; | 67 AudioRendererSink::RenderCallback* callback_; |
| 57 scoped_ptr<AudioBus> audio_bus_; | 68 scoped_ptr<AudioBus> audio_bus_; |
| 58 scoped_ptr<base::WaitableEvent> stop_event_; | 69 scoped_ptr<base::WaitableEvent> stop_event_; |
| 59 scoped_ptr<base::DelegateSimpleThread> thread_; | 70 scoped_ptr<base::DelegateSimpleThread> thread_; |
| 60 base::TimeDelta playback_time_; | 71 base::TimeDelta playback_time_; |
| 72 scoped_ptr<AudioHash> audio_hash_; | |
| 61 }; | 73 }; |
| 62 | 74 |
| 63 ClocklessAudioSink::ClocklessAudioSink() | 75 ClocklessAudioSink::ClocklessAudioSink() |
| 64 : initialized_(false), | 76 : initialized_(false), playing_(false), hashing_(false) {} |
| 65 playing_(false) {} | |
| 66 | 77 |
| 67 ClocklessAudioSink::~ClocklessAudioSink() {} | 78 ClocklessAudioSink::~ClocklessAudioSink() {} |
| 68 | 79 |
| 69 void ClocklessAudioSink::Initialize(const AudioParameters& params, | 80 void ClocklessAudioSink::Initialize(const AudioParameters& params, |
| 70 RenderCallback* callback) { | 81 RenderCallback* callback) { |
| 71 DCHECK(!initialized_); | 82 DCHECK(!initialized_); |
| 72 thread_.reset(new ClocklessAudioSinkThread(params, callback)); | 83 thread_.reset(new ClocklessAudioSinkThread(params, callback, hashing_)); |
| 73 initialized_ = true; | 84 initialized_ = true; |
| 74 } | 85 } |
| 75 | 86 |
| 76 void ClocklessAudioSink::Start() { | 87 void ClocklessAudioSink::Start() { |
| 77 DCHECK(initialized_); | 88 DCHECK(initialized_); |
| 78 DCHECK(!playing_); | 89 DCHECK(!playing_); |
| 79 } | 90 } |
| 80 | 91 |
| 81 void ClocklessAudioSink::Stop() { | 92 void ClocklessAudioSink::Stop() { |
| 82 if (initialized_) | 93 if (initialized_) |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 108 return volume == 0.0; | 119 return volume == 0.0; |
| 109 } | 120 } |
| 110 | 121 |
| 111 void ClocklessAudioSink::SwitchOutputDevice( | 122 void ClocklessAudioSink::SwitchOutputDevice( |
| 112 const std::string& device_id, | 123 const std::string& device_id, |
| 113 const GURL& security_origin, | 124 const GURL& security_origin, |
| 114 const SwitchOutputDeviceCB& callback) { | 125 const SwitchOutputDeviceCB& callback) { |
| 115 callback.Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED); | 126 callback.Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED); |
| 116 } | 127 } |
| 117 | 128 |
| 129 void ClocklessAudioSink::StartAudioHashForTesting() { | |
| 130 DCHECK(!initialized_); | |
| 131 hashing_ = true; | |
| 132 } | |
| 133 | |
| 134 std::string ClocklessAudioSink::GetAudioHashForTesting() { | |
| 135 return thread_ && hashing_ ? thread_->GetAudioHash() : std::string(); | |
| 136 } | |
| 137 | |
| 118 } // namespace media | 138 } // namespace media |
| OLD | NEW |