Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "media/base/audio_block_fifo.h" | |
| 6 #include "testing/gtest/include/gtest/gtest.h" | |
| 7 | |
| 8 namespace media { | |
| 9 | |
| 10 class AudioBlockFifoTest : public testing::Test { | |
| 11 public: | |
| 12 AudioBlockFifoTest() {} | |
| 13 virtual ~AudioBlockFifoTest() {} | |
| 14 | |
| 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; | |
|
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
| |
| 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); | |
| 21 | |
| 22 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.
| |
| 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(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.
| |
| 27 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.
| |
| 28 static_cast<int>(filled_frames / block_frames)); | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 protected: | |
|
DaleCurtis
2014/07/14 20:14:27
must be under private.
no longer working on chromium
2014/07/15 21:43:13
Done.
| |
| 33 DISALLOW_COPY_AND_ASSIGN(AudioBlockFifoTest); | |
| 34 }; | |
| 35 | |
| 36 // Verify that construction works as intended. | |
| 37 TEST_F(AudioBlockFifoTest, Construct) { | |
| 38 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
| |
| 39 static const int kFrames = 128; | |
| 40 static const int kBlocks = 4; | |
| 41 AudioBlockFifo fifo(kChannels, kFrames, kBlocks); | |
| 42 EXPECT_EQ(fifo.available_blocks(), 0); | |
| 43 EXPECT_EQ(fifo.unfilled_frames(), kFrames * kBlocks); | |
| 44 } | |
| 45 | |
| 46 // Pushes audio bus objects to/from a FIFO up to different degrees. | |
| 47 TEST_F(AudioBlockFifoTest, Push) { | |
| 48 static const int kChannels = 2; | |
| 49 static const int kFrames = 128; | |
| 50 static const int kBlocks = 2; | |
| 51 AudioBlockFifo fifo(kChannels, kFrames, kBlocks); | |
| 52 | |
| 53 // Push kFrames / 2 of data until FIFO is full. | |
| 54 PushAndVerify(&fifo, kFrames / 2, kChannels, kFrames, kFrames * kBlocks); | |
| 55 fifo.Clear(); | |
| 56 | |
| 57 // Push kFrames of data until FIFO is full. | |
| 58 PushAndVerify(&fifo, kFrames, kChannels, kFrames, kFrames * kBlocks); | |
| 59 fifo.Clear(); | |
| 60 | |
| 61 // Push 1.5 * kFrames of data. | |
| 62 PushAndVerify(&fifo, kFrames * 1.5, kChannels, kFrames, kFrames * kBlocks); | |
| 63 fifo.Clear(); | |
| 64 } | |
| 65 | |
| 66 // Perform a sequence of Push/Consume calls to different degrees, and verify | |
| 67 // things are correct. | |
| 68 TEST_F(AudioBlockFifoTest, PushAndConsume) { | |
| 69 static const int kChannels = 2; | |
| 70 static const int kFrames = 441; | |
| 71 static const int kBlocks = 4; | |
| 72 AudioBlockFifo fifo(kChannels, kFrames, kBlocks); | |
| 73 PushAndVerify(&fifo, kFrames, kChannels, kFrames, kFrames * kBlocks); | |
| 74 EXPECT_TRUE(fifo.unfilled_frames() == 0); | |
| 75 EXPECT_TRUE(fifo.available_blocks() == kBlocks); | |
| 76 | |
| 77 { | |
|
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
| |
| 78 // Consume 1 block of data. | |
| 79 const AudioBus* bus = fifo.Consume(); | |
| 80 EXPECT_TRUE(kChannels == bus->channels()); | |
| 81 EXPECT_TRUE(kFrames == bus->frames()); | |
| 82 EXPECT_TRUE(fifo.available_blocks() == (kBlocks - 1)); | |
| 83 EXPECT_TRUE(fifo.unfilled_frames() == kFrames); | |
| 84 } | |
| 85 | |
| 86 { | |
| 87 // Fill it up again. | |
| 88 PushAndVerify(&fifo, kFrames, kChannels, kFrames, kFrames * kBlocks); | |
| 89 EXPECT_TRUE(fifo.unfilled_frames() == 0); | |
| 90 EXPECT_TRUE(fifo.available_blocks() == kBlocks); | |
| 91 | |
| 92 // Consume all blocks of data. | |
| 93 for (int i = 1; i <= kBlocks; ++i) { | |
| 94 const AudioBus* bus = fifo.Consume(); | |
| 95 EXPECT_TRUE(kChannels == bus->channels()); | |
| 96 EXPECT_TRUE(kFrames == bus->frames()); | |
| 97 EXPECT_TRUE(fifo.unfilled_frames() == kFrames * i); | |
| 98 EXPECT_TRUE(fifo.available_blocks() == (kBlocks - i)); | |
| 99 } | |
| 100 EXPECT_TRUE(fifo.unfilled_frames() == kFrames * kBlocks); | |
| 101 EXPECT_TRUE(fifo.available_blocks() == 0); | |
| 102 } | |
| 103 | |
| 104 { | |
| 105 fifo.Clear(); | |
| 106 int new_push_frames = 128; | |
| 107 // Change the input frame and try to fill up the FIFO. | |
| 108 PushAndVerify(&fifo, new_push_frames, kChannels, kFrames, | |
| 109 kFrames * kBlocks); | |
| 110 EXPECT_TRUE(fifo.unfilled_frames() != 0); | |
| 111 EXPECT_TRUE(fifo.available_blocks() == kBlocks -1); | |
| 112 | |
| 113 // Consume all the existing filled blocks of data. | |
| 114 while (fifo.available_blocks()) { | |
| 115 const AudioBus* bus = fifo.Consume(); | |
| 116 EXPECT_TRUE(kChannels == bus->channels()); | |
| 117 EXPECT_TRUE(kFrames == bus->frames()); | |
| 118 } | |
| 119 | |
| 120 // Since one block of FIFO has not been completely filled up, there should | |
| 121 // be remaining frames. | |
| 122 const int number_of_push = | |
| 123 static_cast<int>(kFrames * kBlocks / new_push_frames); | |
| 124 const int remain_frames = kFrames * kBlocks - fifo.unfilled_frames(); | |
| 125 EXPECT_EQ(remain_frames, | |
| 126 number_of_push * new_push_frames - kFrames * (kBlocks - 1)); | |
| 127 | |
| 128 // Completely fill up the buffer again. | |
| 129 new_push_frames = kFrames * kBlocks - remain_frames; | |
| 130 PushAndVerify(&fifo, new_push_frames, kChannels, kFrames, | |
| 131 kFrames * kBlocks); | |
| 132 EXPECT_TRUE(fifo.unfilled_frames() == 0); | |
| 133 EXPECT_TRUE(fifo.available_blocks() == kBlocks); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 // Perform a sequence of Push/Consume calls to a 1 block FIFO. | |
| 138 TEST_F(AudioBlockFifoTest, PushAndConsumeOneBlockFifo) { | |
| 139 static const int kChannels = 2; | |
| 140 static const int kFrames = 441; | |
| 141 static const int kBlocks = 1; | |
| 142 AudioBlockFifo fifo(kChannels, kFrames, kBlocks); | |
| 143 PushAndVerify(&fifo, kFrames, kChannels, kFrames, kFrames * kBlocks); | |
| 144 EXPECT_TRUE(fifo.unfilled_frames() == 0); | |
| 145 EXPECT_TRUE(fifo.available_blocks() == kBlocks); | |
| 146 | |
| 147 // Consume 1 block of data. | |
| 148 const AudioBus* bus = fifo.Consume(); | |
| 149 EXPECT_TRUE(kChannels == bus->channels()); | |
| 150 EXPECT_TRUE(kFrames == bus->frames()); | |
| 151 EXPECT_TRUE(fifo.available_blocks() == 0); | |
| 152 EXPECT_TRUE(fifo.unfilled_frames() == kFrames); | |
| 153 } | |
| 154 | |
| 155 } // namespace media | |
| OLD | NEW |