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 16 matching lines...) Expand all Loading... |
27 int FrontTimestampInDays() { return clock_.front_timestamp().InDays(); } | 27 int FrontTimestampInDays() { return clock_.front_timestamp().InDays(); } |
28 | 28 |
29 int FrontTimestampInMilliseconds() { | 29 int FrontTimestampInMilliseconds() { |
30 return clock_.front_timestamp().InMilliseconds(); | 30 return clock_.front_timestamp().InMilliseconds(); |
31 } | 31 } |
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) { | |
38 return clock_.TimestampSinceWriting(base::TimeDelta::FromMilliseconds( | |
39 milliseconds)).InMilliseconds(); | |
40 } | |
41 | |
42 int TimeUntilPlaybackInMilliseconds(int timestamp_ms) { | 37 int TimeUntilPlaybackInMilliseconds(int timestamp_ms) { |
43 return clock_.TimeUntilPlayback(base::TimeDelta::FromMilliseconds( | 38 return clock_.TimeUntilPlayback(base::TimeDelta::FromMilliseconds( |
44 timestamp_ms)).InMilliseconds(); | 39 timestamp_ms)).InMilliseconds(); |
45 } | 40 } |
46 | 41 |
47 int ContiguousAudioDataBufferedInDays() { | 42 int ContiguousAudioDataBufferedInDays() { |
48 return clock_.contiguous_audio_data_buffered().InDays(); | 43 return clock_.contiguous_audio_data_buffered().InDays(); |
49 } | 44 } |
50 | 45 |
51 int ContiguousAudioDataBufferedInMilliseconds() { | 46 int ContiguousAudioDataBufferedInMilliseconds() { |
(...skipping 19 matching lines...) Expand all Loading... |
71 EXPECT_EQ(expected, clock.front_timestamp()); | 66 EXPECT_EQ(expected, clock.front_timestamp()); |
72 } | 67 } |
73 | 68 |
74 TEST_F(AudioClockTest, BackTimestampStartsAtStartTimestamp) { | 69 TEST_F(AudioClockTest, BackTimestampStartsAtStartTimestamp) { |
75 base::TimeDelta expected = base::TimeDelta::FromSeconds(123); | 70 base::TimeDelta expected = base::TimeDelta::FromSeconds(123); |
76 AudioClock clock(expected, sample_rate_); | 71 AudioClock clock(expected, sample_rate_); |
77 | 72 |
78 EXPECT_EQ(expected, clock.back_timestamp()); | 73 EXPECT_EQ(expected, clock.back_timestamp()); |
79 } | 74 } |
80 | 75 |
81 TEST_F(AudioClockTest, TimestampSinceWritingStartsAtStartTimestamp) { | |
82 base::TimeDelta expected = base::TimeDelta::FromSeconds(123); | |
83 AudioClock clock(expected, sample_rate_); | |
84 | |
85 base::TimeDelta time_since_writing = base::TimeDelta::FromSeconds(456); | |
86 EXPECT_EQ(expected, clock.TimestampSinceWriting(time_since_writing)); | |
87 } | |
88 | |
89 TEST_F(AudioClockTest, ContiguousAudioDataBufferedStartsAtZero) { | 76 TEST_F(AudioClockTest, ContiguousAudioDataBufferedStartsAtZero) { |
90 EXPECT_EQ(base::TimeDelta(), clock_.contiguous_audio_data_buffered()); | 77 EXPECT_EQ(base::TimeDelta(), clock_.contiguous_audio_data_buffered()); |
91 } | 78 } |
92 | 79 |
93 TEST_F(AudioClockTest, ContiguousAudioDataBufferedAtSameRateStartsAtZero) { | 80 TEST_F(AudioClockTest, ContiguousAudioDataBufferedAtSameRateStartsAtZero) { |
94 EXPECT_EQ(base::TimeDelta(), | 81 EXPECT_EQ(base::TimeDelta(), |
95 clock_.contiguous_audio_data_buffered_at_same_rate()); | 82 clock_.contiguous_audio_data_buffered_at_same_rate()); |
96 } | 83 } |
97 | 84 |
98 TEST_F(AudioClockTest, Playback) { | 85 TEST_F(AudioClockTest, Playback) { |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 EXPECT_EQ(3000, FrontTimestampInMilliseconds()); | 260 EXPECT_EQ(3000, FrontTimestampInMilliseconds()); |
274 EXPECT_EQ(3000, BackTimestampInMilliseconds()); | 261 EXPECT_EQ(3000, BackTimestampInMilliseconds()); |
275 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); | 262 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); |
276 | 263 |
277 WroteAudio(0, 10, 0, 1.0); | 264 WroteAudio(0, 10, 0, 1.0); |
278 EXPECT_EQ(3000, FrontTimestampInMilliseconds()); | 265 EXPECT_EQ(3000, FrontTimestampInMilliseconds()); |
279 EXPECT_EQ(3000, BackTimestampInMilliseconds()); | 266 EXPECT_EQ(3000, BackTimestampInMilliseconds()); |
280 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); | 267 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); |
281 } | 268 } |
282 | 269 |
283 TEST_F(AudioClockTest, TimestampSinceLastWriting) { | |
284 // Construct an audio clock with the following representation: | |
285 // | |
286 // |- existing delay -|------------ calls to WroteAudio() -----------------| | |
287 // +-------------------+----------------+------------------+----------------+ | |
288 // | 20 frames silence | 10 frames @ 1x | 10 frames @ 0.5x | 10 frames @ 2x | | |
289 // +-------------------+----------------+------------------+----------------+ | |
290 // Media timestamp: 0 1000 1500 3500 | |
291 // Wall clock time: 2000 3000 4000 5000 | |
292 WroteAudio(10, 10, 40, 1.0); | |
293 WroteAudio(10, 10, 40, 0.5); | |
294 WroteAudio(10, 10, 40, 2.0); | |
295 EXPECT_EQ(0, FrontTimestampInMilliseconds()); | |
296 EXPECT_EQ(3500, BackTimestampInMilliseconds()); | |
297 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds()); | |
298 | |
299 // Simulate passing 2000ms of initial delay in the audio hardware. | |
300 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(0)); | |
301 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(500)); | |
302 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(1000)); | |
303 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(1500)); | |
304 EXPECT_EQ(0, TimestampSinceLastWritingInMilliseconds(2000)); | |
305 | |
306 // Now we should see the 1.0x buffer. | |
307 EXPECT_EQ(500, TimestampSinceLastWritingInMilliseconds(2500)); | |
308 EXPECT_EQ(1000, TimestampSinceLastWritingInMilliseconds(3000)); | |
309 | |
310 // Now we should see the 0.5x buffer. | |
311 EXPECT_EQ(1250, TimestampSinceLastWritingInMilliseconds(3500)); | |
312 EXPECT_EQ(1500, TimestampSinceLastWritingInMilliseconds(4000)); | |
313 | |
314 // Now we should see the 2.0x buffer. | |
315 EXPECT_EQ(2500, TimestampSinceLastWritingInMilliseconds(4500)); | |
316 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(5000)); | |
317 | |
318 // Times beyond the known length of the audio clock should return the last | |
319 // media timestamp we know of. | |
320 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(5001)); | |
321 EXPECT_EQ(3500, TimestampSinceLastWritingInMilliseconds(6000)); | |
322 } | |
323 | |
324 TEST_F(AudioClockTest, TimeUntilPlayback) { | 270 TEST_F(AudioClockTest, TimeUntilPlayback) { |
325 // Construct an audio clock with the following representation: | 271 // Construct an audio clock with the following representation: |
326 // | 272 // |
327 // existing | 273 // existing |
328 // |- delay -|------------------ calls to WroteAudio() ------------------| | 274 // |- delay -|------------------ calls to WroteAudio() ------------------| |
329 // +------------+---------+------------+-----------+------------+-----------+ | 275 // +------------+---------+------------+-----------+------------+-----------+ |
330 // | 20 silence | 10 @ 1x | 10 silence | 10 @ 0.5x | 10 silence | 10 @ 2.0x | | 276 // | 20 silence | 10 @ 1x | 10 silence | 10 @ 0.5x | 10 silence | 10 @ 2.0x | |
331 // +------------+---------+------------+-----------+------------+-----------+ | 277 // +------------+---------+------------+-----------+------------+-----------+ |
332 // Media: 0 1000 1000 1500 1500 3500 | 278 // Media: 0 1000 1000 1500 1500 3500 |
333 // Wall: 2000 3000 4000 5000 6000 7000 | 279 // Wall: 2000 3000 4000 5000 6000 7000 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 EXPECT_EQ(huge.InDays(), ContiguousAudioDataBufferedInDays()); | 324 EXPECT_EQ(huge.InDays(), ContiguousAudioDataBufferedInDays()); |
379 | 325 |
380 // Use huge delay to test calculation of buffered data. | 326 // Use huge delay to test calculation of buffered data. |
381 WroteAudio( | 327 WroteAudio( |
382 huge_amount_of_frames, huge_amount_of_frames, huge_amount_of_frames, 1.0); | 328 huge_amount_of_frames, huge_amount_of_frames, huge_amount_of_frames, 1.0); |
383 EXPECT_EQ((huge * 3).InDays(), FrontTimestampInDays()); | 329 EXPECT_EQ((huge * 3).InDays(), FrontTimestampInDays()); |
384 EXPECT_EQ((huge * 2).InDays(), ContiguousAudioDataBufferedInDays()); | 330 EXPECT_EQ((huge * 2).InDays(), ContiguousAudioDataBufferedInDays()); |
385 } | 331 } |
386 | 332 |
387 } // namespace media | 333 } // namespace media |
OLD | NEW |