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() { SetupClock(base::TimeDelta(), 10); } |
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(base::TimeDelta start_time, int sample_rate) { |
| 28 sample_rate_ = sample_rate; |
| 29 clock_.reset(new AudioClock(start_time, sample_rate_)); |
| 30 } |
| 31 |
| 32 int FrontTimestampInDays() { return clock_->front_timestamp().InDays(); } |
28 | 33 |
29 int FrontTimestampInMilliseconds() { | 34 int FrontTimestampInMilliseconds() { |
30 return clock_.front_timestamp().InMilliseconds(); | 35 return clock_->front_timestamp().InMilliseconds(); |
31 } | 36 } |
32 | 37 |
33 int BackTimestampInMilliseconds() { | 38 int BackTimestampInMilliseconds() { |
34 return clock_.back_timestamp().InMilliseconds(); | 39 return clock_->back_timestamp().InMilliseconds(); |
35 } | 40 } |
36 | 41 |
37 int TimeUntilPlaybackInMilliseconds(int timestamp_ms) { | 42 int TimeUntilPlaybackInMilliseconds(int timestamp_ms) { |
38 return clock_.TimeUntilPlayback(base::TimeDelta::FromMilliseconds( | 43 return clock_ |
39 timestamp_ms)).InMilliseconds(); | 44 ->TimeUntilPlayback(base::TimeDelta::FromMilliseconds(timestamp_ms)) |
| 45 .InMilliseconds(); |
40 } | 46 } |
41 | 47 |
42 int ContiguousAudioDataBufferedInDays() { | 48 int ContiguousAudioDataBufferedInDays() { |
43 base::TimeDelta total, same_rate_total; | 49 base::TimeDelta total, same_rate_total; |
44 clock_.ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); | 50 clock_->ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); |
45 return total.InDays(); | 51 return total.InDays(); |
46 } | 52 } |
47 | 53 |
48 int ContiguousAudioDataBufferedInMilliseconds() { | 54 int ContiguousAudioDataBufferedInMilliseconds() { |
49 base::TimeDelta total, same_rate_total; | 55 base::TimeDelta total, same_rate_total; |
50 clock_.ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); | 56 clock_->ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); |
51 return total.InMilliseconds(); | 57 return total.InMilliseconds(); |
52 } | 58 } |
53 | 59 |
54 int ContiguousAudioDataBufferedAtSameRateInMilliseconds() { | 60 int ContiguousAudioDataBufferedAtSameRateInMilliseconds() { |
55 base::TimeDelta total, same_rate_total; | 61 base::TimeDelta total, same_rate_total; |
56 clock_.ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); | 62 clock_->ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); |
57 return same_rate_total.InMilliseconds(); | 63 return same_rate_total.InMilliseconds(); |
58 } | 64 } |
59 | 65 |
60 const int sample_rate_; | 66 int sample_rate_; |
61 AudioClock clock_; | 67 scoped_ptr<AudioClock> clock_; |
62 | 68 |
63 private: | 69 private: |
64 DISALLOW_COPY_AND_ASSIGN(AudioClockTest); | 70 DISALLOW_COPY_AND_ASSIGN(AudioClockTest); |
65 }; | 71 }; |
66 | 72 |
67 TEST_F(AudioClockTest, FrontTimestampStartsAtStartTimestamp) { | 73 TEST_F(AudioClockTest, FrontTimestampStartsAtStartTimestamp) { |
68 base::TimeDelta expected = base::TimeDelta::FromSeconds(123); | 74 base::TimeDelta expected = base::TimeDelta::FromSeconds(123); |
69 AudioClock clock(expected, sample_rate_); | 75 AudioClock clock(expected, sample_rate_); |
70 | 76 |
71 EXPECT_EQ(expected, clock.front_timestamp()); | 77 EXPECT_EQ(expected, clock.front_timestamp()); |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 // Buffer 6 seconds of delay and 1 second of audio data. | 336 // Buffer 6 seconds of delay and 1 second of audio data. |
331 WroteAudio(10, 10, 60, 1.0); | 337 WroteAudio(10, 10, 60, 1.0); |
332 | 338 |
333 // Media timestamp zero has to wait for silence to pass. | 339 // Media timestamp zero has to wait for silence to pass. |
334 const int kBaseTimeMs = 6000; | 340 const int kBaseTimeMs = 6000; |
335 EXPECT_EQ(kBaseTimeMs, TimeUntilPlaybackInMilliseconds(0)); | 341 EXPECT_EQ(kBaseTimeMs, TimeUntilPlaybackInMilliseconds(0)); |
336 | 342 |
337 // Elapsing frames less than we have buffered should do nothing. | 343 // Elapsing frames less than we have buffered should do nothing. |
338 const int kDelayFrames = 2; | 344 const int kDelayFrames = 2; |
339 for (int i = 1000; i <= kBaseTimeMs; i += 1000) { | 345 for (int i = 1000; i <= kBaseTimeMs; i += 1000) { |
340 clock_.CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(i), | 346 clock_->CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(i), |
341 kDelayFrames); | 347 kDelayFrames); |
342 EXPECT_EQ(kBaseTimeMs - (i - 1000), TimeUntilPlaybackInMilliseconds(0)); | 348 EXPECT_EQ(kBaseTimeMs - (i - 1000), TimeUntilPlaybackInMilliseconds(0)); |
343 | 349 |
344 // Write silence to simulate maintaining a 7s output buffer. | 350 // Write silence to simulate maintaining a 7s output buffer. |
345 WroteAudio(0, 10, 60, 1.0); | 351 WroteAudio(0, 10, 60, 1.0); |
346 } | 352 } |
347 | 353 |
348 // Exhausting all frames should advance timestamps and prime the buffer with | 354 // Exhausting all frames should advance timestamps and prime the buffer with |
349 // our delay frames value. | 355 // our delay frames value. |
350 clock_.CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(7000), | 356 clock_->CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(7000), |
351 kDelayFrames); | 357 kDelayFrames); |
352 EXPECT_EQ(kDelayFrames * 100, TimeUntilPlaybackInMilliseconds(1000)); | 358 EXPECT_EQ(kDelayFrames * 100, TimeUntilPlaybackInMilliseconds(1000)); |
353 } | 359 } |
354 | 360 |
| 361 TEST_F(AudioClockTest, FramesToTimePrecision) { |
| 362 SetupClock(base::TimeDelta(), 48000); |
| 363 double micros_per_frame = base::Time::kMicrosecondsPerSecond / 48000.0; |
| 364 int frames_written = 0; |
| 365 |
| 366 // Write ~2 hours of data to clock to give any error a significant chance to |
| 367 // accumulate. |
| 368 while (clock_->back_timestamp() <= base::TimeDelta::FromHours(2)) { |
| 369 frames_written += 1024; |
| 370 WroteAudio(1024, 1024, 0, 1); |
| 371 } |
| 372 |
| 373 // Verify no error accumulated. |
| 374 EXPECT_EQ(std::round(frames_written * micros_per_frame), |
| 375 clock_->back_timestamp().InMicroseconds()); |
| 376 } |
| 377 |
355 } // namespace media | 378 } // namespace media |
OLD | NEW |