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

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

Issue 10915123: Adds media::AudioPullFifo class to Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: AudioFIFO changes based on comments from Andrew Created 8 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "base/bind.h"
6 #include "base/bind_helpers.h"
7 #include "media/base/audio_pull_fifo.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace media {
11
12 // Block diagram of a possible real-world usage:
13 //
14 // | Provider | ----> | AudioPullFifo | ----> | Consumer |
15 // push pull
16 // 2048 ----> (4096) ----> ~441 [#frames]
17
18 // Number of channels in each audio bus.
19 static int kChannels = 2;
20
21 // Max number of audio framed the FIFO can contain.
22 static const int kMaxFramesInFifo = 4096;
23
24 // The provider will be asked to provide audio frames of this size by the
25 // consumer.
26 static const int kReadFramesFromProvider = 2048;
27
28 // Max number of frames the consumer can ask for.
29 static const int kMaxFramesToConsume = 496;
30
31 // List of |frames_to_consume| which will be used as input parameter to
32 // AudioPullFifo::Consume() when the consumer asks for data.
33 // Sum(kConsumeFrames)=5733 and Mean(kConsumeFrames)=441, hence the provider
34 // will be called three times in this example and provide in total 6144 audio
35 // frames.
36 static const int kConsumeFrames[] =
37 {440, 441, 442, 440, 440, 441, 442, 440, 441, 442, 441, 441, 442};
38
39 class AudioPullFifoTest : public testing::Test {
40 public:
41 AudioPullFifoTest()
42 : audio_bus_(AudioBus::Create(kChannels, kMaxFramesToConsume)),
43 fill_value_(0) {}
44 ~AudioPullFifoTest() {}
scherkus (not reviewing) 2012/09/07 14:19:03 virtual
henrika (OOO until Aug 14) 2012/09/10 07:23:04 Done.
45
46 void VerifyValue(const float data[], int size, float start_value) {
47 float value = start_value;
48 for (int i = 0; i < size; ++i) {
49 ASSERT_FLOAT_EQ(value++, data[i]) << "i=" << i;
50 }
51 }
52
53 // AudioPullFifo::ReadCB implementation where we increase a value for each
54 // audio frame that we provide. Note that all channels are given the same
55 // value to simplify the verification.
56 virtual void ProvideInput(AudioBus* audio_bus) {
57 EXPECT_EQ(audio_bus->channels(), audio_bus_->channels());
58 EXPECT_EQ(audio_bus->frames(), kReadFramesFromProvider);
59 for (int i = 0; i < audio_bus->frames(); ++i) {
60 for (int j = 0; j < audio_bus->channels(); ++j) {
61 // Store same value in all channels.
62 audio_bus->channel(j)[i] = fill_value_;
63 }
64 fill_value_++;
65 }
66 }
67
68 protected:
69 scoped_ptr<AudioBus> audio_bus_;
70 int fill_value_;
71
72 DISALLOW_COPY_AND_ASSIGN(AudioPullFifoTest);
73 };
74
75 // Emulate a scenario where the consumer ask for data using the sizes in
76 // kConsumeFrames at each call. The provider is asked to provide input when
77 // the FIFO can't fulfill the request and kReadFramesFromProvider will be
78 // provided in each callback. The provider updates and stores a data value for
79 // each audio frame it provides which makes it possible to verify that the
80 // consumer reads out correct values.
81 TEST_F(AudioPullFifoTest, Consume) {
82 AudioPullFifo pull_fifo(kChannels, kMaxFramesInFifo, base::Bind(
83 &AudioPullFifoTest::ProvideInput, base::Unretained(this)),
84 kReadFramesFromProvider);
85
86 // Consume data using different sizes, acquire audio frames from the FIFO
87 // and verify that the retrieved values matches the values written by the
88 // provider.
89 int start_value = 0;
90 for (int i = 0; i < sizeof(kConsumeFrames) / sizeof(kConsumeFrames[0]); ++i) {
91 int frames_to_consume = kConsumeFrames[i];
92 pull_fifo.Consume(audio_bus_.get(), frames_to_consume);
93 for (int j = 0; j < kChannels; ++j) {
94 VerifyValue(audio_bus_->channel(j), frames_to_consume, start_value);
95 }
96 start_value += frames_to_consume;
97 }
98 }
99
100 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698