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