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/Functional.h" |
| 12 #include "platform/heap/Persistent.h" |
11 #include "platform/wtf/PtrUtil.h" | 13 #include "platform/wtf/PtrUtil.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 | 15 |
14 namespace blink { | 16 namespace blink { |
15 | 17 |
16 namespace { | 18 namespace { |
17 | 19 |
| 20 class MockAudioDestination { |
| 21 public: |
| 22 MockAudioDestination() {} |
| 23 ~MockAudioDestination() {} |
| 24 void RequestRender (size_t frames_requested, size_t frames_to_render) {} |
| 25 }; |
| 26 |
18 // Check the basic contract of FIFO. | 27 // Check the basic contract of FIFO. |
19 TEST(PushPullFIFOBasicTest, BasicTests) { | 28 TEST(PushPullFIFOBasicTest, BasicTests) { |
20 // This suppresses the multi-thread warning for GTest. Potently it increases | 29 // 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. | 30 // the test execution time, but this specific test is very short and simple. |
22 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; | 31 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
23 | 32 |
24 // FIFO length exceeding the maximum length allowed will cause crash. | 33 // FIFO length exceeding the maximum length allowed will cause crash. |
25 // i.e.) m_fifoLength <= kMaxFIFOLength | 34 // i.e.) m_fifoLength <= kMaxFIFOLength |
26 EXPECT_DEATH(new PushPullFIFO(2, PushPullFIFO::kMaxFIFOLength + 1), ""); | 35 EXPECT_DEATH(new PushPullFIFO(2, PushPullFIFO::kMaxFIFOLength + 1), ""); |
27 | 36 |
| 37 MockAudioDestination* test_destination = new MockAudioDestination(); |
| 38 |
28 std::unique_ptr<PushPullFIFO> test_fifo = | 39 std::unique_ptr<PushPullFIFO> test_fifo = |
29 WTF::WrapUnique(new PushPullFIFO(2, 1024)); | 40 WTF::WrapUnique(new PushPullFIFO(2, 1024)); |
30 | 41 |
31 // The input bus length must be |AudioUtilities::kRenderQuantumFrames|. | 42 // The input bus length must be |AudioUtilities::kRenderQuantumFrames|. |
32 // i.e.) inputBus->length() == kRenderQuantumFrames | 43 // i.e.) inputBus->length() == kRenderQuantumFrames |
33 RefPtr<AudioBus> input_bus_of129_frames = | 44 RefPtr<AudioBus> input_bus_129_frames = |
34 AudioBus::Create(2, AudioUtilities::kRenderQuantumFrames + 1); | 45 AudioBus::Create(2, AudioUtilities::kRenderQuantumFrames + 1); |
35 EXPECT_DEATH(test_fifo->Push(input_bus_of129_frames.Get()), ""); | 46 EXPECT_DEATH(test_fifo->Push(input_bus_129_frames.Get()), ""); |
36 RefPtr<AudioBus> input_bus_of127_frames = | 47 RefPtr<AudioBus> input_bus_127_frames = |
37 AudioBus::Create(2, AudioUtilities::kRenderQuantumFrames - 1); | 48 AudioBus::Create(2, AudioUtilities::kRenderQuantumFrames - 1); |
38 EXPECT_DEATH(test_fifo->Push(input_bus_of127_frames.Get()), ""); | 49 EXPECT_DEATH(test_fifo->Push(input_bus_127_frames.Get()), ""); |
39 | 50 |
40 // Pull request frames cannot exceed the length of output bus. | 51 // Pull request frames cannot exceed the length of output bus. |
41 // i.e.) framesRequested <= outputBus->length() | 52 // i.e.) framesRequested <= outputBus->length() |
42 RefPtr<AudioBus> output_bus_of512_frames = AudioBus::Create(2, 512); | 53 RefPtr<AudioBus> output_bus_512_frames = AudioBus::Create(2, 512); |
43 EXPECT_DEATH(test_fifo->Pull(output_bus_of512_frames.Get(), 513), ""); | 54 EXPECT_DEATH(test_fifo->Pull(output_bus_512_frames.Get(), 513, |
| 55 WTF::Bind(&MockAudioDestination::RequestRender, |
| 56 WTF::Unretained(test_destination))), |
| 57 ""); |
44 | 58 |
45 // Pull request frames cannot exceed the length of FIFO. | 59 // Pull request frames cannot exceed the length of FIFO. |
46 // i.e.) framesRequested <= m_fifoLength | 60 // i.e.) framesRequested <= m_fifoLength |
47 RefPtr<AudioBus> output_bus_of1025_frames = AudioBus::Create(2, 1025); | 61 RefPtr<AudioBus> output_bus_1025_frames = AudioBus::Create(2, 1025); |
48 EXPECT_DEATH(test_fifo->Pull(output_bus_of1025_frames.Get(), 1025), ""); | 62 EXPECT_DEATH(test_fifo->Pull(output_bus_1025_frames.Get(), 1025, |
| 63 WTF::Bind(&MockAudioDestination::RequestRender, |
| 64 WTF::Unretained(test_destination))), |
| 65 ""); |
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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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; |
136 | 153 |
| 154 MockAudioDestination* test_destination = new MockAudioDestination(); |
| 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)); |
140 | 159 |
141 RefPtr<AudioBus> output_bus; | 160 RefPtr<AudioBus> output_bus; |
142 | 161 |
143 // Iterate all the scheduled push/pull actions. | 162 // Iterate all the scheduled push/pull actions. |
144 size_t frame_counter = 0; | 163 size_t frame_counter = 0; |
145 for (const auto& action : setup.fifo_actions) { | 164 for (const auto& action : setup.fifo_actions) { |
146 if (strcmp(action.action, "PUSH") == 0) { | 165 if (strcmp(action.action, "PUSH") == 0) { |
147 RefPtr<AudioBus> input_bus = | 166 RefPtr<AudioBus> input_bus = |
148 AudioBus::Create(setup.number_of_channels, action.number_of_frames); | 167 AudioBus::Create(setup.number_of_channels, action.number_of_frames); |
149 frame_counter = FillBusWithLinearRamp(input_bus.Get(), frame_counter); | 168 frame_counter = FillBusWithLinearRamp(input_bus.Get(), frame_counter); |
150 fifo->Push(input_bus.Get()); | 169 fifo->Push(input_bus.Get()); |
151 LOG(INFO) << "PUSH " << action.number_of_frames | 170 LOG(INFO) << "PUSH " << action.number_of_frames |
152 << " frames (frameCounter=" << frame_counter << ")"; | 171 << " frames (frameCounter=" << frame_counter << ")"; |
153 } else { | 172 } else { |
154 output_bus = | 173 output_bus = |
155 AudioBus::Create(setup.number_of_channels, action.number_of_frames); | 174 AudioBus::Create(setup.number_of_channels, action.number_of_frames); |
156 fifo->Pull(output_bus.Get(), action.number_of_frames); | 175 fifo->Pull(output_bus.Get(), action.number_of_frames, |
| 176 WTF::Bind(&MockAudioDestination::RequestRender, |
| 177 WTF::Unretained(test_destination))); |
157 LOG(INFO) << "PULL " << action.number_of_frames << " frames"; | 178 LOG(INFO) << "PULL " << action.number_of_frames << " frames"; |
158 } | 179 } |
159 } | 180 } |
160 | 181 |
161 // Get FIFO config data. | 182 // Get FIFO config data. |
162 const PushPullFIFOStateForTest actual_state = fifo->GetStateForTest(); | 183 const PushPullFIFOStateForTest actual_state = fifo->GetStateForTest(); |
163 | 184 |
164 // Verify the read/write indexes. | 185 // Verify the read/write indexes. |
165 EXPECT_EQ(expected_state.index_read, actual_state.index_read); | 186 EXPECT_EQ(expected_state.index_read, actual_state.index_read); |
166 EXPECT_EQ(expected_state.index_write, actual_state.index_write); | 187 EXPECT_EQ(expected_state.index_write, actual_state.index_write); |
167 EXPECT_EQ(expected_state.overflow_count, actual_state.overflow_count); | 188 EXPECT_EQ(expected_state.overflow_count, actual_state.overflow_count); |
168 EXPECT_EQ(expected_state.underflow_count, actual_state.underflow_count); | 189 EXPECT_EQ(expected_state.underflow_count, actual_state.underflow_count); |
169 | 190 |
170 // Verify in-FIFO samples. | 191 // Verify in-FIFO samples. |
171 for (const auto& sample : expected_state.fifo_samples) { | 192 for (const auto& sample : expected_state.fifo_samples) { |
172 EXPECT_TRUE(VerifyBusValueAtIndex(fifo->Bus(), sample.index, sample.value)); | 193 EXPECT_TRUE(VerifyBusValueAtIndex(fifo->GetFIFOBusForTest(), |
| 194 sample.index, 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 |