1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/base/audio_block_fifo.h"
6 #include "testing/gtest/include/gtest/gtest.h"
10 class AudioBlockFifoTest : public testing::Test {
12 AudioBlockFifoTest() {}
13 virtual ~AudioBlockFifoTest() {}
15 void PushAndVerify(AudioBlockFifo* fifo, int frames_to_push,
16 int channels, int block_frames, int max_frames) {
17 const int bytes_per_sample = 2;
18 const int data_byte_size = bytes_per_sample * channels * frames_to_push;
19 scoped_ptr<uint8[]> data(new uint8[data_byte_size]);
20 memset(data.get(), 0, data_byte_size);
22 for (int filled_frames = max_frames - fifo->GetUnfilledFrames();
23 filled_frames + frames_to_push <= max_frames;) {
24 fifo->Push(data.get(), frames_to_push, bytes_per_sample);
25 filled_frames += frames_to_push;
26 EXPECT_EQ(max_frames - filled_frames, fifo->GetUnfilledFrames());
27 EXPECT_EQ(static_cast<int>(filled_frames / block_frames),
28 fifo->available_blocks());
33 DISALLOW_COPY_AND_ASSIGN(AudioBlockFifoTest);
36 // Verify that construction works as intended.
37 TEST_F(AudioBlockFifoTest, Construct) {
38 const int channels = 6;
39 const int frames = 128;
41 AudioBlockFifo fifo(channels, frames, blocks);
42 EXPECT_EQ(0, fifo.available_blocks());
43 EXPECT_EQ(frames * blocks, fifo.GetUnfilledFrames());
46 // Pushes audio bus objects to/from a FIFO up to different degrees.
47 TEST_F(AudioBlockFifoTest, Push) {
48 const int channels = 2;
49 const int frames = 128;
51 AudioBlockFifo fifo(channels, frames, blocks);
53 // Push frames / 2 of data until FIFO is full.
54 PushAndVerify(&fifo, frames / 2, channels, frames, frames * blocks);
57 // Push frames of data until FIFO is full.
58 PushAndVerify(&fifo, frames, channels, frames, frames * blocks);
61 // Push 1.5 * frames of data.
62 PushAndVerify(&fifo, frames * 1.5, channels, frames, frames * blocks);
66 // Perform a sequence of Push/Consume calls to different degrees, and verify
67 // things are correct.
68 TEST_F(AudioBlockFifoTest, PushAndConsume) {
69 const int channels = 2;
70 const int frames = 441;
72 AudioBlockFifo fifo(channels, frames, blocks);
73 PushAndVerify(&fifo, frames, channels, frames, frames * blocks);
74 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0);
75 EXPECT_TRUE(fifo.available_blocks() == blocks);
77 // Consume 1 block of data.
78 const AudioBus* bus = fifo.Consume();
79 EXPECT_TRUE(channels == bus->channels());
80 EXPECT_TRUE(frames == bus->frames());
81 EXPECT_TRUE(fifo.available_blocks() == (blocks - 1));
82 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames);
85 PushAndVerify(&fifo, frames, channels, frames, frames * blocks);
86 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0);
87 EXPECT_TRUE(fifo.available_blocks() == blocks);
89 // Consume all blocks of data.
90 for (int i = 1; i <= blocks; ++i) {
91 const AudioBus* bus = fifo.Consume();
92 EXPECT_TRUE(channels == bus->channels());
93 EXPECT_TRUE(frames == bus->frames());
94 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames * i);
95 EXPECT_TRUE(fifo.available_blocks() == (blocks - i));
97 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames * blocks);
98 EXPECT_TRUE(fifo.available_blocks() == 0);
101 int new_push_frames = 128;
102 // Change the input frame and try to fill up the FIFO.
103 PushAndVerify(&fifo, new_push_frames, channels, frames,
105 EXPECT_TRUE(fifo.GetUnfilledFrames() != 0);
106 EXPECT_TRUE(fifo.available_blocks() == blocks -1);
108 // Consume all the existing filled blocks of data.
109 while (fifo.available_blocks()) {
110 const AudioBus* bus = fifo.Consume();
111 EXPECT_TRUE(channels == bus->channels());
112 EXPECT_TRUE(frames == bus->frames());
115 // Since one block of FIFO has not been completely filled up, there should
116 // be remaining frames.
117 const int number_of_push =
118 static_cast<int>(frames * blocks / new_push_frames);
119 const int remain_frames = frames * blocks - fifo.GetUnfilledFrames();
120 EXPECT_EQ(number_of_push * new_push_frames - frames * (blocks - 1),
123 // Completely fill up the buffer again.
124 new_push_frames = frames * blocks - remain_frames;
125 PushAndVerify(&fifo, new_push_frames, channels, frames,
127 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0);
128 EXPECT_TRUE(fifo.available_blocks() == blocks);
131 // Perform a sequence of Push/Consume calls to a 1 block FIFO.
132 TEST_F(AudioBlockFifoTest, PushAndConsumeOneBlockFifo) {
133 static const int channels = 2;
134 static const int frames = 441;
135 static const int blocks = 1;
136 AudioBlockFifo fifo(channels, frames, blocks);
137 PushAndVerify(&fifo, frames, channels, frames, frames * blocks);
138 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0);
139 EXPECT_TRUE(fifo.available_blocks() == blocks);
141 // Consume 1 block of data.
142 const AudioBus* bus = fifo.Consume();
143 EXPECT_TRUE(channels == bus->channels());
144 EXPECT_TRUE(frames == bus->frames());
145 EXPECT_TRUE(fifo.available_blocks() == 0);
146 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames);