OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/macros.h" | 5 #include "base/macros.h" |
6 #include "base/memory/scoped_ptr.h" | |
6 #include "media/base/audio_timestamp_helper.h" | 7 #include "media/base/audio_timestamp_helper.h" |
7 #include "media/filters/audio_clock.h" | 8 #include "media/filters/audio_clock.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 namespace media { | 11 namespace media { |
11 | 12 |
12 class AudioClockTest : public testing::Test { | 13 class AudioClockTest : public testing::Test { |
13 public: | 14 public: |
14 AudioClockTest() | 15 AudioClockTest() {} |
15 : sample_rate_(10), clock_(base::TimeDelta(), sample_rate_) {} | |
16 | 16 |
17 ~AudioClockTest() override {} | 17 ~AudioClockTest() override {} |
18 | 18 |
19 void WroteAudio(int frames_written, | 19 void WroteAudio(int frames_written, |
20 int frames_requested, | 20 int frames_requested, |
21 int delay_frames, | 21 int delay_frames, |
22 double playback_rate) { | 22 double playback_rate) { |
23 clock_.WroteAudio( | 23 clock_->WroteAudio(frames_written, frames_requested, delay_frames, |
24 frames_written, frames_requested, delay_frames, playback_rate); | 24 playback_rate); |
25 } | 25 } |
26 | 26 |
27 int FrontTimestampInDays() { return clock_.front_timestamp().InDays(); } | 27 void SetupClock() { SetupClock(base::TimeDelta(), 10); } |
28 | |
29 void SetupClock(base::TimeDelta start_time, int sample_rate) { | |
30 sample_rate_ = sample_rate; | |
31 clock_.reset(new AudioClock(start_time, sample_rate_)); | |
32 } | |
33 | |
34 int FrontTimestampInDays() { return clock_->front_timestamp().InDays(); } | |
28 | 35 |
29 int FrontTimestampInMilliseconds() { | 36 int FrontTimestampInMilliseconds() { |
30 return clock_.front_timestamp().InMilliseconds(); | 37 return clock_->front_timestamp().InMilliseconds(); |
31 } | 38 } |
32 | 39 |
33 int BackTimestampInMilliseconds() { | 40 int BackTimestampInMilliseconds() { |
34 return clock_.back_timestamp().InMilliseconds(); | 41 return clock_->back_timestamp().InMilliseconds(); |
35 } | 42 } |
36 | 43 |
37 int TimeUntilPlaybackInMilliseconds(int timestamp_ms) { | 44 int TimeUntilPlaybackInMilliseconds(int timestamp_ms) { |
38 return clock_.TimeUntilPlayback(base::TimeDelta::FromMilliseconds( | 45 return clock_ |
39 timestamp_ms)).InMilliseconds(); | 46 ->TimeUntilPlayback(base::TimeDelta::FromMilliseconds(timestamp_ms)) |
47 .InMilliseconds(); | |
40 } | 48 } |
41 | 49 |
42 int ContiguousAudioDataBufferedInDays() { | 50 int ContiguousAudioDataBufferedInDays() { |
43 base::TimeDelta total, same_rate_total; | 51 base::TimeDelta total, same_rate_total; |
44 clock_.ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); | 52 clock_->ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); |
45 return total.InDays(); | 53 return total.InDays(); |
46 } | 54 } |
47 | 55 |
48 int ContiguousAudioDataBufferedInMilliseconds() { | 56 int ContiguousAudioDataBufferedInMilliseconds() { |
49 base::TimeDelta total, same_rate_total; | 57 base::TimeDelta total, same_rate_total; |
50 clock_.ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); | 58 clock_->ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); |
51 return total.InMilliseconds(); | 59 return total.InMilliseconds(); |
52 } | 60 } |
53 | 61 |
54 int ContiguousAudioDataBufferedAtSameRateInMilliseconds() { | 62 int ContiguousAudioDataBufferedAtSameRateInMilliseconds() { |
55 base::TimeDelta total, same_rate_total; | 63 base::TimeDelta total, same_rate_total; |
56 clock_.ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); | 64 clock_->ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); |
57 return same_rate_total.InMilliseconds(); | 65 return same_rate_total.InMilliseconds(); |
58 } | 66 } |
59 | 67 |
60 const int sample_rate_; | 68 int sample_rate_; |
61 AudioClock clock_; | 69 scoped_ptr<AudioClock> clock_; |
62 | 70 |
63 private: | 71 private: |
64 DISALLOW_COPY_AND_ASSIGN(AudioClockTest); | 72 DISALLOW_COPY_AND_ASSIGN(AudioClockTest); |
65 }; | 73 }; |
66 | 74 |
67 TEST_F(AudioClockTest, FrontTimestampStartsAtStartTimestamp) { | 75 TEST_F(AudioClockTest, FrontTimestampStartsAtStartTimestamp) { |
76 SetupClock(); | |
DaleCurtis
2016/02/18 01:48:33
Instead of having every method call this, just cal
chcunningham
2016/02/18 20:43:37
Done.
| |
77 | |
68 base::TimeDelta expected = base::TimeDelta::FromSeconds(123); | 78 base::TimeDelta expected = base::TimeDelta::FromSeconds(123); |
69 AudioClock clock(expected, sample_rate_); | 79 AudioClock clock(expected, sample_rate_); |
70 | 80 |
71 EXPECT_EQ(expected, clock.front_timestamp()); | 81 EXPECT_EQ(expected, clock.front_timestamp()); |
72 } | 82 } |
73 | 83 |
74 TEST_F(AudioClockTest, BackTimestampStartsAtStartTimestamp) { | 84 TEST_F(AudioClockTest, BackTimestampStartsAtStartTimestamp) { |
85 SetupClock(); | |
86 | |
75 base::TimeDelta expected = base::TimeDelta::FromSeconds(123); | 87 base::TimeDelta expected = base::TimeDelta::FromSeconds(123); |
76 AudioClock clock(expected, sample_rate_); | 88 AudioClock clock(expected, sample_rate_); |
77 | 89 |
78 EXPECT_EQ(expected, clock.back_timestamp()); | 90 EXPECT_EQ(expected, clock.back_timestamp()); |
79 } | 91 } |
80 | 92 |
81 TEST_F(AudioClockTest, Playback) { | 93 TEST_F(AudioClockTest, Playback) { |
94 SetupClock(); | |
95 | |
82 // The first time we write data we should still expect our start timestamp | 96 // The first time we write data we should still expect our start timestamp |
83 // due to delay. | 97 // due to delay. |
84 WroteAudio(10, 10, 20, 1.0); | 98 WroteAudio(10, 10, 20, 1.0); |
85 EXPECT_EQ(0, FrontTimestampInMilliseconds()); | 99 EXPECT_EQ(0, FrontTimestampInMilliseconds()); |
86 EXPECT_EQ(1000, BackTimestampInMilliseconds()); | 100 EXPECT_EQ(1000, BackTimestampInMilliseconds()); |
87 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); | 101 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); |
88 EXPECT_EQ(0, ContiguousAudioDataBufferedAtSameRateInMilliseconds()); | 102 EXPECT_EQ(0, ContiguousAudioDataBufferedAtSameRateInMilliseconds()); |
89 | 103 |
90 // The media time should remain at start timestamp as we write data. | 104 // The media time should remain at start timestamp as we write data. |
91 WroteAudio(10, 10, 20, 1.0); | 105 WroteAudio(10, 10, 20, 1.0); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 | 204 |
191 // At this point media time should stop increasing. | 205 // At this point media time should stop increasing. |
192 WroteAudio(0, 10, 20, 2); | 206 WroteAudio(0, 10, 20, 2); |
193 EXPECT_EQ(15000, FrontTimestampInMilliseconds()); | 207 EXPECT_EQ(15000, FrontTimestampInMilliseconds()); |
194 EXPECT_EQ(15000, BackTimestampInMilliseconds()); | 208 EXPECT_EQ(15000, BackTimestampInMilliseconds()); |
195 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); | 209 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); |
196 EXPECT_EQ(0, ContiguousAudioDataBufferedAtSameRateInMilliseconds()); | 210 EXPECT_EQ(0, ContiguousAudioDataBufferedAtSameRateInMilliseconds()); |
197 } | 211 } |
198 | 212 |
199 TEST_F(AudioClockTest, AlternatingAudioAndSilence) { | 213 TEST_F(AudioClockTest, AlternatingAudioAndSilence) { |
214 SetupClock(); | |
215 | |
200 // Buffer #1: [0, 1000) | 216 // Buffer #1: [0, 1000) |
201 WroteAudio(10, 10, 20, 1.0); | 217 WroteAudio(10, 10, 20, 1.0); |
202 EXPECT_EQ(0, FrontTimestampInMilliseconds()); | 218 EXPECT_EQ(0, FrontTimestampInMilliseconds()); |
203 EXPECT_EQ(1000, BackTimestampInMilliseconds()); | 219 EXPECT_EQ(1000, BackTimestampInMilliseconds()); |
204 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); | 220 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); |
205 | 221 |
206 // Buffer #2: 1000ms of silence | 222 // Buffer #2: 1000ms of silence |
207 WroteAudio(0, 10, 20, 1.0); | 223 WroteAudio(0, 10, 20, 1.0); |
208 EXPECT_EQ(0, FrontTimestampInMilliseconds()); | 224 EXPECT_EQ(0, FrontTimestampInMilliseconds()); |
209 EXPECT_EQ(1000, BackTimestampInMilliseconds()); | 225 EXPECT_EQ(1000, BackTimestampInMilliseconds()); |
(...skipping 16 matching lines...) Expand all Loading... | |
226 | 242 |
227 // Buffer #5: [2000, 3000): | 243 // Buffer #5: [2000, 3000): |
228 // - Buffer #3 is at front with 1000ms of contiguous audio data | 244 // - Buffer #3 is at front with 1000ms of contiguous audio data |
229 WroteAudio(10, 10, 20, 1.0); | 245 WroteAudio(10, 10, 20, 1.0); |
230 EXPECT_EQ(1000, FrontTimestampInMilliseconds()); | 246 EXPECT_EQ(1000, FrontTimestampInMilliseconds()); |
231 EXPECT_EQ(3000, BackTimestampInMilliseconds()); | 247 EXPECT_EQ(3000, BackTimestampInMilliseconds()); |
232 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds()); | 248 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds()); |
233 } | 249 } |
234 | 250 |
235 TEST_F(AudioClockTest, ZeroDelay) { | 251 TEST_F(AudioClockTest, ZeroDelay) { |
252 SetupClock(); | |
253 | |
236 // The first time we write data we should expect the first timestamp | 254 // The first time we write data we should expect the first timestamp |
237 // immediately. | 255 // immediately. |
238 WroteAudio(10, 10, 0, 1.0); | 256 WroteAudio(10, 10, 0, 1.0); |
239 EXPECT_EQ(0, FrontTimestampInMilliseconds()); | 257 EXPECT_EQ(0, FrontTimestampInMilliseconds()); |
240 EXPECT_EQ(1000, BackTimestampInMilliseconds()); | 258 EXPECT_EQ(1000, BackTimestampInMilliseconds()); |
241 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds()); | 259 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds()); |
242 | 260 |
243 // Ditto for all subsequent buffers. | 261 // Ditto for all subsequent buffers. |
244 WroteAudio(10, 10, 0, 1.0); | 262 WroteAudio(10, 10, 0, 1.0); |
245 EXPECT_EQ(1000, FrontTimestampInMilliseconds()); | 263 EXPECT_EQ(1000, FrontTimestampInMilliseconds()); |
(...skipping 11 matching lines...) Expand all Loading... | |
257 EXPECT_EQ(3000, BackTimestampInMilliseconds()); | 275 EXPECT_EQ(3000, BackTimestampInMilliseconds()); |
258 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); | 276 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); |
259 | 277 |
260 WroteAudio(0, 10, 0, 1.0); | 278 WroteAudio(0, 10, 0, 1.0); |
261 EXPECT_EQ(3000, FrontTimestampInMilliseconds()); | 279 EXPECT_EQ(3000, FrontTimestampInMilliseconds()); |
262 EXPECT_EQ(3000, BackTimestampInMilliseconds()); | 280 EXPECT_EQ(3000, BackTimestampInMilliseconds()); |
263 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); | 281 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); |
264 } | 282 } |
265 | 283 |
266 TEST_F(AudioClockTest, TimeUntilPlayback) { | 284 TEST_F(AudioClockTest, TimeUntilPlayback) { |
285 SetupClock(); | |
286 | |
267 // Construct an audio clock with the following representation: | 287 // Construct an audio clock with the following representation: |
268 // | 288 // |
269 // existing | 289 // existing |
270 // |- delay -|------------------ calls to WroteAudio() ------------------| | 290 // |- delay -|------------------ calls to WroteAudio() ------------------| |
271 // +------------+---------+------------+-----------+------------+-----------+ | 291 // +------------+---------+------------+-----------+------------+-----------+ |
272 // | 20 silence | 10 @ 1x | 10 silence | 10 @ 0.5x | 10 silence | 10 @ 2.0x | | 292 // | 20 silence | 10 @ 1x | 10 silence | 10 @ 0.5x | 10 silence | 10 @ 2.0x | |
273 // +------------+---------+------------+-----------+------------+-----------+ | 293 // +------------+---------+------------+-----------+------------+-----------+ |
274 // Media: 0 1000 1000 1500 1500 3500 | 294 // Media: 0 1000 1000 1500 1500 3500 |
275 // Wall: 2000 3000 4000 5000 6000 7000 | 295 // Wall: 2000 3000 4000 5000 6000 7000 |
276 WroteAudio(10, 10, 60, 1.0); | 296 WroteAudio(10, 10, 60, 1.0); |
(...skipping 12 matching lines...) Expand all Loading... | |
289 // silence into account. | 309 // silence into account. |
290 EXPECT_EQ(2500, TimeUntilPlaybackInMilliseconds(500)); | 310 EXPECT_EQ(2500, TimeUntilPlaybackInMilliseconds(500)); |
291 EXPECT_EQ(3000, TimeUntilPlaybackInMilliseconds(1000)); | 311 EXPECT_EQ(3000, TimeUntilPlaybackInMilliseconds(1000)); |
292 EXPECT_EQ(4500, TimeUntilPlaybackInMilliseconds(1250)); | 312 EXPECT_EQ(4500, TimeUntilPlaybackInMilliseconds(1250)); |
293 EXPECT_EQ(5000, TimeUntilPlaybackInMilliseconds(1500)); | 313 EXPECT_EQ(5000, TimeUntilPlaybackInMilliseconds(1500)); |
294 EXPECT_EQ(6500, TimeUntilPlaybackInMilliseconds(2500)); | 314 EXPECT_EQ(6500, TimeUntilPlaybackInMilliseconds(2500)); |
295 EXPECT_EQ(7000, TimeUntilPlaybackInMilliseconds(3500)); | 315 EXPECT_EQ(7000, TimeUntilPlaybackInMilliseconds(3500)); |
296 } | 316 } |
297 | 317 |
298 TEST_F(AudioClockTest, SupportsYearsWorthOfAudioData) { | 318 TEST_F(AudioClockTest, SupportsYearsWorthOfAudioData) { |
319 SetupClock(); | |
320 | |
299 // Use number of frames that would be likely to overflow 32-bit integer math. | 321 // Use number of frames that would be likely to overflow 32-bit integer math. |
300 const int huge_amount_of_frames = std::numeric_limits<int>::max(); | 322 const int huge_amount_of_frames = std::numeric_limits<int>::max(); |
301 const base::TimeDelta huge = | 323 const base::TimeDelta huge = |
302 base::TimeDelta::FromSeconds(huge_amount_of_frames / sample_rate_); | 324 base::TimeDelta::FromSeconds(huge_amount_of_frames / sample_rate_); |
303 EXPECT_EQ(2485, huge.InDays()); // Just to give some context on how big... | 325 EXPECT_EQ(2485, huge.InDays()); // Just to give some context on how big... |
304 | 326 |
305 // Use zero delay to test calculation of current timestamp. | 327 // Use zero delay to test calculation of current timestamp. |
306 WroteAudio(huge_amount_of_frames, huge_amount_of_frames, 0, 1.0); | 328 WroteAudio(huge_amount_of_frames, huge_amount_of_frames, 0, 1.0); |
307 EXPECT_EQ(0, FrontTimestampInDays()); | 329 EXPECT_EQ(0, FrontTimestampInDays()); |
308 EXPECT_EQ(2485, ContiguousAudioDataBufferedInDays()); | 330 EXPECT_EQ(2485, ContiguousAudioDataBufferedInDays()); |
(...skipping 11 matching lines...) Expand all Loading... | |
320 EXPECT_EQ(huge.InDays(), ContiguousAudioDataBufferedInDays()); | 342 EXPECT_EQ(huge.InDays(), ContiguousAudioDataBufferedInDays()); |
321 | 343 |
322 // Use huge delay to test calculation of buffered data. | 344 // Use huge delay to test calculation of buffered data. |
323 WroteAudio( | 345 WroteAudio( |
324 huge_amount_of_frames, huge_amount_of_frames, huge_amount_of_frames, 1.0); | 346 huge_amount_of_frames, huge_amount_of_frames, huge_amount_of_frames, 1.0); |
325 EXPECT_EQ((huge * 3).InDays(), FrontTimestampInDays()); | 347 EXPECT_EQ((huge * 3).InDays(), FrontTimestampInDays()); |
326 EXPECT_EQ((huge * 2).InDays(), ContiguousAudioDataBufferedInDays()); | 348 EXPECT_EQ((huge * 2).InDays(), ContiguousAudioDataBufferedInDays()); |
327 } | 349 } |
328 | 350 |
329 TEST_F(AudioClockTest, CompensateForSuspendedWrites) { | 351 TEST_F(AudioClockTest, CompensateForSuspendedWrites) { |
352 SetupClock(); | |
353 | |
330 // Buffer 6 seconds of delay and 1 second of audio data. | 354 // Buffer 6 seconds of delay and 1 second of audio data. |
331 WroteAudio(10, 10, 60, 1.0); | 355 WroteAudio(10, 10, 60, 1.0); |
332 | 356 |
333 // Media timestamp zero has to wait for silence to pass. | 357 // Media timestamp zero has to wait for silence to pass. |
334 const int kBaseTimeMs = 6000; | 358 const int kBaseTimeMs = 6000; |
335 EXPECT_EQ(kBaseTimeMs, TimeUntilPlaybackInMilliseconds(0)); | 359 EXPECT_EQ(kBaseTimeMs, TimeUntilPlaybackInMilliseconds(0)); |
336 | 360 |
337 // Elapsing frames less than we have buffered should do nothing. | 361 // Elapsing frames less than we have buffered should do nothing. |
338 const int kDelayFrames = 2; | 362 const int kDelayFrames = 2; |
339 for (int i = 1000; i <= kBaseTimeMs; i += 1000) { | 363 for (int i = 1000; i <= kBaseTimeMs; i += 1000) { |
340 clock_.CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(i), | 364 clock_->CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(i), |
341 kDelayFrames); | 365 kDelayFrames); |
342 EXPECT_EQ(kBaseTimeMs - (i - 1000), TimeUntilPlaybackInMilliseconds(0)); | 366 EXPECT_EQ(kBaseTimeMs - (i - 1000), TimeUntilPlaybackInMilliseconds(0)); |
343 | 367 |
344 // Write silence to simulate maintaining a 7s output buffer. | 368 // Write silence to simulate maintaining a 7s output buffer. |
345 WroteAudio(0, 10, 60, 1.0); | 369 WroteAudio(0, 10, 60, 1.0); |
346 } | 370 } |
347 | 371 |
348 // Exhausting all frames should advance timestamps and prime the buffer with | 372 // Exhausting all frames should advance timestamps and prime the buffer with |
349 // our delay frames value. | 373 // our delay frames value. |
350 clock_.CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(7000), | 374 clock_->CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(7000), |
351 kDelayFrames); | 375 kDelayFrames); |
352 EXPECT_EQ(kDelayFrames * 100, TimeUntilPlaybackInMilliseconds(1000)); | 376 EXPECT_EQ(kDelayFrames * 100, TimeUntilPlaybackInMilliseconds(1000)); |
353 } | 377 } |
354 | 378 |
379 TEST_F(AudioClockTest, FramesToTimePrecision) { | |
380 SetupClock(base::TimeDelta(), 48000); | |
381 int frames_written = 0; | |
382 | |
383 double micros_per_frame = | |
384 static_cast<double>(base::Time::kMicrosecondsPerSecond) / 48000; | |
DaleCurtis
2016/02/18 01:48:33
I'd just use 48000.0 to avoid the cast.
chcunningham
2016/02/18 20:43:37
Done.
| |
385 | |
386 // Write ~2 hours of data to clock to give any error a significant chance to | |
387 // accumulate. | |
388 while (clock_->back_timestamp() <= base::TimeDelta::FromHours(2)) { | |
389 frames_written += 1024; | |
390 WroteAudio(1024, 1024, 0, 1); | |
391 } | |
392 | |
393 // Verify no error accumulated. | |
394 EXPECT_EQ(std::round(frames_written * micros_per_frame), | |
395 clock_->back_timestamp().InMicroseconds()); | |
396 } | |
397 | |
355 } // namespace media | 398 } // namespace media |
OLD | NEW |