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"
12 class AudioClockTest
: public testing::Test
{
16 timestamp_helper_(sample_rate_
),
17 clock_(sample_rate_
) {
18 timestamp_helper_
.SetBaseTimestamp(base::TimeDelta());
21 virtual ~AudioClockTest() {}
23 void WroteAudio(int frames
, int delay_frames
, float playback_rate
) {
24 timestamp_helper_
.AddFrames(static_cast<int>(frames
* playback_rate
));
26 frames
, delay_frames
, playback_rate
, timestamp_helper_
.GetTimestamp());
29 void WroteSilence(int frames
, int delay_frames
) {
30 clock_
.WroteSilence(frames
, delay_frames
);
33 int CurrentMediaTimestampInMilliseconds() {
34 return clock_
.CurrentMediaTimestamp().InMilliseconds();
37 int LastEndpointTimestampInMilliseconds() {
38 return clock_
.last_endpoint_timestamp().InMilliseconds();
41 const int sample_rate_
;
42 AudioTimestampHelper timestamp_helper_
;
46 DISALLOW_COPY_AND_ASSIGN(AudioClockTest
);
49 TEST_F(AudioClockTest
, TimestampsStartAtNoTimestamp
) {
50 EXPECT_EQ(kNoTimestamp(), clock_
.CurrentMediaTimestamp());
51 EXPECT_EQ(kNoTimestamp(), clock_
.last_endpoint_timestamp());
54 TEST_F(AudioClockTest
, Playback
) {
55 // The first time we write data we should expect a negative time matching the
57 WroteAudio(10, 20, 1.0);
58 EXPECT_EQ(-2000, CurrentMediaTimestampInMilliseconds());
59 EXPECT_EQ(1000, LastEndpointTimestampInMilliseconds());
61 // The media time should keep advancing as we write data.
62 WroteAudio(10, 20, 1.0);
63 EXPECT_EQ(-1000, CurrentMediaTimestampInMilliseconds());
64 EXPECT_EQ(2000, LastEndpointTimestampInMilliseconds());
66 WroteAudio(10, 20, 1.0);
67 EXPECT_EQ(0, CurrentMediaTimestampInMilliseconds());
68 EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds());
70 WroteAudio(10, 20, 1.0);
71 EXPECT_EQ(1000, CurrentMediaTimestampInMilliseconds());
72 EXPECT_EQ(4000, LastEndpointTimestampInMilliseconds());
74 // Introduce a rate change to slow down time. Current time will keep advancing
75 // by one second until it hits the slowed down audio.
76 WroteAudio(10, 20, 0.5);
77 EXPECT_EQ(2000, CurrentMediaTimestampInMilliseconds());
78 EXPECT_EQ(4500, LastEndpointTimestampInMilliseconds());
80 WroteAudio(10, 20, 0.5);
81 EXPECT_EQ(3000, CurrentMediaTimestampInMilliseconds());
82 EXPECT_EQ(5000, LastEndpointTimestampInMilliseconds());
84 WroteAudio(10, 20, 0.5);
85 EXPECT_EQ(4000, CurrentMediaTimestampInMilliseconds());
86 EXPECT_EQ(5500, LastEndpointTimestampInMilliseconds());
88 WroteAudio(10, 20, 0.5);
89 EXPECT_EQ(4500, CurrentMediaTimestampInMilliseconds());
90 EXPECT_EQ(6000, LastEndpointTimestampInMilliseconds());
92 // Introduce a rate change to speed up time. Current time will keep advancing
93 // by half a second until it hits the the sped up audio.
94 WroteAudio(10, 20, 2);
95 EXPECT_EQ(5000, CurrentMediaTimestampInMilliseconds());
96 EXPECT_EQ(8000, LastEndpointTimestampInMilliseconds());
98 WroteAudio(10, 20, 2);
99 EXPECT_EQ(5500, CurrentMediaTimestampInMilliseconds());
100 EXPECT_EQ(10000, LastEndpointTimestampInMilliseconds());
102 WroteAudio(10, 20, 2);
103 EXPECT_EQ(6000, CurrentMediaTimestampInMilliseconds());
104 EXPECT_EQ(12000, LastEndpointTimestampInMilliseconds());
106 WroteAudio(10, 20, 2);
107 EXPECT_EQ(8000, CurrentMediaTimestampInMilliseconds());
108 EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds());
110 // Write silence to simulate reaching end of stream.
111 WroteSilence(10, 20);
112 EXPECT_EQ(10000, CurrentMediaTimestampInMilliseconds());
113 EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds());
115 WroteSilence(10, 20);
116 EXPECT_EQ(12000, CurrentMediaTimestampInMilliseconds());
117 EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds());
119 WroteSilence(10, 20);
120 EXPECT_EQ(14000, CurrentMediaTimestampInMilliseconds());
121 EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds());
123 // At this point media time should stop increasing.
124 WroteSilence(10, 20);
125 EXPECT_EQ(14000, CurrentMediaTimestampInMilliseconds());
126 EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds());
129 TEST_F(AudioClockTest
, AlternatingAudioAndSilence
) {
130 // Buffer #1: [0, 1000)
131 WroteAudio(10, 20, 1.0);
132 EXPECT_EQ(-2000, CurrentMediaTimestampInMilliseconds());
134 // Buffer #2: 1000ms of silence
135 WroteSilence(10, 20);
136 EXPECT_EQ(-1000, CurrentMediaTimestampInMilliseconds());
138 // Buffer #3: [1000, 2000), buffer #1 is at front
139 WroteAudio(10, 20, 1.0);
140 EXPECT_EQ(0, CurrentMediaTimestampInMilliseconds());
142 // Buffer #4: 1000ms of silence, time shouldn't advance
143 WroteSilence(10, 20);
144 EXPECT_EQ(0, CurrentMediaTimestampInMilliseconds());
146 // Buffer #5: [2000, 3000), buffer #3 is at front
147 WroteAudio(10, 20, 1.0);
148 EXPECT_EQ(1000, CurrentMediaTimestampInMilliseconds());
151 TEST_F(AudioClockTest
, ZeroDelay
) {
152 // The first time we write data we should expect the first timestamp
154 WroteAudio(10, 0, 1.0);
155 EXPECT_EQ(0, CurrentMediaTimestampInMilliseconds());
156 EXPECT_EQ(1000, LastEndpointTimestampInMilliseconds());
158 // Ditto for all subsequent buffers.
159 WroteAudio(10, 0, 1.0);
160 EXPECT_EQ(1000, CurrentMediaTimestampInMilliseconds());
161 EXPECT_EQ(2000, LastEndpointTimestampInMilliseconds());
163 WroteAudio(10, 0, 1.0);
164 EXPECT_EQ(2000, CurrentMediaTimestampInMilliseconds());
165 EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds());
167 // Ditto for silence.
169 EXPECT_EQ(3000, CurrentMediaTimestampInMilliseconds());
170 EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds());
173 EXPECT_EQ(3000, CurrentMediaTimestampInMilliseconds());
174 EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds());