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" |
(...skipping 18 matching lines...) Expand all Loading... |
29 : fifo_(fifo), | 29 : fifo_(fifo), |
30 bus_(AudioBus::Create(fifo->NumberOfChannels(), bus_length)), | 30 bus_(AudioBus::Create(fifo->NumberOfChannels(), bus_length)), |
31 client_thread_(Platform::Current()->CreateThread("client thread")), | 31 client_thread_(Platform::Current()->CreateThread("client thread")), |
32 done_event_(WTF::MakeUnique<WaitableEvent>()), | 32 done_event_(WTF::MakeUnique<WaitableEvent>()), |
33 jitter_range_ms_(jitter_range_ms) {} | 33 jitter_range_ms_(jitter_range_ms) {} |
34 | 34 |
35 WaitableEvent* Start(double duration_ms, double interval_ms) { | 35 WaitableEvent* Start(double duration_ms, double interval_ms) { |
36 duration_ms_ = duration_ms; | 36 duration_ms_ = duration_ms; |
37 interval_ms_ = interval_ms; | 37 interval_ms_ = interval_ms; |
38 client_thread_->GetWebTaskRunner()->PostTask( | 38 client_thread_->GetWebTaskRunner()->PostTask( |
39 BLINK_FROM_HERE, | 39 BLINK_FROM_HERE, CrossThreadBind(&FIFOClient::RunTaskOnOwnThread, |
40 CrossThreadBind(&FIFOClient::RunTaskOnOwnThread, | 40 CrossThreadUnretained(this))); |
41 CrossThreadUnretained(this))); | |
42 return done_event_.get(); | 41 return done_event_.get(); |
43 } | 42 } |
44 | 43 |
45 virtual void Stop(int callback_counter) = 0; | 44 virtual void Stop(int callback_counter) = 0; |
46 virtual void RunTask() = 0; | 45 virtual void RunTask() = 0; |
47 | 46 |
48 void Pull(size_t frames_to_pull) { | 47 void Pull(size_t frames_to_pull) { fifo_->Pull(bus_.Get(), frames_to_pull); } |
49 fifo_->Pull(bus_.Get(), frames_to_pull); | |
50 } | |
51 | 48 |
52 void Push() { | 49 void Push() { fifo_->Push(bus_.Get()); } |
53 fifo_->Push(bus_.Get()); | |
54 } | |
55 | 50 |
56 private: | 51 private: |
57 void RunTaskOnOwnThread() { | 52 void RunTaskOnOwnThread() { |
58 double interval_with_jitter = interval_ms_ | 53 double interval_with_jitter = |
59 + (static_cast<double>(std::rand()) / RAND_MAX) * jitter_range_ms_; | 54 interval_ms_ + |
| 55 (static_cast<double>(std::rand()) / RAND_MAX) * jitter_range_ms_; |
60 elapsed_ms_ += interval_with_jitter; | 56 elapsed_ms_ += interval_with_jitter; |
61 ++counter_; | 57 ++counter_; |
62 RunTask(); | 58 RunTask(); |
63 if (elapsed_ms_ < duration_ms_) { | 59 if (elapsed_ms_ < duration_ms_) { |
64 client_thread_->GetWebTaskRunner()->PostDelayedTask( | 60 client_thread_->GetWebTaskRunner()->PostDelayedTask( |
65 BLINK_FROM_HERE, | 61 BLINK_FROM_HERE, |
66 CrossThreadBind(&FIFOClient::RunTaskOnOwnThread, | 62 CrossThreadBind(&FIFOClient::RunTaskOnOwnThread, |
67 CrossThreadUnretained(this)), | 63 CrossThreadUnretained(this)), |
68 TimeDelta::FromMillisecondsD(interval_with_jitter)); | 64 TimeDelta::FromMillisecondsD(interval_with_jitter)); |
69 } else { | 65 } else { |
(...skipping 23 matching lines...) Expand all Loading... |
93 // Counter variable for the total number of callbacks invoked. | 89 // Counter variable for the total number of callbacks invoked. |
94 int counter_ = 0; | 90 int counter_ = 0; |
95 }; | 91 }; |
96 | 92 |
97 // FIFO-pulling client (consumer). This mimics the audio device thread. | 93 // FIFO-pulling client (consumer). This mimics the audio device thread. |
98 // |frames_to_pull| is variable. | 94 // |frames_to_pull| is variable. |
99 class PullClient : public FIFOClient { | 95 class PullClient : public FIFOClient { |
100 public: | 96 public: |
101 PullClient(PushPullFIFO* fifo, size_t frames_to_pull, double jitter_range_ms) | 97 PullClient(PushPullFIFO* fifo, size_t frames_to_pull, double jitter_range_ms) |
102 : FIFOClient(fifo, frames_to_pull, jitter_range_ms), | 98 : FIFOClient(fifo, frames_to_pull, jitter_range_ms), |
103 frames_to_pull_(frames_to_pull) { | 99 frames_to_pull_(frames_to_pull) {} |
104 } | |
105 | 100 |
106 void RunTask() override { | 101 void RunTask() override { Pull(frames_to_pull_); } |
107 Pull(frames_to_pull_); | |
108 } | |
109 | 102 |
110 void Stop(int callback_counter) override { | 103 void Stop(int callback_counter) override { |
111 LOG(INFO) << "PullClient stopped. (" << callback_counter << " calls)"; | 104 LOG(INFO) << "PullClient stopped. (" << callback_counter << " calls)"; |
112 } | 105 } |
113 | 106 |
114 private: | 107 private: |
115 size_t frames_to_pull_; | 108 size_t frames_to_pull_; |
116 }; | 109 }; |
117 | 110 |
118 // FIFO-pushing client (producer). This mimics the WebAudio rendering thread. | 111 // FIFO-pushing client (producer). This mimics the WebAudio rendering thread. |
119 // The frames to push are static as 128 frames. | 112 // The frames to push are static as 128 frames. |
120 class PushClient : public FIFOClient { | 113 class PushClient : public FIFOClient { |
121 public: | 114 public: |
122 PushClient(PushPullFIFO* fifo, size_t frames_to_push, double jitter_range_ms) | 115 PushClient(PushPullFIFO* fifo, size_t frames_to_push, double jitter_range_ms) |
123 : FIFOClient(fifo, frames_to_push, jitter_range_ms) {} | 116 : FIFOClient(fifo, frames_to_push, jitter_range_ms) {} |
124 | 117 |
125 void RunTask() override { | 118 void RunTask() override { Push(); } |
126 Push(); | |
127 } | |
128 | 119 |
129 void Stop(int callback_counter) override { | 120 void Stop(int callback_counter) override { |
130 LOG(INFO) << "PushClient stopped. (" << callback_counter << " calls)"; | 121 LOG(INFO) << "PushClient stopped. (" << callback_counter << " calls)"; |
131 } | 122 } |
132 }; | 123 }; |
133 | 124 |
134 struct FIFOSmokeTestParam { | 125 struct FIFOSmokeTestParam { |
135 const double sample_rate; | 126 const double sample_rate; |
136 const unsigned number_of_channels; | 127 const unsigned number_of_channels; |
137 const size_t fifo_length; | 128 const size_t fifo_length; |
138 const double test_duration_ms; | 129 const double test_duration_ms; |
139 // Buffer size for pulling. Equivalent of |callback_buffer_size|. | 130 // Buffer size for pulling. Equivalent of |callback_buffer_size|. |
140 const size_t pull_buffer_size; | 131 const size_t pull_buffer_size; |
141 // Jitter range for the pulling interval. | 132 // Jitter range for the pulling interval. |
142 const double pull_jitter_range_ms; | 133 const double pull_jitter_range_ms; |
143 // Buffer size for pushing. Equivalent of WebAudio render quantum. | 134 // Buffer size for pushing. Equivalent of WebAudio render quantum. |
144 const size_t push_buffer_size; | 135 const size_t push_buffer_size; |
145 // Jitter range for the pushing interval. | 136 // Jitter range for the pushing interval. |
146 const double push_jitter_range_ms; | 137 const double push_jitter_range_ms; |
147 }; | 138 }; |
148 | 139 |
149 class PushPullFIFOSmokeTest | 140 class PushPullFIFOSmokeTest |
150 : public ::testing::TestWithParam<FIFOSmokeTestParam> {}; | 141 : public ::testing::TestWithParam<FIFOSmokeTestParam> {}; |
151 | 142 |
152 TEST_P(PushPullFIFOSmokeTest, SmokeTests) { | 143 TEST_P(PushPullFIFOSmokeTest, SmokeTests) { |
153 const FIFOSmokeTestParam param = GetParam(); | 144 const FIFOSmokeTestParam param = GetParam(); |
154 const double sample_rate = param.sample_rate * 4; | 145 const double sample_rate = param.sample_rate * 4; |
155 | 146 |
156 const double pull_interval_ms = | 147 const double pull_interval_ms = param.pull_buffer_size / sample_rate * 1000; |
157 param.pull_buffer_size / sample_rate * 1000; | 148 const double push_interval_ms = param.push_buffer_size / sample_rate * 1000; |
158 const double push_interval_ms = | |
159 param.push_buffer_size / sample_rate * 1000; | |
160 | 149 |
161 std::unique_ptr<PushPullFIFO> test_fifo = WTF::WrapUnique( | 150 std::unique_ptr<PushPullFIFO> test_fifo = WTF::WrapUnique( |
162 new PushPullFIFO(param.number_of_channels, param.fifo_length)); | 151 new PushPullFIFO(param.number_of_channels, param.fifo_length)); |
163 std::unique_ptr<PullClient> pull_client = WTF::WrapUnique(new PullClient( | 152 std::unique_ptr<PullClient> pull_client = WTF::WrapUnique(new PullClient( |
164 test_fifo.get(), param.pull_buffer_size, param.pull_jitter_range_ms)); | 153 test_fifo.get(), param.pull_buffer_size, param.pull_jitter_range_ms)); |
165 std::unique_ptr<PushClient> push_client = WTF::WrapUnique(new PushClient( | 154 std::unique_ptr<PushClient> push_client = WTF::WrapUnique(new PushClient( |
166 test_fifo.get(), param.push_buffer_size, param.push_jitter_range_ms)); | 155 test_fifo.get(), param.push_buffer_size, param.push_jitter_range_ms)); |
167 | 156 |
168 Vector<WaitableEvent*> done_events; | 157 Vector<WaitableEvent*> done_events; |
169 done_events.push_back( | 158 done_events.push_back( |
170 pull_client->Start(param.test_duration_ms, pull_interval_ms)); | 159 pull_client->Start(param.test_duration_ms, pull_interval_ms)); |
171 done_events.push_back( | 160 done_events.push_back( |
172 push_client->Start(param.test_duration_ms, push_interval_ms)); | 161 push_client->Start(param.test_duration_ms, push_interval_ms)); |
173 | 162 |
174 LOG(INFO) << "PushPullFIFOSmokeTest - Started"; | 163 LOG(INFO) << "PushPullFIFOSmokeTest - Started"; |
175 | 164 |
176 // We have to wait both of events to be signaled. | 165 // We have to wait both of events to be signaled. |
177 WaitableEvent::WaitMultiple(done_events); | 166 WaitableEvent::WaitMultiple(done_events); |
178 WaitableEvent::WaitMultiple(done_events); | 167 WaitableEvent::WaitMultiple(done_events); |
179 } | 168 } |
180 | 169 |
181 FIFOSmokeTestParam smoke_test_params[] = { | 170 FIFOSmokeTestParam smoke_test_params[] = { |
182 // Test case 0 (OSX): 256 Pull, 128 Push, Minimal jitter. | 171 // Test case 0 (OSX): 256 Pull, 128 Push, Minimal jitter. |
183 // WebThread's priority is lower than the device thread, so its jitter range | 172 // WebThread's priority is lower than the device thread, so its jitter range |
184 // is slightly bigger than the other. | 173 // is slightly bigger than the other. |
185 {48000, 2, 8192, 1000, 256, 1, 128, 2}, | 174 {48000, 2, 8192, 1000, 256, 1, 128, 2}, |
186 | 175 |
187 // Test case 1 (Windows): 441 Pull, 128 Push. Moderate Jitter. | 176 // Test case 1 (Windows): 441 Pull, 128 Push. Moderate Jitter. |
188 // Windows' audio callback is known to be ~10ms and UMA data shows the | 177 // Windows' audio callback is known to be ~10ms and UMA data shows the |
189 // evidence for it. The jitter range was determined speculatively. | 178 // evidence for it. The jitter range was determined speculatively. |
190 {44100, 2, 8192, 1000, 441, 2, 128, 3}, | 179 {44100, 2, 8192, 1000, 441, 2, 128, 3}, |
191 | 180 |
192 // Test case 2 (Ubuntu/Linux): 512 Pull, 128 Push. Unstable callback, but | 181 // Test case 2 (Ubuntu/Linux): 512 Pull, 128 Push. Unstable callback, but |
193 // fast CPU. A typical configuration for Ubuntu + PulseAudio setup. | 182 // fast CPU. A typical configuration for Ubuntu + PulseAudio setup. |
194 // PulseAudio's callback is known to be rather unstable. | 183 // PulseAudio's callback is known to be rather unstable. |
195 {48000, 2, 8192, 1000, 512, 8, 128, 1}, | 184 {48000, 2, 8192, 1000, 512, 8, 128, 1}, |
196 | 185 |
197 // Test case 3 (Android-Reference): 512 Pull, 128 Push. Similar to Linux, but | 186 // Test case 3 (Android-Reference): 512 Pull, 128 Push. Similar to Linux, |
198 // low profile CPU. | 187 // but |
199 {44100, 2, 8192, 1000, 512, 8, 128, 3}, | 188 // low profile CPU. |
| 189 {44100, 2, 8192, 1000, 512, 8, 128, 3}, |
200 | 190 |
201 // Test case 4 (Android-ExternalA): 441 Pull, 128 Push. Extreme jitter with | 191 // Test case 4 (Android-ExternalA): 441 Pull, 128 Push. Extreme jitter with |
202 // low profile CPU. | 192 // low profile CPU. |
203 {44100, 2, 8192, 1000, 441, 24, 128, 8}, | 193 {44100, 2, 8192, 1000, 441, 24, 128, 8}, |
204 | 194 |
205 // Test case 5 (Android-ExternalB): 5768 Pull, 128 Push. Huge callback with | 195 // Test case 5 (Android-ExternalB): 5768 Pull, 128 Push. Huge callback with |
206 // large jitter. Low profile CPU. | 196 // large jitter. Low profile CPU. |
207 {44100, 2, 8192, 1000, 5768, 120, 128, 12}, | 197 {44100, 2, 8192, 1000, 5768, 120, 128, 12}, |
208 | 198 |
209 // Test case 6 (User-specified buffer size): 960 Pull, 128 Push. Minimal | 199 // Test case 6 (User-specified buffer size): 960 Pull, 128 Push. Minimal |
210 // Jitter. 960 frames = 20ms at 48KHz. | 200 // Jitter. 960 frames = 20ms at 48KHz. |
211 {48000, 2, 8192, 1000, 960, 1, 128, 1}, | 201 {48000, 2, 8192, 1000, 960, 1, 128, 1}, |
212 | 202 |
213 // Test case 7 (Longer test duration): 256 Pull, 128 Push. 10 seconds. | 203 // Test case 7 (Longer test duration): 256 Pull, 128 Push. 10 seconds. |
214 {48000, 2, 8192, 10000, 256, 0, 128, 1} | 204 {48000, 2, 8192, 10000, 256, 0, 128, 1}}; |
215 }; | |
216 | 205 |
217 INSTANTIATE_TEST_CASE_P(PushPullFIFOSmokeTest, | 206 INSTANTIATE_TEST_CASE_P(PushPullFIFOSmokeTest, |
218 PushPullFIFOSmokeTest, | 207 PushPullFIFOSmokeTest, |
219 ::testing::ValuesIn(smoke_test_params)); | 208 ::testing::ValuesIn(smoke_test_params)); |
220 | 209 |
221 } // namespace | 210 } // namespace |
222 | 211 |
223 } // namespace blink | 212 } // namespace blink |
OLD | NEW |