| 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 13 matching lines...) Expand all Loading... |
| 24 timestamp_helper_.AddFrames(static_cast<int>(frames * playback_rate)); | 24 timestamp_helper_.AddFrames(static_cast<int>(frames * playback_rate)); |
| 25 clock_.WroteAudio( | 25 clock_.WroteAudio( |
| 26 frames, delay_frames, playback_rate, timestamp_helper_.GetTimestamp()); | 26 frames, delay_frames, playback_rate, timestamp_helper_.GetTimestamp()); |
| 27 } | 27 } |
| 28 | 28 |
| 29 void WroteSilence(int frames, int delay_frames) { | 29 void WroteSilence(int frames, int delay_frames) { |
| 30 clock_.WroteSilence(frames, delay_frames); | 30 clock_.WroteSilence(frames, delay_frames); |
| 31 } | 31 } |
| 32 | 32 |
| 33 int CurrentMediaTimestampInMilliseconds() { | 33 int CurrentMediaTimestampInMilliseconds() { |
| 34 return clock_.CurrentMediaTimestamp().InMilliseconds(); | 34 return CurrentMediaTimestampSinceLastWritingInMilliseconds(0); |
| 35 } |
| 36 |
| 37 int CurrentMediaTimestampSinceLastWritingInMilliseconds(int milliseconds) { |
| 38 return clock_.CurrentMediaTimestamp(base::TimeDelta::FromMilliseconds( |
| 39 milliseconds)).InMilliseconds(); |
| 35 } | 40 } |
| 36 | 41 |
| 37 int LastEndpointTimestampInMilliseconds() { | 42 int LastEndpointTimestampInMilliseconds() { |
| 38 return clock_.last_endpoint_timestamp().InMilliseconds(); | 43 return clock_.last_endpoint_timestamp().InMilliseconds(); |
| 39 } | 44 } |
| 40 | 45 |
| 41 const int sample_rate_; | 46 const int sample_rate_; |
| 42 AudioTimestampHelper timestamp_helper_; | 47 AudioTimestampHelper timestamp_helper_; |
| 43 AudioClock clock_; | 48 AudioClock clock_; |
| 44 | 49 |
| 45 private: | 50 private: |
| 46 DISALLOW_COPY_AND_ASSIGN(AudioClockTest); | 51 DISALLOW_COPY_AND_ASSIGN(AudioClockTest); |
| 47 }; | 52 }; |
| 48 | 53 |
| 49 TEST_F(AudioClockTest, TimestampsStartAtNoTimestamp) { | 54 TEST_F(AudioClockTest, TimestampsStartAtNoTimestamp) { |
| 50 EXPECT_EQ(kNoTimestamp(), clock_.CurrentMediaTimestamp()); | 55 EXPECT_EQ(kNoTimestamp(), clock_.CurrentMediaTimestamp(base::TimeDelta())); |
| 51 EXPECT_EQ(kNoTimestamp(), clock_.last_endpoint_timestamp()); | 56 EXPECT_EQ(kNoTimestamp(), clock_.last_endpoint_timestamp()); |
| 52 } | 57 } |
| 53 | 58 |
| 54 TEST_F(AudioClockTest, Playback) { | 59 TEST_F(AudioClockTest, Playback) { |
| 55 // The first time we write data we should expect a negative time matching the | 60 // The first time we write data we should expect a negative time matching the |
| 56 // current delay. | 61 // current delay. |
| 57 WroteAudio(10, 20, 1.0); | 62 WroteAudio(10, 20, 1.0); |
| 58 EXPECT_EQ(-2000, CurrentMediaTimestampInMilliseconds()); | 63 EXPECT_EQ(-2000, CurrentMediaTimestampInMilliseconds()); |
| 59 EXPECT_EQ(1000, LastEndpointTimestampInMilliseconds()); | 64 EXPECT_EQ(1000, LastEndpointTimestampInMilliseconds()); |
| 60 | 65 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 // Ditto for silence. | 172 // Ditto for silence. |
| 168 WroteSilence(10, 0); | 173 WroteSilence(10, 0); |
| 169 EXPECT_EQ(3000, CurrentMediaTimestampInMilliseconds()); | 174 EXPECT_EQ(3000, CurrentMediaTimestampInMilliseconds()); |
| 170 EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds()); | 175 EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds()); |
| 171 | 176 |
| 172 WroteSilence(10, 0); | 177 WroteSilence(10, 0); |
| 173 EXPECT_EQ(3000, CurrentMediaTimestampInMilliseconds()); | 178 EXPECT_EQ(3000, CurrentMediaTimestampInMilliseconds()); |
| 174 EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds()); | 179 EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds()); |
| 175 } | 180 } |
| 176 | 181 |
| 182 TEST_F(AudioClockTest, CurrentMediaTimestampSinceLastWriting) { |
| 183 // Construct an audio clock with the following representation: |
| 184 // |
| 185 // +-------------------+----------------+------------------+----------------+ |
| 186 // | 10 frames silence | 10 frames @ 1x | 10 frames @ 0.5x | 10 frames @ 2x | |
| 187 // +-------------------+----------------+------------------+----------------+ |
| 188 // Media timestamp: 0 1000 1500 3500 |
| 189 // Wall clock time: 2000 3000 4000 5000 |
| 190 WroteAudio(10, 40, 1.0); |
| 191 WroteAudio(10, 40, 0.5); |
| 192 WroteAudio(10, 40, 2.0); |
| 193 EXPECT_EQ(-2000, CurrentMediaTimestampInMilliseconds()); |
| 194 EXPECT_EQ(3500, LastEndpointTimestampInMilliseconds()); |
| 195 |
| 196 // Simulate passing 2000ms of initial delay in the audio hardware. |
| 197 EXPECT_EQ(-2000, CurrentMediaTimestampSinceLastWritingInMilliseconds(0)); |
| 198 EXPECT_EQ(-1500, CurrentMediaTimestampSinceLastWritingInMilliseconds(500)); |
| 199 EXPECT_EQ(-1000, CurrentMediaTimestampSinceLastWritingInMilliseconds(1000)); |
| 200 EXPECT_EQ(-500, CurrentMediaTimestampSinceLastWritingInMilliseconds(1500)); |
| 201 EXPECT_EQ(0, CurrentMediaTimestampSinceLastWritingInMilliseconds(2000)); |
| 202 |
| 203 // New we should see the 1.0x buffer. |
| 204 EXPECT_EQ(500, CurrentMediaTimestampSinceLastWritingInMilliseconds(2500)); |
| 205 EXPECT_EQ(1000, CurrentMediaTimestampSinceLastWritingInMilliseconds(3000)); |
| 206 |
| 207 // Now we should see the 0.5x buffer. |
| 208 EXPECT_EQ(1250, CurrentMediaTimestampSinceLastWritingInMilliseconds(3500)); |
| 209 EXPECT_EQ(1500, CurrentMediaTimestampSinceLastWritingInMilliseconds(4000)); |
| 210 |
| 211 // Now we should see the 2.0x buffer. |
| 212 EXPECT_EQ(2500, CurrentMediaTimestampSinceLastWritingInMilliseconds(4500)); |
| 213 EXPECT_EQ(3500, CurrentMediaTimestampSinceLastWritingInMilliseconds(5000)); |
| 214 |
| 215 // Times beyond the known length of the audio clock should return the last |
| 216 // value we know of. |
| 217 EXPECT_EQ(LastEndpointTimestampInMilliseconds(), |
| 218 CurrentMediaTimestampSinceLastWritingInMilliseconds(5001)); |
| 219 EXPECT_EQ(LastEndpointTimestampInMilliseconds(), |
| 220 CurrentMediaTimestampSinceLastWritingInMilliseconds(6000)); |
| 221 } |
| 222 |
| 177 } // namespace media | 223 } // namespace media |
| OLD | NEW |