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 "media/base/audio_timestamp_helper.h" | 5 #include "media/base/audio_timestamp_helper.h" |
6 #include "media/base/buffers.h" | 6 #include "media/base/buffers.h" |
7 #include "media/filters/audio_clock.h" | 7 #include "media/filters/audio_clock.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace media { | 10 namespace media { |
(...skipping 21 matching lines...) Expand all Loading... | |
32 | 32 |
33 int BackTimestampInMilliseconds() { | 33 int BackTimestampInMilliseconds() { |
34 return clock_.back_timestamp().InMilliseconds(); | 34 return clock_.back_timestamp().InMilliseconds(); |
35 } | 35 } |
36 | 36 |
37 int TimestampSinceLastWritingInMilliseconds(int milliseconds) { | 37 int TimestampSinceLastWritingInMilliseconds(int milliseconds) { |
38 return clock_.TimestampSinceWriting(base::TimeDelta::FromMilliseconds( | 38 return clock_.TimestampSinceWriting(base::TimeDelta::FromMilliseconds( |
39 milliseconds)).InMilliseconds(); | 39 milliseconds)).InMilliseconds(); |
40 } | 40 } |
41 | 41 |
42 int TimeUntilPlaybackInMilliseconds(int timestamp_ms) { | |
43 return clock_.TimeUntilPlayback(base::TimeDelta::FromMilliseconds( | |
44 timestamp_ms)).InMilliseconds(); | |
DaleCurtis
2014/09/22 18:48:57
Weird formatting, did clang-format do this?
scherkus (not reviewing)
2014/09/22 18:49:57
It did!
| |
45 } | |
46 | |
42 int ContiguousAudioDataBufferedInDays() { | 47 int ContiguousAudioDataBufferedInDays() { |
43 return clock_.contiguous_audio_data_buffered().InDays(); | 48 return clock_.contiguous_audio_data_buffered().InDays(); |
44 } | 49 } |
45 | 50 |
46 int ContiguousAudioDataBufferedInMilliseconds() { | 51 int ContiguousAudioDataBufferedInMilliseconds() { |
47 return clock_.contiguous_audio_data_buffered().InMilliseconds(); | 52 return clock_.contiguous_audio_data_buffered().InMilliseconds(); |
48 } | 53 } |
49 | 54 |
50 int ContiguousAudioDataBufferedAtSameRateInMilliseconds() { | 55 int ContiguousAudioDataBufferedAtSameRateInMilliseconds() { |
51 return clock_.contiguous_audio_data_buffered_at_same_rate() | 56 return clock_.contiguous_audio_data_buffered_at_same_rate() |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
271 | 276 |
272 WroteAudio(0, 10, 0, 1.0); | 277 WroteAudio(0, 10, 0, 1.0); |
273 EXPECT_EQ(3000, FrontTimestampInMilliseconds()); | 278 EXPECT_EQ(3000, FrontTimestampInMilliseconds()); |
274 EXPECT_EQ(3000, BackTimestampInMilliseconds()); | 279 EXPECT_EQ(3000, BackTimestampInMilliseconds()); |
275 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); | 280 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); |
276 } | 281 } |
277 | 282 |
278 TEST_F(AudioClockTest, TimestampSinceLastWriting) { | 283 TEST_F(AudioClockTest, TimestampSinceLastWriting) { |
279 // Construct an audio clock with the following representation: | 284 // Construct an audio clock with the following representation: |
280 // | 285 // |
286 // |- existing delay -|------------ calls to WroteAudio() -----------------| | |
281 // +-------------------+----------------+------------------+----------------+ | 287 // +-------------------+----------------+------------------+----------------+ |
282 // | 10 frames silence | 10 frames @ 1x | 10 frames @ 0.5x | 10 frames @ 2x | | 288 // | 20 frames silence | 10 frames @ 1x | 10 frames @ 0.5x | 10 frames @ 2x | |
283 // +-------------------+----------------+------------------+----------------+ | 289 // +-------------------+----------------+------------------+----------------+ |
284 // Media timestamp: 0 1000 1500 3500 | 290 // Media timestamp: 0 1000 1500 3500 |
285 // Wall clock time: 2000 3000 4000 5000 | 291 // Wall clock time: 2000 3000 4000 5000 |
286 WroteAudio(10, 10, 40, 1.0); | 292 WroteAudio(10, 10, 40, 1.0); |
287 WroteAudio(10, 10, 40, 0.5); | 293 WroteAudio(10, 10, 40, 0.5); |
288 WroteAudio(10, 10, 40, 2.0); | 294 WroteAudio(10, 10, 40, 2.0); |
289 EXPECT_EQ(0, FrontTimestampInMilliseconds()); | 295 EXPECT_EQ(0, FrontTimestampInMilliseconds()); |
296 EXPECT_EQ(3500, BackTimestampInMilliseconds()); | |
290 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); | 297 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); |
291 | 298 |
292 // Simulate passing 2000ms of initial delay in the audio hardware. | 299 // Simulate passing 2000ms of initial delay in the audio hardware. |
293 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(0)); | 300 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(0)); |
294 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(500)); | 301 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(500)); |
295 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(1000)); | 302 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(1000)); |
296 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(1500)); | 303 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(1500)); |
297 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(2000)); | 304 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(2000)); |
298 | 305 |
299 // Now we should see the 1.0x buffer. | 306 // Now we should see the 1.0x buffer. |
300 EXPECT_EQ(500, TimestampSinceLastWritingInMilliseconds(2500)); | 307 EXPECT_EQ(500, TimestampSinceLastWritingInMilliseconds(2500)); |
301 EXPECT_EQ(1000, TimestampSinceLastWritingInMilliseconds(3000)); | 308 EXPECT_EQ(1000, TimestampSinceLastWritingInMilliseconds(3000)); |
302 | 309 |
303 // Now we should see the 0.5x buffer. | 310 // Now we should see the 0.5x buffer. |
304 EXPECT_EQ(1250, TimestampSinceLastWritingInMilliseconds(3500)); | 311 EXPECT_EQ(1250, TimestampSinceLastWritingInMilliseconds(3500)); |
305 EXPECT_EQ(1500, TimestampSinceLastWritingInMilliseconds(4000)); | 312 EXPECT_EQ(1500, TimestampSinceLastWritingInMilliseconds(4000)); |
306 | 313 |
307 // Now we should see the 2.0x buffer. | 314 // Now we should see the 2.0x buffer. |
308 EXPECT_EQ(2500, TimestampSinceLastWritingInMilliseconds(4500)); | 315 EXPECT_EQ(2500, TimestampSinceLastWritingInMilliseconds(4500)); |
309 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(5000)); | 316 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(5000)); |
310 | 317 |
311 // Times beyond the known length of the audio clock should return the last | 318 // Times beyond the known length of the audio clock should return the last |
312 // media timestamp we know of. | 319 // media timestamp we know of. |
313 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(5001)); | 320 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(5001)); |
314 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(6000)); | 321 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(6000)); |
315 } | 322 } |
316 | 323 |
324 TEST_F(AudioClockTest, TimeUntilPlayback) { | |
325 // Construct an audio clock with the following representation: | |
326 // | |
327 // existing | |
328 // |- delay -|------------------ calls to WroteAudio() ------------------| | |
329 // +------------+---------+------------+-----------+------------+-----------+ | |
330 // | 20 silence | 10 @ 1x | 10 silence | 10 @ 0.5x | 10 silence | 10 @ 2.0x | | |
331 // +------------+---------+------------+-----------+------------+-----------+ | |
332 // Media: 0 1000 1000 1500 1500 3500 | |
333 // Wall: 2000 3000 4000 5000 6000 7000 | |
334 WroteAudio(10, 10, 60, 1.0); | |
335 WroteAudio(0, 10, 60, 1.0); | |
336 WroteAudio(10, 10, 60, 0.5); | |
337 WroteAudio(0, 10, 60, 0.5); | |
338 WroteAudio(10, 10, 60, 2.0); | |
339 EXPECT_EQ(0, FrontTimestampInMilliseconds()); | |
340 EXPECT_EQ(3500, BackTimestampInMilliseconds()); | |
341 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); | |
342 | |
343 // Media timestamp zero has to wait for silence to pass. | |
344 EXPECT_EQ(2000, TimeUntilPlaybackInMilliseconds(0)); | |
345 | |
346 // From then on out it's simply adding up the number of frames and taking | |
347 // silence into account. | |
348 EXPECT_EQ(2500, TimeUntilPlaybackInMilliseconds(500)); | |
349 EXPECT_EQ(3000, TimeUntilPlaybackInMilliseconds(1000)); | |
350 EXPECT_EQ(4500, TimeUntilPlaybackInMilliseconds(1250)); | |
351 EXPECT_EQ(5000, TimeUntilPlaybackInMilliseconds(1500)); | |
352 EXPECT_EQ(6500, TimeUntilPlaybackInMilliseconds(2500)); | |
353 EXPECT_EQ(7000, TimeUntilPlaybackInMilliseconds(3500)); | |
354 } | |
355 | |
317 TEST_F(AudioClockTest, SupportsYearsWorthOfAudioData) { | 356 TEST_F(AudioClockTest, SupportsYearsWorthOfAudioData) { |
318 // Use number of frames that would be likely to overflow 32-bit integer math. | 357 // Use number of frames that would be likely to overflow 32-bit integer math. |
319 const int huge_amount_of_frames = std::numeric_limits<int>::max(); | 358 const int huge_amount_of_frames = std::numeric_limits<int>::max(); |
320 const base::TimeDelta huge = | 359 const base::TimeDelta huge = |
321 base::TimeDelta::FromSeconds(huge_amount_of_frames / sample_rate_); | 360 base::TimeDelta::FromSeconds(huge_amount_of_frames / sample_rate_); |
322 EXPECT_EQ(2485, huge.InDays()); // Just to give some context on how big... | 361 EXPECT_EQ(2485, huge.InDays()); // Just to give some context on how big... |
323 | 362 |
324 // Use zero delay to test calculation of current timestamp. | 363 // Use zero delay to test calculation of current timestamp. |
325 WroteAudio(huge_amount_of_frames, huge_amount_of_frames, 0, 1.0); | 364 WroteAudio(huge_amount_of_frames, huge_amount_of_frames, 0, 1.0); |
326 EXPECT_EQ(0, FrontTimestampInDays()); | 365 EXPECT_EQ(0, FrontTimestampInDays()); |
(...skipping 12 matching lines...) Expand all Loading... | |
339 EXPECT_EQ(huge.InDays(), ContiguousAudioDataBufferedInDays()); | 378 EXPECT_EQ(huge.InDays(), ContiguousAudioDataBufferedInDays()); |
340 | 379 |
341 // Use huge delay to test calculation of buffered data. | 380 // Use huge delay to test calculation of buffered data. |
342 WroteAudio( | 381 WroteAudio( |
343 huge_amount_of_frames, huge_amount_of_frames, huge_amount_of_frames, 1.0); | 382 huge_amount_of_frames, huge_amount_of_frames, huge_amount_of_frames, 1.0); |
344 EXPECT_EQ((huge * 3).InDays(), FrontTimestampInDays()); | 383 EXPECT_EQ((huge * 3).InDays(), FrontTimestampInDays()); |
345 EXPECT_EQ((huge * 2).InDays(), ContiguousAudioDataBufferedInDays()); | 384 EXPECT_EQ((huge * 2).InDays(), ContiguousAudioDataBufferedInDays()); |
346 } | 385 } |
347 | 386 |
348 } // namespace media | 387 } // namespace media |
OLD | NEW |