TestDriftCompensation.cpp (3618B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 6 7 #include "DriftCompensation.h" 8 #include "gtest/gtest.h" 9 #include "mozilla/SpinEventLoopUntil.h" 10 11 using namespace mozilla; 12 13 class DriftCompensatorTest : public ::testing::Test { 14 public: 15 const TrackRate mRate = 44100; 16 const TimeStamp mStart; 17 const RefPtr<DriftCompensator> mComp; 18 19 DriftCompensatorTest() 20 : mStart(TimeStamp::Now()), 21 mComp(MakeRefPtr<DriftCompensator>(GetCurrentSerialEventTarget(), 22 mRate)) { 23 mComp->NotifyAudioStart(mStart); 24 // NotifyAudioStart dispatched a runnable to update the audio mStart time on 25 // the video thread. Because this is a test, the video thread is the current 26 // thread. We spin the event loop until we know the mStart time is updated. 27 { 28 bool updated = false; 29 NS_DispatchToCurrentThread( 30 NS_NewRunnableFunction(__func__, [&] { updated = true; })); 31 SpinEventLoopUntil("DriftCompensatorTest::DriftCompensatorTest"_ns, 32 [&] { return updated; }); 33 } 34 } 35 36 // Past() is half as far from `mStart` as `aNow`. 37 TimeStamp Past(TimeStamp aNow) { 38 return mStart + (aNow - mStart) / (int64_t)2; 39 } 40 41 // Future() is twice as far from `mStart` as `aNow`. 42 TimeStamp Future(TimeStamp aNow) { return mStart + (aNow - mStart) * 2; } 43 }; 44 45 TEST_F(DriftCompensatorTest, Initialized) { 46 EXPECT_EQ(mComp->GetVideoTime(mStart, mStart), mStart); 47 } 48 49 TEST_F(DriftCompensatorTest, SlowerAudio) { 50 // 10s of audio took 20 seconds of wall clock to play out 51 mComp->NotifyAudio(mRate * 10); 52 TimeStamp now = mStart + TimeDuration::FromSeconds(20); 53 EXPECT_EQ((mComp->GetVideoTime(now, mStart) - mStart).ToSeconds(), 0.0); 54 EXPECT_EQ((mComp->GetVideoTime(now, Past(now)) - mStart).ToSeconds(), 5.0); 55 EXPECT_EQ((mComp->GetVideoTime(now, now) - mStart).ToSeconds(), 10.0); 56 EXPECT_EQ((mComp->GetVideoTime(now, Future(now)) - mStart).ToSeconds(), 20.0); 57 } 58 59 TEST_F(DriftCompensatorTest, NoDrift) { 60 // 10s of audio took 10 seconds of wall clock to play out 61 mComp->NotifyAudio(mRate * 10); 62 TimeStamp now = mStart + TimeDuration::FromSeconds(10); 63 EXPECT_EQ((mComp->GetVideoTime(now, mStart) - mStart).ToSeconds(), 0.0); 64 EXPECT_EQ((mComp->GetVideoTime(now, Past(now)) - mStart).ToSeconds(), 5.0); 65 EXPECT_EQ((mComp->GetVideoTime(now, now) - mStart).ToSeconds(), 10.0); 66 EXPECT_EQ((mComp->GetVideoTime(now, Future(now)) - mStart).ToSeconds(), 20.0); 67 } 68 69 TEST_F(DriftCompensatorTest, NoProgress) { 70 // 10s of audio took 0 seconds of wall clock to play out 71 mComp->NotifyAudio(mRate * 10); 72 TimeStamp now = mStart; 73 TimeStamp future = mStart + TimeDuration::FromSeconds(5); 74 EXPECT_EQ((mComp->GetVideoTime(now, mStart) - mStart).ToSeconds(), 0.0); 75 EXPECT_EQ((mComp->GetVideoTime(now, future) - mStart).ToSeconds(), 5.0); 76 } 77 78 TEST_F(DriftCompensatorTest, FasterAudio) { 79 // 20s of audio took 10 seconds of wall clock to play out 80 mComp->NotifyAudio(mRate * 20); 81 TimeStamp now = mStart + TimeDuration::FromSeconds(10); 82 EXPECT_EQ((mComp->GetVideoTime(now, mStart) - mStart).ToSeconds(), 0.0); 83 EXPECT_EQ((mComp->GetVideoTime(now, Past(now)) - mStart).ToSeconds(), 10.0); 84 EXPECT_EQ((mComp->GetVideoTime(now, now) - mStart).ToSeconds(), 20.0); 85 EXPECT_EQ((mComp->GetVideoTime(now, Future(now)) - mStart).ToSeconds(), 40.0); 86 }