Chromium Code Reviews| 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 WallTimeForTimestampInMilliseconds(int timestamp_ms) { | |
| 43 return clock_.WallTimeForTimestamp(base::TimeDelta::FromMilliseconds( | |
| 44 timestamp_ms)).InMilliseconds(); | |
| 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 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 // | 285 // |
| 281 // +-------------------+----------------+------------------+----------------+ | 286 // +-------------------+----------------+------------------+----------------+ |
| 282 // | 10 frames silence | 10 frames @ 1x | 10 frames @ 0.5x | 10 frames @ 2x | | 287 // | 10 frames silence | 10 frames @ 1x | 10 frames @ 0.5x | 10 frames @ 2x | |
| 283 // +-------------------+----------------+------------------+----------------+ | 288 // +-------------------+----------------+------------------+----------------+ |
| 284 // Media timestamp: 0 1000 1500 3500 | 289 // Media timestamp: 0 1000 1500 3500 |
| 285 // Wall clock time: 2000 3000 4000 5000 | 290 // Wall clock time: 2000 3000 4000 5000 |
| 286 WroteAudio(10, 10, 40, 1.0); | 291 WroteAudio(10, 10, 40, 1.0); |
| 287 WroteAudio(10, 10, 40, 0.5); | 292 WroteAudio(10, 10, 40, 0.5); |
| 288 WroteAudio(10, 10, 40, 2.0); | 293 WroteAudio(10, 10, 40, 2.0); |
| 289 EXPECT_EQ(0, FrontTimestampInMilliseconds()); | 294 EXPECT_EQ(0, FrontTimestampInMilliseconds()); |
| 295 EXPECT_EQ(3500, BackTimestampInMilliseconds()); | |
| 290 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); | 296 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); |
| 291 | 297 |
| 292 // Simulate passing 2000ms of initial delay in the audio hardware. | 298 // Simulate passing 2000ms of initial delay in the audio hardware. |
| 293 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(0)); | 299 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(0)); |
| 294 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(500)); | 300 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(500)); |
| 295 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(1000)); | 301 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(1000)); |
| 296 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(1500)); | 302 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(1500)); |
| 297 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(2000)); | 303 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(2000)); |
| 298 | 304 |
| 299 // Now we should see the 1.0x buffer. | 305 // Now we should see the 1.0x buffer. |
| 300 EXPECT_EQ(500, TimestampSinceLastWritingInMilliseconds(2500)); | 306 EXPECT_EQ(500, TimestampSinceLastWritingInMilliseconds(2500)); |
| 301 EXPECT_EQ(1000, TimestampSinceLastWritingInMilliseconds(3000)); | 307 EXPECT_EQ(1000, TimestampSinceLastWritingInMilliseconds(3000)); |
| 302 | 308 |
| 303 // Now we should see the 0.5x buffer. | 309 // Now we should see the 0.5x buffer. |
| 304 EXPECT_EQ(1250, TimestampSinceLastWritingInMilliseconds(3500)); | 310 EXPECT_EQ(1250, TimestampSinceLastWritingInMilliseconds(3500)); |
| 305 EXPECT_EQ(1500, TimestampSinceLastWritingInMilliseconds(4000)); | 311 EXPECT_EQ(1500, TimestampSinceLastWritingInMilliseconds(4000)); |
| 306 | 312 |
| 307 // Now we should see the 2.0x buffer. | 313 // Now we should see the 2.0x buffer. |
| 308 EXPECT_EQ(2500, TimestampSinceLastWritingInMilliseconds(4500)); | 314 EXPECT_EQ(2500, TimestampSinceLastWritingInMilliseconds(4500)); |
| 309 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(5000)); | 315 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(5000)); |
| 310 | 316 |
| 311 // Times beyond the known length of the audio clock should return the last | 317 // Times beyond the known length of the audio clock should return the last |
| 312 // media timestamp we know of. | 318 // media timestamp we know of. |
| 313 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(5001)); | 319 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(5001)); |
| 314 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(6000)); | 320 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(6000)); |
| 315 } | 321 } |
| 316 | 322 |
| 323 TEST_F(AudioClockTest, WallTimeForTimestamp) { | |
| 324 // Construct an audio clock with the following representation: | |
| 325 // | |
| 326 // +------------+---------+------------+-----------+------------+-----------+ | |
|
DaleCurtis
2014/09/20 00:17:12
As discussed, this diagram is wrong, here's a fixe
scherkus (not reviewing)
2014/09/22 18:34:12
Done.
| |
| 327 // | 10 silence | 10 @ 1x | 10 silence | 10 @ 0.5x | 10 silence | 10 @ 2.0x | | |
| 328 // +------------+---------+------------+-----------+------------+-----------+ | |
| 329 // Media: 0 1000 1000 1500 1500 3500 | |
| 330 // Wall: 2000 3000 4000 5000 6000 7000 | |
| 331 WroteAudio(10, 10, 60, 1.0); | |
| 332 WroteAudio(0, 10, 60, 1.0); | |
| 333 WroteAudio(10, 10, 60, 0.5); | |
| 334 WroteAudio(0, 10, 60, 0.5); | |
| 335 WroteAudio(10, 10, 60, 2.0); | |
| 336 EXPECT_EQ(0, FrontTimestampInMilliseconds()); | |
| 337 EXPECT_EQ(3500, BackTimestampInMilliseconds()); | |
| 338 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); | |
| 339 | |
| 340 // Media timestamp zero has to wait for silence to pass. | |
| 341 EXPECT_EQ(2000, WallTimeForTimestampInMilliseconds(0)); | |
| 342 | |
| 343 // From then on out it's simply adding up the number of frames and taking | |
| 344 // silence into account. | |
| 345 EXPECT_EQ(2500, WallTimeForTimestampInMilliseconds(500)); | |
| 346 EXPECT_EQ(3000, WallTimeForTimestampInMilliseconds(1000)); | |
| 347 EXPECT_EQ(4500, WallTimeForTimestampInMilliseconds(1250)); | |
| 348 EXPECT_EQ(5000, WallTimeForTimestampInMilliseconds(1500)); | |
| 349 EXPECT_EQ(6500, WallTimeForTimestampInMilliseconds(2500)); | |
| 350 EXPECT_EQ(7000, WallTimeForTimestampInMilliseconds(3500)); | |
| 351 } | |
| 352 | |
| 317 TEST_F(AudioClockTest, SupportsYearsWorthOfAudioData) { | 353 TEST_F(AudioClockTest, SupportsYearsWorthOfAudioData) { |
| 318 // Use number of frames that would be likely to overflow 32-bit integer math. | 354 // 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(); | 355 const int huge_amount_of_frames = std::numeric_limits<int>::max(); |
| 320 const base::TimeDelta huge = | 356 const base::TimeDelta huge = |
| 321 base::TimeDelta::FromSeconds(huge_amount_of_frames / sample_rate_); | 357 base::TimeDelta::FromSeconds(huge_amount_of_frames / sample_rate_); |
| 322 EXPECT_EQ(2485, huge.InDays()); // Just to give some context on how big... | 358 EXPECT_EQ(2485, huge.InDays()); // Just to give some context on how big... |
| 323 | 359 |
| 324 // Use zero delay to test calculation of current timestamp. | 360 // Use zero delay to test calculation of current timestamp. |
| 325 WroteAudio(huge_amount_of_frames, huge_amount_of_frames, 0, 1.0); | 361 WroteAudio(huge_amount_of_frames, huge_amount_of_frames, 0, 1.0); |
| 326 EXPECT_EQ(0, FrontTimestampInDays()); | 362 EXPECT_EQ(0, FrontTimestampInDays()); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 339 EXPECT_EQ(huge.InDays(), ContiguousAudioDataBufferedInDays()); | 375 EXPECT_EQ(huge.InDays(), ContiguousAudioDataBufferedInDays()); |
| 340 | 376 |
| 341 // Use huge delay to test calculation of buffered data. | 377 // Use huge delay to test calculation of buffered data. |
| 342 WroteAudio( | 378 WroteAudio( |
| 343 huge_amount_of_frames, huge_amount_of_frames, huge_amount_of_frames, 1.0); | 379 huge_amount_of_frames, huge_amount_of_frames, huge_amount_of_frames, 1.0); |
| 344 EXPECT_EQ((huge * 3).InDays(), FrontTimestampInDays()); | 380 EXPECT_EQ((huge * 3).InDays(), FrontTimestampInDays()); |
| 345 EXPECT_EQ((huge * 2).InDays(), ContiguousAudioDataBufferedInDays()); | 381 EXPECT_EQ((huge * 2).InDays(), ContiguousAudioDataBufferedInDays()); |
| 346 } | 382 } |
| 347 | 383 |
| 348 } // namespace media | 384 } // namespace media |
| OLD | NEW |