| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/audio/PushPullFIFO.h" | 5 #include "platform/audio/PushPullFIFO.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <vector> | 8 #include <vector> |
| 9 #include "platform/audio/AudioUtilities.h" | 9 #include "platform/audio/AudioUtilities.h" |
| 10 #include "platform/testing/TestingPlatformSupport.h" | 10 #include "platform/testing/TestingPlatformSupport.h" |
| 11 #include "platform/wtf/PtrUtil.h" | 11 #include "platform/wtf/PtrUtil.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace blink { | 14 namespace blink { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 class MockAudioIOCallback : public AudioIOCallback { |
| 19 public: |
| 20 MockAudioIOCallback() {}; |
| 21 ~MockAudioIOCallback() {}; |
| 22 |
| 23 void Render(AudioBus* source_bus, |
| 24 AudioBus* destination_bus, |
| 25 size_t number_of_frames, |
| 26 const AudioIOPosition& output_position) final {} |
| 27 }; |
| 28 |
| 18 // Check the basic contract of FIFO. | 29 // Check the basic contract of FIFO. |
| 19 TEST(PushPullFIFOBasicTest, BasicTests) { | 30 TEST(PushPullFIFOBasicTest, BasicTests) { |
| 20 // This suppresses the multi-thread warning for GTest. Potently it increases | 31 // This suppresses the multi-thread warning for GTest. Potently it increases |
| 21 // the test execution time, but this specific test is very short and simple. | 32 // the test execution time, but this specific test is very short and simple. |
| 22 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; | 33 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 23 | 34 |
| 35 const ThreadIdentifier thread_id = CurrentThread(); |
| 36 MockAudioIOCallback* mock_audio_io_callback = new MockAudioIOCallback(); |
| 37 |
| 24 // FIFO length exceeding the maximum length allowed will cause crash. | 38 // FIFO length exceeding the maximum length allowed will cause crash. |
| 25 // i.e.) m_fifoLength <= kMaxFIFOLength | 39 // i.e.) m_fifoLength <= kMaxFIFOLength |
| 26 EXPECT_DEATH(new PushPullFIFO(2, PushPullFIFO::kMaxFIFOLength + 1), ""); | 40 EXPECT_DEATH(new PushPullFIFO(2, PushPullFIFO::kMaxFIFOLength + 1, |
| 41 thread_id, *mock_audio_io_callback), |
| 42 ""); |
| 27 | 43 |
| 28 std::unique_ptr<PushPullFIFO> test_fifo = | 44 std::unique_ptr<PushPullFIFO> test_fifo = |
| 29 WTF::WrapUnique(new PushPullFIFO(2, 1024)); | 45 WTF::WrapUnique(new PushPullFIFO(2, 1024, thread_id, |
| 46 *mock_audio_io_callback)); |
| 30 | 47 |
| 31 // The input bus length must be |AudioUtilities::kRenderQuantumFrames|. | 48 // The input bus length must be |AudioUtilities::kRenderQuantumFrames|. |
| 32 // i.e.) inputBus->length() == kRenderQuantumFrames | 49 // i.e.) inputBus->length() == kRenderQuantumFrames |
| 33 RefPtr<AudioBus> input_bus_of129_frames = | 50 RefPtr<AudioBus> input_bus_129_frames = |
| 34 AudioBus::Create(2, AudioUtilities::kRenderQuantumFrames + 1); | 51 AudioBus::Create(2, AudioUtilities::kRenderQuantumFrames + 1); |
| 35 EXPECT_DEATH(test_fifo->Push(input_bus_of129_frames.Get()), ""); | 52 EXPECT_DEATH(test_fifo->Push(input_bus_129_frames.Get()), ""); |
| 36 RefPtr<AudioBus> input_bus_of127_frames = | 53 RefPtr<AudioBus> input_bus_127_frames = |
| 37 AudioBus::Create(2, AudioUtilities::kRenderQuantumFrames - 1); | 54 AudioBus::Create(2, AudioUtilities::kRenderQuantumFrames - 1); |
| 38 EXPECT_DEATH(test_fifo->Push(input_bus_of127_frames.Get()), ""); | 55 EXPECT_DEATH(test_fifo->Push(input_bus_127_frames.Get()), ""); |
| 39 | 56 |
| 40 // Pull request frames cannot exceed the length of output bus. | 57 // Pull request frames cannot exceed the length of output bus. |
| 41 // i.e.) framesRequested <= outputBus->length() | 58 // i.e.) framesRequested <= outputBus->length() |
| 42 RefPtr<AudioBus> output_bus_of512_frames = AudioBus::Create(2, 512); | 59 RefPtr<AudioBus> output_bus_512_frames = AudioBus::Create(2, 512); |
| 43 EXPECT_DEATH(test_fifo->Pull(output_bus_of512_frames.Get(), 513), ""); | 60 EXPECT_DEATH(test_fifo->Pull(output_bus_512_frames.Get(), 513), ""); |
| 44 | 61 |
| 45 // Pull request frames cannot exceed the length of FIFO. | 62 // Pull request frames cannot exceed the length of FIFO. |
| 46 // i.e.) framesRequested <= m_fifoLength | 63 // i.e.) framesRequested <= m_fifoLength |
| 47 RefPtr<AudioBus> output_bus_of1025_frames = AudioBus::Create(2, 1025); | 64 RefPtr<AudioBus> output_bus_1025_frames = AudioBus::Create(2, 1025); |
| 48 EXPECT_DEATH(test_fifo->Pull(output_bus_of1025_frames.Get(), 1025), ""); | 65 EXPECT_DEATH(test_fifo->Pull(output_bus_1025_frames.Get(), 1025), ""); |
| 49 } | 66 } |
| 50 | 67 |
| 51 // Fills each AudioChannel in an AudioBus with a series of linearly increasing | 68 // Fills each AudioChannel in an AudioBus with a series of linearly increasing |
| 52 // values starting from |startingValue| and incrementing by 1. Then return value | 69 // values starting from |startingValue| and incrementing by 1. Then return value |
| 53 // will be |startingValue| + |bus_length|. | 70 // will be |startingValue| + |bus_length|. |
| 54 size_t FillBusWithLinearRamp(AudioBus* target_bus, size_t starting_value) { | 71 size_t FillBusWithLinearRamp(AudioBus* target_bus, size_t starting_value) { |
| 55 for (unsigned c = 0; c < target_bus->NumberOfChannels(); ++c) { | 72 for (unsigned c = 0; c < target_bus->NumberOfChannels(); ++c) { |
| 56 float* bus_channel = target_bus->Channel(c)->MutableData(); | 73 float* bus_channel = target_bus->Channel(c)->MutableData(); |
| 57 for (size_t i = 0; i < target_bus->Channel(c)->length(); ++i) { | 74 for (size_t i = 0; i < target_bus->Channel(c)->length(); ++i) { |
| 58 bus_channel[i] = static_cast<float>(starting_value + i); | 75 bus_channel[i] = static_cast<float>(starting_value + i); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 << " numberOfChannels=" << param.setup.number_of_channels; | 143 << " numberOfChannels=" << param.setup.number_of_channels; |
| 127 return out; | 144 return out; |
| 128 } | 145 } |
| 129 | 146 |
| 130 class PushPullFIFOFeatureTest : public ::testing::TestWithParam<FIFOTestParam> { | 147 class PushPullFIFOFeatureTest : public ::testing::TestWithParam<FIFOTestParam> { |
| 131 }; | 148 }; |
| 132 | 149 |
| 133 TEST_P(PushPullFIFOFeatureTest, FeatureTests) { | 150 TEST_P(PushPullFIFOFeatureTest, FeatureTests) { |
| 134 const FIFOTestSetup setup = GetParam().setup; | 151 const FIFOTestSetup setup = GetParam().setup; |
| 135 const FIFOTestExpectedState expected_state = GetParam().expected_state; | 152 const FIFOTestExpectedState expected_state = GetParam().expected_state; |
| 153 const ThreadIdentifier thread_id = CurrentThread(); |
| 154 MockAudioIOCallback* mock_audio_io_callback = new MockAudioIOCallback(); |
| 136 | 155 |
| 137 // Create a FIFO with a specified configuration. | 156 // Create a FIFO with a specified configuration. |
| 138 std::unique_ptr<PushPullFIFO> fifo = WTF::WrapUnique( | 157 std::unique_ptr<PushPullFIFO> fifo = WTF::WrapUnique( |
| 139 new PushPullFIFO(setup.number_of_channels, setup.fifo_length)); | 158 new PushPullFIFO(setup.number_of_channels, setup.fifo_length, thread_id, |
| 159 *mock_audio_io_callback)); |
| 140 | 160 |
| 141 RefPtr<AudioBus> output_bus; | 161 RefPtr<AudioBus> output_bus; |
| 142 | 162 |
| 143 // Iterate all the scheduled push/pull actions. | 163 // Iterate all the scheduled push/pull actions. |
| 144 size_t frame_counter = 0; | 164 size_t frame_counter = 0; |
| 145 for (const auto& action : setup.fifo_actions) { | 165 for (const auto& action : setup.fifo_actions) { |
| 146 if (strcmp(action.action, "PUSH") == 0) { | 166 if (strcmp(action.action, "PUSH") == 0) { |
| 147 RefPtr<AudioBus> input_bus = | 167 RefPtr<AudioBus> input_bus = |
| 148 AudioBus::Create(setup.number_of_channels, action.number_of_frames); | 168 AudioBus::Create(setup.number_of_channels, action.number_of_frames); |
| 149 frame_counter = FillBusWithLinearRamp(input_bus.Get(), frame_counter); | 169 frame_counter = FillBusWithLinearRamp(input_bus.Get(), frame_counter); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 162 const PushPullFIFOStateForTest actual_state = fifo->GetStateForTest(); | 182 const PushPullFIFOStateForTest actual_state = fifo->GetStateForTest(); |
| 163 | 183 |
| 164 // Verify the read/write indexes. | 184 // Verify the read/write indexes. |
| 165 EXPECT_EQ(expected_state.index_read, actual_state.index_read); | 185 EXPECT_EQ(expected_state.index_read, actual_state.index_read); |
| 166 EXPECT_EQ(expected_state.index_write, actual_state.index_write); | 186 EXPECT_EQ(expected_state.index_write, actual_state.index_write); |
| 167 EXPECT_EQ(expected_state.overflow_count, actual_state.overflow_count); | 187 EXPECT_EQ(expected_state.overflow_count, actual_state.overflow_count); |
| 168 EXPECT_EQ(expected_state.underflow_count, actual_state.underflow_count); | 188 EXPECT_EQ(expected_state.underflow_count, actual_state.underflow_count); |
| 169 | 189 |
| 170 // Verify in-FIFO samples. | 190 // Verify in-FIFO samples. |
| 171 for (const auto& sample : expected_state.fifo_samples) { | 191 for (const auto& sample : expected_state.fifo_samples) { |
| 172 EXPECT_TRUE(VerifyBusValueAtIndex(fifo->Bus(), sample.index, sample.value)); | 192 EXPECT_TRUE(VerifyBusValueAtIndex(fifo->GetFIFOBusForTest(), |
| 193 sample.index, |
| 194 sample.value)); |
| 173 } | 195 } |
| 174 | 196 |
| 175 // Verify samples from the most recent output bus. | 197 // Verify samples from the most recent output bus. |
| 176 for (const auto& sample : expected_state.output_samples) { | 198 for (const auto& sample : expected_state.output_samples) { |
| 177 EXPECT_TRUE( | 199 EXPECT_TRUE( |
| 178 VerifyBusValueAtIndex(output_bus.Get(), sample.index, sample.value)); | 200 VerifyBusValueAtIndex(output_bus.Get(), sample.index, sample.value)); |
| 179 } | 201 } |
| 180 } | 202 } |
| 181 | 203 |
| 182 FIFOTestParam g_feature_test_params[] = { | 204 FIFOTestParam g_feature_test_params[] = { |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 // - Output bus samples (index, expectedValue) = (0, 0), (143, 0) | 376 // - Output bus samples (index, expectedValue) = (0, 0), (143, 0) |
| 355 {0, 0, 0, 4, {{0, 0}, {1023, 0}}, {{0, 0}, {143, 0}}}}}; | 377 {0, 0, 0, 4, {{0, 0}, {1023, 0}}, {{0, 0}, {143, 0}}}}}; |
| 356 | 378 |
| 357 INSTANTIATE_TEST_CASE_P(PushPullFIFOFeatureTest, | 379 INSTANTIATE_TEST_CASE_P(PushPullFIFOFeatureTest, |
| 358 PushPullFIFOFeatureTest, | 380 PushPullFIFOFeatureTest, |
| 359 ::testing::ValuesIn(g_feature_test_params)); | 381 ::testing::ValuesIn(g_feature_test_params)); |
| 360 | 382 |
| 361 } // namespace | 383 } // namespace |
| 362 | 384 |
| 363 } // namespace blink | 385 } // namespace blink |
| OLD | NEW |