Index: media/base/audio_block_fifo_unittest.cc |
diff --git a/media/base/audio_block_fifo_unittest.cc b/media/base/audio_block_fifo_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..794bab010268dfc88235a617361f8f3a6eeac5d6 |
--- /dev/null |
+++ b/media/base/audio_block_fifo_unittest.cc |
@@ -0,0 +1,155 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/base/audio_block_fifo.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace media { |
+ |
+class AudioBlockFifoTest : public testing::Test { |
+ public: |
+ AudioBlockFifoTest() {} |
+ virtual ~AudioBlockFifoTest() {} |
+ |
+ void PushAndVerify(AudioBlockFifo* fifo, int frames_to_push, |
+ int channels, int block_frames, int max_frames) { |
+ const int bytes_per_sample = 2; |
DaleCurtis
2014/07/14 20:14:27
Global static const?
no longer working on chromium
2014/07/15 21:43:13
It is only used in this function, there is no need
|
+ const int data_byte_size = bytes_per_sample * channels * frames_to_push; |
+ scoped_ptr<uint8[]> data(new uint8[data_byte_size]); |
+ memset(data.get(), 0, data_byte_size); |
+ |
+ for (int filled_frames =max_frames - fifo->unfilled_frames(); |
DaleCurtis
2014/07/14 20:14:27
formatting.
no longer working on chromium
2014/07/15 21:43:13
Done.
|
+ filled_frames + frames_to_push <= max_frames;) { |
+ fifo->Push(data.get(), frames_to_push, bytes_per_sample); |
+ filled_frames += frames_to_push; |
+ EXPECT_EQ(fifo->unfilled_frames(), max_frames - filled_frames); |
DaleCurtis
2014/07/14 20:14:27
expect_eq order is swapped (expected, actual).
no longer working on chromium
2014/07/15 21:43:13
Done.
|
+ EXPECT_EQ(fifo->available_blocks(), |
DaleCurtis
2014/07/14 20:14:27
Ditto.
no longer working on chromium
2014/07/15 21:43:13
Done.
|
+ static_cast<int>(filled_frames / block_frames)); |
+ } |
+ } |
+ |
+ protected: |
DaleCurtis
2014/07/14 20:14:27
must be under private.
no longer working on chromium
2014/07/15 21:43:13
Done.
|
+ DISALLOW_COPY_AND_ASSIGN(AudioBlockFifoTest); |
+}; |
+ |
+// Verify that construction works as intended. |
+TEST_F(AudioBlockFifoTest, Construct) { |
+ static const int kChannels = 6; |
DaleCurtis
2014/07/14 20:14:27
No need for static const within functions.
no longer working on chromium
2014/07/15 21:43:13
I changed it to const int in order to make it clea
|
+ static const int kFrames = 128; |
+ static const int kBlocks = 4; |
+ AudioBlockFifo fifo(kChannels, kFrames, kBlocks); |
+ EXPECT_EQ(fifo.available_blocks(), 0); |
+ EXPECT_EQ(fifo.unfilled_frames(), kFrames * kBlocks); |
+} |
+ |
+// Pushes audio bus objects to/from a FIFO up to different degrees. |
+TEST_F(AudioBlockFifoTest, Push) { |
+ static const int kChannels = 2; |
+ static const int kFrames = 128; |
+ static const int kBlocks = 2; |
+ AudioBlockFifo fifo(kChannels, kFrames, kBlocks); |
+ |
+ // Push kFrames / 2 of data until FIFO is full. |
+ PushAndVerify(&fifo, kFrames / 2, kChannels, kFrames, kFrames * kBlocks); |
+ fifo.Clear(); |
+ |
+ // Push kFrames of data until FIFO is full. |
+ PushAndVerify(&fifo, kFrames, kChannels, kFrames, kFrames * kBlocks); |
+ fifo.Clear(); |
+ |
+ // Push 1.5 * kFrames of data. |
+ PushAndVerify(&fifo, kFrames * 1.5, kChannels, kFrames, kFrames * kBlocks); |
+ fifo.Clear(); |
+} |
+ |
+// Perform a sequence of Push/Consume calls to different degrees, and verify |
+// things are correct. |
+TEST_F(AudioBlockFifoTest, PushAndConsume) { |
+ static const int kChannels = 2; |
+ static const int kFrames = 441; |
+ static const int kBlocks = 4; |
+ AudioBlockFifo fifo(kChannels, kFrames, kBlocks); |
+ PushAndVerify(&fifo, kFrames, kChannels, kFrames, kFrames * kBlocks); |
+ EXPECT_TRUE(fifo.unfilled_frames() == 0); |
+ EXPECT_TRUE(fifo.available_blocks() == kBlocks); |
+ |
+ { |
DaleCurtis
2014/07/14 20:14:27
Seems unnecessary to block these?
no longer working on chromium
2014/07/15 21:43:13
It is simply a way to help understanding the tests
|
+ // Consume 1 block of data. |
+ const AudioBus* bus = fifo.Consume(); |
+ EXPECT_TRUE(kChannels == bus->channels()); |
+ EXPECT_TRUE(kFrames == bus->frames()); |
+ EXPECT_TRUE(fifo.available_blocks() == (kBlocks - 1)); |
+ EXPECT_TRUE(fifo.unfilled_frames() == kFrames); |
+ } |
+ |
+ { |
+ // Fill it up again. |
+ PushAndVerify(&fifo, kFrames, kChannels, kFrames, kFrames * kBlocks); |
+ EXPECT_TRUE(fifo.unfilled_frames() == 0); |
+ EXPECT_TRUE(fifo.available_blocks() == kBlocks); |
+ |
+ // Consume all blocks of data. |
+ for (int i = 1; i <= kBlocks; ++i) { |
+ const AudioBus* bus = fifo.Consume(); |
+ EXPECT_TRUE(kChannels == bus->channels()); |
+ EXPECT_TRUE(kFrames == bus->frames()); |
+ EXPECT_TRUE(fifo.unfilled_frames() == kFrames * i); |
+ EXPECT_TRUE(fifo.available_blocks() == (kBlocks - i)); |
+ } |
+ EXPECT_TRUE(fifo.unfilled_frames() == kFrames * kBlocks); |
+ EXPECT_TRUE(fifo.available_blocks() == 0); |
+ } |
+ |
+ { |
+ fifo.Clear(); |
+ int new_push_frames = 128; |
+ // Change the input frame and try to fill up the FIFO. |
+ PushAndVerify(&fifo, new_push_frames, kChannels, kFrames, |
+ kFrames * kBlocks); |
+ EXPECT_TRUE(fifo.unfilled_frames() != 0); |
+ EXPECT_TRUE(fifo.available_blocks() == kBlocks -1); |
+ |
+ // Consume all the existing filled blocks of data. |
+ while (fifo.available_blocks()) { |
+ const AudioBus* bus = fifo.Consume(); |
+ EXPECT_TRUE(kChannels == bus->channels()); |
+ EXPECT_TRUE(kFrames == bus->frames()); |
+ } |
+ |
+ // Since one block of FIFO has not been completely filled up, there should |
+ // be remaining frames. |
+ const int number_of_push = |
+ static_cast<int>(kFrames * kBlocks / new_push_frames); |
+ const int remain_frames = kFrames * kBlocks - fifo.unfilled_frames(); |
+ EXPECT_EQ(remain_frames, |
+ number_of_push * new_push_frames - kFrames * (kBlocks - 1)); |
+ |
+ // Completely fill up the buffer again. |
+ new_push_frames = kFrames * kBlocks - remain_frames; |
+ PushAndVerify(&fifo, new_push_frames, kChannels, kFrames, |
+ kFrames * kBlocks); |
+ EXPECT_TRUE(fifo.unfilled_frames() == 0); |
+ EXPECT_TRUE(fifo.available_blocks() == kBlocks); |
+ } |
+} |
+ |
+// Perform a sequence of Push/Consume calls to a 1 block FIFO. |
+TEST_F(AudioBlockFifoTest, PushAndConsumeOneBlockFifo) { |
+ static const int kChannels = 2; |
+ static const int kFrames = 441; |
+ static const int kBlocks = 1; |
+ AudioBlockFifo fifo(kChannels, kFrames, kBlocks); |
+ PushAndVerify(&fifo, kFrames, kChannels, kFrames, kFrames * kBlocks); |
+ EXPECT_TRUE(fifo.unfilled_frames() == 0); |
+ EXPECT_TRUE(fifo.available_blocks() == kBlocks); |
+ |
+ // Consume 1 block of data. |
+ const AudioBus* bus = fifo.Consume(); |
+ EXPECT_TRUE(kChannels == bus->channels()); |
+ EXPECT_TRUE(kFrames == bus->frames()); |
+ EXPECT_TRUE(fifo.available_blocks() == 0); |
+ EXPECT_TRUE(fifo.unfilled_frames() == kFrames); |
+} |
+ |
+} // namespace media |