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 "base/basictypes.h" 6 #include "base/logging.h" 7 #include "base/memory/scoped_ptr.h" 8 #include "base/strings/stringprintf.h" 9 #include "base/time/time.h" 10 #include "media/base/audio_buffer.h" 11 #include "media/base/audio_buffer_queue.h" 12 #include "media/base/audio_bus.h" 13 #include "media/base/buffers.h" 14 #include "media/base/test_helpers.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 17 namespace media { 18 19 static void VerifyResult(float* channel_data, 20 int frames, 21 float start, 22 float increment) { 23 for (int i = 0; i < frames; ++i) { 24 SCOPED_TRACE(base::StringPrintf( 25 "i=%d/%d start=%f, increment=%f", i, frames, start, increment)); 26 ASSERT_EQ(start, channel_data[i]); 27 start += increment; 28 } 29 } 30 31 TEST(AudioBufferQueueTest, AppendAndClear) { 32 const int channels = 1; 33 const int frames = 8; 34 const base::TimeDelta kNoTime = kNoTimestamp(); 35 AudioBufferQueue buffer; 36 EXPECT_EQ(0, buffer.frames()); 37 buffer.Append(MakeInterleavedAudioBuffer<uint8>( 38 kSampleFormatU8, channels, 10, 1, frames, kNoTime, kNoTime)); 39 EXPECT_EQ(frames, buffer.frames()); 40 buffer.Clear(); 41 EXPECT_EQ(0, buffer.frames()); 42 buffer.Append(MakeInterleavedAudioBuffer<uint8>( 43 kSampleFormatU8, channels, 20, 1, frames, kNoTime, kNoTime)); 44 EXPECT_EQ(frames, buffer.frames()); 45 } 46 47 TEST(AudioBufferQueueTest, MultipleAppend) { 48 const int channels = 1; 49 const int frames = 8; 50 const base::TimeDelta kNoTime = kNoTimestamp(); 51 AudioBufferQueue buffer; 52 53 // Append 40 frames in 5 buffers. 54 buffer.Append(MakeInterleavedAudioBuffer<uint8>( 55 kSampleFormatU8, channels, 10, 1, frames, kNoTime, kNoTime)); 56 EXPECT_EQ(8, buffer.frames()); 57 buffer.Append(MakeInterleavedAudioBuffer<uint8>( 58 kSampleFormatU8, channels, 10, 1, frames, kNoTime, kNoTime)); 59 EXPECT_EQ(16, buffer.frames()); 60 buffer.Append(MakeInterleavedAudioBuffer<uint8>( 61 kSampleFormatU8, channels, 10, 1, frames, kNoTime, kNoTime)); 62 EXPECT_EQ(24, buffer.frames()); 63 buffer.Append(MakeInterleavedAudioBuffer<uint8>( 64 kSampleFormatU8, channels, 10, 1, frames, kNoTime, kNoTime)); 65 EXPECT_EQ(32, buffer.frames()); 66 buffer.Append(MakeInterleavedAudioBuffer<uint8>( 67 kSampleFormatU8, channels, 10, 1, frames, kNoTime, kNoTime)); 68 EXPECT_EQ(40, buffer.frames()); 69 } 70 71 TEST(AudioBufferQueueTest, IteratorCheck) { 72 const int channels = 1; 73 const int frames = 8; 74 const base::TimeDelta kNoTime = kNoTimestamp(); 75 AudioBufferQueue buffer; 76 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 77 78 // Append 40 frames in 5 buffers. Intersperse ReadFrames() to make the 79 // iterator is pointing to the correct position. 80 buffer.Append(MakeInterleavedAudioBuffer<float>( 81 kSampleFormatF32, channels, 10.0f, 1.0f, frames, kNoTime, kNoTime)); 82 EXPECT_EQ(8, buffer.frames()); 83 84 EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get())); 85 EXPECT_EQ(4, buffer.frames()); 86 VerifyResult(bus->channel(0), 4, 10.0f, 1.0f); 87 88 buffer.Append(MakeInterleavedAudioBuffer<float>( 89 kSampleFormatF32, channels, 20.0f, 1.0f, frames, kNoTime, kNoTime)); 90 EXPECT_EQ(12, buffer.frames()); 91 buffer.Append(MakeInterleavedAudioBuffer<float>( 92 kSampleFormatF32, channels, 30.0f, 1.0f, frames, kNoTime, kNoTime)); 93 EXPECT_EQ(20, buffer.frames()); 94 95 buffer.SeekFrames(16); 96 EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get())); 97 EXPECT_EQ(0, buffer.frames()); 98 VerifyResult(bus->channel(0), 4, 34.0f, 1.0f); 99 100 buffer.Append(MakeInterleavedAudioBuffer<float>( 101 kSampleFormatF32, channels, 40.0f, 1.0f, frames, kNoTime, kNoTime)); 102 EXPECT_EQ(8, buffer.frames()); 103 buffer.Append(MakeInterleavedAudioBuffer<float>( 104 kSampleFormatF32, channels, 50.0f, 1.0f, frames, kNoTime, kNoTime)); 105 EXPECT_EQ(16, buffer.frames()); 106 107 EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get())); 108 VerifyResult(bus->channel(0), 4, 40.0f, 1.0f); 109 110 // Read off the end of the buffer. 111 EXPECT_EQ(12, buffer.frames()); 112 buffer.SeekFrames(8); 113 EXPECT_EQ(4, buffer.ReadFrames(100, 0, bus.get())); 114 VerifyResult(bus->channel(0), 4, 54.0f, 1.0f); 115 } 116 117 TEST(AudioBufferQueueTest, Seek) { 118 const int channels = 2; 119 const int frames = 6; 120 const base::TimeDelta kNoTime = kNoTimestamp(); 121 AudioBufferQueue buffer; 122 123 // Add 6 frames of data. 124 buffer.Append(MakeInterleavedAudioBuffer<float>( 125 kSampleFormatF32, channels, 1.0f, 1.0f, frames, kNoTime, kNoTime)); 126 EXPECT_EQ(6, buffer.frames()); 127 128 // Seek past 2 frames. 129 buffer.SeekFrames(2); 130 EXPECT_EQ(4, buffer.frames()); 131 132 // Seek to end of data. 133 buffer.SeekFrames(4); 134 EXPECT_EQ(0, buffer.frames()); 135 136 // At end, seek now fails unless 0 specified. 137 buffer.SeekFrames(0); 138 } 139 140 TEST(AudioBufferQueueTest, ReadF32) { 141 const int channels = 2; 142 const base::TimeDelta kNoTime = kNoTimestamp(); 143 AudioBufferQueue buffer; 144 145 // Add 76 frames of data. 146 buffer.Append(MakeInterleavedAudioBuffer<float>( 147 kSampleFormatF32, channels, 1.0f, 1.0f, 6, kNoTime, kNoTime)); 148 buffer.Append(MakeInterleavedAudioBuffer<float>( 149 kSampleFormatF32, channels, 13.0f, 1.0f, 10, kNoTime, kNoTime)); 150 buffer.Append(MakeInterleavedAudioBuffer<float>( 151 kSampleFormatF32, channels, 33.0f, 1.0f, 60, kNoTime, kNoTime)); 152 EXPECT_EQ(76, buffer.frames()); 153 154 // Read 3 frames from the buffer. F32 is interleaved, so ch[0] should be 155 // 1, 3, 5, and ch[1] should be 2, 4, 6. 156 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 157 EXPECT_EQ(3, buffer.ReadFrames(3, 0, bus.get())); 158 EXPECT_EQ(73, buffer.frames()); 159 VerifyResult(bus->channel(0), 3, 1.0f, 2.0f); 160 VerifyResult(bus->channel(1), 3, 2.0f, 2.0f); 161 162 // Now read 5 frames, which will span buffers. Append the data into AudioBus. 163 EXPECT_EQ(5, buffer.ReadFrames(5, 3, bus.get())); 164 EXPECT_EQ(68, buffer.frames()); 165 VerifyResult(bus->channel(0), 8, 1.0f, 2.0f); 166 VerifyResult(bus->channel(1), 8, 2.0f, 2.0f); 167 168 // Now skip into the third buffer. 169 buffer.SeekFrames(20); 170 EXPECT_EQ(48, buffer.frames()); 171 172 // Now read 2 frames, which are in the third buffer. 173 EXPECT_EQ(2, buffer.ReadFrames(2, 0, bus.get())); 174 VerifyResult(bus->channel(0), 2, 57.0f, 2.0f); 175 VerifyResult(bus->channel(1), 2, 58.0f, 2.0f); 176 } 177 178 TEST(AudioBufferQueueTest, ReadU8) { 179 const int channels = 4; 180 const int frames = 4; 181 const base::TimeDelta kNoTime = kNoTimestamp(); 182 AudioBufferQueue buffer; 183 184 // Add 4 frames of data. 185 buffer.Append(MakeInterleavedAudioBuffer<uint8>( 186 kSampleFormatU8, channels, 128, 1, frames, kNoTime, kNoTime)); 187 188 // Read all 4 frames from the buffer. Data is interleaved, so ch[0] should be 189 // 128, 132, 136, 140, other channels similar. However, values are converted 190 // from [0, 255] to [-1.0, 1.0] with a bias of 128. Thus the first buffer 191 // value should be 0.0, then 1/127, 2/127, etc. 192 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 193 EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get())); 194 EXPECT_EQ(0, buffer.frames()); 195 VerifyResult(bus->channel(0), 4, 0.0f, 4.0f / 127.0f); 196 VerifyResult(bus->channel(1), 4, 1.0f / 127.0f, 4.0f / 127.0f); 197 VerifyResult(bus->channel(2), 4, 2.0f / 127.0f, 4.0f / 127.0f); 198 VerifyResult(bus->channel(3), 4, 3.0f / 127.0f, 4.0f / 127.0f); 199 } 200 201 TEST(AudioBufferQueueTest, ReadS16) { 202 const int channels = 2; 203 const base::TimeDelta kNoTime = kNoTimestamp(); 204 AudioBufferQueue buffer; 205 206 // Add 24 frames of data. 207 buffer.Append(MakeInterleavedAudioBuffer<int16>( 208 kSampleFormatS16, channels, 1, 1, 4, kNoTime, kNoTime)); 209 buffer.Append(MakeInterleavedAudioBuffer<int16>( 210 kSampleFormatS16, channels, 9, 1, 20, kNoTime, kNoTime)); 211 EXPECT_EQ(24, buffer.frames()); 212 213 // Read 6 frames from the buffer. Data is interleaved, so ch[0] should be 214 // 1, 3, 5, 7, 9, 11, and ch[1] should be 2, 4, 6, 8, 10, 12. 215 // Data is converted to float from -1.0 to 1.0 based on int16 range. 216 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 217 EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get())); 218 EXPECT_EQ(18, buffer.frames()); 219 VerifyResult(bus->channel(0), 6, 1.0f / kint16max, 2.0f / kint16max); 220 VerifyResult(bus->channel(1), 6, 2.0f / kint16max, 2.0f / kint16max); 221 } 222 223 TEST(AudioBufferQueueTest, ReadS32) { 224 const int channels = 2; 225 const base::TimeDelta kNoTime = kNoTimestamp(); 226 AudioBufferQueue buffer; 227 228 // Add 24 frames of data. 229 buffer.Append(MakeInterleavedAudioBuffer<int32>( 230 kSampleFormatS32, channels, 1, 1, 4, kNoTime, kNoTime)); 231 buffer.Append(MakeInterleavedAudioBuffer<int32>( 232 kSampleFormatS32, channels, 9, 1, 20, kNoTime, kNoTime)); 233 EXPECT_EQ(24, buffer.frames()); 234 235 // Read 6 frames from the buffer. Data is interleaved, so ch[0] should be 236 // 1, 3, 5, 7, 100, 106, and ch[1] should be 2, 4, 6, 8, 103, 109. 237 // Data is converted to float from -1.0 to 1.0 based on int32 range. 238 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 239 EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get())); 240 EXPECT_EQ(18, buffer.frames()); 241 VerifyResult(bus->channel(0), 6, 1.0f / kint32max, 2.0f / kint32max); 242 VerifyResult(bus->channel(1), 6, 2.0f / kint32max, 2.0f / kint32max); 243 244 // Read the next 2 frames. 245 EXPECT_EQ(2, buffer.ReadFrames(2, 0, bus.get())); 246 EXPECT_EQ(16, buffer.frames()); 247 VerifyResult(bus->channel(0), 2, 13.0f / kint32max, 2.0f / kint32max); 248 VerifyResult(bus->channel(1), 2, 14.0f / kint32max, 2.0f / kint32max); 249 } 250 251 TEST(AudioBufferQueueTest, ReadF32Planar) { 252 const int channels = 2; 253 const base::TimeDelta kNoTime = kNoTimestamp(); 254 AudioBufferQueue buffer; 255 256 // Add 14 frames of data. 257 buffer.Append(MakePlanarAudioBuffer<float>( 258 kSampleFormatPlanarF32, channels, 1.0f, 1.0f, 4, kNoTime, kNoTime)); 259 buffer.Append(MakePlanarAudioBuffer<float>( 260 kSampleFormatPlanarF32, channels, 50.0f, 1.0f, 10, kNoTime, kNoTime)); 261 EXPECT_EQ(14, buffer.frames()); 262 263 // Read 6 frames from the buffer. F32 is planar, so ch[0] should be 264 // 1, 2, 3, 4, 50, 51, and ch[1] should be 5, 6, 7, 8, 60, 61. 265 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 266 EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get())); 267 EXPECT_EQ(8, buffer.frames()); 268 VerifyResult(bus->channel(0), 4, 1.0f, 1.0f); 269 VerifyResult(bus->channel(0) + 4, 2, 50.0f, 1.0f); 270 VerifyResult(bus->channel(1), 4, 5.0f, 1.0f); 271 VerifyResult(bus->channel(1) + 4, 2, 60.0f, 1.0f); 272 } 273 274 TEST(AudioBufferQueueTest, ReadS16Planar) { 275 const int channels = 2; 276 const base::TimeDelta kNoTime = kNoTimestamp(); 277 AudioBufferQueue buffer; 278 279 // Add 24 frames of data. 280 buffer.Append(MakePlanarAudioBuffer<int16>( 281 kSampleFormatPlanarS16, channels, 1, 1, 4, kNoTime, kNoTime)); 282 buffer.Append(MakePlanarAudioBuffer<int16>( 283 kSampleFormatPlanarS16, channels, 100, 5, 20, kNoTime, kNoTime)); 284 EXPECT_EQ(24, buffer.frames()); 285 286 // Read 6 frames from the buffer. Data is planar, so ch[0] should be 287 // 1, 2, 3, 4, 100, 105, and ch[1] should be 5, 6, 7, 8, 200, 205. 288 // Data is converted to float from -1.0 to 1.0 based on int16 range. 289 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 290 EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get())); 291 EXPECT_EQ(18, buffer.frames()); 292 VerifyResult(bus->channel(0), 4, 1.0f / kint16max, 1.0f / kint16max); 293 VerifyResult(bus->channel(0) + 4, 2, 100.0f / kint16max, 5.0f / kint16max); 294 VerifyResult(bus->channel(1), 4, 5.0f / kint16max, 1.0f / kint16max); 295 VerifyResult(bus->channel(1) + 4, 2, 200.0f / kint16max, 5.0f / kint16max); 296 } 297 298 TEST(AudioBufferQueueTest, ReadManyChannels) { 299 const int channels = 16; 300 const base::TimeDelta kNoTime = kNoTimestamp(); 301 AudioBufferQueue buffer; 302 303 // Add 76 frames of data. 304 buffer.Append(MakeInterleavedAudioBuffer<float>( 305 kSampleFormatF32, channels, 0.0f, 1.0f, 6, kNoTime, kNoTime)); 306 buffer.Append(MakeInterleavedAudioBuffer<float>( 307 kSampleFormatF32, channels, 6.0f * channels, 1.0f, 10, kNoTime, kNoTime)); 308 buffer.Append(MakeInterleavedAudioBuffer<float>(kSampleFormatF32, 309 channels, 310 16.0f * channels, 311 1.0f, 312 60, 313 kNoTime, 314 kNoTime)); 315 EXPECT_EQ(76, buffer.frames()); 316 317 // Read 3 frames from the buffer. F32 is interleaved, so ch[0] should be 318 // 1, 17, 33, and ch[1] should be 2, 18, 34. Just check a few channels. 319 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 320 EXPECT_EQ(30, buffer.ReadFrames(30, 0, bus.get())); 321 EXPECT_EQ(46, buffer.frames()); 322 for (int i = 0; i < channels; ++i) { 323 VerifyResult(bus->channel(i), 30, static_cast<float>(i), 16.0f); 324 } 325 } 326 327 TEST(AudioBufferQueueTest, Peek) { 328 const int channels = 4; 329 const base::TimeDelta kNoTime = kNoTimestamp(); 330 AudioBufferQueue buffer; 331 332 // Add 60 frames of data. 333 buffer.Append(MakeInterleavedAudioBuffer<float>( 334 kSampleFormatF32, channels, 0.0f, 1.0f, 60, kNoTime, kNoTime)); 335 EXPECT_EQ(60, buffer.frames()); 336 337 // Peek at the first 30 frames. 338 scoped_ptr<AudioBus> bus1 = AudioBus::Create(channels, 100); 339 EXPECT_EQ(60, buffer.frames()); 340 EXPECT_EQ(60, buffer.PeekFrames(100, 0, 0, bus1.get())); 341 EXPECT_EQ(30, buffer.PeekFrames(30, 0, 0, bus1.get())); 342 EXPECT_EQ(60, buffer.frames()); 343 344 // Now read the next 30 frames (which should be the same as those peeked at). 345 scoped_ptr<AudioBus> bus2 = AudioBus::Create(channels, 100); 346 EXPECT_EQ(30, buffer.ReadFrames(30, 0, bus2.get())); 347 for (int i = 0; i < channels; ++i) { 348 VerifyResult(bus1->channel(i), 349 30, 350 static_cast<float>(i), 351 static_cast<float>(channels)); 352 VerifyResult(bus2->channel(i), 353 30, 354 static_cast<float>(i), 355 static_cast<float>(channels)); 356 } 357 358 // Peek 10 frames forward 359 EXPECT_EQ(5, buffer.PeekFrames(5, 10, 0, bus1.get())); 360 for (int i = 0; i < channels; ++i) { 361 VerifyResult(bus1->channel(i), 362 5, 363 static_cast<float>(i + 40 * channels), 364 static_cast<float>(channels)); 365 } 366 367 // Peek to the end of the buffer. 368 EXPECT_EQ(30, buffer.frames()); 369 EXPECT_EQ(30, buffer.PeekFrames(100, 0, 0, bus1.get())); 370 EXPECT_EQ(30, buffer.PeekFrames(30, 0, 0, bus1.get())); 371 } 372 373 TEST(AudioBufferQueueTest, Time) { 374 const int channels = 2; 375 const base::TimeDelta start_time1; 376 const base::TimeDelta start_time2 = base::TimeDelta::FromSeconds(30); 377 const base::TimeDelta duration = base::TimeDelta::FromSeconds(10); 378 AudioBufferQueue buffer; 379 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 380 381 // Add two buffers (second one added later): 382 // first: start=0s, duration=10s 383 // second: start=30s, duration=10s 384 buffer.Append(MakeInterleavedAudioBuffer<int16>( 385 kSampleFormatS16, channels, 1, 1, 10, start_time1, duration)); 386 EXPECT_EQ(10, buffer.frames()); 387 388 // Check starting time. 389 EXPECT_EQ(start_time1, buffer.current_time()); 390 391 // Read 2 frames, should be 2s in (since duration is 1s per sample). 392 EXPECT_EQ(2, buffer.ReadFrames(2, 0, bus.get())); 393 EXPECT_EQ(start_time1 + base::TimeDelta::FromSeconds(2), 394 buffer.current_time()); 395 396 // Skip 2 frames. 397 buffer.SeekFrames(2); 398 EXPECT_EQ(start_time1 + base::TimeDelta::FromSeconds(4), 399 buffer.current_time()); 400 401 // Add second buffer for more data. 402 buffer.Append(MakeInterleavedAudioBuffer<int16>( 403 kSampleFormatS16, channels, 1, 1, 10, start_time2, duration)); 404 EXPECT_EQ(16, buffer.frames()); 405 406 // Read until almost the end of buffer1. 407 EXPECT_EQ(5, buffer.ReadFrames(5, 0, bus.get())); 408 EXPECT_EQ(start_time1 + base::TimeDelta::FromSeconds(9), 409 buffer.current_time()); 410 411 // Read 1 value, so time moved to buffer2. 412 EXPECT_EQ(1, buffer.ReadFrames(1, 0, bus.get())); 413 EXPECT_EQ(start_time2, buffer.current_time()); 414 415 // Read all 10 frames in buffer2, timestamp should be last time from buffer2. 416 EXPECT_EQ(10, buffer.ReadFrames(10, 0, bus.get())); 417 EXPECT_EQ(start_time2 + base::TimeDelta::FromSeconds(10), 418 buffer.current_time()); 419 420 // Try to read more frames (which don't exist), timestamp should remain. 421 EXPECT_EQ(0, buffer.ReadFrames(5, 0, bus.get())); 422 EXPECT_EQ(start_time2 + base::TimeDelta::FromSeconds(10), 423 buffer.current_time()); 424 } 425 426 TEST(AudioBufferQueueTest, NoTime) { 427 const int channels = 2; 428 const base::TimeDelta kNoTime = kNoTimestamp(); 429 AudioBufferQueue buffer; 430 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 431 432 // Add two buffers with no timestamps. Time should always be unknown. 433 buffer.Append(MakeInterleavedAudioBuffer<int16>( 434 kSampleFormatS16, channels, 1, 1, 10, kNoTime, kNoTime)); 435 buffer.Append(MakeInterleavedAudioBuffer<int16>( 436 kSampleFormatS16, channels, 1, 1, 10, kNoTime, kNoTime)); 437 EXPECT_EQ(20, buffer.frames()); 438 439 // Check starting time. 440 EXPECT_EQ(kNoTime, buffer.current_time()); 441 442 // Read 2 frames. 443 EXPECT_EQ(2, buffer.ReadFrames(2, 0, bus.get())); 444 EXPECT_EQ(kNoTime, buffer.current_time()); 445 446 // Skip 2 frames. 447 buffer.SeekFrames(2); 448 EXPECT_EQ(kNoTime, buffer.current_time()); 449 450 // Read until almost the end of buffer1. 451 EXPECT_EQ(5, buffer.ReadFrames(5, 0, bus.get())); 452 EXPECT_EQ(kNoTime, buffer.current_time()); 453 454 // Read 1 value, so time moved to buffer2. 455 EXPECT_EQ(1, buffer.ReadFrames(1, 0, bus.get())); 456 EXPECT_EQ(kNoTime, buffer.current_time()); 457 458 // Read all 10 frames in buffer2. 459 EXPECT_EQ(10, buffer.ReadFrames(10, 0, bus.get())); 460 EXPECT_EQ(kNoTime, buffer.current_time()); 461 462 // Try to read more frames (which don't exist), timestamp should remain. 463 EXPECT_EQ(0, buffer.ReadFrames(5, 0, bus.get())); 464 EXPECT_EQ(kNoTime, buffer.current_time()); 465 } 466 467 } // namespace media 468