| 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..2ade6714b2f25c6b93ed37dda360f0ecb36eaba6
|
| --- /dev/null
|
| +++ b/media/base/audio_block_fifo_unittest.cc
|
| @@ -0,0 +1,137 @@
|
| +// 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;
|
| + 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->empty_frames();
|
| + 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->empty_frames(), max_frames - filled_frames);
|
| + EXPECT_EQ(fifo->filled_blocks(),
|
| + static_cast<int>(filled_frames / block_frames));
|
| + }
|
| + }
|
| +
|
| + protected:
|
| + DISALLOW_COPY_AND_ASSIGN(AudioBlockFifoTest);
|
| +};
|
| +
|
| +// Verify that construction works as intended.
|
| +TEST_F(AudioBlockFifoTest, Construct) {
|
| + static const int kChannels = 6;
|
| + static const int kFrames = 128;
|
| + static const int kBlocks = 4;
|
| + AudioBlockFifo fifo(kChannels, kFrames, kBlocks);
|
| + EXPECT_EQ(fifo.filled_blocks(), 0);
|
| + EXPECT_EQ(fifo.empty_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.empty_frames() == 0);
|
| + EXPECT_TRUE(fifo.filled_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.filled_blocks() == (kBlocks - 1));
|
| + EXPECT_TRUE(fifo.empty_frames() == kFrames);
|
| + }
|
| +
|
| + {
|
| + // Fill it up again.
|
| + PushAndVerify(&fifo, kFrames, kChannels, kFrames, kFrames * kBlocks);
|
| + EXPECT_TRUE(fifo.empty_frames() == 0);
|
| + EXPECT_TRUE(fifo.filled_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.empty_frames() == kFrames * i);
|
| + EXPECT_TRUE(fifo.filled_blocks() == (kBlocks - i));
|
| + }
|
| + EXPECT_TRUE(fifo.empty_frames() == kFrames * kBlocks);
|
| + EXPECT_TRUE(fifo.filled_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.empty_frames() != 0);
|
| + EXPECT_TRUE(fifo.filled_blocks() == kBlocks -1);
|
| +
|
| + // Consume all the existing filled blocks of data.
|
| + while (fifo.filled_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.empty_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.empty_frames() == 0);
|
| + EXPECT_TRUE(fifo.filled_blocks() == kBlocks);
|
| + }
|
| +}
|
| +
|
| +} // namespace media
|
|
|