summaryrefslogtreecommitdiff
path: root/media/filters/audio_clock_unittest.cc
blob: 00179f9094fda648251732c5e4c384b97b2a66f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/audio_timestamp_helper.h"
#include "media/base/buffers.h"
#include "media/filters/audio_clock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

class AudioClockTest : public testing::Test {
 public:
  AudioClockTest()
      : sample_rate_(10),
        timestamp_helper_(sample_rate_),
        clock_(sample_rate_) {
    timestamp_helper_.SetBaseTimestamp(base::TimeDelta());
  }

  virtual ~AudioClockTest() {}

  void WroteAudio(int frames, int delay_frames, float playback_rate) {
    timestamp_helper_.AddFrames(static_cast<int>(frames * playback_rate));
    clock_.WroteAudio(
        frames, delay_frames, playback_rate, timestamp_helper_.GetTimestamp());
  }

  void WroteSilence(int frames, int delay_frames) {
    clock_.WroteSilence(frames, delay_frames);
  }

  int CurrentMediaTimestampInMilliseconds() {
    return CurrentMediaTimestampSinceLastWritingInMilliseconds(0);
  }

  int CurrentMediaTimestampSinceLastWritingInMilliseconds(int milliseconds) {
    return clock_.CurrentMediaTimestamp(base::TimeDelta::FromMilliseconds(
                                            milliseconds)).InMilliseconds();
  }

  int LastEndpointTimestampInMilliseconds() {
    return clock_.last_endpoint_timestamp().InMilliseconds();
  }

  const int sample_rate_;
  AudioTimestampHelper timestamp_helper_;
  AudioClock clock_;

 private:
  DISALLOW_COPY_AND_ASSIGN(AudioClockTest);
};

TEST_F(AudioClockTest, TimestampsStartAtNoTimestamp) {
  EXPECT_EQ(kNoTimestamp(), clock_.CurrentMediaTimestamp(base::TimeDelta()));
  EXPECT_EQ(kNoTimestamp(), clock_.last_endpoint_timestamp());
}

TEST_F(AudioClockTest, Playback) {
  // The first time we write data we should expect a negative time matching the
  // current delay.
  WroteAudio(10, 20, 1.0);
  EXPECT_EQ(-2000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(1000, LastEndpointTimestampInMilliseconds());

  // The media time should keep advancing as we write data.
  WroteAudio(10, 20, 1.0);
  EXPECT_EQ(-1000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(2000, LastEndpointTimestampInMilliseconds());

  WroteAudio(10, 20, 1.0);
  EXPECT_EQ(0, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds());

  WroteAudio(10, 20, 1.0);
  EXPECT_EQ(1000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(4000, LastEndpointTimestampInMilliseconds());

  // Introduce a rate change to slow down time. Current time will keep advancing
  // by one second until it hits the slowed down audio.
  WroteAudio(10, 20, 0.5);
  EXPECT_EQ(2000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(4500, LastEndpointTimestampInMilliseconds());

  WroteAudio(10, 20, 0.5);
  EXPECT_EQ(3000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(5000, LastEndpointTimestampInMilliseconds());

  WroteAudio(10, 20, 0.5);
  EXPECT_EQ(4000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(5500, LastEndpointTimestampInMilliseconds());

  WroteAudio(10, 20, 0.5);
  EXPECT_EQ(4500, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(6000, LastEndpointTimestampInMilliseconds());

  // Introduce a rate change to speed up time. Current time will keep advancing
  // by half a second until it hits the the sped up audio.
  WroteAudio(10, 20, 2);
  EXPECT_EQ(5000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(8000, LastEndpointTimestampInMilliseconds());

  WroteAudio(10, 20, 2);
  EXPECT_EQ(5500, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(10000, LastEndpointTimestampInMilliseconds());

  WroteAudio(10, 20, 2);
  EXPECT_EQ(6000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(12000, LastEndpointTimestampInMilliseconds());

  WroteAudio(10, 20, 2);
  EXPECT_EQ(8000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds());

  // Write silence to simulate reaching end of stream.
  WroteSilence(10, 20);
  EXPECT_EQ(10000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds());

  WroteSilence(10, 20);
  EXPECT_EQ(12000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds());

  WroteSilence(10, 20);
  EXPECT_EQ(14000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds());

  // At this point media time should stop increasing.
  WroteSilence(10, 20);
  EXPECT_EQ(14000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds());
}

TEST_F(AudioClockTest, AlternatingAudioAndSilence) {
  // Buffer #1: [0, 1000)
  WroteAudio(10, 20, 1.0);
  EXPECT_EQ(-2000, CurrentMediaTimestampInMilliseconds());

  // Buffer #2: 1000ms of silence
  WroteSilence(10, 20);
  EXPECT_EQ(-1000, CurrentMediaTimestampInMilliseconds());

  // Buffer #3: [1000, 2000), buffer #1 is at front
  WroteAudio(10, 20, 1.0);
  EXPECT_EQ(0, CurrentMediaTimestampInMilliseconds());

  // Buffer #4: 1000ms of silence, time shouldn't advance
  WroteSilence(10, 20);
  EXPECT_EQ(0, CurrentMediaTimestampInMilliseconds());

  // Buffer #5: [2000, 3000), buffer #3 is at front
  WroteAudio(10, 20, 1.0);
  EXPECT_EQ(1000, CurrentMediaTimestampInMilliseconds());
}

TEST_F(AudioClockTest, ZeroDelay) {
  // The first time we write data we should expect the first timestamp
  // immediately.
  WroteAudio(10, 0, 1.0);
  EXPECT_EQ(0, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(1000, LastEndpointTimestampInMilliseconds());

  // Ditto for all subsequent buffers.
  WroteAudio(10, 0, 1.0);
  EXPECT_EQ(1000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(2000, LastEndpointTimestampInMilliseconds());

  WroteAudio(10, 0, 1.0);
  EXPECT_EQ(2000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds());

  // Ditto for silence.
  WroteSilence(10, 0);
  EXPECT_EQ(3000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds());

  WroteSilence(10, 0);
  EXPECT_EQ(3000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds());
}

TEST_F(AudioClockTest, CurrentMediaTimestampSinceLastWriting) {
  // Construct an audio clock with the following representation:
  //
  // +-------------------+----------------+------------------+----------------+
  // | 10 frames silence | 10 frames @ 1x | 10 frames @ 0.5x | 10 frames @ 2x |
  // +-------------------+----------------+------------------+----------------+
  // Media timestamp:    0              1000               1500             3500
  // Wall clock time:  2000             3000               4000             5000
  WroteAudio(10, 40, 1.0);
  WroteAudio(10, 40, 0.5);
  WroteAudio(10, 40, 2.0);
  EXPECT_EQ(-2000, CurrentMediaTimestampInMilliseconds());
  EXPECT_EQ(3500, LastEndpointTimestampInMilliseconds());

  // Simulate passing 2000ms of initial delay in the audio hardware.
  EXPECT_EQ(-2000, CurrentMediaTimestampSinceLastWritingInMilliseconds(0));
  EXPECT_EQ(-1500, CurrentMediaTimestampSinceLastWritingInMilliseconds(500));
  EXPECT_EQ(-1000, CurrentMediaTimestampSinceLastWritingInMilliseconds(1000));
  EXPECT_EQ(-500, CurrentMediaTimestampSinceLastWritingInMilliseconds(1500));
  EXPECT_EQ(0, CurrentMediaTimestampSinceLastWritingInMilliseconds(2000));

  // New we should see the 1.0x buffer.
  EXPECT_EQ(500, CurrentMediaTimestampSinceLastWritingInMilliseconds(2500));
  EXPECT_EQ(1000, CurrentMediaTimestampSinceLastWritingInMilliseconds(3000));

  // Now we should see the 0.5x buffer.
  EXPECT_EQ(1250, CurrentMediaTimestampSinceLastWritingInMilliseconds(3500));
  EXPECT_EQ(1500, CurrentMediaTimestampSinceLastWritingInMilliseconds(4000));

  // Now we should see the 2.0x buffer.
  EXPECT_EQ(2500, CurrentMediaTimestampSinceLastWritingInMilliseconds(4500));
  EXPECT_EQ(3500, CurrentMediaTimestampSinceLastWritingInMilliseconds(5000));

  // Times beyond the known length of the audio clock should return the last
  // value we know of.
  EXPECT_EQ(LastEndpointTimestampInMilliseconds(),
            CurrentMediaTimestampSinceLastWritingInMilliseconds(5001));
  EXPECT_EQ(LastEndpointTimestampInMilliseconds(),
            CurrentMediaTimestampSinceLastWritingInMilliseconds(6000));
}

}  // namespace media