Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / filters / audio_clock_unittest.cc
blob0e60abd379c68c3b4a309dd40944141cbc252525
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
3 // found in the LICENSE file.
5 #include "media/base/audio_timestamp_helper.h"
6 #include "media/base/buffers.h"
7 #include "media/filters/audio_clock.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace media {
12 class AudioClockTest : public testing::Test {
13 public:
14 AudioClockTest()
15 : sample_rate_(10), clock_(base::TimeDelta(), sample_rate_) {}
17 ~AudioClockTest() override {}
19 void WroteAudio(int frames_written,
20 int frames_requested,
21 int delay_frames,
22 double playback_rate) {
23 clock_.WroteAudio(
24 frames_written, frames_requested, delay_frames, playback_rate);
27 int FrontTimestampInDays() { return clock_.front_timestamp().InDays(); }
29 int FrontTimestampInMilliseconds() {
30 return clock_.front_timestamp().InMilliseconds();
33 int BackTimestampInMilliseconds() {
34 return clock_.back_timestamp().InMilliseconds();
37 int TimeUntilPlaybackInMilliseconds(int timestamp_ms) {
38 return clock_.TimeUntilPlayback(base::TimeDelta::FromMilliseconds(
39 timestamp_ms)).InMilliseconds();
42 int ContiguousAudioDataBufferedInDays() {
43 base::TimeDelta total, same_rate_total;
44 clock_.ContiguousAudioDataBufferedForTesting(&total, &same_rate_total);
45 return total.InDays();
48 int ContiguousAudioDataBufferedInMilliseconds() {
49 base::TimeDelta total, same_rate_total;
50 clock_.ContiguousAudioDataBufferedForTesting(&total, &same_rate_total);
51 return total.InMilliseconds();
54 int ContiguousAudioDataBufferedAtSameRateInMilliseconds() {
55 base::TimeDelta total, same_rate_total;
56 clock_.ContiguousAudioDataBufferedForTesting(&total, &same_rate_total);
57 return same_rate_total.InMilliseconds();
60 const int sample_rate_;
61 AudioClock clock_;
63 private:
64 DISALLOW_COPY_AND_ASSIGN(AudioClockTest);
67 TEST_F(AudioClockTest, FrontTimestampStartsAtStartTimestamp) {
68 base::TimeDelta expected = base::TimeDelta::FromSeconds(123);
69 AudioClock clock(expected, sample_rate_);
71 EXPECT_EQ(expected, clock.front_timestamp());
74 TEST_F(AudioClockTest, BackTimestampStartsAtStartTimestamp) {
75 base::TimeDelta expected = base::TimeDelta::FromSeconds(123);
76 AudioClock clock(expected, sample_rate_);
78 EXPECT_EQ(expected, clock.back_timestamp());
81 TEST_F(AudioClockTest, Playback) {
82 // The first time we write data we should still expect our start timestamp
83 // due to delay.
84 WroteAudio(10, 10, 20, 1.0);
85 EXPECT_EQ(0, FrontTimestampInMilliseconds());
86 EXPECT_EQ(1000, BackTimestampInMilliseconds());
87 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
88 EXPECT_EQ(0, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
90 // The media time should remain at start timestamp as we write data.
91 WroteAudio(10, 10, 20, 1.0);
92 EXPECT_EQ(0, FrontTimestampInMilliseconds());
93 EXPECT_EQ(2000, BackTimestampInMilliseconds());
94 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
95 EXPECT_EQ(0, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
97 WroteAudio(10, 10, 20, 1.0);
98 EXPECT_EQ(0, FrontTimestampInMilliseconds());
99 EXPECT_EQ(3000, BackTimestampInMilliseconds());
100 EXPECT_EQ(3000, ContiguousAudioDataBufferedInMilliseconds());
101 EXPECT_EQ(3000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
103 // The media time should now start advanced now that delay has been covered.
104 WroteAudio(10, 10, 20, 1.0);
105 EXPECT_EQ(1000, FrontTimestampInMilliseconds());
106 EXPECT_EQ(4000, BackTimestampInMilliseconds());
107 EXPECT_EQ(3000, ContiguousAudioDataBufferedInMilliseconds());
108 EXPECT_EQ(3000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
110 WroteAudio(10, 10, 20, 1.0);
111 EXPECT_EQ(2000, FrontTimestampInMilliseconds());
112 EXPECT_EQ(5000, BackTimestampInMilliseconds());
113 EXPECT_EQ(3000, ContiguousAudioDataBufferedInMilliseconds());
114 EXPECT_EQ(3000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
116 // Introduce a rate change to slow down time:
117 // - Current time will advance by one second until it hits rate change
118 // - Contiguous audio data will start shrinking immediately
119 WroteAudio(10, 10, 20, 0.5);
120 EXPECT_EQ(3000, FrontTimestampInMilliseconds());
121 EXPECT_EQ(5500, BackTimestampInMilliseconds());
122 EXPECT_EQ(2500, ContiguousAudioDataBufferedInMilliseconds());
123 EXPECT_EQ(2000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
125 WroteAudio(10, 10, 20, 0.5);
126 EXPECT_EQ(4000, FrontTimestampInMilliseconds());
127 EXPECT_EQ(6000, BackTimestampInMilliseconds());
128 EXPECT_EQ(2000, ContiguousAudioDataBufferedInMilliseconds());
129 EXPECT_EQ(1000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
131 WroteAudio(10, 10, 20, 0.5);
132 EXPECT_EQ(5000, FrontTimestampInMilliseconds());
133 EXPECT_EQ(6500, BackTimestampInMilliseconds());
134 EXPECT_EQ(1500, ContiguousAudioDataBufferedInMilliseconds());
135 EXPECT_EQ(1500, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
137 WroteAudio(10, 10, 20, 0.5);
138 EXPECT_EQ(5500, FrontTimestampInMilliseconds());
139 EXPECT_EQ(7000, BackTimestampInMilliseconds());
140 EXPECT_EQ(1500, ContiguousAudioDataBufferedInMilliseconds());
141 EXPECT_EQ(1500, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
143 // Introduce a rate change to speed up time:
144 // - Current time will advance by half a second until it hits rate change
145 // - Contiguous audio data will start growing immediately
146 WroteAudio(10, 10, 20, 2);
147 EXPECT_EQ(6000, FrontTimestampInMilliseconds());
148 EXPECT_EQ(9000, BackTimestampInMilliseconds());
149 EXPECT_EQ(3000, ContiguousAudioDataBufferedInMilliseconds());
150 EXPECT_EQ(1000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
152 WroteAudio(10, 10, 20, 2);
153 EXPECT_EQ(6500, FrontTimestampInMilliseconds());
154 EXPECT_EQ(11000, BackTimestampInMilliseconds());
155 EXPECT_EQ(4500, ContiguousAudioDataBufferedInMilliseconds());
156 EXPECT_EQ(500, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
158 WroteAudio(10, 10, 20, 2);
159 EXPECT_EQ(7000, FrontTimestampInMilliseconds());
160 EXPECT_EQ(13000, BackTimestampInMilliseconds());
161 EXPECT_EQ(6000, ContiguousAudioDataBufferedInMilliseconds());
162 EXPECT_EQ(6000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
164 WroteAudio(10, 10, 20, 2);
165 EXPECT_EQ(9000, FrontTimestampInMilliseconds());
166 EXPECT_EQ(15000, BackTimestampInMilliseconds());
167 EXPECT_EQ(6000, ContiguousAudioDataBufferedInMilliseconds());
168 EXPECT_EQ(6000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
170 // Write silence to simulate reaching end of stream:
171 // - Current time will advance by half a second until it hits silence
172 // - Contiguous audio data will start shrinking towards zero
173 WroteAudio(0, 10, 20, 2);
174 EXPECT_EQ(11000, FrontTimestampInMilliseconds());
175 EXPECT_EQ(15000, BackTimestampInMilliseconds());
176 EXPECT_EQ(4000, ContiguousAudioDataBufferedInMilliseconds());
177 EXPECT_EQ(4000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
179 WroteAudio(0, 10, 20, 2);
180 EXPECT_EQ(13000, FrontTimestampInMilliseconds());
181 EXPECT_EQ(15000, BackTimestampInMilliseconds());
182 EXPECT_EQ(2000, ContiguousAudioDataBufferedInMilliseconds());
183 EXPECT_EQ(2000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
185 WroteAudio(0, 10, 20, 2);
186 EXPECT_EQ(15000, FrontTimestampInMilliseconds());
187 EXPECT_EQ(15000, BackTimestampInMilliseconds());
188 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
189 EXPECT_EQ(0, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
191 // At this point media time should stop increasing.
192 WroteAudio(0, 10, 20, 2);
193 EXPECT_EQ(15000, FrontTimestampInMilliseconds());
194 EXPECT_EQ(15000, BackTimestampInMilliseconds());
195 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
196 EXPECT_EQ(0, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
199 TEST_F(AudioClockTest, AlternatingAudioAndSilence) {
200 // Buffer #1: [0, 1000)
201 WroteAudio(10, 10, 20, 1.0);
202 EXPECT_EQ(0, FrontTimestampInMilliseconds());
203 EXPECT_EQ(1000, BackTimestampInMilliseconds());
204 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
206 // Buffer #2: 1000ms of silence
207 WroteAudio(0, 10, 20, 1.0);
208 EXPECT_EQ(0, FrontTimestampInMilliseconds());
209 EXPECT_EQ(1000, BackTimestampInMilliseconds());
210 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
212 // Buffer #3: [1000, 2000):
213 // - Buffer #1 is at front with 1000ms of contiguous audio data
214 WroteAudio(10, 10, 20, 1.0);
215 EXPECT_EQ(0, FrontTimestampInMilliseconds());
216 EXPECT_EQ(2000, BackTimestampInMilliseconds());
217 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds());
219 // Buffer #4: 1000ms of silence
220 // - Buffer #1 has been played out
221 // - Buffer #2 of silence leaves us with 0ms of contiguous audio data
222 WroteAudio(0, 10, 20, 1.0);
223 EXPECT_EQ(1000, FrontTimestampInMilliseconds());
224 EXPECT_EQ(2000, BackTimestampInMilliseconds());
225 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
227 // Buffer #5: [2000, 3000):
228 // - Buffer #3 is at front with 1000ms of contiguous audio data
229 WroteAudio(10, 10, 20, 1.0);
230 EXPECT_EQ(1000, FrontTimestampInMilliseconds());
231 EXPECT_EQ(3000, BackTimestampInMilliseconds());
232 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds());
235 TEST_F(AudioClockTest, ZeroDelay) {
236 // The first time we write data we should expect the first timestamp
237 // immediately.
238 WroteAudio(10, 10, 0, 1.0);
239 EXPECT_EQ(0, FrontTimestampInMilliseconds());
240 EXPECT_EQ(1000, BackTimestampInMilliseconds());
241 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds());
243 // Ditto for all subsequent buffers.
244 WroteAudio(10, 10, 0, 1.0);
245 EXPECT_EQ(1000, FrontTimestampInMilliseconds());
246 EXPECT_EQ(2000, BackTimestampInMilliseconds());
247 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds());
249 WroteAudio(10, 10, 0, 1.0);
250 EXPECT_EQ(2000, FrontTimestampInMilliseconds());
251 EXPECT_EQ(3000, BackTimestampInMilliseconds());
252 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds());
254 // Ditto for silence.
255 WroteAudio(0, 10, 0, 1.0);
256 EXPECT_EQ(3000, FrontTimestampInMilliseconds());
257 EXPECT_EQ(3000, BackTimestampInMilliseconds());
258 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
260 WroteAudio(0, 10, 0, 1.0);
261 EXPECT_EQ(3000, FrontTimestampInMilliseconds());
262 EXPECT_EQ(3000, BackTimestampInMilliseconds());
263 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
266 TEST_F(AudioClockTest, TimeUntilPlayback) {
267 // Construct an audio clock with the following representation:
269 // existing
270 // |- delay -|------------------ calls to WroteAudio() ------------------|
271 // +------------+---------+------------+-----------+------------+-----------+
272 // | 20 silence | 10 @ 1x | 10 silence | 10 @ 0.5x | 10 silence | 10 @ 2.0x |
273 // +------------+---------+------------+-----------+------------+-----------+
274 // Media: 0 1000 1000 1500 1500 3500
275 // Wall: 2000 3000 4000 5000 6000 7000
276 WroteAudio(10, 10, 60, 1.0);
277 WroteAudio(0, 10, 60, 1.0);
278 WroteAudio(10, 10, 60, 0.5);
279 WroteAudio(0, 10, 60, 0.5);
280 WroteAudio(10, 10, 60, 2.0);
281 EXPECT_EQ(0, FrontTimestampInMilliseconds());
282 EXPECT_EQ(3500, BackTimestampInMilliseconds());
283 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
285 // Media timestamp zero has to wait for silence to pass.
286 EXPECT_EQ(2000, TimeUntilPlaybackInMilliseconds(0));
288 // From then on out it's simply adding up the number of frames and taking
289 // silence into account.
290 EXPECT_EQ(2500, TimeUntilPlaybackInMilliseconds(500));
291 EXPECT_EQ(3000, TimeUntilPlaybackInMilliseconds(1000));
292 EXPECT_EQ(4500, TimeUntilPlaybackInMilliseconds(1250));
293 EXPECT_EQ(5000, TimeUntilPlaybackInMilliseconds(1500));
294 EXPECT_EQ(6500, TimeUntilPlaybackInMilliseconds(2500));
295 EXPECT_EQ(7000, TimeUntilPlaybackInMilliseconds(3500));
298 TEST_F(AudioClockTest, SupportsYearsWorthOfAudioData) {
299 // Use number of frames that would be likely to overflow 32-bit integer math.
300 const int huge_amount_of_frames = std::numeric_limits<int>::max();
301 const base::TimeDelta huge =
302 base::TimeDelta::FromSeconds(huge_amount_of_frames / sample_rate_);
303 EXPECT_EQ(2485, huge.InDays()); // Just to give some context on how big...
305 // Use zero delay to test calculation of current timestamp.
306 WroteAudio(huge_amount_of_frames, huge_amount_of_frames, 0, 1.0);
307 EXPECT_EQ(0, FrontTimestampInDays());
308 EXPECT_EQ(2485, ContiguousAudioDataBufferedInDays());
310 WroteAudio(huge_amount_of_frames, huge_amount_of_frames, 0, 1.0);
311 EXPECT_EQ(huge.InDays(), FrontTimestampInDays());
312 EXPECT_EQ(huge.InDays(), ContiguousAudioDataBufferedInDays());
314 WroteAudio(huge_amount_of_frames, huge_amount_of_frames, 0, 1.0);
315 EXPECT_EQ((huge * 2).InDays(), FrontTimestampInDays());
316 EXPECT_EQ(huge.InDays(), ContiguousAudioDataBufferedInDays());
318 WroteAudio(huge_amount_of_frames, huge_amount_of_frames, 0, 1.0);
319 EXPECT_EQ((huge * 3).InDays(), FrontTimestampInDays());
320 EXPECT_EQ(huge.InDays(), ContiguousAudioDataBufferedInDays());
322 // Use huge delay to test calculation of buffered data.
323 WroteAudio(
324 huge_amount_of_frames, huge_amount_of_frames, huge_amount_of_frames, 1.0);
325 EXPECT_EQ((huge * 3).InDays(), FrontTimestampInDays());
326 EXPECT_EQ((huge * 2).InDays(), ContiguousAudioDataBufferedInDays());
329 TEST_F(AudioClockTest, CompensateForSuspendedWrites) {
330 // Buffer 6 seconds of delay and 1 second of audio data.
331 WroteAudio(10, 10, 60, 1.0);
333 // Media timestamp zero has to wait for silence to pass.
334 const int kBaseTimeMs = 6000;
335 EXPECT_EQ(kBaseTimeMs, TimeUntilPlaybackInMilliseconds(0));
337 // Elapsing frames less than we have buffered should do nothing.
338 const int kDelayFrames = 2;
339 for (int i = 1000; i <= kBaseTimeMs; i += 1000) {
340 clock_.CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(i),
341 kDelayFrames);
342 EXPECT_EQ(kBaseTimeMs - (i - 1000), TimeUntilPlaybackInMilliseconds(0));
344 // Write silence to simulate maintaining a 7s output buffer.
345 WroteAudio(0, 10, 60, 1.0);
348 // Exhausting all frames should advance timestamps and prime the buffer with
349 // our delay frames value.
350 clock_.CompensateForSuspendedWrites(base::TimeDelta::FromMilliseconds(7000),
351 kDelayFrames);
352 EXPECT_EQ(kDelayFrames * 100, TimeUntilPlaybackInMilliseconds(1000));
355 } // namespace media