/* * Copyright 2019 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #include #include "absl/types/optional.h" #include "api/scoped_refptr.h" #include "pc/playout_latency.h" #include "pc/test/mock_delayable.h" #include "rtc_base/ref_counted_object.h" #include "rtc_base/thread.h" #include "test/gmock.h" #include "test/gtest.h" using ::testing::Return; namespace { constexpr int kSsrc = 1234; } // namespace namespace webrtc { class PlayoutLatencyTest : public testing::Test { public: PlayoutLatencyTest() : latency_( new rtc::RefCountedObject(rtc::Thread::Current())) { } protected: rtc::scoped_refptr latency_; MockDelayable delayable_; }; TEST_F(PlayoutLatencyTest, DefaultValue) { EXPECT_DOUBLE_EQ(0.0, latency_->GetLatency()); } TEST_F(PlayoutLatencyTest, GetLatency) { latency_->OnStart(&delayable_, kSsrc); EXPECT_CALL(delayable_, GetBaseMinimumPlayoutDelayMs(kSsrc)) .WillOnce(Return(2000)); // Latency in seconds. EXPECT_DOUBLE_EQ(2.0, latency_->GetLatency()); EXPECT_CALL(delayable_, GetBaseMinimumPlayoutDelayMs(kSsrc)) .WillOnce(Return(absl::nullopt)); // When no value is returned by GetBaseMinimumPlayoutDelayMs, and there are // no caching, then return default value. EXPECT_DOUBLE_EQ(0.0, latency_->GetLatency()); } TEST_F(PlayoutLatencyTest, SetLatency) { latency_->OnStart(&delayable_, kSsrc); EXPECT_CALL(delayable_, SetBaseMinimumPlayoutDelayMs(kSsrc, 3000)) .WillOnce(Return(true)); // Latency in seconds. latency_->SetLatency(3.0); } TEST_F(PlayoutLatencyTest, Caching) { // Check that value is cached before start. latency_->SetLatency(4.0); // Latency in seconds. EXPECT_DOUBLE_EQ(4.0, latency_->GetLatency()); // Check that cached value applied on the start. EXPECT_CALL(delayable_, SetBaseMinimumPlayoutDelayMs(kSsrc, 4000)) .WillOnce(Return(true)); latency_->OnStart(&delayable_, kSsrc); EXPECT_CALL(delayable_, GetBaseMinimumPlayoutDelayMs(kSsrc)) .WillOnce(Return(absl::nullopt)); // On false the latest cached value is returned. EXPECT_DOUBLE_EQ(4.0, latency_->GetLatency()); latency_->OnStop(); // Check that after stop it returns last cached value. EXPECT_DOUBLE_EQ(4.0, latency_->GetLatency()); } TEST_F(PlayoutLatencyTest, Rounding) { latency_->OnStart(&delayable_, kSsrc); // In Jitter Buffer (Audio or Video) delay 0 has a special meaning of // unconstrained variable, that is why here if latency is small enough we // round it to 0 delay. EXPECT_CALL(delayable_, SetBaseMinimumPlayoutDelayMs(kSsrc, 0)) .WillOnce(Return(true)); latency_->SetLatency(0.005); } } // namespace webrtc