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" | |
7 #include "media/base/audio_timestamp_helper.h" | 6 #include "media/base/audio_timestamp_helper.h" |
8 #include "media/filters/audio_clock.h" | 7 #include "media/filters/audio_clock.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
10 | 9 |
11 namespace media { | 10 namespace media { |
12 | 11 |
13 class AudioClockTest : public testing::Test { | 12 class AudioClockTest : public testing::Test { |
14 public: | 13 public: |
15 AudioClockTest() { SetupClock(base::TimeDelta(), 10); } | 14 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(frames_written, frames_requested, delay_frames, | 23 clock_.WroteAudio( |
24 playback_rate); | 24 frames_written, frames_requested, delay_frames, playback_rate); |
25 } | 25 } |
26 | 26 |
27 void SetupClock(base::TimeDelta start_time, int sample_rate) { | 27 int FrontTimestampInDays() { return clock_.front_timestamp().InDays(); } |
28 sample_rate_ = sample_rate; | |
29 clock_.reset(new AudioClock(start_time, sample_rate_)); | |
30 } | |
31 | |
32 int FrontTimestampInDays() { return clock_->front_timestamp().InDays(); } | |
33 | 28 |
34 int FrontTimestampInMilliseconds() { | 29 int FrontTimestampInMilliseconds() { |
35 return clock_->front_timestamp().InMilliseconds(); | 30 return clock_.front_timestamp().InMilliseconds(); |
36 } | 31 } |
37 | 32 |
38 int BackTimestampInMilliseconds() { | 33 int BackTimestampInMilliseconds() { |
39 return clock_->back_timestamp().InMilliseconds(); | 34 return clock_.back_timestamp().InMilliseconds(); |
40 } | 35 } |
41 | 36 |
42 int TimeUntilPlaybackInMilliseconds(int timestamp_ms) { | 37 int TimeUntilPlaybackInMilliseconds(int timestamp_ms) { |
43 return clock_ | 38 return clock_.TimeUntilPlayback(base::TimeDelta::FromMilliseconds( |
44 ->TimeUntilPlayback(base::TimeDelta::FromMilliseconds(timestamp_ms)) | 39 timestamp_ms)).InMilliseconds(); |
45 .InMilliseconds(); | |
46 } | 40 } |
47 | 41 |
48 int ContiguousAudioDataBufferedInDays() { | 42 int ContiguousAudioDataBufferedInDays() { |
49 base::TimeDelta total, same_rate_total; | 43 base::TimeDelta total, same_rate_total; |
50 clock_->ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); | 44 clock_.ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); |
51 return total.InDays(); | 45 return total.InDays(); |
52 } | 46 } |
53 | 47 |
54 int ContiguousAudioDataBufferedInMilliseconds() { | 48 int ContiguousAudioDataBufferedInMilliseconds() { |
55 base::TimeDelta total, same_rate_total; | 49 base::TimeDelta total, same_rate_total; |
56 clock_->ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); | 50 clock_.ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); |
57 return total.InMilliseconds(); | 51 return total.InMilliseconds(); |
58 } | 52 } |
59 | 53 |
60 int ContiguousAudioDataBufferedAtSameRateInMilliseconds() { | 54 int ContiguousAudioDataBufferedAtSameRateInMilliseconds() { |
61 base::TimeDelta total, same_rate_total; | 55 base::TimeDelta total, same_rate_total; |
62 clock_->ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); | 56 clock_.ContiguousAudioDataBufferedForTesting(&total, &same_rate_total); |
63 return same_rate_total.InMilliseconds(); | 57 return same_rate_total.InMilliseconds(); |
64 } | 58 } |
65 | 59 |
66 int sample_rate_; | 60 const int sample_rate_; |
67 scoped_ptr<AudioClock> clock_; | 61 AudioClock clock_; |
68 | 62 |
69 private: | 63 private: |
70 DISALLOW_COPY_AND_ASSIGN(AudioClockTest); | 64 DISALLOW_COPY_AND_ASSIGN(AudioClockTest); |
71 }; | 65 }; |
72 | 66 |
73 TEST_F(AudioClockTest, FrontTimestampStartsAtStartTimestamp) { | 67 TEST_F(AudioClockTest, FrontTimestampStartsAtStartTimestamp) { |
74 base::TimeDelta expected = base::TimeDelta::FromSeconds(123); | 68 base::TimeDelta expected = base::TimeDelta::FromSeconds(123); |
75 AudioClock clock(expected, sample_rate_); | 69 AudioClock clock(expected, sample_rate_); |
76 | 70 |
77 EXPECT_EQ(expected, clock.front_timestamp()); | 71 EXPECT_EQ(expected, clock.front_timestamp()); |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 // Buffer 6 seconds of delay and 1 second of audio data. | 330 // Buffer 6 seconds of delay and 1 second of audio data. |
337 WroteAudio(10, 10, 60, 1.0); | 331 WroteAudio(10, 10, 60, 1.0); |
338 | 332 |
339 // Media timestamp zero has to wait for silence to pass. | 333 // Media timestamp zero has to wait for silence to pass. |
340 const int kBaseTimeMs = 6000; | 334 const int kBaseTimeMs = 6000; |
341 EXPECT_EQ(kBaseTimeMs, TimeUntilPlaybackInMilliseconds(0)); | 335 EXPECT_EQ(kBaseTimeMs, TimeUntilPlaybackInMilliseconds(0)); |
342 | 336 |
343 // Elapsing frames less than we have buffered should do nothing. | 337 // Elapsing frames less than we have buffered should do nothing. |
344 const int kDelayFrames = 2; | 338 const int kDelayFrames = 2; |
345 for (int i = 1000; i <= kBaseTimeMs; i += 1000) { | 339 for (int i = 1000; i <= kBaseTimeMs; i += 1000) { |
346 clock_->CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(i), | 340 clock_.CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(i), |
347 kDelayFrames); | 341 kDelayFrames); |
348 EXPECT_EQ(kBaseTimeMs - (i - 1000), TimeUntilPlaybackInMilliseconds(0)); | 342 EXPECT_EQ(kBaseTimeMs - (i - 1000), TimeUntilPlaybackInMilliseconds(0)); |
349 | 343 |
350 // Write silence to simulate maintaining a 7s output buffer. | 344 // Write silence to simulate maintaining a 7s output buffer. |
351 WroteAudio(0, 10, 60, 1.0); | 345 WroteAudio(0, 10, 60, 1.0); |
352 } | 346 } |
353 | 347 |
354 // Exhausting all frames should advance timestamps and prime the buffer with | 348 // Exhausting all frames should advance timestamps and prime the buffer with |
355 // our delay frames value. | 349 // our delay frames value. |
356 clock_->CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(7000), | 350 clock_.CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(7000), |
357 kDelayFrames); | 351 kDelayFrames); |
358 EXPECT_EQ(kDelayFrames * 100, TimeUntilPlaybackInMilliseconds(1000)); | 352 EXPECT_EQ(kDelayFrames * 100, TimeUntilPlaybackInMilliseconds(1000)); |
359 } | 353 } |
360 | 354 |
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 | |
378 } // namespace media | 355 } // namespace media |
OLD | NEW |