danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h" |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 12 | |
Erik Språng | de5507d | 2020-09-11 13:35:32 | [diff] [blame] | 13 | #include <memory> |
| 14 | #include <utility> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "modules/rtp_rtcp/source/rtcp_packet.h" |
| 17 | #include "modules/rtp_rtcp/source/rtcp_packet/bye.h" |
| 18 | #include "modules/rtp_rtcp/source/rtcp_packet/fir.h" |
| 19 | #include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h" |
| 20 | #include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h" |
Danil Chapovalov | 5c3cc41 | 2017-12-07 09:15:53 | [diff] [blame] | 21 | #include "test/gmock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 22 | #include "test/gtest.h" |
| 23 | #include "test/rtcp_packet_parser.h" |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 24 | |
Danil Chapovalov | 5c3cc41 | 2017-12-07 09:15:53 | [diff] [blame] | 25 | using ::testing::_; |
| 26 | using ::testing::Invoke; |
| 27 | using ::testing::MockFunction; |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 28 | using webrtc::rtcp::Bye; |
| 29 | using webrtc::rtcp::CompoundPacket; |
| 30 | using webrtc::rtcp::Fir; |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 31 | using webrtc::rtcp::ReceiverReport; |
| 32 | using webrtc::rtcp::ReportBlock; |
| 33 | using webrtc::rtcp::SenderReport; |
| 34 | using webrtc::test::RtcpPacketParser; |
| 35 | |
| 36 | namespace webrtc { |
| 37 | |
| 38 | const uint32_t kSenderSsrc = 0x12345678; |
Danil Chapovalov | 32e590e | 2016-01-22 10:04:56 | [diff] [blame] | 39 | const uint32_t kRemoteSsrc = 0x23456789; |
| 40 | const uint8_t kSeqNo = 13; |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 41 | |
| 42 | TEST(RtcpCompoundPacketTest, AppendPacket) { |
danilchap | 7a4116a | 2016-03-14 15:19:28 | [diff] [blame] | 43 | CompoundPacket compound; |
Erik Språng | de5507d | 2020-09-11 13:35:32 | [diff] [blame] | 44 | auto fir = std::make_unique<Fir>(); |
| 45 | fir->AddRequestTo(kRemoteSsrc, kSeqNo); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 46 | ReportBlock rb; |
Erik Språng | de5507d | 2020-09-11 13:35:32 | [diff] [blame] | 47 | auto rr = std::make_unique<ReceiverReport>(); |
| 48 | rr->SetSenderSsrc(kSenderSsrc); |
| 49 | EXPECT_TRUE(rr->AddReportBlock(rb)); |
| 50 | compound.Append(std::move(rr)); |
| 51 | compound.Append(std::move(fir)); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 52 | |
danilchap | 7a4116a | 2016-03-14 15:19:28 | [diff] [blame] | 53 | rtc::Buffer packet = compound.Build(); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 54 | RtcpPacketParser parser; |
danilchap | 69e59e6 | 2016-02-17 11:11:42 | [diff] [blame] | 55 | parser.Parse(packet.data(), packet.size()); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 56 | EXPECT_EQ(1, parser.receiver_report()->num_packets()); |
Danil Chapovalov | ba6f7be | 2016-09-02 16:29:10 | [diff] [blame] | 57 | EXPECT_EQ(kSenderSsrc, parser.receiver_report()->sender_ssrc()); |
| 58 | EXPECT_EQ(1u, parser.receiver_report()->report_blocks().size()); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 59 | EXPECT_EQ(1, parser.fir()->num_packets()); |
| 60 | } |
| 61 | |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 62 | TEST(RtcpCompoundPacketTest, AppendPacketWithOwnAppendedPacket) { |
danilchap | 7a4116a | 2016-03-14 15:19:28 | [diff] [blame] | 63 | CompoundPacket root; |
Erik Språng | de5507d | 2020-09-11 13:35:32 | [diff] [blame] | 64 | auto leaf = std::make_unique<CompoundPacket>(); |
| 65 | |
| 66 | auto fir = std::make_unique<Fir>(); |
| 67 | fir->AddRequestTo(kRemoteSsrc, kSeqNo); |
| 68 | auto bye = std::make_unique<Bye>(); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 69 | ReportBlock rb; |
| 70 | |
Erik Språng | de5507d | 2020-09-11 13:35:32 | [diff] [blame] | 71 | auto rr = std::make_unique<ReceiverReport>(); |
| 72 | EXPECT_TRUE(rr->AddReportBlock(rb)); |
| 73 | leaf->Append(std::move(rr)); |
| 74 | leaf->Append(std::move(fir)); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 75 | |
Erik Språng | de5507d | 2020-09-11 13:35:32 | [diff] [blame] | 76 | auto sr = std::make_unique<SenderReport>(); |
| 77 | root.Append(std::move(sr)); |
| 78 | root.Append(std::move(bye)); |
| 79 | root.Append(std::move(leaf)); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 80 | |
danilchap | 7a4116a | 2016-03-14 15:19:28 | [diff] [blame] | 81 | rtc::Buffer packet = root.Build(); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 82 | RtcpPacketParser parser; |
danilchap | 69e59e6 | 2016-02-17 11:11:42 | [diff] [blame] | 83 | parser.Parse(packet.data(), packet.size()); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 84 | EXPECT_EQ(1, parser.sender_report()->num_packets()); |
| 85 | EXPECT_EQ(1, parser.receiver_report()->num_packets()); |
Danil Chapovalov | ba6f7be | 2016-09-02 16:29:10 | [diff] [blame] | 86 | EXPECT_EQ(1u, parser.receiver_report()->report_blocks().size()); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 87 | EXPECT_EQ(1, parser.bye()->num_packets()); |
| 88 | EXPECT_EQ(1, parser.fir()->num_packets()); |
| 89 | } |
| 90 | |
| 91 | TEST(RtcpCompoundPacketTest, BuildWithInputBuffer) { |
danilchap | 7a4116a | 2016-03-14 15:19:28 | [diff] [blame] | 92 | CompoundPacket compound; |
Erik Språng | de5507d | 2020-09-11 13:35:32 | [diff] [blame] | 93 | auto fir = std::make_unique<Fir>(); |
| 94 | fir->AddRequestTo(kRemoteSsrc, kSeqNo); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 95 | ReportBlock rb; |
Erik Språng | de5507d | 2020-09-11 13:35:32 | [diff] [blame] | 96 | auto rr = std::make_unique<ReceiverReport>(); |
| 97 | rr->SetSenderSsrc(kSenderSsrc); |
| 98 | EXPECT_TRUE(rr->AddReportBlock(rb)); |
| 99 | compound.Append(std::move(rr)); |
| 100 | compound.Append(std::move(fir)); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 101 | |
| 102 | const size_t kRrLength = 8; |
| 103 | const size_t kReportBlockLength = 24; |
| 104 | const size_t kFirLength = 20; |
| 105 | |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 106 | const size_t kBufferSize = kRrLength + kReportBlockLength + kFirLength; |
Danil Chapovalov | 5c3cc41 | 2017-12-07 09:15:53 | [diff] [blame] | 107 | MockFunction<void(rtc::ArrayView<const uint8_t>)> callback; |
| 108 | EXPECT_CALL(callback, Call(_)) |
| 109 | .WillOnce(Invoke([&](rtc::ArrayView<const uint8_t> packet) { |
| 110 | RtcpPacketParser parser; |
| 111 | parser.Parse(packet.data(), packet.size()); |
| 112 | EXPECT_EQ(1, parser.receiver_report()->num_packets()); |
| 113 | EXPECT_EQ(1u, parser.receiver_report()->report_blocks().size()); |
| 114 | EXPECT_EQ(1, parser.fir()->num_packets()); |
| 115 | })); |
| 116 | |
| 117 | EXPECT_TRUE(compound.Build(kBufferSize, callback.AsStdFunction())); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | TEST(RtcpCompoundPacketTest, BuildWithTooSmallBuffer_FragmentedSend) { |
danilchap | 7a4116a | 2016-03-14 15:19:28 | [diff] [blame] | 121 | CompoundPacket compound; |
Erik Språng | de5507d | 2020-09-11 13:35:32 | [diff] [blame] | 122 | auto fir = std::make_unique<Fir>(); |
| 123 | fir->AddRequestTo(kRemoteSsrc, kSeqNo); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 124 | ReportBlock rb; |
Erik Språng | de5507d | 2020-09-11 13:35:32 | [diff] [blame] | 125 | auto rr = std::make_unique<ReceiverReport>(); |
| 126 | rr->SetSenderSsrc(kSenderSsrc); |
| 127 | EXPECT_TRUE(rr->AddReportBlock(rb)); |
| 128 | compound.Append(std::move(rr)); |
| 129 | compound.Append(std::move(fir)); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 130 | |
| 131 | const size_t kRrLength = 8; |
| 132 | const size_t kReportBlockLength = 24; |
| 133 | |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 134 | const size_t kBufferSize = kRrLength + kReportBlockLength; |
Danil Chapovalov | 5c3cc41 | 2017-12-07 09:15:53 | [diff] [blame] | 135 | MockFunction<void(rtc::ArrayView<const uint8_t>)> callback; |
| 136 | EXPECT_CALL(callback, Call(_)) |
| 137 | .WillOnce(Invoke([&](rtc::ArrayView<const uint8_t> packet) { |
| 138 | RtcpPacketParser parser; |
| 139 | parser.Parse(packet.data(), packet.size()); |
| 140 | EXPECT_EQ(1, parser.receiver_report()->num_packets()); |
| 141 | EXPECT_EQ(1U, parser.receiver_report()->report_blocks().size()); |
| 142 | EXPECT_EQ(0, parser.fir()->num_packets()); |
| 143 | })) |
| 144 | .WillOnce(Invoke([&](rtc::ArrayView<const uint8_t> packet) { |
| 145 | RtcpPacketParser parser; |
| 146 | parser.Parse(packet.data(), packet.size()); |
| 147 | EXPECT_EQ(0, parser.receiver_report()->num_packets()); |
| 148 | EXPECT_EQ(0U, parser.receiver_report()->report_blocks().size()); |
| 149 | EXPECT_EQ(1, parser.fir()->num_packets()); |
| 150 | })); |
| 151 | |
| 152 | EXPECT_TRUE(compound.Build(kBufferSize, callback.AsStdFunction())); |
danilchap | 2f7dea1 | 2016-01-13 10:03:04 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace webrtc |