diff options
Diffstat (limited to 'media')
-rw-r--r-- | media/base/buffer_queue.cc | 8 | ||||
-rw-r--r-- | media/base/buffer_queue.h | 5 | ||||
-rw-r--r-- | media/base/buffer_queue_unittest.cc | 41 | ||||
-rw-r--r-- | media/filters/audio_renderer_algorithm_base.cc | 4 | ||||
-rw-r--r-- | media/filters/audio_renderer_algorithm_base.h | 3 |
5 files changed, 61 insertions, 0 deletions
diff --git a/media/base/buffer_queue.cc b/media/base/buffer_queue.cc index 18b30d50..f78fb1d 100644 --- a/media/base/buffer_queue.cc +++ b/media/base/buffer_queue.cc @@ -80,6 +80,14 @@ void BufferQueue::Enqueue(Buffer* buffer_in) { size_in_bytes_ += buffer_in->GetDataSize(); } +base::TimeDelta BufferQueue::GetTime(double bytes_to_sec) { + double bytes_to_usec = bytes_to_sec * base::Time::kMicrosecondsPerSecond; + + return queue_.front()->GetTimestamp() + + base::TimeDelta::FromMicroseconds(static_cast<int64>( + data_offset_ * bytes_to_usec)); +} + void BufferQueue::Clear() { queue_.clear(); size_in_bytes_ = 0; diff --git a/media/base/buffer_queue.h b/media/base/buffer_queue.h index 363b136..39e1976 100644 --- a/media/base/buffer_queue.h +++ b/media/base/buffer_queue.h @@ -14,6 +14,7 @@ #include <deque> #include "base/ref_counted.h" +#include "base/time.h" namespace media { @@ -38,6 +39,10 @@ class BufferQueue { // Enqueues |buffer_in| and adds a reference. void Enqueue(Buffer* buffer_in); + // Returns the timestamp of the first buffer plus |data_offset_| in + // microseconds, calculated using the conversion |bytes_to_sec|. + base::TimeDelta GetTime(double bytes_to_sec); + // Returns true if the |queue_| is empty. bool IsEmpty(); diff --git a/media/base/buffer_queue_unittest.cc b/media/base/buffer_queue_unittest.cc index 7f64b43..91e10e0 100644 --- a/media/base/buffer_queue_unittest.cc +++ b/media/base/buffer_queue_unittest.cc @@ -4,6 +4,7 @@ #include "base/scoped_ptr.h" #include "base/string_util.h" +#include "base/time.h" #include "media/base/buffer_queue.h" #include "media/base/data_buffer.h" #include "testing/gtest/include/gtest/gtest.h" @@ -123,4 +124,44 @@ TEST_F(BufferQueueTest, CopyFromMiddleOfBuffer) { EXPECT_EQ(0, memcmp(dataBig, kData, kDataSize)); } +TEST_F(BufferQueueTest, GetTime) { + const struct { + int64 first_time_seconds; + size_t consume_bytes; + double bytes_to_seconds; + } tests[] = { + { 0, 0, 0.5 }, + { 0, 0, 1 }, + { 0, 0, 1.75 }, + { 0, 4, 0.5 }, + { 0, 4, 1 }, + { 0, 4, 1.75 }, + { 5, 0, 0.5 }, + { 5, 0, 1 }, + { 5, 0, 1.75 }, + { 5, 4, 0.5 }, + { 5, 4, 1 }, + { 5, 4, 1.75 }, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { + buffer2->SetTimestamp(base::TimeDelta::FromSeconds( + tests[i].first_time_seconds)); + queue_.Enqueue(buffer2.get()); + queue_.Consume(tests[i].consume_bytes); + + int64 expected = base::TimeDelta::FromSeconds(static_cast<int64>( + tests[i].first_time_seconds + (tests[i].consume_bytes * + tests[i].bytes_to_seconds))).ToInternalValue(); + + int64 actual = queue_.GetTime(tests[i].bytes_to_seconds).ToInternalValue(); + + EXPECT_EQ(expected, actual) << "With test = {" + << tests[i].first_time_seconds << ", " << tests[i].consume_bytes + << ", " << tests[i].bytes_to_seconds << "}\n"; + + queue_.Clear(); + } +} + } // namespace media diff --git a/media/filters/audio_renderer_algorithm_base.cc b/media/filters/audio_renderer_algorithm_base.cc index ef6c0ec..8e64656 100644 --- a/media/filters/audio_renderer_algorithm_base.cc +++ b/media/filters/audio_renderer_algorithm_base.cc @@ -54,6 +54,10 @@ void AudioRendererAlgorithmBase::FlushBuffers() { request_read_callback_->Run(); } +base::TimeDelta AudioRendererAlgorithmBase::GetTime() { + return queue_.GetTime(1.0 / (channels() * sample_rate() * sample_bytes())); +} + void AudioRendererAlgorithmBase::EnqueueBuffer(Buffer* buffer_in) { // If we're at end of stream, |buffer_in| contains no data. if (!buffer_in->IsEndOfStream()) diff --git a/media/filters/audio_renderer_algorithm_base.h b/media/filters/audio_renderer_algorithm_base.h index 3fc8fde..9449c25 100644 --- a/media/filters/audio_renderer_algorithm_base.h +++ b/media/filters/audio_renderer_algorithm_base.h @@ -58,6 +58,9 @@ class AudioRendererAlgorithmBase { // Clears |queue_|. virtual void FlushBuffers(); + // Returns the time of the next byte in our data. + virtual base::TimeDelta GetTime(); + // Enqueues a buffer. It is called from the owner of the algorithm after a // read completes. virtual void EnqueueBuffer(Buffer* buffer_in); |