2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webrtc/common_types.h"
15 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
16 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h"
21 class RtcpRttStatsTestImpl : public RtcpRttObserver {
23 RtcpRttStatsTestImpl() : rtt_ms_(0) {}
24 virtual ~RtcpRttStatsTestImpl() {}
26 virtual void OnRttUpdate(uint32_t rtt_ms) {
32 class SendTransport : public Transport,
35 SendTransport() : rtp_rtcp_impl_(NULL), clock_(NULL), delay_ms_(0) {}
37 void SetRtpRtcpModule(ModuleRtpRtcpImpl* rtp_rtcp_impl) {
38 rtp_rtcp_impl_ = rtp_rtcp_impl;
40 void SimulateNetworkDelay(int delay_ms, SimulatedClock* clock) {
44 virtual int SendPacket(int /*ch*/, const void* /*data*/, int /*len*/) {
47 virtual int SendRTCPPacket(int /*ch*/, const void *data, int len) {
49 clock_->AdvanceTimeMilliseconds(delay_ms_);
51 EXPECT_TRUE(rtp_rtcp_impl_ != NULL);
52 EXPECT_EQ(0, rtp_rtcp_impl_->IncomingRtcpPacket(
53 static_cast<const uint8_t*>(data), len));
56 ModuleRtpRtcpImpl* rtp_rtcp_impl_;
57 SimulatedClock* clock_;
62 class RtpRtcpImplTest : public ::testing::Test {
66 receive_statistics_(ReceiveStatistics::Create(&clock_)) {
67 RtpRtcp::Configuration configuration;
69 configuration.audio = false;
70 configuration.clock = &clock_;
71 configuration.outgoing_transport = &transport_;
72 configuration.receive_statistics = receive_statistics_.get();
73 configuration.rtt_observer = &rtt_stats_;
75 rtp_rtcp_impl_.reset(new ModuleRtpRtcpImpl(configuration));
76 transport_.SetRtpRtcpModule(rtp_rtcp_impl_.get());
79 SimulatedClock clock_;
80 scoped_ptr<ReceiveStatistics> receive_statistics_;
81 scoped_ptr<ModuleRtpRtcpImpl> rtp_rtcp_impl_;
82 SendTransport transport_;
83 RtcpRttStatsTestImpl rtt_stats_;
86 TEST_F(RtpRtcpImplTest, Rtt) {
87 const uint32_t kSsrc = 0x12345;
88 RTPHeader header = {};
90 header.sequenceNumber = 123;
92 header.headerLength = 12;
93 receive_statistics_->IncomingPacket(header, 100, false);
95 rtp_rtcp_impl_->SetRemoteSSRC(kSsrc);
96 EXPECT_EQ(0, rtp_rtcp_impl_->SetSendingStatus(true));
97 EXPECT_EQ(0, rtp_rtcp_impl_->SetRTCPStatus(kRtcpCompound));
98 EXPECT_EQ(0, rtp_rtcp_impl_->SetSSRC(kSsrc));
100 // A SR should have been sent and received.
101 EXPECT_EQ(0, rtp_rtcp_impl_->SendRTCP(kRtcpReport));
103 // Send new SR. A response to the last SR should be sent.
104 clock_.AdvanceTimeMilliseconds(1000);
105 transport_.SimulateNetworkDelay(100, &clock_);
106 EXPECT_EQ(0, rtp_rtcp_impl_->SendRTCP(kRtcpReport));
113 EXPECT_EQ(0, rtp_rtcp_impl_->RTT(kSsrc, &rtt, &avg_rtt, &min_rtt, &max_rtt));
115 EXPECT_EQ(100, avg_rtt);
116 EXPECT_EQ(100, min_rtt);
117 EXPECT_EQ(100, max_rtt);
119 // No RTT from other ssrc.
121 rtp_rtcp_impl_->RTT(kSsrc + 1, &rtt, &avg_rtt, &min_rtt, &max_rtt));
124 TEST_F(RtpRtcpImplTest, RttForReceiverOnly) {
125 rtp_rtcp_impl_->SetRtcpXrRrtrStatus(true);
126 EXPECT_EQ(0, rtp_rtcp_impl_->SetSendingStatus(false));
127 EXPECT_EQ(0, rtp_rtcp_impl_->SetRTCPStatus(kRtcpCompound));
128 EXPECT_EQ(0, rtp_rtcp_impl_->SetSSRC(0x12345));
130 // A Receiver time reference report (RTRR) should be sent and received.
131 EXPECT_EQ(0, rtp_rtcp_impl_->SendRTCP(kRtcpReport));
133 // Send new RTRR. A response to the last RTRR should be sent.
134 clock_.AdvanceTimeMilliseconds(1000);
135 transport_.SimulateNetworkDelay(100, &clock_);
136 EXPECT_EQ(0, rtp_rtcp_impl_->SendRTCP(kRtcpReport));
139 EXPECT_EQ(0U, rtt_stats_.rtt_ms_);
141 rtp_rtcp_impl_->Process();
142 EXPECT_EQ(100U, rtt_stats_.rtt_ms_);
145 } // namespace webrtc