OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 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 <algorithm> |
| 12 |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "webrtc/base/logging.h" |
| 15 #include "webrtc/base/random.h" |
| 16 #include "webrtc/modules/rtp_rtcp/source/report_block_information.h" |
| 17 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/report_block.h" |
| 18 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" |
| 19 #include "webrtc/modules/rtp_rtcp/source/time_util.h" |
| 20 |
| 21 using webrtc::rtcp::ReportBlock; |
| 22 using webrtc::rtcp::ReportBlockInformation; |
| 23 using webrtc::RTCPUtility::RTCPPacketReportBlockItem; |
| 24 |
| 25 namespace webrtc { |
| 26 |
| 27 class RtcpReportBlockInformationTest : public ::testing::Test { |
| 28 protected: |
| 29 Random rand_; |
| 30 const uint32_t kSourceSsrc; |
| 31 const uint32_t kRemoteSsrc; |
| 32 const uint32_t kDelayNtp; |
| 33 const uint8_t kFractionLost; |
| 34 const uint32_t kCumulativeLost; |
| 35 const uint32_t kSequenceNumber; |
| 36 const uint32_t kJitter; |
| 37 const uint32_t kLastSr; |
| 38 RtcpReportBlockInformationTest() |
| 39 : rand_(0x123456789abcdef), |
| 40 kSourceSsrc(RandSsrc()), |
| 41 kRemoteSsrc(RandSsrc()), |
| 42 kDelayNtp(Rand<uint32_t>()), |
| 43 kFractionLost(Rand<uint8_t>()), |
| 44 kCumulativeLost(Rand((1 << 24) - 1)), |
| 45 kSequenceNumber(Rand<uint32_t>()), |
| 46 kJitter(Rand<uint32_t>()), |
| 47 kLastSr(Rand<uint32_t>()) {} |
| 48 |
| 49 uint32_t RandSsrc() { return rand_.Rand(0x00000001u, 0xfffffffeu); } |
| 50 template <typename T> |
| 51 T Rand() { |
| 52 return rand_.Rand<T>(); |
| 53 } |
| 54 uint32_t Rand(uint32_t max) { return rand_.Rand(max); } |
| 55 uint32_t Rand(uint32_t min, uint32_t max) { return rand_.Rand(min, max); } |
| 56 }; |
| 57 |
| 58 template <> |
| 59 NtpTime RtcpReportBlockInformationTest::Rand<NtpTime>() { |
| 60 return NtpTime(Rand<uint32_t>(), Rand<uint32_t>()); |
| 61 } |
| 62 |
| 63 TEST_F(RtcpReportBlockInformationTest, RemembersLastReportBlock) { |
| 64 ReportBlockInformation rbi; |
| 65 const NtpTime kNow = Rand<NtpTime>(); |
| 66 ReportBlock b; |
| 67 b.To(kSourceSsrc); |
| 68 b.WithFractionLost(kFractionLost); |
| 69 b.WithCumulativeLost(kCumulativeLost); |
| 70 b.WithExtHighestSeqNum(kSequenceNumber); |
| 71 b.WithJitter(kJitter); |
| 72 b.WithLastSr(kLastSr); |
| 73 b.WithDelayLastSr(kDelayNtp); |
| 74 |
| 75 rbi.AddBlock(b, kRemoteSsrc, kNow); |
| 76 |
| 77 EXPECT_EQ(kSourceSsrc, rbi.LastBlock().sourceSSRC); |
| 78 EXPECT_EQ(kRemoteSsrc, rbi.LastBlock().remoteSSRC); |
| 79 EXPECT_EQ(kFractionLost, rbi.LastBlock().fractionLost); |
| 80 EXPECT_EQ(kCumulativeLost, rbi.LastBlock().cumulativeLost); |
| 81 EXPECT_EQ(kSequenceNumber, rbi.LastBlock().extendedHighSeqNum); |
| 82 EXPECT_EQ(kJitter, rbi.LastBlock().jitter); |
| 83 EXPECT_EQ(kLastSr, rbi.LastBlock().lastSR); |
| 84 EXPECT_EQ(kDelayNtp, rbi.LastBlock().delaySinceLastSR); |
| 85 } |
| 86 |
| 87 TEST_F(RtcpReportBlockInformationTest, AcceptsRTCPUtilityReportBlockItem) { |
| 88 ReportBlockInformation rbi; |
| 89 const NtpTime kNow = Rand<NtpTime>(); |
| 90 RTCPPacketReportBlockItem b; |
| 91 b.SSRC = kSourceSsrc; |
| 92 b.FractionLost = kFractionLost; |
| 93 b.CumulativeNumOfPacketsLost = kCumulativeLost; |
| 94 b.ExtendedHighestSequenceNumber = kSequenceNumber; |
| 95 b.Jitter = kJitter; |
| 96 b.LastSR = kLastSr; |
| 97 b.DelayLastSR = kDelayNtp; |
| 98 |
| 99 rbi.AddBlock(b, kRemoteSsrc, kNow); |
| 100 |
| 101 EXPECT_EQ(kSourceSsrc, rbi.LastBlock().sourceSSRC); |
| 102 EXPECT_EQ(kRemoteSsrc, rbi.LastBlock().remoteSSRC); |
| 103 EXPECT_EQ(kFractionLost, rbi.LastBlock().fractionLost); |
| 104 EXPECT_EQ(kCumulativeLost, rbi.LastBlock().cumulativeLost); |
| 105 EXPECT_EQ(kSequenceNumber, rbi.LastBlock().extendedHighSeqNum); |
| 106 EXPECT_EQ(kJitter, rbi.LastBlock().jitter); |
| 107 EXPECT_EQ(kLastSr, rbi.LastBlock().lastSR); |
| 108 EXPECT_EQ(kDelayNtp, rbi.LastBlock().delaySinceLastSR); |
| 109 } |
| 110 |
| 111 TEST_F(RtcpReportBlockInformationTest, ReportBlockWithoutRTT) { |
| 112 ReportBlockInformation rbi; |
| 113 const NtpTime kNow = Rand<NtpTime>(); |
| 114 ReportBlock b; |
| 115 b.To(kSourceSsrc); |
| 116 b.WithLastSr(0); // LastSR = 0 means RTT can't be calculated. |
| 117 |
| 118 rbi.AddBlock(b, kRemoteSsrc, kNow); |
| 119 |
| 120 EXPECT_FALSE(rbi.HasRtt()); |
| 121 EXPECT_EQ(kSourceSsrc, rbi.LastBlock().sourceSSRC); |
| 122 EXPECT_EQ(kRemoteSsrc, rbi.LastBlock().remoteSSRC); |
| 123 } |
| 124 |
| 125 TEST_F(RtcpReportBlockInformationTest, ReportBlockWithRTT) { |
| 126 const uint32_t kRttMs = Rand(1, 18 * 3600 * 1000); |
| 127 ReportBlockInformation rbi; |
| 128 SimulatedClock clock(Rand<uint32_t>()); |
| 129 ReportBlock b; |
| 130 b.To(kSourceSsrc); |
| 131 NtpTime start(clock); |
| 132 b.WithLastSr(CompactNtp(start)); |
| 133 b.WithDelayLastSr(kDelayNtp); |
| 134 clock.AdvanceTimeMilliseconds(kRttMs + CompactNtpIntervalToMs(kDelayNtp)); |
| 135 NtpTime now(clock); |
| 136 |
| 137 rbi.AddBlock(b, kRemoteSsrc, now); |
| 138 |
| 139 EXPECT_TRUE(rbi.HasRtt()); |
| 140 EXPECT_NEAR(kRttMs, rbi.LastRttMs(), 1); |
| 141 } |
| 142 |
| 143 TEST_F(RtcpReportBlockInformationTest, RTTStats) { |
| 144 const uint32_t kRttMs1 = Rand(1, 18 * 3600 * 1000); |
| 145 const uint32_t kRttMs2 = Rand(1, 18 * 3600 * 1000); |
| 146 const uint32_t kRttMs3 = Rand(1, 18 * 3600 * 1000); |
| 147 const uint32_t kRttMin = std::min({kRttMs1, kRttMs2, kRttMs3}); |
| 148 const uint32_t kRttMax = std::max({kRttMs1, kRttMs2, kRttMs3}); |
| 149 const uint32_t kRttAvg = (kRttMs1 + kRttMs2 + kRttMs3) / 3; |
| 150 const uint32_t kDelayMs = CompactNtpIntervalToMs(kDelayNtp); |
| 151 ReportBlockInformation rbi; |
| 152 SimulatedClock clock(Rand<uint32_t>()); |
| 153 ReportBlock b; |
| 154 b.To(kSourceSsrc); |
| 155 const NtpTime start(clock); |
| 156 b.WithDelayLastSr(kDelayNtp); |
| 157 |
| 158 b.WithLastSr(CompactNtp(start)); |
| 159 clock.AdvanceTimeMilliseconds(kRttMs1 + kDelayMs); |
| 160 const NtpTime time1(clock); |
| 161 rbi.AddBlock(b, kRemoteSsrc, time1); |
| 162 |
| 163 b.WithLastSr(CompactNtp(time1)); |
| 164 clock.AdvanceTimeMilliseconds(kRttMs2 + kDelayMs); |
| 165 const NtpTime time2(clock); |
| 166 rbi.AddBlock(b, kRemoteSsrc, time2); |
| 167 |
| 168 b.WithLastSr(CompactNtp(time2)); |
| 169 clock.AdvanceTimeMilliseconds(kRttMs3 + kDelayMs); |
| 170 const NtpTime time3(clock); |
| 171 rbi.AddBlock(b, kRemoteSsrc, time3); |
| 172 |
| 173 EXPECT_TRUE(rbi.HasRtt()); |
| 174 EXPECT_NEAR(kRttMs3, rbi.LastRttMs(), 1); |
| 175 EXPECT_NEAR(kRttMin, rbi.MinRttMs(), 1); |
| 176 EXPECT_NEAR(kRttMax, rbi.MaxRttMs(), 1); |
| 177 EXPECT_NEAR(kRttAvg, rbi.AvgRttMs(), 1); |
| 178 } |
| 179 |
| 180 } // namespace webrtc |
OLD | NEW |