1 // Copyright 2013 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_buffer_queue.h"
6 
7 #include <stdint.h>
8 
9 #include <limits>
10 #include <memory>
11 
12 #include "base/time/time.h"
13 #include "media/base/audio_buffer.h"
14 #include "media/base/audio_bus.h"
15 #include "media/base/test_helpers.h"
16 #include "media/base/timestamp_constants.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 
19 namespace media {
20 
21 constexpr int kSampleRate = 44100;
22 
23 enum class ValueType { kNormal, kFloat };
VerifyBus(AudioBus * bus,int offset,int frames,int buffer_size,float start,float increment,ValueType type=ValueType::kNormal)24 static void VerifyBus(AudioBus* bus,
25                       int offset,
26                       int frames,
27                       int buffer_size,
28                       float start,
29                       float increment,
30                       ValueType type = ValueType::kNormal) {
31   for (int ch = 0; ch < bus->channels(); ++ch) {
32     const float v = start + ch * buffer_size * increment;
33     for (int i = offset; i < offset + frames; ++i) {
34       float expected_value = v + (i - offset) * increment;
35       if (type == ValueType::kFloat)
36         expected_value /= std::numeric_limits<uint16_t>::max();
37 
38       ASSERT_FLOAT_EQ(expected_value, bus->channel(ch)[i])
39           << "i=" << i << ", ch=" << ch;
40     }
41   }
42 }
43 
44 template <typename T>
MakeTestBuffer(SampleFormat format,ChannelLayout channel_layout,T start,T step,int frames)45 static scoped_refptr<AudioBuffer> MakeTestBuffer(SampleFormat format,
46                                                  ChannelLayout channel_layout,
47                                                  T start,
48                                                  T step,
49                                                  int frames) {
50   return MakeAudioBuffer<T>(format, channel_layout,
51                             ChannelLayoutToChannelCount(channel_layout),
52                             kSampleRate, start, step, frames, kNoTimestamp);
53 }
54 
TEST(AudioBufferQueueTest,AppendAndClear)55 TEST(AudioBufferQueueTest, AppendAndClear) {
56   const ChannelLayout channel_layout = CHANNEL_LAYOUT_MONO;
57   AudioBufferQueue buffer;
58   EXPECT_EQ(0, buffer.frames());
59   buffer.Append(
60       MakeTestBuffer<uint8_t>(kSampleFormatU8, channel_layout, 10, 1, 8));
61   EXPECT_EQ(8, buffer.frames());
62   buffer.Clear();
63   EXPECT_EQ(0, buffer.frames());
64   buffer.Append(
65       MakeTestBuffer<uint8_t>(kSampleFormatU8, channel_layout, 20, 1, 8));
66   EXPECT_EQ(8, buffer.frames());
67 }
68 
TEST(AudioBufferQueueTest,MultipleAppend)69 TEST(AudioBufferQueueTest, MultipleAppend) {
70   const ChannelLayout channel_layout = CHANNEL_LAYOUT_MONO;
71   AudioBufferQueue buffer;
72   buffer.Append(
73       MakeTestBuffer<uint8_t>(kSampleFormatU8, channel_layout, 10, 1, 8));
74   EXPECT_EQ(8, buffer.frames());
75   buffer.Append(
76       MakeTestBuffer<uint8_t>(kSampleFormatU8, channel_layout, 10, 1, 8));
77   EXPECT_EQ(16, buffer.frames());
78   buffer.Append(
79       MakeTestBuffer<uint8_t>(kSampleFormatU8, channel_layout, 10, 1, 8));
80   EXPECT_EQ(24, buffer.frames());
81   buffer.Append(
82       MakeTestBuffer<uint8_t>(kSampleFormatU8, channel_layout, 10, 1, 8));
83   EXPECT_EQ(32, buffer.frames());
84   buffer.Append(
85       MakeTestBuffer<uint8_t>(kSampleFormatU8, channel_layout, 10, 1, 8));
86   EXPECT_EQ(40, buffer.frames());
87 }
88 
TEST(AudioBufferQueueTest,IteratorCheck)89 TEST(AudioBufferQueueTest, IteratorCheck) {
90   const ChannelLayout channel_layout = CHANNEL_LAYOUT_MONO;
91   const int channels = ChannelLayoutToChannelCount(channel_layout);
92   AudioBufferQueue buffer;
93   std::unique_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
94 
95   // Append 40 frames in 5 buffers. Intersperse ReadFrames() to make the
96   // iterator is pointing to the correct position.
97   buffer.Append(MakeTestBuffer<float>(
98       kSampleFormatF32, channel_layout, 10.0f, 1.0f, 8));
99   EXPECT_EQ(8, buffer.frames());
100 
101   EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get()));
102   EXPECT_EQ(4, buffer.frames());
103   VerifyBus(bus.get(), 0, 4, bus->frames(), 10, 1, ValueType::kFloat);
104 
105   buffer.Append(MakeTestBuffer<float>(
106       kSampleFormatF32, channel_layout, 20.0f, 1.0f, 8));
107   EXPECT_EQ(12, buffer.frames());
108   buffer.Append(MakeTestBuffer<float>(
109       kSampleFormatF32, channel_layout, 30.0f, 1.0f, 8));
110   EXPECT_EQ(20, buffer.frames());
111 
112   buffer.SeekFrames(16);
113   EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get()));
114   EXPECT_EQ(0, buffer.frames());
115   VerifyBus(bus.get(), 0, 4, bus->frames(), 34, 1, ValueType::kFloat);
116 
117   buffer.Append(MakeTestBuffer<float>(
118       kSampleFormatF32, channel_layout, 40.0f, 1.0f, 8));
119   EXPECT_EQ(8, buffer.frames());
120   buffer.Append(MakeTestBuffer<float>(
121       kSampleFormatF32, channel_layout, 50.0f, 1.0f, 8));
122   EXPECT_EQ(16, buffer.frames());
123 
124   EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get()));
125   VerifyBus(bus.get(), 0, 4, bus->frames(), 40, 1, ValueType::kFloat);
126 
127   // Read off the end of the buffer.
128   EXPECT_EQ(12, buffer.frames());
129   buffer.SeekFrames(8);
130   EXPECT_EQ(4, buffer.ReadFrames(100, 0, bus.get()));
131   VerifyBus(bus.get(), 0, 4, bus->frames(), 54, 1, ValueType::kFloat);
132 }
133 
TEST(AudioBufferQueueTest,Seek)134 TEST(AudioBufferQueueTest, Seek) {
135   const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
136   AudioBufferQueue buffer;
137 
138   // Add 6 frames of data.
139   buffer.Append(MakeTestBuffer<float>(
140       kSampleFormatF32, channel_layout, 1.0f, 1.0f, 6));
141   EXPECT_EQ(6, buffer.frames());
142 
143   // Seek past 2 frames.
144   buffer.SeekFrames(2);
145   EXPECT_EQ(4, buffer.frames());
146 
147   // Seek to end of data.
148   buffer.SeekFrames(4);
149   EXPECT_EQ(0, buffer.frames());
150 
151   // At end, seek now fails unless 0 specified.
152   buffer.SeekFrames(0);
153 }
154 
TEST(AudioBufferQueueTest,ReadBitstream)155 TEST(AudioBufferQueueTest, ReadBitstream) {
156   const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
157   const int channels = ChannelLayoutToChannelCount(channel_layout);
158   AudioBufferQueue buffer;
159 
160   // Add 24 frames of data.
161   buffer.Append(MakeBitstreamAudioBuffer(kSampleFormatEac3, channel_layout,
162                                          channels, kSampleRate, 1, 1, 4, 2,
163                                          kNoTimestamp));
164   buffer.Append(MakeBitstreamAudioBuffer(kSampleFormatEac3, channel_layout,
165                                          channels, kSampleRate, 9, 1, 20, 10,
166                                          kNoTimestamp));
167   EXPECT_EQ(24, buffer.frames());
168 
169   // The first audio buffer contains 4 frames.
170   std::unique_ptr<AudioBus> bus = AudioBus::Create(channels, buffer.frames());
171   EXPECT_EQ(4, buffer.ReadFrames(buffer.frames(), 0, bus.get()));
172   VerifyBitstreamAudioBus(bus.get(), 2, 1, 1);
173 
174   // The second audio buffer contains 20 frames.
175   EXPECT_EQ(20, buffer.ReadFrames(buffer.frames(), 0, bus.get()));
176   VerifyBitstreamAudioBus(bus.get(), 10, 9, 1);
177 }
178 
TEST(AudioBufferQueueTest,ReadF32)179 TEST(AudioBufferQueueTest, ReadF32) {
180   const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
181   const int channels = ChannelLayoutToChannelCount(channel_layout);
182   AudioBufferQueue buffer;
183 
184   // Add 76 frames of data.
185   buffer.Append(
186       MakeTestBuffer<float>(kSampleFormatF32, channel_layout, 1.0f, 1.0f, 6));
187   buffer.Append(
188       MakeTestBuffer<float>(kSampleFormatF32, channel_layout, 13.0f, 1.0f, 10));
189   buffer.Append(
190       MakeTestBuffer<float>(kSampleFormatF32, channel_layout, 33.0f, 1.0f, 60));
191   EXPECT_EQ(76, buffer.frames());
192 
193   // Read 3 frames from the buffer.
194   std::unique_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
195   EXPECT_EQ(3, buffer.ReadFrames(3, 0, bus.get()));
196   EXPECT_EQ(73, buffer.frames());
197   VerifyBus(bus.get(), 0, 3, 6, 1, 1, ValueType::kFloat);
198 
199   // Now read 5 frames, which will span buffers. Append the data into AudioBus.
200   EXPECT_EQ(5, buffer.ReadFrames(5, 3, bus.get()));
201   EXPECT_EQ(68, buffer.frames());
202   VerifyBus(bus.get(), 0, 6, 6, 1, 1, ValueType::kFloat);
203   VerifyBus(bus.get(), 6, 2, 10, 13, 1, ValueType::kFloat);
204 
205   // Now skip into the third buffer.
206   buffer.SeekFrames(20);
207   EXPECT_EQ(48, buffer.frames());
208 
209   // Now read 2 frames, which are in the third buffer.
210   EXPECT_EQ(2, buffer.ReadFrames(2, 0, bus.get()));
211   VerifyBus(bus.get(), 0, 2, 60, 45, 1, ValueType::kFloat);
212 }
213 
TEST(AudioBufferQueueTest,ReadU8)214 TEST(AudioBufferQueueTest, ReadU8) {
215   const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0;
216   const int channels = ChannelLayoutToChannelCount(channel_layout);
217   const int frames = 4;
218   AudioBufferQueue buffer;
219 
220   // Add 4 frames of data.
221   buffer.Append(
222       MakeTestBuffer<uint8_t>(kSampleFormatU8, channel_layout, 128, 1, frames));
223 
224   // Read all 4 frames from the buffer.
225   std::unique_ptr<AudioBus> bus = AudioBus::Create(channels, frames);
226   EXPECT_EQ(frames, buffer.ReadFrames(frames, 0, bus.get()));
227   EXPECT_EQ(0, buffer.frames());
228   VerifyBus(bus.get(), 0, frames, bus->frames(), 0, 1.0f / 127.0f);
229 }
230 
TEST(AudioBufferQueueTest,ReadS16)231 TEST(AudioBufferQueueTest, ReadS16) {
232   const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
233   const int channels = ChannelLayoutToChannelCount(channel_layout);
234   AudioBufferQueue buffer;
235 
236   // Add 24 frames of data.
237   buffer.Append(
238       MakeTestBuffer<int16_t>(kSampleFormatS16, channel_layout, 1, 1, 4));
239   buffer.Append(
240       MakeTestBuffer<int16_t>(kSampleFormatS16, channel_layout, 9, 1, 20));
241   EXPECT_EQ(24, buffer.frames());
242 
243   // Read 6 frames from the buffer.
244   const int frames = 6;
245   std::unique_ptr<AudioBus> bus = AudioBus::Create(channels, buffer.frames());
246   EXPECT_EQ(frames, buffer.ReadFrames(frames, 0, bus.get()));
247   EXPECT_EQ(18, buffer.frames());
248   VerifyBus(bus.get(), 0, 4, 4, 1.0f / std::numeric_limits<int16_t>::max(),
249             1.0f / std::numeric_limits<int16_t>::max());
250   VerifyBus(bus.get(), 4, 2, 20, 9.0f / std::numeric_limits<int16_t>::max(),
251             1.0f / std::numeric_limits<int16_t>::max());
252 }
253 
TEST(AudioBufferQueueTest,ReadS32)254 TEST(AudioBufferQueueTest, ReadS32) {
255   const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
256   const int channels = ChannelLayoutToChannelCount(channel_layout);
257   AudioBufferQueue buffer;
258 
259   // Add 24 frames of data.
260   buffer.Append(
261       MakeTestBuffer<int32_t>(kSampleFormatS32, channel_layout, 1, 1, 4));
262   buffer.Append(
263       MakeTestBuffer<int32_t>(kSampleFormatS32, channel_layout, 9, 1, 20));
264   EXPECT_EQ(24, buffer.frames());
265 
266   // Read 6 frames from the buffer.
267   std::unique_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
268   EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get()));
269   EXPECT_EQ(18, buffer.frames());
270   VerifyBus(bus.get(), 0, 4, 4, 1.0f / std::numeric_limits<int32_t>::max(),
271             1.0f / std::numeric_limits<int32_t>::max());
272   VerifyBus(bus.get(), 4, 2, 20, 9.0f / std::numeric_limits<int32_t>::max(),
273             1.0f / std::numeric_limits<int32_t>::max());
274 
275   // Read the next 2 frames.
276   EXPECT_EQ(2, buffer.ReadFrames(2, 0, bus.get()));
277   EXPECT_EQ(16, buffer.frames());
278   VerifyBus(bus.get(), 0, 2, 20, 11.0f / std::numeric_limits<int32_t>::max(),
279             1.0f / std::numeric_limits<int32_t>::max());
280 }
281 
TEST(AudioBufferQueueTest,ReadF32Planar)282 TEST(AudioBufferQueueTest, ReadF32Planar) {
283   const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
284   const int channels = ChannelLayoutToChannelCount(channel_layout);
285   AudioBufferQueue buffer;
286 
287   // Add 14 frames of data.
288   buffer.Append(MakeTestBuffer<float>(
289       kSampleFormatPlanarF32, channel_layout, 1.0f, 1.0f, 4));
290   buffer.Append(MakeTestBuffer<float>(
291       kSampleFormatPlanarF32, channel_layout, 50.0f, 1.0f, 10));
292   EXPECT_EQ(14, buffer.frames());
293 
294   // Read 6 frames from the buffer.
295   std::unique_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
296   EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get()));
297   EXPECT_EQ(8, buffer.frames());
298   VerifyBus(bus.get(), 0, 4, 4, 1, 1, ValueType::kFloat);
299   VerifyBus(bus.get(), 4, 2, 10, 50, 1, ValueType::kFloat);
300 }
301 
TEST(AudioBufferQueueTest,ReadS16Planar)302 TEST(AudioBufferQueueTest, ReadS16Planar) {
303   const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
304   const int channels = ChannelLayoutToChannelCount(channel_layout);
305   AudioBufferQueue buffer;
306 
307   // Add 24 frames of data.
308   buffer.Append(
309       MakeTestBuffer<int16_t>(kSampleFormatPlanarS16, channel_layout, 1, 1, 4));
310   buffer.Append(MakeTestBuffer<int16_t>(kSampleFormatPlanarS16, channel_layout,
311                                         5, 1, 20));
312   EXPECT_EQ(24, buffer.frames());
313 
314   // Read 6 frames from the buffer.
315   std::unique_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
316   EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get()));
317   EXPECT_EQ(18, buffer.frames());
318   VerifyBus(bus.get(), 0, 4, 4, 1.0f / std::numeric_limits<int16_t>::max(),
319             1.0f / std::numeric_limits<int16_t>::max());
320   VerifyBus(bus.get(), 4, 2, 20, 5.0f / std::numeric_limits<int16_t>::max(),
321             1.0f / std::numeric_limits<int16_t>::max());
322 }
323 
TEST(AudioBufferQueueTest,ReadManyChannels)324 TEST(AudioBufferQueueTest, ReadManyChannels) {
325   const ChannelLayout channel_layout = CHANNEL_LAYOUT_OCTAGONAL;
326   const int channels = ChannelLayoutToChannelCount(channel_layout);
327   AudioBufferQueue buffer;
328 
329   // Add 76 frames of data.
330   buffer.Append(
331       MakeTestBuffer<float>(kSampleFormatF32, channel_layout, 0.0f, 1.0f, 6));
332   buffer.Append(MakeTestBuffer<float>(
333       kSampleFormatF32, channel_layout, 6.0f * channels, 1.0f, 10));
334   buffer.Append(MakeTestBuffer<float>(
335       kSampleFormatF32, channel_layout, 16.0f * channels, 1.0f, 60));
336   EXPECT_EQ(76, buffer.frames());
337 
338   // Read 3 frames from the buffer.
339   std::unique_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
340   EXPECT_EQ(30, buffer.ReadFrames(30, 0, bus.get()));
341   EXPECT_EQ(46, buffer.frames());
342   VerifyBus(bus.get(), 0, 6, 6, 0, 1, ValueType::kFloat);
343   VerifyBus(bus.get(), 6, 10, 10, 6 * channels, 1, ValueType::kFloat);
344   VerifyBus(bus.get(), 16, 14, 60, 16 * channels, 1, ValueType::kFloat);
345 }
346 
TEST(AudioBufferQueueTest,Peek)347 TEST(AudioBufferQueueTest, Peek) {
348   const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0;
349   const int channels = ChannelLayoutToChannelCount(channel_layout);
350   AudioBufferQueue buffer;
351 
352   // Add 60 frames of data.
353   const int frames = 60;
354   buffer.Append(MakeTestBuffer<float>(
355       kSampleFormatF32, channel_layout, 0.0f, 1.0f, frames));
356   EXPECT_EQ(frames, buffer.frames());
357 
358   // Peek at the first 30 frames.
359   std::unique_ptr<AudioBus> bus1 = AudioBus::Create(channels, frames);
360   EXPECT_EQ(frames, buffer.frames());
361   EXPECT_EQ(frames, buffer.PeekFrames(60, 0, 0, bus1.get()));
362   EXPECT_EQ(30, buffer.PeekFrames(30, 0, 0, bus1.get()));
363   EXPECT_EQ(frames, buffer.frames());
364   VerifyBus(bus1.get(), 0, 30, bus1->frames(), 0, 1, ValueType::kFloat);
365 
366   // Now read the next 30 frames (which should be the same as those peeked at).
367   std::unique_ptr<AudioBus> bus2 = AudioBus::Create(channels, frames);
368   EXPECT_EQ(30, buffer.ReadFrames(30, 0, bus2.get()));
369   VerifyBus(bus2.get(), 0, 30, bus2->frames(), 0, 1, ValueType::kFloat);
370 
371   // Peek 10 frames forward
372   bus1->Zero();
373   EXPECT_EQ(5, buffer.PeekFrames(5, 10, 0, bus1.get()));
374   VerifyBus(bus1.get(), 0, 5, bus1->frames(), 40, 1, ValueType::kFloat);
375 
376   // Peek to the end of the buffer.
377   EXPECT_EQ(30, buffer.frames());
378   EXPECT_EQ(30, buffer.PeekFrames(60, 0, 0, bus1.get()));
379   EXPECT_EQ(30, buffer.PeekFrames(30, 0, 0, bus1.get()));
380 }
381 
382 }  // namespace media
383