Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: media/base/audio_block_fifo_unittest.cc

Issue 557693003: Dynamically allocate more memory to AudioBlockFifo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed the comments and added unittests. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/time/time.h"
6 #include "media/audio/audio_power_monitor.h"
5 #include "media/base/audio_block_fifo.h" 7 #include "media/base/audio_block_fifo.h"
6 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
7 9
8 namespace media { 10 namespace media {
9 11
10 class AudioBlockFifoTest : public testing::Test { 12 class AudioBlockFifoTest : public testing::Test {
11 public: 13 public:
12 AudioBlockFifoTest() {} 14 AudioBlockFifoTest() {}
13 virtual ~AudioBlockFifoTest() {} 15 virtual ~AudioBlockFifoTest() {}
14 16
15 void PushAndVerify(AudioBlockFifo* fifo, int frames_to_push, 17 void PushAndVerify(AudioBlockFifo* fifo, int frames_to_push,
16 int channels, int block_frames, int max_frames) { 18 int channels, int block_frames, int max_frames) {
17 const int bytes_per_sample = 2; 19 const int bytes_per_sample = 2;
18 const int data_byte_size = bytes_per_sample * channels * frames_to_push; 20 const int data_byte_size = bytes_per_sample * channels * frames_to_push;
19 scoped_ptr<uint8[]> data(new uint8[data_byte_size]); 21 scoped_ptr<uint8[]> data(new uint8[data_byte_size]);
20 memset(data.get(), 0, data_byte_size); 22 memset(data.get(), 1, data_byte_size);
21 23
22 for (int filled_frames = max_frames - fifo->GetUnfilledFrames(); 24 for (int filled_frames = max_frames - fifo->GetUnfilledFrames();
23 filled_frames + frames_to_push <= max_frames;) { 25 filled_frames + frames_to_push <= max_frames;) {
24 fifo->Push(data.get(), frames_to_push, bytes_per_sample); 26 fifo->Push(data.get(), frames_to_push, bytes_per_sample);
25 filled_frames += frames_to_push; 27 filled_frames += frames_to_push;
26 EXPECT_EQ(max_frames - filled_frames, fifo->GetUnfilledFrames()); 28 EXPECT_EQ(max_frames - filled_frames, fifo->GetUnfilledFrames());
27 EXPECT_EQ(static_cast<int>(filled_frames / block_frames), 29 EXPECT_EQ(static_cast<int>(filled_frames / block_frames),
28 fifo->available_blocks()); 30 fifo->available_blocks());
29 } 31 }
30 } 32 }
31 33
34 void ConsumeAndVerify(AudioBlockFifo* fifo, int expected_unfilled_frames,
35 int expected_available_blocks) {
36 const AudioBus* bus = fifo->Consume();
37 EXPECT_EQ(fifo->GetUnfilledFrames(), expected_unfilled_frames);
38 EXPECT_EQ(fifo->available_blocks(), expected_available_blocks);
39 const int dummy_sample_rate = 44100;
40 AudioPowerMonitor audio_level(dummy_sample_rate,
DaleCurtis 2014/09/16 19:36:53 This is kind of confusing, I'd just check for non-
no longer working on chromium 2014/09/18 12:42:49 Done, now it only checks the value.
41 base::TimeDelta::FromMilliseconds(10));
42 audio_level.Scan(*bus, bus->frames());
43 std::pair<float, bool> power = audio_level.ReadCurrentPowerAndClip();
44 EXPECT_NE(power.first, AudioPowerMonitor::zero_power());
45 }
46
32 private: 47 private:
33 DISALLOW_COPY_AND_ASSIGN(AudioBlockFifoTest); 48 DISALLOW_COPY_AND_ASSIGN(AudioBlockFifoTest);
34 }; 49 };
35 50
36 // Verify that construction works as intended. 51 // Verify that construction works as intended.
37 TEST_F(AudioBlockFifoTest, Construct) { 52 TEST_F(AudioBlockFifoTest, Construct) {
38 const int channels = 6; 53 const int channels = 6;
39 const int frames = 128; 54 const int frames = 128;
40 const int blocks = 4; 55 const int blocks = 4;
41 AudioBlockFifo fifo(channels, frames, blocks); 56 AudioBlockFifo fifo(channels, frames, blocks);
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 EXPECT_TRUE(fifo.available_blocks() == blocks); 154 EXPECT_TRUE(fifo.available_blocks() == blocks);
140 155
141 // Consume 1 block of data. 156 // Consume 1 block of data.
142 const AudioBus* bus = fifo.Consume(); 157 const AudioBus* bus = fifo.Consume();
143 EXPECT_TRUE(channels == bus->channels()); 158 EXPECT_TRUE(channels == bus->channels());
144 EXPECT_TRUE(frames == bus->frames()); 159 EXPECT_TRUE(frames == bus->frames());
145 EXPECT_TRUE(fifo.available_blocks() == 0); 160 EXPECT_TRUE(fifo.available_blocks() == 0);
146 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames); 161 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames);
147 } 162 }
148 163
164 // Dynamically increase the capacity of FIFO and verify buffers are correct.
165 TEST_F(AudioBlockFifoTest, DynamicallyIncreaseCapacity) {
166 // Create a FIFO with default blocks of buffers.
167 const int channels = 2;
168 const int frames = 441;
169 const int default_blocks = 2;
170 AudioBlockFifo fifo(channels, frames, default_blocks);
171 PushAndVerify(&fifo, frames, channels, frames, frames * default_blocks);
172 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0);
173 EXPECT_TRUE(fifo.available_blocks() == default_blocks);
174
175 // Increase the capacity dynamically for the first time.
176 const int new_blocks_1 = 3;
177 fifo.IncreaseCapacity(new_blocks_1);
178 int expected_unfilled_frames = new_blocks_1 * frames;
179 int expected_available_blocks = default_blocks;
180 EXPECT_EQ(fifo.GetUnfilledFrames(), expected_unfilled_frames);
181 EXPECT_EQ(fifo.available_blocks(), expected_available_blocks);
182
183 // Verify the previous buffer is not affected by the dynamic capacity
184 // increment.
185 expected_unfilled_frames = (new_blocks_1 + 1) * frames;
186 expected_available_blocks = default_blocks - 1;
187 ConsumeAndVerify(&fifo, expected_unfilled_frames, expected_available_blocks);
188
189 // Fill another |new_blocks_1 + 0.5| blocks of data to the FIFO.
190 const int frames_to_push = static_cast<int>((new_blocks_1 + 0.5) * frames);
191 int max_frames = frames * (default_blocks + new_blocks_1);
192 PushAndVerify(&fifo, frames_to_push, channels, frames, max_frames);
193 expected_unfilled_frames = max_frames - frames - frames_to_push;
194 expected_available_blocks = new_blocks_1 + 1;
195 EXPECT_EQ(fifo.GetUnfilledFrames(), expected_unfilled_frames);
196 EXPECT_EQ(fifo.available_blocks(), expected_available_blocks);
197
198 // Increase the capacity dynamically for the second time.
199 const int new_blocks_2 = 2;
200 fifo.IncreaseCapacity(new_blocks_2);
201 expected_unfilled_frames += new_blocks_2 * frames;
202 EXPECT_EQ(fifo.GetUnfilledFrames(), expected_unfilled_frames);
203 EXPECT_EQ(fifo.available_blocks(), expected_available_blocks);
204
205 // Verify the previous buffers are not affected by the dynamic capacity
206 // increment.
207 for (int i = 1; i <= expected_available_blocks; ++i) {
208 expected_unfilled_frames += frames;
209 expected_available_blocks -= 1;
210 ConsumeAndVerify(&fifo, expected_unfilled_frames,
211 expected_available_blocks);
212 }
213 }
214
149 } // namespace media 215 } // namespace media
OLDNEW
« media/base/audio_block_fifo.cc ('K') | « media/base/audio_block_fifo.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698