Chromium Code Reviews| Index: media/base/audio_bus_unittest.cc |
| diff --git a/media/base/audio_bus_unittest.cc b/media/base/audio_bus_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..65c34dcd6c78e6e15aab863d555c389c7931c839 |
| --- /dev/null |
| +++ b/media/base/audio_bus_unittest.cc |
| @@ -0,0 +1,122 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/audio/audio_parameters.h" |
| +#include "media/base/audio_bus.h" |
| +#include "media/base/channel_layout.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +static const int kChannels = 6; |
| +static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_5_1; |
| +// Use a buffer size which is intentionally not a multiple of 16. |
|
scherkus (not reviewing)
2012/08/07 02:51:16
nit: "See kChannelAlignment in audio_bus.cc" ?
DaleCurtis
2012/08/07 18:29:45
Done.
|
| +static const int kFrameCount = 1234; |
| + |
| +namespace media { |
| + |
| +class AudioBusTest : public testing::Test { |
| + public: |
| + AudioBusTest() {} |
| + ~AudioBusTest() { |
| + for (size_t i = 0; i < data_.size(); ++i) |
| + base::AlignedFree(data_[i]); |
| + } |
| + |
| + // Validate parameters returned by AudioBus v.s. the constructed parameters. |
| + void VerifyParams(AudioBus* bus) { |
| + EXPECT_EQ(kChannels, bus->channels()); |
| + EXPECT_EQ(kFrameCount, bus->frames()); |
| + } |
| + |
| + void VerifyValue(const float data[], int size, float value) { |
| + for (int i = 0; i < size; ++i) |
| + ASSERT_FLOAT_EQ(value, data[i]); |
| + } |
| + |
| + // Read and write to the full extent of the allocated channel data. Also test |
| + // the Zero() method and verify it does as advertised. Also test data if data |
| + // is aligned properly. |
|
henrika (OOO until Aug 14)
2012/08/07 09:52:55
Can you be more specific than "properly"? Makes it
DaleCurtis
2012/08/07 18:29:45
Done.
|
| + void VerifyChannelData(AudioBus* bus) { |
| + for (int i = 0; i < bus->channels(); ++i) { |
| + ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(bus->channel(i)) & 0x0F); |
| + std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i); |
| + } |
| + |
| + for (int i = 0; i < bus->channels(); ++i) |
| + VerifyValue(bus->channel(i), bus->frames(), i); |
| + |
| + bus->Zero(); |
| + for (int i = 0; i < bus->channels(); ++i) |
| + VerifyValue(bus->channel(i), bus->frames(), 0); |
| + } |
| + |
| + |
| + protected: |
| + std::vector<float*> data_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AudioBusTest); |
| +}; |
| + |
| +TEST_F(AudioBusTest, CreateAndSetLayout) { |
| + scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); |
| + VerifyParams(bus.get()); |
| + VerifyChannelData(bus.get()); |
| +} |
| + |
| +TEST_F(AudioBusTest, CreateUsingAudioParameters) { |
| + scoped_ptr<AudioBus> bus = AudioBus::Create(AudioParameters( |
| + AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, 48000, 32, |
| + kFrameCount)); |
| + VerifyParams(bus.get()); |
| + VerifyChannelData(bus.get()); |
| +} |
| + |
| +TEST_F(AudioBusTest, Wrap) { |
|
henrika (OOO until Aug 14)
2012/08/07 09:52:55
Short comment for this test would be nice.
DaleCurtis
2012/08/07 18:29:45
Done.
|
| + data_.reserve(kChannels); |
| + for (int i = 0; i < kChannels; ++i) { |
| + data_.push_back(static_cast<float*>(base::AlignedAlloc( |
| + sizeof(*data_[i]) * kFrameCount, 16))); |
| + } |
| + |
| + scoped_ptr<AudioBus> bus = AudioBus::WrapVector(kFrameCount, data_); |
| + VerifyParams(bus.get()); |
| + VerifyChannelData(bus.get()); |
| +} |
| + |
| +// Simulate a shared memory transfer and verify results. |
| +TEST_F(AudioBusTest, AudioData) { |
| + scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); |
|
henrika (OOO until Aug 14)
2012/08/07 09:52:55
Nit, why not bus1 and bus2?
DaleCurtis
2012/08/07 18:29:45
Done.
|
| + scoped_ptr<AudioBus> bus2 = AudioBus::Create(kChannels, kFrameCount); |
| + |
| + // Fill first bus with dummy data and zero out the second bus. |
| + for (int i = 0; i < bus->channels(); ++i) |
| + std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i); |
| + bus2->Zero(); |
| + |
| + // Transfer audio data from |bus| to |bus2|. |
| + ASSERT_EQ(bus->data_size(), bus2->data_size()); |
| + memcpy(bus2->data(), bus->data(), bus->data_size()); |
| + |
| + for (int i = 0; i < bus2->channels(); ++i) |
| + VerifyValue(bus2->channel(i), bus2->frames(), i); |
| +} |
| + |
| +TEST_F(AudioBusTest, Zero) { |
|
henrika (OOO until Aug 14)
2012/08/07 09:52:55
Comment?
DaleCurtis
2012/08/07 18:29:45
Done.
|
| + scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); |
| + |
| + // First fill the bus with dummy data. |
| + for (int i = 0; i < bus->channels(); ++i) |
| + std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1); |
| + |
| + // Zero half the frames of each channel. |
| + bus->ZeroFrames(kFrameCount / 2); |
| + for (int i = 0; i < bus->channels(); ++i) |
| + VerifyValue(bus->channel(i), kFrameCount / 2, 0); |
| + |
| + // Zero all the frames of each channel. |
| + bus->Zero(); |
| + for (int i = 0; i < bus->channels(); ++i) |
| + VerifyValue(bus->channel(i), bus->frames(), 0); |
| +} |
| + |
| +} // namespace media |