| 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/CrossThreadFunctional.h" | 9 #include "platform/CrossThreadFunctional.h" |
| 10 #include "platform/WaitableEvent.h" | 10 #include "platform/WaitableEvent.h" |
| 11 #include "platform/WebTaskRunner.h" | 11 #include "platform/WebTaskRunner.h" |
| 12 #include "platform/audio/AudioUtilities.h" | 12 #include "platform/audio/AudioUtilities.h" |
| 13 #include "platform/testing/UnitTestHelpers.h" | 13 #include "platform/testing/UnitTestHelpers.h" |
| 14 #include "platform/wtf/Functional.h" | 14 #include "platform/wtf/Functional.h" |
| 15 #include "platform/wtf/PtrUtil.h" | 15 #include "platform/wtf/PtrUtil.h" |
| 16 #include "public/platform/Platform.h" | 16 #include "public/platform/Platform.h" |
| 17 #include "public/platform/WebThread.h" | 17 #include "public/platform/WebThread.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 namespace blink { | 20 namespace blink { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // To forcibly stop the message loop. | |
| 25 // TODO(hongchan): move this hack into Test class when the solution is found. | |
| 26 void FinishTest() { | |
| 27 LOG(INFO) << "FinishTest"; | |
| 28 testing::ExitRunLoop(); | |
| 29 } | |
| 30 | |
| 31 // To wait for spawned threads to finish their tasks. | |
| 32 // TODO(hongchan): move this hack into Test class when the solution is found. | |
| 33 void HoldTestForDuration(double duration_ms) { | |
| 34 LOG(INFO) << "HoldTestForDuration"; | |
| 35 Platform::Current()->CurrentThread()->GetWebTaskRunner()->PostDelayedTask( | |
| 36 BLINK_FROM_HERE, | |
| 37 WTF::Bind(&FinishTest), | |
| 38 duration_ms); | |
| 39 testing::EnterRunLoop(); | |
| 40 } | |
| 41 | |
| 42 // Base FIFOClient with an extra thread for looping and jitter control. The | 24 // Base FIFOClient with an extra thread for looping and jitter control. The |
| 43 // child class must define a specific task to run on the thread. | 25 // child class must define a specific task to run on the thread. |
| 44 class FIFOClient { | 26 class FIFOClient { |
| 45 public: | 27 public: |
| 46 FIFOClient(PushPullFIFO* fifo, size_t bus_length, size_t jitter_range_ms) | 28 FIFOClient(PushPullFIFO* fifo, size_t bus_length, size_t jitter_range_ms) |
| 47 : fifo_(fifo), | 29 : fifo_(fifo), |
| 48 bus_(AudioBus::Create(fifo->NumberOfChannels(), bus_length)), | 30 bus_(AudioBus::Create(fifo->NumberOfChannels(), bus_length)), |
| 49 client_thread_(Platform::Current()->CreateThread("client thread")), | 31 client_thread_(Platform::Current()->CreateThread("client thread")), |
| 32 done_event_(WTF::MakeUnique<WaitableEvent>()), |
| 50 jitter_range_ms_(jitter_range_ms) {} | 33 jitter_range_ms_(jitter_range_ms) {} |
| 51 | 34 |
| 52 void Start(double duration_ms, double interval_ms) { | 35 WaitableEvent* Start(double duration_ms, double interval_ms) { |
| 53 duration_ms_ = duration_ms; | 36 duration_ms_ = duration_ms; |
| 54 interval_ms_ = interval_ms; | 37 interval_ms_ = interval_ms; |
| 55 client_thread_->GetWebTaskRunner()->PostTask( | 38 client_thread_->GetWebTaskRunner()->PostTask( |
| 56 BLINK_FROM_HERE, | 39 BLINK_FROM_HERE, |
| 57 CrossThreadBind(&FIFOClient::RunTaskOnOwnThread, | 40 CrossThreadBind(&FIFOClient::RunTaskOnOwnThread, |
| 58 CrossThreadUnretained(this))); | 41 CrossThreadUnretained(this))); |
| 42 return done_event_.get(); |
| 59 } | 43 } |
| 60 | 44 |
| 61 virtual void Stop(int callback_counter) = 0; | 45 virtual void Stop(int callback_counter) = 0; |
| 62 virtual void RunTask() = 0; | 46 virtual void RunTask() = 0; |
| 63 | 47 |
| 64 void Pull(size_t frames_to_pull) { | 48 void Pull(size_t frames_to_pull) { |
| 65 fifo_->Pull(bus_.Get(), frames_to_pull); | 49 fifo_->Pull(bus_.Get(), frames_to_pull); |
| 66 } | 50 } |
| 67 | 51 |
| 68 void Push() { | 52 void Push() { |
| 69 fifo_->Push(bus_.Get()); | 53 fifo_->Push(bus_.Get()); |
| 70 } | 54 } |
| 71 | 55 |
| 72 private: | 56 private: |
| 73 void RunTaskOnOwnThread() { | 57 void RunTaskOnOwnThread() { |
| 74 double interval_with_jitter = interval_ms_ | 58 double interval_with_jitter = interval_ms_ |
| 75 + (static_cast<double>(std::rand()) / RAND_MAX) * jitter_range_ms_; | 59 + (static_cast<double>(std::rand()) / RAND_MAX) * jitter_range_ms_; |
| 76 elapsed_ms_ += interval_with_jitter; | 60 elapsed_ms_ += interval_with_jitter; |
| 77 ++counter_; | 61 ++counter_; |
| 78 RunTask(); | 62 RunTask(); |
| 79 if (elapsed_ms_ < duration_ms_) { | 63 if (elapsed_ms_ < duration_ms_) { |
| 80 client_thread_->GetWebTaskRunner()->PostDelayedTask( | 64 client_thread_->GetWebTaskRunner()->PostDelayedTask( |
| 81 BLINK_FROM_HERE, | 65 BLINK_FROM_HERE, |
| 82 CrossThreadBind(&FIFOClient::RunTaskOnOwnThread, | 66 CrossThreadBind(&FIFOClient::RunTaskOnOwnThread, |
| 83 CrossThreadUnretained(this)), | 67 CrossThreadUnretained(this)), |
| 84 interval_with_jitter); | 68 interval_with_jitter); |
| 85 } else { | 69 } else { |
| 86 Stop(counter_); | 70 Stop(counter_); |
| 71 done_event_->Signal(); |
| 87 } | 72 } |
| 88 } | 73 } |
| 89 | 74 |
| 90 PushPullFIFO* fifo_; | 75 PushPullFIFO* fifo_; |
| 91 RefPtr<AudioBus> bus_; | 76 RefPtr<AudioBus> bus_; |
| 92 std::unique_ptr<WebThread> client_thread_; | 77 std::unique_ptr<WebThread> client_thread_; |
| 78 std::unique_ptr<WaitableEvent> done_event_; |
| 93 | 79 |
| 94 // Test duration. | 80 // Test duration. |
| 95 double duration_ms_; | 81 double duration_ms_; |
| 96 | 82 |
| 97 // Interval between each callback. | 83 // Interval between each callback. |
| 98 double interval_ms_; | 84 double interval_ms_; |
| 99 | 85 |
| 100 // Jitter added to the regular pushing/pulling interval. | 86 // Jitter added to the regular pushing/pulling interval. |
| 101 // (where j is 0 < j < jitter_range_ms) | 87 // (where j is 0 < j < jitter_range_ms) |
| 102 double jitter_range_ms_; | 88 double jitter_range_ms_; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 const double push_interval_ms = | 158 const double push_interval_ms = |
| 173 param.push_buffer_size / sample_rate * 1000; | 159 param.push_buffer_size / sample_rate * 1000; |
| 174 | 160 |
| 175 std::unique_ptr<PushPullFIFO> test_fifo = WTF::WrapUnique( | 161 std::unique_ptr<PushPullFIFO> test_fifo = WTF::WrapUnique( |
| 176 new PushPullFIFO(param.number_of_channels, param.fifo_length)); | 162 new PushPullFIFO(param.number_of_channels, param.fifo_length)); |
| 177 std::unique_ptr<PullClient> pull_client = WTF::WrapUnique(new PullClient( | 163 std::unique_ptr<PullClient> pull_client = WTF::WrapUnique(new PullClient( |
| 178 test_fifo.get(), param.pull_buffer_size, param.pull_jitter_range_ms)); | 164 test_fifo.get(), param.pull_buffer_size, param.pull_jitter_range_ms)); |
| 179 std::unique_ptr<PushClient> push_client = WTF::WrapUnique(new PushClient( | 165 std::unique_ptr<PushClient> push_client = WTF::WrapUnique(new PushClient( |
| 180 test_fifo.get(), param.push_buffer_size, param.push_jitter_range_ms)); | 166 test_fifo.get(), param.push_buffer_size, param.push_jitter_range_ms)); |
| 181 | 167 |
| 182 LOG(INFO) << "PushPullFIFOSmokeTest - Start"; | 168 Vector<WaitableEvent*> done_events; |
| 169 done_events.push_back( |
| 170 pull_client->Start(param.test_duration_ms, pull_interval_ms)); |
| 171 done_events.push_back( |
| 172 push_client->Start(param.test_duration_ms, push_interval_ms)); |
| 183 | 173 |
| 184 pull_client->Start(param.test_duration_ms, pull_interval_ms); | 174 LOG(INFO) << "PushPullFIFOSmokeTest - Started"; |
| 185 push_client->Start(param.test_duration_ms, push_interval_ms); | |
| 186 | 175 |
| 187 // If the operation does not cause a crash for the test period, it's passed. | 176 // We have to wait both of events to be signaled. |
| 188 // Also give a bit more time to finish the tear-down process. | 177 WaitableEvent::WaitMultiple(done_events); |
| 189 HoldTestForDuration(param.test_duration_ms + 150); | 178 WaitableEvent::WaitMultiple(done_events); |
| 190 } | 179 } |
| 191 | 180 |
| 192 FIFOSmokeTestParam smoke_test_params[] = { | 181 FIFOSmokeTestParam smoke_test_params[] = { |
| 193 // Test case 0 (OSX): 256 Pull, 128 Push, Minimal jitter. | 182 // Test case 0 (OSX): 256 Pull, 128 Push, Minimal jitter. |
| 194 // WebThread's priority is lower than the device thread, so its jitter range | 183 // WebThread's priority is lower than the device thread, so its jitter range |
| 195 // is slightly bigger than the other. | 184 // is slightly bigger than the other. |
| 196 {48000, 2, 8192, 1000, 256, 1, 128, 2}, | 185 {48000, 2, 8192, 1000, 256, 1, 128, 2}, |
| 197 | 186 |
| 198 // Test case 1 (Windows): 441 Pull, 128 Push. Moderate Jitter. | 187 // Test case 1 (Windows): 441 Pull, 128 Push. Moderate Jitter. |
| 199 // Windows' audio callback is known to be ~10ms and UMA data shows the | 188 // Windows' audio callback is known to be ~10ms and UMA data shows the |
| (...skipping 13 matching lines...) Expand all Loading... |
| 213 // low profile CPU. | 202 // low profile CPU. |
| 214 {44100, 2, 8192, 1000, 441, 24, 128, 8}, | 203 {44100, 2, 8192, 1000, 441, 24, 128, 8}, |
| 215 | 204 |
| 216 // Test case 5 (Android-ExternalB): 5768 Pull, 128 Push. Huge callback with | 205 // Test case 5 (Android-ExternalB): 5768 Pull, 128 Push. Huge callback with |
| 217 // large jitter. Low profile CPU. | 206 // large jitter. Low profile CPU. |
| 218 {44100, 2, 8192, 1000, 5768, 120, 128, 12}, | 207 {44100, 2, 8192, 1000, 5768, 120, 128, 12}, |
| 219 | 208 |
| 220 // Test case 6 (User-specified buffer size): 960 Pull, 128 Push. Minimal | 209 // Test case 6 (User-specified buffer size): 960 Pull, 128 Push. Minimal |
| 221 // Jitter. 960 frames = 20ms at 48KHz. | 210 // Jitter. 960 frames = 20ms at 48KHz. |
| 222 {48000, 2, 8192, 1000, 960, 1, 128, 1}, | 211 {48000, 2, 8192, 1000, 960, 1, 128, 1}, |
| 212 |
| 213 // Test case 7 (Longer test duration): 256 Pull, 128 Push. 10 seconds. |
| 214 {48000, 2, 8192, 10000, 256, 0, 128, 1} |
| 223 }; | 215 }; |
| 224 | 216 |
| 225 INSTANTIATE_TEST_CASE_P(PushPullFIFOSmokeTest, | 217 INSTANTIATE_TEST_CASE_P(PushPullFIFOSmokeTest, |
| 226 PushPullFIFOSmokeTest, | 218 PushPullFIFOSmokeTest, |
| 227 ::testing::ValuesIn(smoke_test_params)); | 219 ::testing::ValuesIn(smoke_test_params)); |
| 228 | 220 |
| 229 } // namespace | 221 } // namespace |
| 230 | 222 |
| 231 } // namespace blink | 223 } // namespace blink |
| OLD | NEW |