eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 06:47:31 +0200 | [diff] [blame] | 11 | #include "call/rtcp_demuxer.h" |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 14 | #include <set> |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 15 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 16 | #include "absl/memory/memory.h" |
Patrik Höglund | 3e11343 | 2017-12-15 14:40:10 +0100 | [diff] [blame] | 17 | #include "api/rtp_headers.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "call/rtcp_packet_sink_interface.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 19 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/rtp_rtcp/source/rtcp_packet/bye.h" |
| 21 | #include "rtc_base/arraysize.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "test/gmock.h" |
| 24 | #include "test/gtest.h" |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | using ::testing::_; |
| 31 | using ::testing::AtLeast; |
| 32 | using ::testing::ElementsAreArray; |
| 33 | using ::testing::InSequence; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 34 | using ::testing::Matcher; |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 35 | using ::testing::NiceMock; |
| 36 | |
| 37 | class MockRtcpPacketSink : public RtcpPacketSinkInterface { |
| 38 | public: |
| 39 | MOCK_METHOD1(OnRtcpPacket, void(rtc::ArrayView<const uint8_t>)); |
| 40 | }; |
| 41 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 42 | class RtcpDemuxerTest : public testing::Test { |
| 43 | protected: |
| 44 | ~RtcpDemuxerTest() { |
| 45 | for (auto* sink : sinks_to_tear_down_) { |
| 46 | demuxer_.RemoveSink(sink); |
| 47 | } |
| 48 | for (auto* sink : broadcast_sinks_to_tear_down_) { |
| 49 | demuxer_.RemoveBroadcastSink(sink); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void AddSsrcSink(uint32_t ssrc, RtcpPacketSinkInterface* sink) { |
| 54 | demuxer_.AddSink(ssrc, sink); |
| 55 | sinks_to_tear_down_.insert(sink); |
| 56 | } |
| 57 | |
| 58 | void AddRsidSink(const std::string& rsid, RtcpPacketSinkInterface* sink) { |
| 59 | demuxer_.AddSink(rsid, sink); |
| 60 | sinks_to_tear_down_.insert(sink); |
| 61 | } |
| 62 | |
| 63 | void RemoveSink(RtcpPacketSinkInterface* sink) { |
| 64 | sinks_to_tear_down_.erase(sink); |
| 65 | demuxer_.RemoveSink(sink); |
| 66 | } |
| 67 | |
| 68 | void AddBroadcastSink(RtcpPacketSinkInterface* sink) { |
| 69 | demuxer_.AddBroadcastSink(sink); |
| 70 | broadcast_sinks_to_tear_down_.insert(sink); |
| 71 | } |
| 72 | |
| 73 | void RemoveBroadcastSink(RtcpPacketSinkInterface* sink) { |
| 74 | broadcast_sinks_to_tear_down_.erase(sink); |
| 75 | demuxer_.RemoveBroadcastSink(sink); |
| 76 | } |
| 77 | |
| 78 | RtcpDemuxer demuxer_; |
| 79 | std::set<RtcpPacketSinkInterface*> sinks_to_tear_down_; |
| 80 | std::set<RtcpPacketSinkInterface*> broadcast_sinks_to_tear_down_; |
| 81 | }; |
| 82 | |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 83 | // Produces a packet buffer representing an RTCP packet with a given SSRC, |
| 84 | // as it would look when sent over the wire. |
| 85 | // |distinguishing_string| allows different RTCP packets with the same SSRC |
| 86 | // to be distinguished. How this is set into the actual packet is |
| 87 | // unimportant, and depends on which RTCP message we choose to use. |
| 88 | rtc::Buffer CreateRtcpPacket(uint32_t ssrc, |
| 89 | const std::string& distinguishing_string = "") { |
| 90 | rtcp::Bye packet; |
| 91 | packet.SetSenderSsrc(ssrc); |
| 92 | if (distinguishing_string != "") { |
| 93 | // Actual way we use |distinguishing_string| is unimportant, so long |
| 94 | // as it ends up in the packet. |
| 95 | packet.SetReason(distinguishing_string); |
| 96 | } |
| 97 | return packet.Build(); |
| 98 | } |
| 99 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 100 | static Matcher<rtc::ArrayView<const uint8_t>> SamePacketAs( |
| 101 | const rtc::Buffer& other) { |
| 102 | return ElementsAreArray(other.cbegin(), other.cend()); |
| 103 | } |
| 104 | |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 105 | } // namespace |
| 106 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 107 | TEST_F(RtcpDemuxerTest, OnRtcpPacketCalledOnCorrectSinkBySsrc) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 108 | constexpr uint32_t ssrcs[] = {101, 202, 303}; |
| 109 | MockRtcpPacketSink sinks[arraysize(ssrcs)]; |
| 110 | for (size_t i = 0; i < arraysize(ssrcs); i++) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 111 | AddSsrcSink(ssrcs[i], &sinks[i]); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | for (size_t i = 0; i < arraysize(ssrcs); i++) { |
| 115 | auto packet = CreateRtcpPacket(ssrcs[i]); |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 116 | EXPECT_CALL(sinks[i], OnRtcpPacket(SamePacketAs(packet))).Times(1); |
| 117 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 121 | TEST_F(RtcpDemuxerTest, OnRtcpPacketCalledOnResolvedRsidSink) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 122 | // Set up some RSID sinks. |
| 123 | const std::string rsids[] = {"a", "b", "c"}; |
| 124 | MockRtcpPacketSink sinks[arraysize(rsids)]; |
| 125 | for (size_t i = 0; i < arraysize(rsids); i++) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 126 | AddRsidSink(rsids[i], &sinks[i]); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // Only resolve one of the sinks. |
| 130 | constexpr size_t resolved_sink_index = 0; |
| 131 | constexpr uint32_t ssrc = 345; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 132 | demuxer_.OnSsrcBoundToRsid(rsids[resolved_sink_index], ssrc); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 133 | |
| 134 | // The resolved sink gets notifications of RTCP messages with its SSRC. |
| 135 | auto packet = CreateRtcpPacket(ssrc); |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 136 | EXPECT_CALL(sinks[resolved_sink_index], OnRtcpPacket(SamePacketAs(packet))) |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 137 | .Times(1); |
| 138 | |
| 139 | // RTCP received; expected calls triggered. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 140 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 143 | TEST_F(RtcpDemuxerTest, |
| 144 | SingleCallbackAfterResolutionOfAnRsidToAlreadyRegisteredSsrc) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 145 | // Associate a sink with an SSRC. |
| 146 | MockRtcpPacketSink sink; |
| 147 | constexpr uint32_t ssrc = 999; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 148 | AddSsrcSink(ssrc, &sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 149 | |
| 150 | // Associate the same sink with an RSID. |
| 151 | const std::string rsid = "r"; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 152 | AddRsidSink(rsid, &sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 153 | |
| 154 | // Resolve the RSID to the aforementioned SSRC. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 155 | demuxer_.OnSsrcBoundToRsid(rsid, ssrc); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 156 | |
| 157 | // OnRtcpPacket still called only a single time for messages with this SSRC. |
| 158 | auto packet = CreateRtcpPacket(ssrc); |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 159 | EXPECT_CALL(sink, OnRtcpPacket(SamePacketAs(packet))).Times(1); |
| 160 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 163 | TEST_F(RtcpDemuxerTest, |
| 164 | OnRtcpPacketCalledOnAllBroadcastSinksForAllRtcpPackets) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 165 | MockRtcpPacketSink sinks[3]; |
| 166 | for (MockRtcpPacketSink& sink : sinks) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 167 | AddBroadcastSink(&sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | constexpr uint32_t ssrc = 747; |
| 171 | auto packet = CreateRtcpPacket(ssrc); |
| 172 | |
| 173 | for (MockRtcpPacketSink& sink : sinks) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 174 | EXPECT_CALL(sink, OnRtcpPacket(SamePacketAs(packet))).Times(1); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // RTCP received; expected calls triggered. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 178 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 181 | TEST_F(RtcpDemuxerTest, PacketsDeliveredInRightOrderToNonBroadcastSink) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 182 | constexpr uint32_t ssrc = 101; |
| 183 | MockRtcpPacketSink sink; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 184 | AddSsrcSink(ssrc, &sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 185 | |
| 186 | std::vector<rtc::Buffer> packets; |
| 187 | for (size_t i = 0; i < 5; i++) { |
| 188 | packets.push_back(CreateRtcpPacket(ssrc, std::to_string(i))); |
| 189 | } |
| 190 | |
| 191 | InSequence sequence; |
| 192 | for (const auto& packet : packets) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 193 | EXPECT_CALL(sink, OnRtcpPacket(SamePacketAs(packet))).Times(1); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | for (const auto& packet : packets) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 197 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 198 | } |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 201 | TEST_F(RtcpDemuxerTest, PacketsDeliveredInRightOrderToBroadcastSink) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 202 | MockRtcpPacketSink sink; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 203 | AddBroadcastSink(&sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 204 | |
| 205 | std::vector<rtc::Buffer> packets; |
| 206 | for (size_t i = 0; i < 5; i++) { |
| 207 | constexpr uint32_t ssrc = 101; |
| 208 | packets.push_back(CreateRtcpPacket(ssrc, std::to_string(i))); |
| 209 | } |
| 210 | |
| 211 | InSequence sequence; |
| 212 | for (const auto& packet : packets) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 213 | EXPECT_CALL(sink, OnRtcpPacket(SamePacketAs(packet))).Times(1); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | for (const auto& packet : packets) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 217 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 218 | } |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 221 | TEST_F(RtcpDemuxerTest, MultipleSinksMappedToSameSsrc) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 222 | MockRtcpPacketSink sinks[3]; |
| 223 | constexpr uint32_t ssrc = 404; |
| 224 | for (auto& sink : sinks) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 225 | AddSsrcSink(ssrc, &sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // Reception of an RTCP packet associated with the shared SSRC triggers the |
| 229 | // callback on all of the sinks associated with it. |
| 230 | auto packet = CreateRtcpPacket(ssrc); |
| 231 | for (auto& sink : sinks) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 232 | EXPECT_CALL(sink, OnRtcpPacket(SamePacketAs(packet))); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 233 | } |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 234 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 235 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 238 | TEST_F(RtcpDemuxerTest, SinkMappedToMultipleSsrcs) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 239 | constexpr uint32_t ssrcs[] = {404, 505, 606}; |
| 240 | MockRtcpPacketSink sink; |
| 241 | for (uint32_t ssrc : ssrcs) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 242 | AddSsrcSink(ssrc, &sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // The sink which is associated with multiple SSRCs gets the callback |
| 246 | // triggered for each of those SSRCs. |
| 247 | for (uint32_t ssrc : ssrcs) { |
| 248 | auto packet = CreateRtcpPacket(ssrc); |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 249 | EXPECT_CALL(sink, OnRtcpPacket(SamePacketAs(packet))); |
| 250 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 251 | } |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 254 | TEST_F(RtcpDemuxerTest, MultipleRsidsOnSameSink) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 255 | // Sink associated with multiple sinks. |
| 256 | MockRtcpPacketSink sink; |
| 257 | const std::string rsids[] = {"a", "b", "c"}; |
| 258 | for (const auto& rsid : rsids) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 259 | AddRsidSink(rsid, &sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // RSIDs resolved to SSRCs. |
| 263 | uint32_t ssrcs[arraysize(rsids)]; |
| 264 | for (size_t i = 0; i < arraysize(rsids); i++) { |
| 265 | ssrcs[i] = 1000 + static_cast<uint32_t>(i); |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 266 | demuxer_.OnSsrcBoundToRsid(rsids[i], ssrcs[i]); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // Set up packets to match those RSIDs/SSRCs. |
| 270 | std::vector<rtc::Buffer> packets; |
| 271 | for (size_t i = 0; i < arraysize(rsids); i++) { |
| 272 | packets.push_back(CreateRtcpPacket(ssrcs[i])); |
| 273 | } |
| 274 | |
| 275 | // The sink expects to receive all of the packets. |
| 276 | for (const auto& packet : packets) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 277 | EXPECT_CALL(sink, OnRtcpPacket(SamePacketAs(packet))).Times(1); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // Packet demuxed correctly; OnRtcpPacket() triggered on sink. |
| 281 | for (const auto& packet : packets) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 282 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 283 | } |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 286 | TEST_F(RtcpDemuxerTest, RsidUsedByMultipleSinks) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 287 | MockRtcpPacketSink sinks[3]; |
| 288 | const std::string shared_rsid = "a"; |
| 289 | |
| 290 | for (MockRtcpPacketSink& sink : sinks) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 291 | AddRsidSink(shared_rsid, &sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | constexpr uint32_t shared_ssrc = 888; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 295 | demuxer_.OnSsrcBoundToRsid(shared_rsid, shared_ssrc); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 296 | |
| 297 | auto packet = CreateRtcpPacket(shared_ssrc); |
| 298 | |
| 299 | for (MockRtcpPacketSink& sink : sinks) { |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 300 | EXPECT_CALL(sink, OnRtcpPacket(SamePacketAs(packet))).Times(1); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 303 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 306 | TEST_F(RtcpDemuxerTest, NoCallbackOnSsrcSinkRemovedBeforeFirstPacket) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 307 | constexpr uint32_t ssrc = 404; |
| 308 | MockRtcpPacketSink sink; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 309 | AddSsrcSink(ssrc, &sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 310 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 311 | RemoveSink(&sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 312 | |
| 313 | // The removed sink does not get callbacks. |
| 314 | auto packet = CreateRtcpPacket(ssrc); |
| 315 | EXPECT_CALL(sink, OnRtcpPacket(_)).Times(0); // Not called. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 316 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 319 | TEST_F(RtcpDemuxerTest, NoCallbackOnSsrcSinkRemovedAfterFirstPacket) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 320 | constexpr uint32_t ssrc = 404; |
| 321 | NiceMock<MockRtcpPacketSink> sink; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 322 | AddSsrcSink(ssrc, &sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 323 | |
| 324 | auto before_packet = CreateRtcpPacket(ssrc); |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 325 | demuxer_.OnRtcpPacket(before_packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 326 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 327 | RemoveSink(&sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 328 | |
| 329 | // The removed sink does not get callbacks. |
| 330 | auto after_packet = CreateRtcpPacket(ssrc); |
| 331 | EXPECT_CALL(sink, OnRtcpPacket(_)).Times(0); // Not called. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 332 | demuxer_.OnRtcpPacket(after_packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 335 | TEST_F(RtcpDemuxerTest, NoCallbackOnRsidSinkRemovedBeforeRsidResolution) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 336 | const std::string rsid = "a"; |
| 337 | constexpr uint32_t ssrc = 404; |
| 338 | MockRtcpPacketSink sink; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 339 | AddRsidSink(rsid, &sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 340 | |
| 341 | // Removal before resolution. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 342 | RemoveSink(&sink); |
| 343 | demuxer_.OnSsrcBoundToRsid(rsid, ssrc); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 344 | |
| 345 | // The removed sink does not get callbacks. |
| 346 | auto packet = CreateRtcpPacket(ssrc); |
| 347 | EXPECT_CALL(sink, OnRtcpPacket(_)).Times(0); // Not called. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 348 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 351 | TEST_F(RtcpDemuxerTest, NoCallbackOnRsidSinkRemovedAfterRsidResolution) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 352 | const std::string rsid = "a"; |
| 353 | constexpr uint32_t ssrc = 404; |
| 354 | MockRtcpPacketSink sink; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 355 | AddRsidSink(rsid, &sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 356 | |
| 357 | // Removal after resolution. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 358 | demuxer_.OnSsrcBoundToRsid(rsid, ssrc); |
| 359 | RemoveSink(&sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 360 | |
| 361 | // The removed sink does not get callbacks. |
| 362 | auto packet = CreateRtcpPacket(ssrc); |
| 363 | EXPECT_CALL(sink, OnRtcpPacket(_)).Times(0); // Not called. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 364 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 367 | TEST_F(RtcpDemuxerTest, NoCallbackOnBroadcastSinkRemovedBeforeFirstPacket) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 368 | MockRtcpPacketSink sink; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 369 | AddBroadcastSink(&sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 370 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 371 | RemoveBroadcastSink(&sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 372 | |
| 373 | // The removed sink does not get callbacks. |
| 374 | constexpr uint32_t ssrc = 404; |
| 375 | auto packet = CreateRtcpPacket(ssrc); |
| 376 | EXPECT_CALL(sink, OnRtcpPacket(_)).Times(0); // Not called. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 377 | demuxer_.OnRtcpPacket(packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 380 | TEST_F(RtcpDemuxerTest, NoCallbackOnBroadcastSinkRemovedAfterFirstPacket) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 381 | NiceMock<MockRtcpPacketSink> sink; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 382 | AddBroadcastSink(&sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 383 | |
| 384 | constexpr uint32_t ssrc = 404; |
| 385 | auto before_packet = CreateRtcpPacket(ssrc); |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 386 | demuxer_.OnRtcpPacket(before_packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 387 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 388 | RemoveBroadcastSink(&sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 389 | |
| 390 | // The removed sink does not get callbacks. |
| 391 | auto after_packet = CreateRtcpPacket(ssrc); |
| 392 | EXPECT_CALL(sink, OnRtcpPacket(_)).Times(0); // Not called. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 393 | demuxer_.OnRtcpPacket(after_packet); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // The RSID to SSRC mapping should be one-to-one. If we end up receiving |
| 397 | // two (or more) packets with the same SSRC, but different RSIDs, we guarantee |
| 398 | // remembering the first one; no guarantees are made about further associations. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 399 | TEST_F(RtcpDemuxerTest, FirstResolutionOfRsidNotForgotten) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 400 | MockRtcpPacketSink sink; |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 401 | const std::string rsid = "a"; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 402 | AddRsidSink(rsid, &sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 403 | |
| 404 | constexpr uint32_t ssrc_a = 111; // First resolution - guaranteed effective. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 405 | demuxer_.OnSsrcBoundToRsid(rsid, ssrc_a); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 406 | |
| 407 | constexpr uint32_t ssrc_b = 222; // Second resolution - no guarantees. |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 408 | demuxer_.OnSsrcBoundToRsid(rsid, ssrc_b); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 409 | |
| 410 | auto packet_a = CreateRtcpPacket(ssrc_a); |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 411 | EXPECT_CALL(sink, OnRtcpPacket(SamePacketAs(packet_a))).Times(1); |
| 412 | demuxer_.OnRtcpPacket(packet_a); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 413 | |
| 414 | auto packet_b = CreateRtcpPacket(ssrc_b); |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 415 | EXPECT_CALL(sink, OnRtcpPacket(SamePacketAs(packet_b))).Times(AtLeast(0)); |
| 416 | demuxer_.OnRtcpPacket(packet_b); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 420 | |
| 421 | TEST_F(RtcpDemuxerTest, RepeatedSsrcToSinkAssociationsDisallowed) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 422 | MockRtcpPacketSink sink; |
| 423 | |
| 424 | constexpr uint32_t ssrc = 101; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 425 | AddSsrcSink(ssrc, &sink); |
| 426 | EXPECT_DEATH(AddSsrcSink(ssrc, &sink), ""); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 429 | TEST_F(RtcpDemuxerTest, RepeatedRsidToSinkAssociationsDisallowed) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 430 | MockRtcpPacketSink sink; |
| 431 | |
| 432 | const std::string rsid = "z"; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 433 | AddRsidSink(rsid, &sink); |
| 434 | EXPECT_DEATH(AddRsidSink(rsid, &sink), ""); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 437 | TEST_F(RtcpDemuxerTest, RepeatedBroadcastSinkRegistrationDisallowed) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 438 | MockRtcpPacketSink sink; |
| 439 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 440 | AddBroadcastSink(&sink); |
| 441 | EXPECT_DEATH(AddBroadcastSink(&sink), ""); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 444 | TEST_F(RtcpDemuxerTest, SsrcSinkCannotAlsoBeRegisteredAsBroadcast) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 445 | MockRtcpPacketSink sink; |
| 446 | |
| 447 | constexpr uint32_t ssrc = 101; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 448 | AddSsrcSink(ssrc, &sink); |
| 449 | EXPECT_DEATH(AddBroadcastSink(&sink), ""); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 452 | TEST_F(RtcpDemuxerTest, RsidSinkCannotAlsoBeRegisteredAsBroadcast) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 453 | MockRtcpPacketSink sink; |
| 454 | |
| 455 | const std::string rsid = "z"; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 456 | AddRsidSink(rsid, &sink); |
| 457 | EXPECT_DEATH(AddBroadcastSink(&sink), ""); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 460 | TEST_F(RtcpDemuxerTest, BroadcastSinkCannotAlsoBeRegisteredAsSsrcSink) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 461 | MockRtcpPacketSink sink; |
| 462 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 463 | AddBroadcastSink(&sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 464 | constexpr uint32_t ssrc = 101; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 465 | EXPECT_DEATH(AddSsrcSink(ssrc, &sink), ""); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 468 | TEST_F(RtcpDemuxerTest, BroadcastSinkCannotAlsoBeRegisteredAsRsidSink) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 469 | MockRtcpPacketSink sink; |
| 470 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 471 | AddBroadcastSink(&sink); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 472 | const std::string rsid = "j"; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 473 | EXPECT_DEATH(AddRsidSink(rsid, &sink), ""); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 476 | TEST_F(RtcpDemuxerTest, MayNotCallRemoveSinkOnNeverAddedSink) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 477 | MockRtcpPacketSink sink; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 478 | EXPECT_DEATH(RemoveSink(&sink), ""); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 481 | TEST_F(RtcpDemuxerTest, MayNotCallRemoveBroadcastSinkOnNeverAddedSink) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 482 | MockRtcpPacketSink sink; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 483 | EXPECT_DEATH(RemoveBroadcastSink(&sink), ""); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 486 | TEST_F(RtcpDemuxerTest, RsidMustBeNonEmpty) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 487 | MockRtcpPacketSink sink; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 488 | EXPECT_DEATH(AddRsidSink("", &sink), ""); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 491 | TEST_F(RtcpDemuxerTest, RsidMustBeAlphaNumeric) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 492 | MockRtcpPacketSink sink; |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 493 | EXPECT_DEATH(AddRsidSink("a_3", &sink), ""); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 496 | TEST_F(RtcpDemuxerTest, RsidMustNotExceedMaximumLength) { |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 497 | MockRtcpPacketSink sink; |
| 498 | std::string rsid(StreamId::kMaxSize + 1, 'a'); |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 499 | EXPECT_DEATH(AddRsidSink(rsid, &sink), ""); |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 500 | } |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 501 | |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 502 | #endif |
Steve Anton | 8018306 | 2017-08-18 11:10:12 -0700 | [diff] [blame] | 503 | |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 504 | } // namespace webrtc |