OLD | NEW |
(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 "media/audio/audio_parameters.h" |
| 6 #include "media/base/audio_bus.h" |
| 7 #include "media/base/channel_layout.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 static const int kChannels = 6; |
| 11 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_5_1; |
| 12 // Use a buffer size which is intentionally not a multiple of 16; see |
| 13 // kChannelAlignment in audio_bus.cc. |
| 14 static const int kFrameCount = 1234; |
| 15 |
| 16 namespace media { |
| 17 |
| 18 class AudioBusTest : public testing::Test { |
| 19 public: |
| 20 AudioBusTest() {} |
| 21 ~AudioBusTest() { |
| 22 for (size_t i = 0; i < data_.size(); ++i) |
| 23 base::AlignedFree(data_[i]); |
| 24 } |
| 25 |
| 26 // Validate parameters returned by AudioBus v.s. the constructed parameters. |
| 27 void VerifyParams(AudioBus* bus) { |
| 28 EXPECT_EQ(kChannels, bus->channels()); |
| 29 EXPECT_EQ(kFrameCount, bus->frames()); |
| 30 } |
| 31 |
| 32 void VerifyValue(const float data[], int size, float value) { |
| 33 for (int i = 0; i < size; ++i) |
| 34 ASSERT_FLOAT_EQ(value, data[i]); |
| 35 } |
| 36 |
| 37 // Read and write to the full extent of the allocated channel data. Also test |
| 38 // the Zero() method and verify it does as advertised. Also test data if data |
| 39 // is 16-byte aligned as advertised (see kChannelAlignment in audio_bus.cc). |
| 40 void VerifyChannelData(AudioBus* bus) { |
| 41 for (int i = 0; i < bus->channels(); ++i) { |
| 42 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(bus->channel(i)) & 0x0F); |
| 43 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i); |
| 44 } |
| 45 |
| 46 for (int i = 0; i < bus->channels(); ++i) |
| 47 VerifyValue(bus->channel(i), bus->frames(), i); |
| 48 |
| 49 bus->Zero(); |
| 50 for (int i = 0; i < bus->channels(); ++i) |
| 51 VerifyValue(bus->channel(i), bus->frames(), 0); |
| 52 } |
| 53 |
| 54 |
| 55 protected: |
| 56 std::vector<float*> data_; |
| 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(AudioBusTest); |
| 59 }; |
| 60 |
| 61 // Verify basic Create(...) method works as advertised. |
| 62 TEST_F(AudioBusTest, Create) { |
| 63 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); |
| 64 VerifyParams(bus.get()); |
| 65 VerifyChannelData(bus.get()); |
| 66 } |
| 67 |
| 68 // Verify Create(...) using AudioParameters works as advertised. |
| 69 TEST_F(AudioBusTest, CreateUsingAudioParameters) { |
| 70 scoped_ptr<AudioBus> bus = AudioBus::Create(AudioParameters( |
| 71 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, 48000, 32, |
| 72 kFrameCount)); |
| 73 VerifyParams(bus.get()); |
| 74 VerifyChannelData(bus.get()); |
| 75 } |
| 76 |
| 77 // Verify an AudioBus created via wrapping works as advertised. |
| 78 TEST_F(AudioBusTest, Wrap) { |
| 79 data_.reserve(kChannels); |
| 80 for (int i = 0; i < kChannels; ++i) { |
| 81 data_.push_back(static_cast<float*>(base::AlignedAlloc( |
| 82 sizeof(*data_[i]) * kFrameCount, 16))); |
| 83 } |
| 84 |
| 85 scoped_ptr<AudioBus> bus = AudioBus::WrapVector(kFrameCount, data_); |
| 86 VerifyParams(bus.get()); |
| 87 VerifyChannelData(bus.get()); |
| 88 } |
| 89 |
| 90 // Simulate a shared memory transfer and verify results. |
| 91 TEST_F(AudioBusTest, AudioData) { |
| 92 scoped_ptr<AudioBus> bus1 = AudioBus::Create(kChannels, kFrameCount); |
| 93 scoped_ptr<AudioBus> bus2 = AudioBus::Create(kChannels, kFrameCount); |
| 94 |
| 95 // Fill |bus1| with dummy data and zero out |bus2|. |
| 96 for (int i = 0; i < bus1->channels(); ++i) |
| 97 std::fill(bus1->channel(i), bus1->channel(i) + bus1->frames(), i); |
| 98 bus2->Zero(); |
| 99 |
| 100 // Transfer audio data from |bus1| to |bus2|. |
| 101 ASSERT_EQ(bus1->data_size(), bus2->data_size()); |
| 102 memcpy(bus2->data(), bus1->data(), bus1->data_size()); |
| 103 |
| 104 for (int i = 0; i < bus2->channels(); ++i) |
| 105 VerifyValue(bus2->channel(i), bus2->frames(), i); |
| 106 } |
| 107 |
| 108 // Verify Zero() and ZeroFrames(...) utility methods work as advertised. |
| 109 TEST_F(AudioBusTest, Zero) { |
| 110 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); |
| 111 |
| 112 // First fill the bus with dummy data. |
| 113 for (int i = 0; i < bus->channels(); ++i) |
| 114 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1); |
| 115 |
| 116 // Zero half the frames of each channel. |
| 117 bus->ZeroFrames(kFrameCount / 2); |
| 118 for (int i = 0; i < bus->channels(); ++i) |
| 119 VerifyValue(bus->channel(i), kFrameCount / 2, 0); |
| 120 |
| 121 // Zero all the frames of each channel. |
| 122 bus->Zero(); |
| 123 for (int i = 0; i < bus->channels(); ++i) |
| 124 VerifyValue(bus->channel(i), bus->frames(), 0); |
| 125 } |
| 126 |
| 127 } // namespace media |
OLD | NEW |