Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / media / filters / audio_clock_unittest.cc
blob3fe437ee24c6f42829281a47ec698bfa3e74818f
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 return clock_.contiguous_audio_data_buffered().InDays();
46 int ContiguousAudioDataBufferedInMilliseconds() {
47 return clock_.contiguous_audio_data_buffered().InMilliseconds();
50 int ContiguousAudioDataBufferedAtSameRateInMilliseconds() {
51 return clock_.contiguous_audio_data_buffered_at_same_rate()
52 .InMilliseconds();
55 const int sample_rate_;
56 AudioClock clock_;
58 private:
59 DISALLOW_COPY_AND_ASSIGN(AudioClockTest);
62 TEST_F(AudioClockTest, FrontTimestampStartsAtStartTimestamp) {
63 base::TimeDelta expected = base::TimeDelta::FromSeconds(123);
64 AudioClock clock(expected, sample_rate_);
66 EXPECT_EQ(expected, clock.front_timestamp());
69 TEST_F(AudioClockTest, BackTimestampStartsAtStartTimestamp) {
70 base::TimeDelta expected = base::TimeDelta::FromSeconds(123);
71 AudioClock clock(expected, sample_rate_);
73 EXPECT_EQ(expected, clock.back_timestamp());
76 TEST_F(AudioClockTest, ContiguousAudioDataBufferedStartsAtZero) {
77 EXPECT_EQ(base::TimeDelta(), clock_.contiguous_audio_data_buffered());
80 TEST_F(AudioClockTest, ContiguousAudioDataBufferedAtSameRateStartsAtZero) {
81 EXPECT_EQ(base::TimeDelta(),
82 clock_.contiguous_audio_data_buffered_at_same_rate());
85 TEST_F(AudioClockTest, Playback) {
86 // The first time we write data we should still expect our start timestamp
87 // due to delay.
88 WroteAudio(10, 10, 20, 1.0);
89 EXPECT_EQ(0, FrontTimestampInMilliseconds());
90 EXPECT_EQ(1000, BackTimestampInMilliseconds());
91 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
92 EXPECT_EQ(0, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
94 // The media time should remain at start timestamp as we write data.
95 WroteAudio(10, 10, 20, 1.0);
96 EXPECT_EQ(0, FrontTimestampInMilliseconds());
97 EXPECT_EQ(2000, BackTimestampInMilliseconds());
98 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
99 EXPECT_EQ(0, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
101 WroteAudio(10, 10, 20, 1.0);
102 EXPECT_EQ(0, FrontTimestampInMilliseconds());
103 EXPECT_EQ(3000, BackTimestampInMilliseconds());
104 EXPECT_EQ(3000, ContiguousAudioDataBufferedInMilliseconds());
105 EXPECT_EQ(3000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
107 // The media time should now start advanced now that delay has been covered.
108 WroteAudio(10, 10, 20, 1.0);
109 EXPECT_EQ(1000, FrontTimestampInMilliseconds());
110 EXPECT_EQ(4000, BackTimestampInMilliseconds());
111 EXPECT_EQ(3000, ContiguousAudioDataBufferedInMilliseconds());
112 EXPECT_EQ(3000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
114 WroteAudio(10, 10, 20, 1.0);
115 EXPECT_EQ(2000, FrontTimestampInMilliseconds());
116 EXPECT_EQ(5000, BackTimestampInMilliseconds());
117 EXPECT_EQ(3000, ContiguousAudioDataBufferedInMilliseconds());
118 EXPECT_EQ(3000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
120 // Introduce a rate change to slow down time:
121 // - Current time will advance by one second until it hits rate change
122 // - Contiguous audio data will start shrinking immediately
123 WroteAudio(10, 10, 20, 0.5);
124 EXPECT_EQ(3000, FrontTimestampInMilliseconds());
125 EXPECT_EQ(5500, BackTimestampInMilliseconds());
126 EXPECT_EQ(2500, ContiguousAudioDataBufferedInMilliseconds());
127 EXPECT_EQ(2000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
129 WroteAudio(10, 10, 20, 0.5);
130 EXPECT_EQ(4000, FrontTimestampInMilliseconds());
131 EXPECT_EQ(6000, BackTimestampInMilliseconds());
132 EXPECT_EQ(2000, ContiguousAudioDataBufferedInMilliseconds());
133 EXPECT_EQ(1000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
135 WroteAudio(10, 10, 20, 0.5);
136 EXPECT_EQ(5000, FrontTimestampInMilliseconds());
137 EXPECT_EQ(6500, BackTimestampInMilliseconds());
138 EXPECT_EQ(1500, ContiguousAudioDataBufferedInMilliseconds());
139 EXPECT_EQ(1500, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
141 WroteAudio(10, 10, 20, 0.5);
142 EXPECT_EQ(5500, FrontTimestampInMilliseconds());
143 EXPECT_EQ(7000, BackTimestampInMilliseconds());
144 EXPECT_EQ(1500, ContiguousAudioDataBufferedInMilliseconds());
145 EXPECT_EQ(1500, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
147 // Introduce a rate change to speed up time:
148 // - Current time will advance by half a second until it hits rate change
149 // - Contiguous audio data will start growing immediately
150 WroteAudio(10, 10, 20, 2);
151 EXPECT_EQ(6000, FrontTimestampInMilliseconds());
152 EXPECT_EQ(9000, BackTimestampInMilliseconds());
153 EXPECT_EQ(3000, ContiguousAudioDataBufferedInMilliseconds());
154 EXPECT_EQ(1000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
156 WroteAudio(10, 10, 20, 2);
157 EXPECT_EQ(6500, FrontTimestampInMilliseconds());
158 EXPECT_EQ(11000, BackTimestampInMilliseconds());
159 EXPECT_EQ(4500, ContiguousAudioDataBufferedInMilliseconds());
160 EXPECT_EQ(500, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
162 WroteAudio(10, 10, 20, 2);
163 EXPECT_EQ(7000, FrontTimestampInMilliseconds());
164 EXPECT_EQ(13000, BackTimestampInMilliseconds());
165 EXPECT_EQ(6000, ContiguousAudioDataBufferedInMilliseconds());
166 EXPECT_EQ(6000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
168 WroteAudio(10, 10, 20, 2);
169 EXPECT_EQ(9000, FrontTimestampInMilliseconds());
170 EXPECT_EQ(15000, BackTimestampInMilliseconds());
171 EXPECT_EQ(6000, ContiguousAudioDataBufferedInMilliseconds());
172 EXPECT_EQ(6000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
174 // Write silence to simulate reaching end of stream:
175 // - Current time will advance by half a second until it hits silence
176 // - Contiguous audio data will start shrinking towards zero
177 WroteAudio(0, 10, 20, 2);
178 EXPECT_EQ(11000, FrontTimestampInMilliseconds());
179 EXPECT_EQ(15000, BackTimestampInMilliseconds());
180 EXPECT_EQ(4000, ContiguousAudioDataBufferedInMilliseconds());
181 EXPECT_EQ(4000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
183 WroteAudio(0, 10, 20, 2);
184 EXPECT_EQ(13000, FrontTimestampInMilliseconds());
185 EXPECT_EQ(15000, BackTimestampInMilliseconds());
186 EXPECT_EQ(2000, ContiguousAudioDataBufferedInMilliseconds());
187 EXPECT_EQ(2000, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
189 WroteAudio(0, 10, 20, 2);
190 EXPECT_EQ(15000, FrontTimestampInMilliseconds());
191 EXPECT_EQ(15000, BackTimestampInMilliseconds());
192 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
193 EXPECT_EQ(0, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
195 // At this point media time should stop increasing.
196 WroteAudio(0, 10, 20, 2);
197 EXPECT_EQ(15000, FrontTimestampInMilliseconds());
198 EXPECT_EQ(15000, BackTimestampInMilliseconds());
199 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
200 EXPECT_EQ(0, ContiguousAudioDataBufferedAtSameRateInMilliseconds());
203 TEST_F(AudioClockTest, AlternatingAudioAndSilence) {
204 // Buffer #1: [0, 1000)
205 WroteAudio(10, 10, 20, 1.0);
206 EXPECT_EQ(0, FrontTimestampInMilliseconds());
207 EXPECT_EQ(1000, BackTimestampInMilliseconds());
208 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
210 // Buffer #2: 1000ms of silence
211 WroteAudio(0, 10, 20, 1.0);
212 EXPECT_EQ(0, FrontTimestampInMilliseconds());
213 EXPECT_EQ(1000, BackTimestampInMilliseconds());
214 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
216 // Buffer #3: [1000, 2000):
217 // - Buffer #1 is at front with 1000ms of contiguous audio data
218 WroteAudio(10, 10, 20, 1.0);
219 EXPECT_EQ(0, FrontTimestampInMilliseconds());
220 EXPECT_EQ(2000, BackTimestampInMilliseconds());
221 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds());
223 // Buffer #4: 1000ms of silence
224 // - Buffer #1 has been played out
225 // - Buffer #2 of silence leaves us with 0ms of contiguous audio data
226 WroteAudio(0, 10, 20, 1.0);
227 EXPECT_EQ(1000, FrontTimestampInMilliseconds());
228 EXPECT_EQ(2000, BackTimestampInMilliseconds());
229 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
231 // Buffer #5: [2000, 3000):
232 // - Buffer #3 is at front with 1000ms of contiguous audio data
233 WroteAudio(10, 10, 20, 1.0);
234 EXPECT_EQ(1000, FrontTimestampInMilliseconds());
235 EXPECT_EQ(3000, BackTimestampInMilliseconds());
236 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds());
239 TEST_F(AudioClockTest, ZeroDelay) {
240 // The first time we write data we should expect the first timestamp
241 // immediately.
242 WroteAudio(10, 10, 0, 1.0);
243 EXPECT_EQ(0, FrontTimestampInMilliseconds());
244 EXPECT_EQ(1000, BackTimestampInMilliseconds());
245 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds());
247 // Ditto for all subsequent buffers.
248 WroteAudio(10, 10, 0, 1.0);
249 EXPECT_EQ(1000, FrontTimestampInMilliseconds());
250 EXPECT_EQ(2000, BackTimestampInMilliseconds());
251 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds());
253 WroteAudio(10, 10, 0, 1.0);
254 EXPECT_EQ(2000, FrontTimestampInMilliseconds());
255 EXPECT_EQ(3000, BackTimestampInMilliseconds());
256 EXPECT_EQ(1000, ContiguousAudioDataBufferedInMilliseconds());
258 // Ditto for silence.
259 WroteAudio(0, 10, 0, 1.0);
260 EXPECT_EQ(3000, FrontTimestampInMilliseconds());
261 EXPECT_EQ(3000, BackTimestampInMilliseconds());
262 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
264 WroteAudio(0, 10, 0, 1.0);
265 EXPECT_EQ(3000, FrontTimestampInMilliseconds());
266 EXPECT_EQ(3000, BackTimestampInMilliseconds());
267 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
270 TEST_F(AudioClockTest, TimeUntilPlayback) {
271 // Construct an audio clock with the following representation:
273 // existing
274 // |- delay -|------------------ calls to WroteAudio() ------------------|
275 // +------------+---------+------------+-----------+------------+-----------+
276 // | 20 silence | 10 @ 1x | 10 silence | 10 @ 0.5x | 10 silence | 10 @ 2.0x |
277 // +------------+---------+------------+-----------+------------+-----------+
278 // Media: 0 1000 1000 1500 1500 3500
279 // Wall: 2000 3000 4000 5000 6000 7000
280 WroteAudio(10, 10, 60, 1.0);
281 WroteAudio(0, 10, 60, 1.0);
282 WroteAudio(10, 10, 60, 0.5);
283 WroteAudio(0, 10, 60, 0.5);
284 WroteAudio(10, 10, 60, 2.0);
285 EXPECT_EQ(0, FrontTimestampInMilliseconds());
286 EXPECT_EQ(3500, BackTimestampInMilliseconds());
287 EXPECT_EQ(0, ContiguousAudioDataBufferedInMilliseconds());
289 // Media timestamp zero has to wait for silence to pass.
290 EXPECT_EQ(2000, TimeUntilPlaybackInMilliseconds(0));
292 // From then on out it's simply adding up the number of frames and taking
293 // silence into account.
294 EXPECT_EQ(2500, TimeUntilPlaybackInMilliseconds(500));
295 EXPECT_EQ(3000, TimeUntilPlaybackInMilliseconds(1000));
296 EXPECT_EQ(4500, TimeUntilPlaybackInMilliseconds(1250));
297 EXPECT_EQ(5000, TimeUntilPlaybackInMilliseconds(1500));
298 EXPECT_EQ(6500, TimeUntilPlaybackInMilliseconds(2500));
299 EXPECT_EQ(7000, TimeUntilPlaybackInMilliseconds(3500));
302 TEST_F(AudioClockTest, SupportsYearsWorthOfAudioData) {
303 // Use number of frames that would be likely to overflow 32-bit integer math.
304 const int huge_amount_of_frames = std::numeric_limits<int>::max();
305 const base::TimeDelta huge =
306 base::TimeDelta::FromSeconds(huge_amount_of_frames / sample_rate_);
307 EXPECT_EQ(2485, huge.InDays()); // Just to give some context on how big...
309 // Use zero delay to test calculation of current timestamp.
310 WroteAudio(huge_amount_of_frames, huge_amount_of_frames, 0, 1.0);
311 EXPECT_EQ(0, FrontTimestampInDays());
312 EXPECT_EQ(2485, ContiguousAudioDataBufferedInDays());
314 WroteAudio(huge_amount_of_frames, huge_amount_of_frames, 0, 1.0);
315 EXPECT_EQ(huge.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 * 2).InDays(), FrontTimestampInDays());
320 EXPECT_EQ(huge.InDays(), ContiguousAudioDataBufferedInDays());
322 WroteAudio(huge_amount_of_frames, huge_amount_of_frames, 0, 1.0);
323 EXPECT_EQ((huge * 3).InDays(), FrontTimestampInDays());
324 EXPECT_EQ(huge.InDays(), ContiguousAudioDataBufferedInDays());
326 // Use huge delay to test calculation of buffered data.
327 WroteAudio(
328 huge_amount_of_frames, huge_amount_of_frames, huge_amount_of_frames, 1.0);
329 EXPECT_EQ((huge * 3).InDays(), FrontTimestampInDays());
330 EXPECT_EQ((huge * 2).InDays(), ContiguousAudioDataBufferedInDays());
333 } // namespace media