- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / rtp_rtcp / source / receive_statistics_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 #include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
14 #include "webrtc/system_wrappers/interface/clock.h"
15 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
16
17 namespace webrtc {
18
19 const int kPacketSize1 = 100;
20 const int kPacketSize2 = 300;
21 const uint32_t kSsrc1 = 1;
22 const uint32_t kSsrc2 = 2;
23
24 class ReceiveStatisticsTest : public ::testing::Test {
25  public:
26   ReceiveStatisticsTest() :
27       clock_(0),
28       receive_statistics_(ReceiveStatistics::Create(&clock_)) {
29     memset(&header1_, 0, sizeof(header1_));
30     header1_.ssrc = kSsrc1;
31     header1_.sequenceNumber = 0;
32     memset(&header2_, 0, sizeof(header2_));
33     header2_.ssrc = kSsrc2;
34     header2_.sequenceNumber = 0;
35   }
36
37  protected:
38   SimulatedClock clock_;
39   scoped_ptr<ReceiveStatistics> receive_statistics_;
40   RTPHeader header1_;
41   RTPHeader header2_;
42 };
43
44 TEST_F(ReceiveStatisticsTest, TwoIncomingSsrcs) {
45   receive_statistics_->IncomingPacket(header1_, kPacketSize1, false);
46   ++header1_.sequenceNumber;
47   receive_statistics_->IncomingPacket(header2_, kPacketSize2, false);
48   ++header2_.sequenceNumber;
49   clock_.AdvanceTimeMilliseconds(100);
50   receive_statistics_->IncomingPacket(header1_, kPacketSize1, false);
51   ++header1_.sequenceNumber;
52   receive_statistics_->IncomingPacket(header2_, kPacketSize2, false);
53   ++header2_.sequenceNumber;
54
55   StreamStatistician* statistician =
56       receive_statistics_->GetStatistician(kSsrc1);
57   ASSERT_TRUE(statistician != NULL);
58   EXPECT_GT(statistician->BitrateReceived(), 0u);
59   uint32_t bytes_received = 0;
60   uint32_t packets_received = 0;
61   statistician->GetDataCounters(&bytes_received, &packets_received);
62   EXPECT_EQ(200u, bytes_received);
63   EXPECT_EQ(2u, packets_received);
64
65   statistician =
66       receive_statistics_->GetStatistician(kSsrc2);
67   ASSERT_TRUE(statistician != NULL);
68   EXPECT_GT(statistician->BitrateReceived(), 0u);
69   statistician->GetDataCounters(&bytes_received, &packets_received);
70   EXPECT_EQ(600u, bytes_received);
71   EXPECT_EQ(2u, packets_received);
72
73   StatisticianMap statisticians = receive_statistics_->GetActiveStatisticians();
74   EXPECT_EQ(2u, statisticians.size());
75   // Add more incoming packets and verify that they are registered in both
76   // access methods.
77   receive_statistics_->IncomingPacket(header1_, kPacketSize1, false);
78   ++header1_.sequenceNumber;
79   receive_statistics_->IncomingPacket(header2_, kPacketSize2, false);
80   ++header2_.sequenceNumber;
81
82   statisticians[kSsrc1]->GetDataCounters(&bytes_received, &packets_received);
83   EXPECT_EQ(300u, bytes_received);
84   EXPECT_EQ(3u, packets_received);
85   statisticians[kSsrc2]->GetDataCounters(&bytes_received, &packets_received);
86   EXPECT_EQ(900u, bytes_received);
87   EXPECT_EQ(3u, packets_received);
88
89   receive_statistics_->GetStatistician(kSsrc1)->GetDataCounters(
90       &bytes_received, &packets_received);
91   EXPECT_EQ(300u, bytes_received);
92   EXPECT_EQ(3u, packets_received);
93   receive_statistics_->GetStatistician(kSsrc2)->GetDataCounters(
94       &bytes_received, &packets_received);
95   EXPECT_EQ(900u, bytes_received);
96   EXPECT_EQ(3u, packets_received);
97 }
98
99 TEST_F(ReceiveStatisticsTest, ActiveStatisticians) {
100   receive_statistics_->IncomingPacket(header1_, kPacketSize1, false);
101   ++header1_.sequenceNumber;
102   clock_.AdvanceTimeMilliseconds(1000);
103   receive_statistics_->IncomingPacket(header2_, kPacketSize2, false);
104   ++header2_.sequenceNumber;
105   StatisticianMap statisticians = receive_statistics_->GetActiveStatisticians();
106   // Nothing should time out since only 1000 ms has passed since the first
107   // packet came in.
108   EXPECT_EQ(2u, statisticians.size());
109
110   clock_.AdvanceTimeMilliseconds(7000);
111   // kSsrc1 should have timed out.
112   statisticians = receive_statistics_->GetActiveStatisticians();
113   EXPECT_EQ(1u, statisticians.size());
114
115   clock_.AdvanceTimeMilliseconds(1000);
116   // kSsrc2 should have timed out.
117   statisticians = receive_statistics_->GetActiveStatisticians();
118   EXPECT_EQ(0u, statisticians.size());
119
120   receive_statistics_->IncomingPacket(header1_, kPacketSize1, false);
121   ++header1_.sequenceNumber;
122   // kSsrc1 should be active again and the data counters should have survived.
123   statisticians = receive_statistics_->GetActiveStatisticians();
124   EXPECT_EQ(1u, statisticians.size());
125   StreamStatistician* statistician =
126       receive_statistics_->GetStatistician(kSsrc1);
127   ASSERT_TRUE(statistician != NULL);
128   uint32_t bytes_received = 0;
129   uint32_t packets_received = 0;
130   statistician->GetDataCounters(&bytes_received, &packets_received);
131   EXPECT_EQ(200u, bytes_received);
132   EXPECT_EQ(2u, packets_received);
133 }
134 }  // namespace webrtc