Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "platform/audio/PushPullFIFO.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <vector> | |
| 9 #include "platform/CrossThreadFunctional.h" | |
| 10 #include "platform/WaitableEvent.h" | |
| 11 #include "platform/WebTaskRunner.h" | |
| 12 #include "platform/audio/AudioUtilities.h" | |
| 13 #include "platform/testing/UnitTestHelpers.h" | |
| 14 #include "platform/wtf/Functional.h" | |
| 15 #include "platform/wtf/PtrUtil.h" | |
| 16 #include "public/platform/Platform.h" | |
| 17 #include "public/platform/WebThread.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace blink { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // To forcibly stop the message loop. | |
| 25 // TODO(hongchan): move this hack into Test class. | |
| 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. | |
|
o1ka
2017/04/25 08:23:22
Fix TODOs?
hongchan
2017/04/25 20:36:42
It seems to be impossible at the moment. Other Bli
| |
| 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 | |
| 43 // child class must define a specific task to run on the thread. | |
| 44 class FIFOClient { | |
| 45 public: | |
| 46 FIFOClient(PushPullFIFO* fifo, size_t jitter_range_ms) | |
| 47 : fifo_(fifo), | |
| 48 jitter_range_ms_(jitter_range_ms), | |
| 49 client_thread_(WTF::WrapUnique( | |
| 50 Platform::Current()->CreateThread("client thread"))) {} | |
| 51 | |
| 52 void Start(double duration_ms, double interval_ms) { | |
| 53 duration_ms_ = duration_ms; | |
| 54 interval_ms_ = interval_ms; | |
| 55 client_thread_->GetWebTaskRunner()->PostTask( | |
| 56 BLINK_FROM_HERE, | |
| 57 CrossThreadBind(&FIFOClient::RunTaskOnOwnThread, | |
| 58 CrossThreadUnretained(this))); | |
| 59 } | |
| 60 | |
| 61 virtual void RunTask() = 0; | |
| 62 virtual void Stop() = 0; | |
| 63 | |
| 64 protected: | |
| 65 RefPtr<AudioBus> bus_; | |
| 66 PushPullFIFO* fifo_; | |
| 67 double elapsed_ms_ = 0; | |
| 68 int counter_ = 0; | |
|
o1ka
2017/04/25 08:23:22
Could you refactor the code to make all the member
hongchan
2017/04/25 20:36:42
Done.
| |
| 69 | |
| 70 private: | |
| 71 void RunTaskOnOwnThread() { | |
| 72 double interval_with_jitter = interval_ms_ | |
| 73 + (static_cast<double>(std::rand()) / RAND_MAX) * jitter_range_ms_; | |
| 74 elapsed_ms_ += interval_with_jitter; | |
| 75 ++counter_; | |
| 76 RunTask(); | |
| 77 if (elapsed_ms_ < duration_ms_) { | |
| 78 client_thread_->GetWebTaskRunner()->PostDelayedTask( | |
| 79 BLINK_FROM_HERE, | |
| 80 CrossThreadBind(&FIFOClient::RunTaskOnOwnThread, | |
| 81 CrossThreadUnretained(this)), | |
| 82 interval_with_jitter); | |
| 83 } else { | |
| 84 Stop(); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 double duration_ms_; | |
| 89 double interval_ms_; | |
| 90 double jitter_range_ms_; | |
|
o1ka
2017/04/25 08:23:22
Add comments for these members?
hongchan
2017/04/25 20:36:42
Done.
| |
| 91 std::unique_ptr<WebThread> client_thread_; | |
| 92 }; | |
| 93 | |
| 94 // FIFO-pulling client (consumer). This mimics the audio device thread. | |
| 95 class PullClient : public FIFOClient { | |
| 96 public: | |
| 97 PullClient(PushPullFIFO* fifo, size_t frames_to_pull, double jitter_range_ms) | |
| 98 : FIFOClient(fifo, jitter_range_ms), | |
| 99 frames_to_pull_(frames_to_pull) { | |
| 100 bus_ = AudioBus::Create(fifo_->NumberOfChannels(), frames_to_pull_); | |
| 101 } | |
| 102 | |
| 103 void RunTask() override { | |
| 104 fifo_->Pull(bus_.Get(), frames_to_pull_); | |
| 105 } | |
| 106 | |
| 107 void Stop() override { | |
| 108 LOG(INFO) << "PullClient::Stop (" << counter_ << "calls)"; | |
| 109 } | |
| 110 | |
| 111 private: | |
| 112 size_t frames_to_pull_; | |
| 113 }; | |
| 114 | |
| 115 // FIFO-pushing client (producer). This mimics the WebAudio rendering thread. | |
| 116 class PushClient : public FIFOClient { | |
| 117 public: | |
| 118 PushClient(PushPullFIFO* fifo, size_t frames_to_push, double jitter_range_ms) | |
| 119 : FIFOClient(fifo, jitter_range_ms) { | |
| 120 bus_ = AudioBus::Create(fifo_->NumberOfChannels(), frames_to_push); | |
| 121 } | |
| 122 | |
| 123 void RunTask() override { | |
| 124 fifo_->Push(bus_.Get()); | |
| 125 } | |
| 126 | |
| 127 void Stop() override { | |
| 128 LOG(INFO) << "PushClient::Stop (" << counter_ << "calls)"; | |
| 129 } | |
| 130 }; | |
| 131 | |
| 132 struct FIFOSmokeTestParam { | |
| 133 const double sample_rate; | |
| 134 const unsigned number_of_channels; | |
| 135 const size_t fifo_length; | |
| 136 const double test_duration_ms; | |
| 137 // Buffer size for pulling. Equivalent of |callback_buffer_size|. | |
| 138 const size_t pull_buffer_size; | |
| 139 // Jitter added to the regular pulling interval, where j is | |
| 140 // 0 < j < jitter_range_ms. | |
| 141 const double pull_jitter_range_ms; | |
| 142 // Buffer size for pushing. Equivalent of WebAudio render quantum. | |
| 143 const size_t push_buffer_size; | |
| 144 // Jitter range for the pushing interval. | |
| 145 const double push_jitter_range_ms; | |
| 146 }; | |
| 147 | |
| 148 class PushPullFIFOSmokeTest | |
| 149 : public ::testing::TestWithParam<FIFOSmokeTestParam> {}; | |
| 150 | |
| 151 TEST_P(PushPullFIFOSmokeTest, SmokeTests) { | |
| 152 const FIFOSmokeTestParam param = GetParam(); | |
| 153 const double sample_rate = param.sample_rate * 4; | |
| 154 | |
| 155 const double pull_interval_ms = | |
| 156 param.pull_buffer_size / sample_rate * 1000; | |
| 157 const double push_interval_ms = | |
| 158 param.push_buffer_size / sample_rate * 1000; | |
| 159 | |
| 160 std::unique_ptr<PushPullFIFO> test_fifo = WTF::WrapUnique( | |
| 161 new PushPullFIFO(param.number_of_channels, param.fifo_length)); | |
| 162 std::unique_ptr<PullClient> pull_client = WTF::WrapUnique(new PullClient( | |
| 163 test_fifo.get(), param.pull_buffer_size, param.pull_jitter_range_ms)); | |
| 164 std::unique_ptr<PushClient> push_client = WTF::WrapUnique(new PushClient( | |
| 165 test_fifo.get(), param.push_buffer_size, param.push_jitter_range_ms)); | |
| 166 | |
| 167 LOG(INFO) << "PushPullFIFOSmokeTest - Start"; | |
| 168 | |
| 169 pull_client->Start(param.test_duration_ms, pull_interval_ms); | |
| 170 push_client->Start(param.test_duration_ms, push_interval_ms); | |
| 171 | |
| 172 // If the operation does not cause a crash for the test period, it's passed. | |
| 173 // Also give a bit more time to finish the tear-down process. | |
| 174 HoldTestForDuration(param.test_duration_ms + 150); | |
| 175 } | |
| 176 | |
| 177 FIFOSmokeTestParam smoke_test_params[] = { | |
| 178 // Test case 0 (OSX): 256 Pull, 128 Push, Minimal jitter. | |
| 179 // WebThread's priority is lower than the device thread, so its jitter range | |
| 180 // is slightly bigger than the other. | |
| 181 {48000, 2, 8192, 1000, 256, 1, 128, 2}, | |
| 182 | |
| 183 // Test case 1 (Windows): 480 Pull, 128 Push. Moderate Jitter. | |
|
o1ka
2017/04/25 08:23:22
Since WebAudio has introduced user-specified buffe
hongchan
2017/04/25 20:36:42
10ms = 480. (in 48000Hz)
I can do 960 as well if
| |
| 184 // Windows' audio callback is known to be ~10ms and UMA data shows the | |
| 185 // evidence for it. The jitter range was determined speculatively. | |
| 186 {48000, 2, 8192, 1000, 480, 2, 128, 3}, | |
| 187 | |
| 188 // Test case 2 (Ubuntu/Linux): 512 Pull, 128 Push. Unstable callback, but | |
| 189 // fast CPU. A typical configuration for Ubuntu + PulseAudio setup. | |
| 190 // PulseAudio's callback is known to be rather unstable. | |
| 191 {48000, 2, 8192, 1000, 512, 8, 128, 1}, | |
| 192 | |
| 193 // Test case 3 (Android-Reference): 512 Pull, 128 Push. Similar to Linux, but | |
| 194 // low profile CPU. | |
| 195 {44100, 2, 8192, 1000, 512, 8, 128, 3}, | |
| 196 | |
| 197 // Test case 4 (Android-ExternalA): 441 Pull, 128 Push. Extreme jitter with | |
| 198 // low profile CPU. | |
| 199 {44100, 2, 8192, 1000, 441, 24, 128, 8}, | |
| 200 | |
| 201 // Test case 5 (Android-ExternalB): 5768 Pull, 128 Push. Huge callback with | |
| 202 // large jitter. Low profile CPU. | |
| 203 {44100, 2, 8192, 1000, 5768, 120, 128, 12} | |
|
o1ka
2017/04/25 08:23:22
Consider running at least one test for a longer ti
hongchan
2017/04/25 20:36:42
Good idea. Done.
| |
| 204 }; | |
| 205 | |
| 206 INSTANTIATE_TEST_CASE_P(PushPullFIFOSmokeTest, | |
| 207 PushPullFIFOSmokeTest, | |
| 208 ::testing::ValuesIn(smoke_test_params)); | |
| 209 | |
| 210 } // namespace | |
| 211 | |
| 212 } // namespace blink | |
| OLD | NEW |