1 /*
2  *  Copyright (c) 2015 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 "modules/rtp_rtcp/source/packet_loss_stats.h"
12 
13 #include "test/gtest.h"
14 
15 namespace webrtc {
16 
17 class PacketLossStatsTest : public ::testing::Test {
18  protected:
19   PacketLossStats stats_;
20 };
21 
22 // Add a lost packet as every other packet, they should all count as single
23 // losses.
TEST_F(PacketLossStatsTest,EveryOtherPacket)24 TEST_F(PacketLossStatsTest, EveryOtherPacket) {
25   for (int i = 0; i < 1000; i += 2) {
26     stats_.AddLostPacket(i);
27   }
28   EXPECT_EQ(500, stats_.GetSingleLossCount());
29   EXPECT_EQ(0, stats_.GetMultipleLossEventCount());
30   EXPECT_EQ(0, stats_.GetMultipleLossPacketCount());
31 }
32 
33 // Add a lost packet as every other packet, but such that the sequence numbers
34 // will wrap around while they are being added.
TEST_F(PacketLossStatsTest,EveryOtherPacketWrapped)35 TEST_F(PacketLossStatsTest, EveryOtherPacketWrapped) {
36   for (int i = 65500; i < 66500; i += 2) {
37     stats_.AddLostPacket(i & 0xFFFF);
38   }
39   EXPECT_EQ(500, stats_.GetSingleLossCount());
40   EXPECT_EQ(0, stats_.GetMultipleLossEventCount());
41   EXPECT_EQ(0, stats_.GetMultipleLossPacketCount());
42 }
43 
44 // Add a lost packet as every other packet, but such that the sequence numbers
45 // will wrap around close to the very end, such that the buffer contains packets
46 // on either side of the wrapping.
TEST_F(PacketLossStatsTest,EveryOtherPacketWrappedAtEnd)47 TEST_F(PacketLossStatsTest, EveryOtherPacketWrappedAtEnd) {
48   for (int i = 64600; i < 65600; i += 2) {
49     stats_.AddLostPacket(i & 0xFFFF);
50   }
51   EXPECT_EQ(500, stats_.GetSingleLossCount());
52   EXPECT_EQ(0, stats_.GetMultipleLossEventCount());
53   EXPECT_EQ(0, stats_.GetMultipleLossPacketCount());
54 }
55 
56 // Add a lost packet as the first three of every eight packets. Each set of
57 // three should count as a multiple loss event and three multiple loss packets.
TEST_F(PacketLossStatsTest,FirstThreeOfEight)58 TEST_F(PacketLossStatsTest, FirstThreeOfEight) {
59   for (int i = 0; i < 1000; ++i) {
60     if ((i & 7) < 3) {
61       stats_.AddLostPacket(i);
62     }
63   }
64   EXPECT_EQ(0, stats_.GetSingleLossCount());
65   EXPECT_EQ(125, stats_.GetMultipleLossEventCount());
66   EXPECT_EQ(375, stats_.GetMultipleLossPacketCount());
67 }
68 
69 // Add a lost packet as the first three of every eight packets such that the
70 // sequence numbers wrap in the middle of adding them.
TEST_F(PacketLossStatsTest,FirstThreeOfEightWrapped)71 TEST_F(PacketLossStatsTest, FirstThreeOfEightWrapped) {
72   for (int i = 65500; i < 66500; ++i) {
73     if ((i & 7) < 3) {
74       stats_.AddLostPacket(i & 0xFFFF);
75     }
76   }
77   EXPECT_EQ(0, stats_.GetSingleLossCount());
78   EXPECT_EQ(125, stats_.GetMultipleLossEventCount());
79   EXPECT_EQ(375, stats_.GetMultipleLossPacketCount());
80 }
81 
82 // Add a lost packet as the first three of every eight packets such that the
83 // sequence numbers wrap near the end of adding them and there are still numbers
84 // in the buffer from before the wrapping.
TEST_F(PacketLossStatsTest,FirstThreeOfEightWrappedAtEnd)85 TEST_F(PacketLossStatsTest, FirstThreeOfEightWrappedAtEnd) {
86   for (int i = 64600; i < 65600; ++i) {
87     if ((i & 7) < 3) {
88       stats_.AddLostPacket(i & 0xFFFF);
89     }
90   }
91   EXPECT_EQ(0, stats_.GetSingleLossCount());
92   EXPECT_EQ(125, stats_.GetMultipleLossEventCount());
93   EXPECT_EQ(375, stats_.GetMultipleLossPacketCount());
94 }
95 
96 // Add loss packets as the first three and the fifth of every eight packets. The
97 // set of three should be multiple loss and the fifth should be single loss.
TEST_F(PacketLossStatsTest,FirstThreeAndFifthOfEight)98 TEST_F(PacketLossStatsTest, FirstThreeAndFifthOfEight) {
99   for (int i = 0; i < 1000; ++i) {
100     if ((i & 7) < 3 || (i & 7) == 4) {
101       stats_.AddLostPacket(i);
102     }
103   }
104   EXPECT_EQ(125, stats_.GetSingleLossCount());
105   EXPECT_EQ(125, stats_.GetMultipleLossEventCount());
106   EXPECT_EQ(375, stats_.GetMultipleLossPacketCount());
107 }
108 
109 // Add loss packets as the first three and the fifth of every eight packets such
110 // that the sequence numbers wrap in the middle of adding them.
TEST_F(PacketLossStatsTest,FirstThreeAndFifthOfEightWrapped)111 TEST_F(PacketLossStatsTest, FirstThreeAndFifthOfEightWrapped) {
112   for (int i = 65500; i < 66500; ++i) {
113     if ((i & 7) < 3 || (i & 7) == 4) {
114       stats_.AddLostPacket(i & 0xFFFF);
115     }
116   }
117   EXPECT_EQ(125, stats_.GetSingleLossCount());
118   EXPECT_EQ(125, stats_.GetMultipleLossEventCount());
119   EXPECT_EQ(375, stats_.GetMultipleLossPacketCount());
120 }
121 
122 // Add loss packets as the first three and the fifth of every eight packets such
123 // that the sequence numbers wrap near the end of adding them and there are
124 // packets from before the wrapping still in the buffer.
TEST_F(PacketLossStatsTest,FirstThreeAndFifthOfEightWrappedAtEnd)125 TEST_F(PacketLossStatsTest, FirstThreeAndFifthOfEightWrappedAtEnd) {
126   for (int i = 64600; i < 65600; ++i) {
127     if ((i & 7) < 3 || (i & 7) == 4) {
128       stats_.AddLostPacket(i & 0xFFFF);
129     }
130   }
131   EXPECT_EQ(125, stats_.GetSingleLossCount());
132   EXPECT_EQ(125, stats_.GetMultipleLossEventCount());
133   EXPECT_EQ(375, stats_.GetMultipleLossPacketCount());
134 }
135 
136 // Add loss packets such that there is a multiple loss event that continues
137 // around the wrapping of sequence numbers.
TEST_F(PacketLossStatsTest,MultipleLossEventWrapped)138 TEST_F(PacketLossStatsTest, MultipleLossEventWrapped) {
139   for (int i = 60000; i < 60500; i += 2) {
140     stats_.AddLostPacket(i);
141   }
142   for (int i = 65530; i < 65540; ++i) {
143     stats_.AddLostPacket(i & 0xFFFF);
144   }
145   EXPECT_EQ(250, stats_.GetSingleLossCount());
146   EXPECT_EQ(1, stats_.GetMultipleLossEventCount());
147   EXPECT_EQ(10, stats_.GetMultipleLossPacketCount());
148 }
149 
150 // Add loss packets such that there is a multiple loss event that continues
151 // around the wrapping of sequence numbers and then is pushed out of the buffer.
TEST_F(PacketLossStatsTest,MultipleLossEventWrappedPushedOut)152 TEST_F(PacketLossStatsTest, MultipleLossEventWrappedPushedOut) {
153   for (int i = 60000; i < 60500; i += 2) {
154     stats_.AddLostPacket(i);
155   }
156   for (int i = 65530; i < 65540; ++i) {
157     stats_.AddLostPacket(i & 0xFFFF);
158   }
159   for (int i = 1000; i < 1500; i += 2) {
160     stats_.AddLostPacket(i);
161   }
162   EXPECT_EQ(500, stats_.GetSingleLossCount());
163   EXPECT_EQ(1, stats_.GetMultipleLossEventCount());
164   EXPECT_EQ(10, stats_.GetMultipleLossPacketCount());
165 }
166 
167 // Add loss packets out of order and ensure that they still get counted
168 // correctly as single or multiple loss events.
TEST_F(PacketLossStatsTest,OutOfOrder)169 TEST_F(PacketLossStatsTest, OutOfOrder) {
170   for (int i = 0; i < 1000; i += 10) {
171     stats_.AddLostPacket(i + 5);
172     stats_.AddLostPacket(i + 7);
173     stats_.AddLostPacket(i + 4);
174     stats_.AddLostPacket(i + 1);
175     stats_.AddLostPacket(i + 2);
176   }
177   EXPECT_EQ(100, stats_.GetSingleLossCount());
178   EXPECT_EQ(200, stats_.GetMultipleLossEventCount());
179   EXPECT_EQ(400, stats_.GetMultipleLossPacketCount());
180 }
181 
182 // Add loss packets out of order and ensure that they still get counted
183 // correctly as single or multiple loss events, and wrap in the middle of
184 // adding.
TEST_F(PacketLossStatsTest,OutOfOrderWrapped)185 TEST_F(PacketLossStatsTest, OutOfOrderWrapped) {
186   for (int i = 65000; i < 66000; i += 10) {
187     stats_.AddLostPacket((i + 5) & 0xFFFF);
188     stats_.AddLostPacket((i + 7) & 0xFFFF);
189     stats_.AddLostPacket((i + 4) & 0xFFFF);
190     stats_.AddLostPacket((i + 1) & 0xFFFF);
191     stats_.AddLostPacket((i + 2) & 0xFFFF);
192   }
193   EXPECT_EQ(100, stats_.GetSingleLossCount());
194   EXPECT_EQ(200, stats_.GetMultipleLossEventCount());
195   EXPECT_EQ(400, stats_.GetMultipleLossPacketCount());
196 }
197 
198 }  // namespace webrtc
199