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. 4 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" 9 10 namespace media { 11 12 class AudioClockTest : public testing::Test { 13 public: 14 AudioClockTest() 15 : sample_rate_(10), 16 timestamp_helper_(sample_rate_), 17 clock_(sample_rate_) { 18 timestamp_helper_.SetBaseTimestamp(base::TimeDelta()); 19 } 20 21 virtual ~AudioClockTest() {} 22 23 void WroteAudio(int frames, int delay_frames, float playback_rate) { 24 timestamp_helper_.AddFrames(static_cast<int>(frames * playback_rate)); 25 clock_.WroteAudio( 26 frames, delay_frames, playback_rate, timestamp_helper_.GetTimestamp()); 27 } 28 29 void WroteSilence(int frames, int delay_frames) { 30 clock_.WroteSilence(frames, delay_frames); 31 } 32 33 int CurrentMediaTimestampInMilliseconds() { 34 return clock_.CurrentMediaTimestamp().InMilliseconds(); 35 } 36 37 int LastEndpointTimestampInMilliseconds() { 38 return clock_.last_endpoint_timestamp().InMilliseconds(); 39 } 40 41 const int sample_rate_; 42 AudioTimestampHelper timestamp_helper_; 43 AudioClock clock_; 44 45 private: 46 DISALLOW_COPY_AND_ASSIGN(AudioClockTest); 47 }; 48 49 TEST_F(AudioClockTest, TimestampsStartAtNoTimestamp) { 50 EXPECT_EQ(kNoTimestamp(), clock_.CurrentMediaTimestamp()); 51 EXPECT_EQ(kNoTimestamp(), clock_.last_endpoint_timestamp()); 52 } 53 54 TEST_F(AudioClockTest, Playback) { 55 // The first time we write data we should expect a negative time matching the 56 // current delay. 57 WroteAudio(10, 20, 1.0); 58 EXPECT_EQ(-2000, CurrentMediaTimestampInMilliseconds()); 59 EXPECT_EQ(1000, LastEndpointTimestampInMilliseconds()); 60 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()); 65 66 WroteAudio(10, 20, 1.0); 67 EXPECT_EQ(0, CurrentMediaTimestampInMilliseconds()); 68 EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds()); 69 70 WroteAudio(10, 20, 1.0); 71 EXPECT_EQ(1000, CurrentMediaTimestampInMilliseconds()); 72 EXPECT_EQ(4000, LastEndpointTimestampInMilliseconds()); 73 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()); 79 80 WroteAudio(10, 20, 0.5); 81 EXPECT_EQ(3000, CurrentMediaTimestampInMilliseconds()); 82 EXPECT_EQ(5000, LastEndpointTimestampInMilliseconds()); 83 84 WroteAudio(10, 20, 0.5); 85 EXPECT_EQ(4000, CurrentMediaTimestampInMilliseconds()); 86 EXPECT_EQ(5500, LastEndpointTimestampInMilliseconds()); 87 88 WroteAudio(10, 20, 0.5); 89 EXPECT_EQ(4500, CurrentMediaTimestampInMilliseconds()); 90 EXPECT_EQ(6000, LastEndpointTimestampInMilliseconds()); 91 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()); 97 98 WroteAudio(10, 20, 2); 99 EXPECT_EQ(5500, CurrentMediaTimestampInMilliseconds()); 100 EXPECT_EQ(10000, LastEndpointTimestampInMilliseconds()); 101 102 WroteAudio(10, 20, 2); 103 EXPECT_EQ(6000, CurrentMediaTimestampInMilliseconds()); 104 EXPECT_EQ(12000, LastEndpointTimestampInMilliseconds()); 105 106 WroteAudio(10, 20, 2); 107 EXPECT_EQ(8000, CurrentMediaTimestampInMilliseconds()); 108 EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds()); 109 110 // Write silence to simulate reaching end of stream. 111 WroteSilence(10, 20); 112 EXPECT_EQ(10000, CurrentMediaTimestampInMilliseconds()); 113 EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds()); 114 115 WroteSilence(10, 20); 116 EXPECT_EQ(12000, CurrentMediaTimestampInMilliseconds()); 117 EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds()); 118 119 WroteSilence(10, 20); 120 EXPECT_EQ(14000, CurrentMediaTimestampInMilliseconds()); 121 EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds()); 122 123 // At this point media time should stop increasing. 124 WroteSilence(10, 20); 125 EXPECT_EQ(14000, CurrentMediaTimestampInMilliseconds()); 126 EXPECT_EQ(14000, LastEndpointTimestampInMilliseconds()); 127 } 128 129 TEST_F(AudioClockTest, AlternatingAudioAndSilence) { 130 // Buffer #1: [0, 1000) 131 WroteAudio(10, 20, 1.0); 132 EXPECT_EQ(-2000, CurrentMediaTimestampInMilliseconds()); 133 134 // Buffer #2: 1000ms of silence 135 WroteSilence(10, 20); 136 EXPECT_EQ(-1000, CurrentMediaTimestampInMilliseconds()); 137 138 // Buffer #3: [1000, 2000), buffer #1 is at front 139 WroteAudio(10, 20, 1.0); 140 EXPECT_EQ(0, CurrentMediaTimestampInMilliseconds()); 141 142 // Buffer #4: 1000ms of silence, time shouldn't advance 143 WroteSilence(10, 20); 144 EXPECT_EQ(0, CurrentMediaTimestampInMilliseconds()); 145 146 // Buffer #5: [2000, 3000), buffer #3 is at front 147 WroteAudio(10, 20, 1.0); 148 EXPECT_EQ(1000, CurrentMediaTimestampInMilliseconds()); 149 } 150 151 TEST_F(AudioClockTest, ZeroDelay) { 152 // The first time we write data we should expect the first timestamp 153 // immediately. 154 WroteAudio(10, 0, 1.0); 155 EXPECT_EQ(0, CurrentMediaTimestampInMilliseconds()); 156 EXPECT_EQ(1000, LastEndpointTimestampInMilliseconds()); 157 158 // Ditto for all subsequent buffers. 159 WroteAudio(10, 0, 1.0); 160 EXPECT_EQ(1000, CurrentMediaTimestampInMilliseconds()); 161 EXPECT_EQ(2000, LastEndpointTimestampInMilliseconds()); 162 163 WroteAudio(10, 0, 1.0); 164 EXPECT_EQ(2000, CurrentMediaTimestampInMilliseconds()); 165 EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds()); 166 167 // Ditto for silence. 168 WroteSilence(10, 0); 169 EXPECT_EQ(3000, CurrentMediaTimestampInMilliseconds()); 170 EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds()); 171 172 WroteSilence(10, 0); 173 EXPECT_EQ(3000, CurrentMediaTimestampInMilliseconds()); 174 EXPECT_EQ(3000, LastEndpointTimestampInMilliseconds()); 175 } 176 177 } // namespace media 178