| 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/null_audio_sink.h" | 5 #include "media/audio/null_audio_sink.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
| 9 #include "media/audio/fake_audio_consumer.h" | 9 #include "media/audio/fake_audio_worker.h" |
| 10 #include "media/base/audio_hash.h" | 10 #include "media/base/audio_hash.h" |
| 11 | 11 |
| 12 namespace media { | 12 namespace media { |
| 13 | 13 |
| 14 NullAudioSink::NullAudioSink( | 14 NullAudioSink::NullAudioSink( |
| 15 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | 15 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 16 : initialized_(false), | 16 : initialized_(false), |
| 17 playing_(false), | 17 playing_(false), |
| 18 callback_(NULL), | 18 callback_(NULL), |
| 19 task_runner_(task_runner) { | 19 task_runner_(task_runner) { |
| 20 } | 20 } |
| 21 | 21 |
| 22 NullAudioSink::~NullAudioSink() {} | 22 NullAudioSink::~NullAudioSink() {} |
| 23 | 23 |
| 24 void NullAudioSink::Initialize(const AudioParameters& params, | 24 void NullAudioSink::Initialize(const AudioParameters& params, |
| 25 RenderCallback* callback) { | 25 RenderCallback* callback) { |
| 26 DCHECK(!initialized_); | 26 DCHECK(!initialized_); |
| 27 fake_consumer_.reset(new FakeAudioConsumer(task_runner_, params)); | 27 fake_worker_.reset(new FakeAudioWorker(task_runner_, params)); |
| 28 audio_bus_ = AudioBus::Create(params); |
| 28 callback_ = callback; | 29 callback_ = callback; |
| 29 initialized_ = true; | 30 initialized_ = true; |
| 30 } | 31 } |
| 31 | 32 |
| 32 void NullAudioSink::Start() { | 33 void NullAudioSink::Start() { |
| 33 DCHECK(task_runner_->BelongsToCurrentThread()); | 34 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 34 DCHECK(!playing_); | 35 DCHECK(!playing_); |
| 35 } | 36 } |
| 36 | 37 |
| 37 void NullAudioSink::Stop() { | 38 void NullAudioSink::Stop() { |
| 38 DCHECK(task_runner_->BelongsToCurrentThread()); | 39 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 39 | 40 |
| 40 // Stop may be called at any time, so we have to check before stopping. | 41 // Stop may be called at any time, so we have to check before stopping. |
| 41 if (fake_consumer_) | 42 if (fake_worker_) |
| 42 fake_consumer_->Stop(); | 43 fake_worker_->Stop(); |
| 43 } | 44 } |
| 44 | 45 |
| 45 void NullAudioSink::Play() { | 46 void NullAudioSink::Play() { |
| 46 DCHECK(task_runner_->BelongsToCurrentThread()); | 47 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 47 DCHECK(initialized_); | 48 DCHECK(initialized_); |
| 48 | 49 |
| 49 if (playing_) | 50 if (playing_) |
| 50 return; | 51 return; |
| 51 | 52 |
| 52 fake_consumer_->Start(base::Bind( | 53 fake_worker_->Start(base::Bind( |
| 53 &NullAudioSink::CallRender, base::Unretained(this))); | 54 &NullAudioSink::CallRender, base::Unretained(this))); |
| 54 playing_ = true; | 55 playing_ = true; |
| 55 } | 56 } |
| 56 | 57 |
| 57 void NullAudioSink::Pause() { | 58 void NullAudioSink::Pause() { |
| 58 DCHECK(task_runner_->BelongsToCurrentThread()); | 59 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 59 | 60 |
| 60 if (!playing_) | 61 if (!playing_) |
| 61 return; | 62 return; |
| 62 | 63 |
| 63 fake_consumer_->Stop(); | 64 fake_worker_->Stop(); |
| 64 playing_ = false; | 65 playing_ = false; |
| 65 } | 66 } |
| 66 | 67 |
| 67 bool NullAudioSink::SetVolume(double volume) { | 68 bool NullAudioSink::SetVolume(double volume) { |
| 68 // Audio is always muted. | 69 // Audio is always muted. |
| 69 return volume == 0.0; | 70 return volume == 0.0; |
| 70 } | 71 } |
| 71 | 72 |
| 72 void NullAudioSink::CallRender(AudioBus* audio_bus) { | 73 void NullAudioSink::CallRender() { |
| 73 DCHECK(task_runner_->BelongsToCurrentThread()); | 74 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 74 | 75 |
| 75 int frames_received = callback_->Render(audio_bus, 0); | 76 int frames_received = callback_->Render(audio_bus_.get(), 0); |
| 76 if (!audio_hash_ || frames_received <= 0) | 77 if (!audio_hash_ || frames_received <= 0) |
| 77 return; | 78 return; |
| 78 | 79 |
| 79 audio_hash_->Update(audio_bus, frames_received); | 80 audio_hash_->Update(audio_bus_.get(), frames_received); |
| 80 } | 81 } |
| 81 | 82 |
| 82 void NullAudioSink::StartAudioHashForTesting() { | 83 void NullAudioSink::StartAudioHashForTesting() { |
| 83 DCHECK(!initialized_); | 84 DCHECK(!initialized_); |
| 84 audio_hash_.reset(new AudioHash()); | 85 audio_hash_.reset(new AudioHash()); |
| 85 } | 86 } |
| 86 | 87 |
| 87 std::string NullAudioSink::GetAudioHashForTesting() { | 88 std::string NullAudioSink::GetAudioHashForTesting() { |
| 88 return audio_hash_ ? audio_hash_->ToString() : std::string(); | 89 return audio_hash_ ? audio_hash_->ToString() : std::string(); |
| 89 } | 90 } |
| 90 | 91 |
| 91 } // namespace media | 92 } // namespace media |
| OLD | NEW |