ff3c32e2201fc1d83fd5211b9ae2213977eedce3
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / rtp_rtcp / source / rtp_rtcp_impl_unittest.cc
1 /*
2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3  *
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.
9  */
10
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
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"
17
18 namespace webrtc {
19 namespace {
20
21 class RtcpRttStatsTestImpl : public RtcpRttObserver {
22  public:
23   RtcpRttStatsTestImpl() : rtt_ms_(0) {}
24   virtual ~RtcpRttStatsTestImpl() {}
25
26   virtual void OnRttUpdate(uint32_t rtt_ms) {
27     rtt_ms_ = rtt_ms;
28   }
29   uint32_t rtt_ms_;
30 };
31
32 class SendTransport : public Transport,
33                       public NullRtpData {
34  public:
35   SendTransport() : rtp_rtcp_impl_(NULL), clock_(NULL), delay_ms_(0) {}
36
37   void SetRtpRtcpModule(ModuleRtpRtcpImpl* rtp_rtcp_impl) {
38     rtp_rtcp_impl_ = rtp_rtcp_impl;
39   }
40   void SimulateNetworkDelay(int delay_ms, SimulatedClock* clock) {
41     clock_ = clock;
42     delay_ms_ = delay_ms;
43   }
44   virtual int SendPacket(int /*ch*/, const void* /*data*/, int /*len*/) {
45     return -1;
46   }
47   virtual int SendRTCPPacket(int /*ch*/, const void *data, int len) {
48     if (clock_) {
49       clock_->AdvanceTimeMilliseconds(delay_ms_);
50     }
51     EXPECT_TRUE(rtp_rtcp_impl_ != NULL);
52     EXPECT_EQ(0, rtp_rtcp_impl_->IncomingRtcpPacket(
53         static_cast<const uint8_t*>(data), len));
54     return len;
55   }
56   ModuleRtpRtcpImpl* rtp_rtcp_impl_;
57   SimulatedClock* clock_;
58   int delay_ms_;
59 };
60 }  // namespace
61
62 class RtpRtcpImplTest : public ::testing::Test {
63  protected:
64   RtpRtcpImplTest()
65       : clock_(1335900000),
66         receive_statistics_(ReceiveStatistics::Create(&clock_)) {
67     RtpRtcp::Configuration configuration;
68     configuration.id = 0;
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_;
74
75     rtp_rtcp_impl_.reset(new ModuleRtpRtcpImpl(configuration));
76     transport_.SetRtpRtcpModule(rtp_rtcp_impl_.get());
77   }
78
79   SimulatedClock clock_;
80   scoped_ptr<ReceiveStatistics> receive_statistics_;
81   scoped_ptr<ModuleRtpRtcpImpl> rtp_rtcp_impl_;
82   SendTransport transport_;
83   RtcpRttStatsTestImpl rtt_stats_;
84 };
85
86 TEST_F(RtpRtcpImplTest, Rtt) {
87   const uint32_t kSsrc = 0x12345;
88   RTPHeader header = {};
89   header.timestamp = 1;
90   header.sequenceNumber = 123;
91   header.ssrc = kSsrc;
92   header.headerLength = 12;
93   receive_statistics_->IncomingPacket(header, 100, false);
94
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));
99
100   // A SR should have been sent and received.
101   EXPECT_EQ(0, rtp_rtcp_impl_->SendRTCP(kRtcpReport));
102
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));
107
108   // Verify RTT.
109   uint16_t rtt;
110   uint16_t avg_rtt;
111   uint16_t min_rtt;
112   uint16_t max_rtt;
113   EXPECT_EQ(0, rtp_rtcp_impl_->RTT(kSsrc, &rtt, &avg_rtt, &min_rtt, &max_rtt));
114   EXPECT_EQ(100, rtt);
115   EXPECT_EQ(100, avg_rtt);
116   EXPECT_EQ(100, min_rtt);
117   EXPECT_EQ(100, max_rtt);
118
119   // No RTT from other ssrc.
120   EXPECT_EQ(-1,
121       rtp_rtcp_impl_->RTT(kSsrc + 1, &rtt, &avg_rtt, &min_rtt, &max_rtt));
122 }
123
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));
129
130   // A Receiver time reference report (RTRR) should be sent and received.
131   EXPECT_EQ(0, rtp_rtcp_impl_->SendRTCP(kRtcpReport));
132
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));
137
138   // Verify RTT.
139   EXPECT_EQ(0U, rtt_stats_.rtt_ms_);
140
141   rtp_rtcp_impl_->Process();
142   EXPECT_EQ(100U, rtt_stats_.rtt_ms_);
143 }
144
145 }  // namespace webrtc