| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/fake_audio_consumer.h" | 5 #include "media/audio/fake_audio_consumer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 FakeAudioConsumer::~FakeAudioConsumer() { | 27 FakeAudioConsumer::~FakeAudioConsumer() { |
| 28 DCHECK(read_cb_.is_null()); | 28 DCHECK(read_cb_.is_null()); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void FakeAudioConsumer::Start(const ReadCB& read_cb) { | 31 void FakeAudioConsumer::Start(const ReadCB& read_cb) { |
| 32 DCHECK(message_loop_->BelongsToCurrentThread()); | 32 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 33 DCHECK(read_cb_.is_null()); | 33 DCHECK(read_cb_.is_null()); |
| 34 DCHECK(!read_cb.is_null()); | 34 DCHECK(!read_cb.is_null()); |
| 35 read_cb_ = read_cb; | 35 read_cb_ = read_cb; |
| 36 next_read_time_ = base::Time::Now(); | 36 next_read_time_ = base::TimeTicks::Now(); |
| 37 read_task_cb_.Reset(base::Bind( | 37 read_task_cb_.Reset(base::Bind( |
| 38 &FakeAudioConsumer::DoRead, base::Unretained(this))); | 38 &FakeAudioConsumer::DoRead, base::Unretained(this))); |
| 39 message_loop_->PostTask(FROM_HERE, read_task_cb_.callback()); | 39 message_loop_->PostTask(FROM_HERE, read_task_cb_.callback()); |
| 40 } | 40 } |
| 41 | 41 |
| 42 void FakeAudioConsumer::Stop() { | 42 void FakeAudioConsumer::Stop() { |
| 43 DCHECK(message_loop_->BelongsToCurrentThread()); | 43 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 44 read_cb_.Reset(); | 44 read_cb_.Reset(); |
| 45 read_task_cb_.Cancel(); | 45 read_task_cb_.Cancel(); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void FakeAudioConsumer::DoRead() { | 48 void FakeAudioConsumer::DoRead() { |
| 49 DCHECK(message_loop_->BelongsToCurrentThread()); | 49 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 50 DCHECK(!read_cb_.is_null()); | 50 DCHECK(!read_cb_.is_null()); |
| 51 | 51 |
| 52 read_cb_.Run(audio_bus_.get()); | 52 read_cb_.Run(audio_bus_.get()); |
| 53 | 53 |
| 54 // Need to account for time spent here due to the cost of |read_cb_| as well | 54 // Need to account for time spent here due to the cost of |read_cb_| as well |
| 55 // as the imprecision of PostDelayedTask(). | 55 // as the imprecision of PostDelayedTask(). |
| 56 base::Time now = base::Time::Now(); | 56 const base::TimeTicks now = base::TimeTicks::Now(); |
| 57 base::TimeDelta delay = next_read_time_ + buffer_duration_ - now; | 57 base::TimeDelta delay = next_read_time_ + buffer_duration_ - now; |
| 58 | 58 |
| 59 // If we're behind, find the next nearest ontime interval. | 59 // If we're behind, find the next nearest ontime interval. |
| 60 if (delay < base::TimeDelta()) | 60 if (delay < base::TimeDelta()) |
| 61 delay += buffer_duration_ * (-delay / buffer_duration_ + 1); | 61 delay += buffer_duration_ * (-delay / buffer_duration_ + 1); |
| 62 next_read_time_ = now + delay; | 62 next_read_time_ = now + delay; |
| 63 | 63 |
| 64 message_loop_->PostDelayedTask(FROM_HERE, read_task_cb_.callback(), delay); | 64 message_loop_->PostDelayedTask(FROM_HERE, read_task_cb_.callback(), delay); |
| 65 } | 65 } |
| 66 | 66 |
| 67 } // namespace media | 67 } // namespace media |
| OLD | NEW |