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
11 #include "call/rtp_demuxer.h"
12
13 #include <memory>
14 #include <set>
15 #include <string>
16
17 #include "absl/strings/string_view.h"
18 #include "call/test/mock_rtp_packet_sink_interface.h"
19 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
20 #include "modules/rtp_rtcp/source/rtp_header_extensions.h"
21 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
22 #include "rtc_base/arraysize.h"
23 #include "rtc_base/checks.h"
24 #include "rtc_base/numerics/safe_conversions.h"
25 #include "test/gmock.h"
26 #include "test/gtest.h"
27
28 namespace webrtc {
29
30 namespace {
31
32 using ::testing::_;
33 using ::testing::AtLeast;
34 using ::testing::InSequence;
35 using ::testing::NiceMock;
36
37 class RtpDemuxerTest : public ::testing::Test {
38 protected:
~RtpDemuxerTest()39 ~RtpDemuxerTest() {
40 for (auto* sink : sinks_to_tear_down_) {
41 demuxer_.RemoveSink(sink);
42 }
43 }
44
45 // These are convenience methods for calling demuxer.AddSink with different
46 // parameters and will ensure that the sink is automatically removed when the
47 // test case finishes.
48
AddSink(const RtpDemuxerCriteria & criteria,RtpPacketSinkInterface * sink)49 bool AddSink(const RtpDemuxerCriteria& criteria,
50 RtpPacketSinkInterface* sink) {
51 bool added = demuxer_.AddSink(criteria, sink);
52 if (added) {
53 sinks_to_tear_down_.insert(sink);
54 }
55 return added;
56 }
57
AddSinkOnlySsrc(uint32_t ssrc,RtpPacketSinkInterface * sink)58 bool AddSinkOnlySsrc(uint32_t ssrc, RtpPacketSinkInterface* sink) {
59 RtpDemuxerCriteria criteria;
60 criteria.ssrcs().insert(ssrc);
61 return AddSink(criteria, sink);
62 }
63
AddSinkOnlyRsid(absl::string_view rsid,RtpPacketSinkInterface * sink)64 bool AddSinkOnlyRsid(absl::string_view rsid, RtpPacketSinkInterface* sink) {
65 RtpDemuxerCriteria criteria(absl::string_view(), rsid);
66 return AddSink(criteria, sink);
67 }
68
AddSinkOnlyMid(absl::string_view mid,RtpPacketSinkInterface * sink)69 bool AddSinkOnlyMid(absl::string_view mid, RtpPacketSinkInterface* sink) {
70 RtpDemuxerCriteria criteria(mid);
71 return AddSink(criteria, sink);
72 }
73
AddSinkBothMidRsid(absl::string_view mid,absl::string_view rsid,RtpPacketSinkInterface * sink)74 bool AddSinkBothMidRsid(absl::string_view mid,
75 absl::string_view rsid,
76 RtpPacketSinkInterface* sink) {
77 RtpDemuxerCriteria criteria(mid, rsid);
78 return AddSink(criteria, sink);
79 }
80
RemoveSink(RtpPacketSinkInterface * sink)81 bool RemoveSink(RtpPacketSinkInterface* sink) {
82 sinks_to_tear_down_.erase(sink);
83 return demuxer_.RemoveSink(sink);
84 }
85
86 // The CreatePacket* methods are helpers for creating new RTP packets with
87 // various attributes set. Tests should use the helper that provides the
88 // minimum information needed to exercise the behavior under test. Tests also
89 // should not rely on any behavior which is not clearly described in the
90 // helper name/arguments. Any additional settings that are not covered by the
91 // helper should be set manually on the packet once it has been returned.
92 // For example, most tests in this file do not care about the RTP sequence
93 // number, but to ensure that the returned packets are valid the helpers will
94 // auto-increment the sequence number starting with 1. Tests that rely on
95 // specific sequence number behavior should call SetSequenceNumber manually on
96 // the returned packet.
97
98 // Intended for use only by other CreatePacket* helpers.
CreatePacket(uint32_t ssrc,RtpPacketReceived::ExtensionManager * extension_manager)99 std::unique_ptr<RtpPacketReceived> CreatePacket(
100 uint32_t ssrc,
101 RtpPacketReceived::ExtensionManager* extension_manager) {
102 auto packet = std::make_unique<RtpPacketReceived>(extension_manager);
103 packet->SetSsrc(ssrc);
104 packet->SetSequenceNumber(next_sequence_number_++);
105 return packet;
106 }
107
CreatePacketWithSsrc(uint32_t ssrc)108 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrc(uint32_t ssrc) {
109 return CreatePacket(ssrc, nullptr);
110 }
111
CreatePacketWithSsrcMid(uint32_t ssrc,absl::string_view mid)112 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcMid(
113 uint32_t ssrc,
114 absl::string_view mid) {
115 RtpPacketReceived::ExtensionManager extension_manager;
116 extension_manager.Register<RtpMid>(11);
117
118 auto packet = CreatePacket(ssrc, &extension_manager);
119 packet->SetExtension<RtpMid>(mid);
120 return packet;
121 }
122
CreatePacketWithSsrcRsid(uint32_t ssrc,absl::string_view rsid)123 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcRsid(
124 uint32_t ssrc,
125 absl::string_view rsid) {
126 RtpPacketReceived::ExtensionManager extension_manager;
127 extension_manager.Register<RtpStreamId>(6);
128
129 auto packet = CreatePacket(ssrc, &extension_manager);
130 packet->SetExtension<RtpStreamId>(rsid);
131 return packet;
132 }
133
CreatePacketWithSsrcRrid(uint32_t ssrc,absl::string_view rrid)134 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcRrid(
135 uint32_t ssrc,
136 absl::string_view rrid) {
137 RtpPacketReceived::ExtensionManager extension_manager;
138 extension_manager.Register<RepairedRtpStreamId>(7);
139
140 auto packet = CreatePacket(ssrc, &extension_manager);
141 packet->SetExtension<RepairedRtpStreamId>(rrid);
142 return packet;
143 }
144
CreatePacketWithSsrcMidRsid(uint32_t ssrc,absl::string_view mid,absl::string_view rsid)145 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcMidRsid(
146 uint32_t ssrc,
147 absl::string_view mid,
148 absl::string_view rsid) {
149 RtpPacketReceived::ExtensionManager extension_manager;
150 extension_manager.Register<RtpMid>(11);
151 extension_manager.Register<RtpStreamId>(6);
152
153 auto packet = CreatePacket(ssrc, &extension_manager);
154 packet->SetExtension<RtpMid>(mid);
155 packet->SetExtension<RtpStreamId>(rsid);
156 return packet;
157 }
158
CreatePacketWithSsrcRsidRrid(uint32_t ssrc,absl::string_view rsid,absl::string_view rrid)159 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcRsidRrid(
160 uint32_t ssrc,
161 absl::string_view rsid,
162 absl::string_view rrid) {
163 RtpPacketReceived::ExtensionManager extension_manager;
164 extension_manager.Register<RtpStreamId>(6);
165 extension_manager.Register<RepairedRtpStreamId>(7);
166
167 auto packet = CreatePacket(ssrc, &extension_manager);
168 packet->SetExtension<RtpStreamId>(rsid);
169 packet->SetExtension<RepairedRtpStreamId>(rrid);
170 return packet;
171 }
172
173 RtpDemuxer demuxer_;
174 std::set<RtpPacketSinkInterface*> sinks_to_tear_down_;
175 uint16_t next_sequence_number_ = 1;
176 };
177
178 class RtpDemuxerDeathTest : public RtpDemuxerTest {};
179
180 MATCHER_P(SamePacketAs, other, "") {
181 return arg.Ssrc() == other.Ssrc() &&
182 arg.SequenceNumber() == other.SequenceNumber();
183 }
184
TEST_F(RtpDemuxerTest,CanAddSinkBySsrc)185 TEST_F(RtpDemuxerTest, CanAddSinkBySsrc) {
186 MockRtpPacketSink sink;
187 constexpr uint32_t ssrc = 1;
188
189 EXPECT_TRUE(AddSinkOnlySsrc(ssrc, &sink));
190 }
191
TEST_F(RtpDemuxerTest,AllowAddSinkWithOverlappingPayloadTypesIfDifferentMid)192 TEST_F(RtpDemuxerTest, AllowAddSinkWithOverlappingPayloadTypesIfDifferentMid) {
193 const std::string mid1 = "v";
194 const std::string mid2 = "a";
195 constexpr uint8_t pt1 = 30;
196 constexpr uint8_t pt2 = 31;
197 constexpr uint8_t pt3 = 32;
198
199 RtpDemuxerCriteria pt1_pt2(mid1);
200 pt1_pt2.payload_types() = {pt1, pt2};
201 MockRtpPacketSink sink1;
202 AddSink(pt1_pt2, &sink1);
203
204 RtpDemuxerCriteria pt1_pt3(mid2);
205 pt1_pt3.payload_types() = {pt1, pt3};
206 MockRtpPacketSink sink2;
207 EXPECT_TRUE(AddSink(pt1_pt3, &sink2));
208 }
209
TEST_F(RtpDemuxerTest,RejectAddSinkForSameMidOnly)210 TEST_F(RtpDemuxerTest, RejectAddSinkForSameMidOnly) {
211 const std::string mid = "mid";
212
213 MockRtpPacketSink sink;
214 AddSinkOnlyMid(mid, &sink);
215 EXPECT_FALSE(AddSinkOnlyMid(mid, &sink));
216 }
217
TEST_F(RtpDemuxerTest,RejectAddSinkForSameMidRsid)218 TEST_F(RtpDemuxerTest, RejectAddSinkForSameMidRsid) {
219 const std::string mid = "v";
220 const std::string rsid = "1";
221
222 MockRtpPacketSink sink1;
223 AddSinkBothMidRsid(mid, rsid, &sink1);
224
225 MockRtpPacketSink sink2;
226 EXPECT_FALSE(AddSinkBothMidRsid(mid, rsid, &sink2));
227 }
228
TEST_F(RtpDemuxerTest,RejectAddSinkForConflictingMidAndMidRsid)229 TEST_F(RtpDemuxerTest, RejectAddSinkForConflictingMidAndMidRsid) {
230 const std::string mid = "v";
231 const std::string rsid = "1";
232
233 MockRtpPacketSink mid_sink;
234 AddSinkOnlyMid(mid, &mid_sink);
235
236 // This sink would never get any packets routed to it because the above sink
237 // would receive them all.
238 MockRtpPacketSink mid_rsid_sink;
239 EXPECT_FALSE(AddSinkBothMidRsid(mid, rsid, &mid_rsid_sink));
240 }
241
TEST_F(RtpDemuxerTest,RejectAddSinkForConflictingMidRsidAndMid)242 TEST_F(RtpDemuxerTest, RejectAddSinkForConflictingMidRsidAndMid) {
243 const std::string mid = "v";
244 const std::string rsid = "";
245
246 MockRtpPacketSink mid_rsid_sink;
247 AddSinkBothMidRsid(mid, rsid, &mid_rsid_sink);
248
249 // This sink would shadow the above sink.
250 MockRtpPacketSink mid_sink;
251 EXPECT_FALSE(AddSinkOnlyMid(mid, &mid_sink));
252 }
253
TEST_F(RtpDemuxerTest,AddSinkFailsIfCalledForTwoSinksWithSameSsrc)254 TEST_F(RtpDemuxerTest, AddSinkFailsIfCalledForTwoSinksWithSameSsrc) {
255 MockRtpPacketSink sink_a;
256 MockRtpPacketSink sink_b;
257 constexpr uint32_t ssrc = 1;
258 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sink_a));
259
260 EXPECT_FALSE(AddSinkOnlySsrc(ssrc, &sink_b));
261 }
262
TEST_F(RtpDemuxerTest,AddSinkFailsIfCalledTwiceEvenIfSameSinkWithSameSsrc)263 TEST_F(RtpDemuxerTest, AddSinkFailsIfCalledTwiceEvenIfSameSinkWithSameSsrc) {
264 MockRtpPacketSink sink;
265 constexpr uint32_t ssrc = 1;
266 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sink));
267
268 EXPECT_FALSE(AddSinkOnlySsrc(ssrc, &sink));
269 }
270
271 // TODO(steveanton): Currently fails because payload type validation is not
272 // complete in AddSink (see note in rtp_demuxer.cc).
TEST_F(RtpDemuxerTest,DISABLED_RejectAddSinkForSamePayloadTypes)273 TEST_F(RtpDemuxerTest, DISABLED_RejectAddSinkForSamePayloadTypes) {
274 constexpr uint8_t pt1 = 30;
275 constexpr uint8_t pt2 = 31;
276
277 RtpDemuxerCriteria pt1_pt2;
278 pt1_pt2.payload_types() = {pt1, pt2};
279 MockRtpPacketSink sink1;
280 AddSink(pt1_pt2, &sink1);
281
282 RtpDemuxerCriteria pt2_pt1;
283 pt2_pt1.payload_types() = {pt2, pt1};
284 MockRtpPacketSink sink2;
285 EXPECT_FALSE(AddSink(pt2_pt1, &sink2));
286 }
287
288 // Routing Tests
289
TEST_F(RtpDemuxerTest,OnRtpPacketCalledOnCorrectSinkBySsrc)290 TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkBySsrc) {
291 constexpr uint32_t ssrcs[] = {101, 202, 303};
292 MockRtpPacketSink sinks[arraysize(ssrcs)];
293 for (size_t i = 0; i < arraysize(ssrcs); i++) {
294 AddSinkOnlySsrc(ssrcs[i], &sinks[i]);
295 }
296
297 for (size_t i = 0; i < arraysize(ssrcs); i++) {
298 auto packet = CreatePacketWithSsrc(ssrcs[i]);
299 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
300 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
301 }
302 }
303
TEST_F(RtpDemuxerTest,OnRtpPacketCalledOnCorrectSinkByRsid)304 TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByRsid) {
305 const std::string rsids[] = {"a", "b", "c"};
306 MockRtpPacketSink sinks[arraysize(rsids)];
307 for (size_t i = 0; i < arraysize(rsids); i++) {
308 AddSinkOnlyRsid(rsids[i], &sinks[i]);
309 }
310
311 for (size_t i = 0; i < arraysize(rsids); i++) {
312 auto packet =
313 CreatePacketWithSsrcRsid(rtc::checked_cast<uint32_t>(i), rsids[i]);
314 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
315 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
316 }
317 }
318
TEST_F(RtpDemuxerTest,OnRtpPacketCalledOnCorrectSinkByMid)319 TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByMid) {
320 const std::string mids[] = {"a", "v", "s"};
321 MockRtpPacketSink sinks[arraysize(mids)];
322 for (size_t i = 0; i < arraysize(mids); i++) {
323 AddSinkOnlyMid(mids[i], &sinks[i]);
324 }
325
326 for (size_t i = 0; i < arraysize(mids); i++) {
327 auto packet =
328 CreatePacketWithSsrcMid(rtc::checked_cast<uint32_t>(i), mids[i]);
329 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
330 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
331 }
332 }
333
TEST_F(RtpDemuxerTest,OnRtpPacketCalledOnCorrectSinkByMidAndRsid)334 TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByMidAndRsid) {
335 const std::string mid = "v";
336 const std::string rsid = "1";
337 constexpr uint32_t ssrc = 10;
338
339 MockRtpPacketSink sink;
340 AddSinkBothMidRsid(mid, rsid, &sink);
341
342 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
343 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
344 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
345 }
346
TEST_F(RtpDemuxerTest,OnRtpPacketCalledOnCorrectSinkByRepairedRsid)347 TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByRepairedRsid) {
348 const std::string rrid = "1";
349 constexpr uint32_t ssrc = 10;
350
351 MockRtpPacketSink sink;
352 AddSinkOnlyRsid(rrid, &sink);
353
354 auto packet_with_rrid = CreatePacketWithSsrcRrid(ssrc, rrid);
355 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_rrid))).Times(1);
356 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_rrid));
357 }
358
TEST_F(RtpDemuxerTest,OnRtpPacketCalledOnCorrectSinkByPayloadType)359 TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByPayloadType) {
360 constexpr uint32_t ssrc = 10;
361 constexpr uint8_t payload_type = 30;
362
363 MockRtpPacketSink sink;
364 RtpDemuxerCriteria criteria;
365 criteria.payload_types() = {payload_type};
366 AddSink(criteria, &sink);
367
368 auto packet = CreatePacketWithSsrc(ssrc);
369 packet->SetPayloadType(payload_type);
370 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
371 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
372 }
373
TEST_F(RtpDemuxerTest,PacketsDeliveredInRightOrder)374 TEST_F(RtpDemuxerTest, PacketsDeliveredInRightOrder) {
375 constexpr uint32_t ssrc = 101;
376 MockRtpPacketSink sink;
377 AddSinkOnlySsrc(ssrc, &sink);
378
379 std::unique_ptr<RtpPacketReceived> packets[5];
380 for (size_t i = 0; i < arraysize(packets); i++) {
381 packets[i] = CreatePacketWithSsrc(ssrc);
382 packets[i]->SetSequenceNumber(rtc::checked_cast<uint16_t>(i));
383 }
384
385 InSequence sequence;
386 for (const auto& packet : packets) {
387 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
388 }
389
390 for (const auto& packet : packets) {
391 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
392 }
393 }
394
TEST_F(RtpDemuxerTest,SinkMappedToMultipleSsrcs)395 TEST_F(RtpDemuxerTest, SinkMappedToMultipleSsrcs) {
396 constexpr uint32_t ssrcs[] = {404, 505, 606};
397 MockRtpPacketSink sink;
398 for (uint32_t ssrc : ssrcs) {
399 AddSinkOnlySsrc(ssrc, &sink);
400 }
401
402 // The sink which is associated with multiple SSRCs gets the callback
403 // triggered for each of those SSRCs.
404 for (uint32_t ssrc : ssrcs) {
405 auto packet = CreatePacketWithSsrc(ssrc);
406 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
407 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
408 }
409 }
410
TEST_F(RtpDemuxerTest,NoCallbackOnSsrcSinkRemovedBeforeFirstPacket)411 TEST_F(RtpDemuxerTest, NoCallbackOnSsrcSinkRemovedBeforeFirstPacket) {
412 constexpr uint32_t ssrc = 404;
413 MockRtpPacketSink sink;
414 AddSinkOnlySsrc(ssrc, &sink);
415
416 ASSERT_TRUE(RemoveSink(&sink));
417
418 // The removed sink does not get callbacks.
419 auto packet = CreatePacketWithSsrc(ssrc);
420 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
421 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
422 }
423
TEST_F(RtpDemuxerTest,NoCallbackOnSsrcSinkRemovedAfterFirstPacket)424 TEST_F(RtpDemuxerTest, NoCallbackOnSsrcSinkRemovedAfterFirstPacket) {
425 constexpr uint32_t ssrc = 404;
426 NiceMock<MockRtpPacketSink> sink;
427 AddSinkOnlySsrc(ssrc, &sink);
428
429 InSequence sequence;
430 for (size_t i = 0; i < 10; i++) {
431 ASSERT_TRUE(demuxer_.OnRtpPacket(*CreatePacketWithSsrc(ssrc)));
432 }
433
434 ASSERT_TRUE(RemoveSink(&sink));
435
436 // The removed sink does not get callbacks.
437 auto packet = CreatePacketWithSsrc(ssrc);
438 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
439 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
440 }
441
442 // An SSRC may only be mapped to a single sink. However, since configuration
443 // of this associations might come from the network, we need to fail gracefully.
TEST_F(RtpDemuxerTest,OnlyOneSinkPerSsrcGetsOnRtpPacketTriggered)444 TEST_F(RtpDemuxerTest, OnlyOneSinkPerSsrcGetsOnRtpPacketTriggered) {
445 MockRtpPacketSink sinks[3];
446 constexpr uint32_t ssrc = 404;
447 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sinks[0]));
448 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sinks[1]));
449 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sinks[2]));
450
451 // The first sink associated with the SSRC remains active; other sinks
452 // were not really added, and so do not get OnRtpPacket() called.
453 auto packet = CreatePacketWithSsrc(ssrc);
454 EXPECT_CALL(sinks[0], OnRtpPacket(SamePacketAs(*packet))).Times(1);
455 EXPECT_CALL(sinks[1], OnRtpPacket(_)).Times(0);
456 EXPECT_CALL(sinks[2], OnRtpPacket(_)).Times(0);
457 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet));
458 }
459
TEST_F(RtpDemuxerTest,NoRepeatedCallbackOnRepeatedAddSinkForSameSink)460 TEST_F(RtpDemuxerTest, NoRepeatedCallbackOnRepeatedAddSinkForSameSink) {
461 constexpr uint32_t ssrc = 111;
462 MockRtpPacketSink sink;
463
464 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sink));
465 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sink));
466
467 auto packet = CreatePacketWithSsrc(ssrc);
468 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
469 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
470 }
471
TEST_F(RtpDemuxerTest,RemoveSinkReturnsFalseForNeverAddedSink)472 TEST_F(RtpDemuxerTest, RemoveSinkReturnsFalseForNeverAddedSink) {
473 MockRtpPacketSink sink;
474 EXPECT_FALSE(RemoveSink(&sink));
475 }
476
TEST_F(RtpDemuxerTest,RemoveSinkReturnsTrueForPreviouslyAddedSsrcSink)477 TEST_F(RtpDemuxerTest, RemoveSinkReturnsTrueForPreviouslyAddedSsrcSink) {
478 constexpr uint32_t ssrc = 101;
479 MockRtpPacketSink sink;
480 AddSinkOnlySsrc(ssrc, &sink);
481
482 EXPECT_TRUE(RemoveSink(&sink));
483 }
484
TEST_F(RtpDemuxerTest,RemoveSinkReturnsTrueForUnresolvedPreviouslyAddedRsidSink)485 TEST_F(RtpDemuxerTest,
486 RemoveSinkReturnsTrueForUnresolvedPreviouslyAddedRsidSink) {
487 const std::string rsid = "a";
488 MockRtpPacketSink sink;
489 AddSinkOnlyRsid(rsid, &sink);
490
491 EXPECT_TRUE(RemoveSink(&sink));
492 }
493
TEST_F(RtpDemuxerTest,RemoveSinkReturnsTrueForResolvedPreviouslyAddedRsidSink)494 TEST_F(RtpDemuxerTest,
495 RemoveSinkReturnsTrueForResolvedPreviouslyAddedRsidSink) {
496 const std::string rsid = "a";
497 constexpr uint32_t ssrc = 101;
498 NiceMock<MockRtpPacketSink> sink;
499 AddSinkOnlyRsid(rsid, &sink);
500 ASSERT_TRUE(demuxer_.OnRtpPacket(*CreatePacketWithSsrcRsid(ssrc, rsid)));
501
502 EXPECT_TRUE(RemoveSink(&sink));
503 }
504
TEST_F(RtpDemuxerTest,RsidLearnedAndLaterPacketsDeliveredWithOnlySsrc)505 TEST_F(RtpDemuxerTest, RsidLearnedAndLaterPacketsDeliveredWithOnlySsrc) {
506 MockRtpPacketSink sink;
507 const std::string rsid = "a";
508 AddSinkOnlyRsid(rsid, &sink);
509
510 // Create a sequence of RTP packets, where only the first one actually
511 // mentions the RSID.
512 std::unique_ptr<RtpPacketReceived> packets[5];
513 constexpr uint32_t rsid_ssrc = 111;
514 packets[0] = CreatePacketWithSsrcRsid(rsid_ssrc, rsid);
515 for (size_t i = 1; i < arraysize(packets); i++) {
516 packets[i] = CreatePacketWithSsrc(rsid_ssrc);
517 }
518
519 // The first packet associates the RSID with the SSRC, thereby allowing the
520 // demuxer to correctly demux all of the packets.
521 InSequence sequence;
522 for (const auto& packet : packets) {
523 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
524 }
525 for (const auto& packet : packets) {
526 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
527 }
528 }
529
TEST_F(RtpDemuxerTest,NoCallbackOnRsidSinkRemovedBeforeFirstPacket)530 TEST_F(RtpDemuxerTest, NoCallbackOnRsidSinkRemovedBeforeFirstPacket) {
531 MockRtpPacketSink sink;
532 const std::string rsid = "a";
533 AddSinkOnlyRsid(rsid, &sink);
534
535 // Sink removed - it won't get triggers even if packets with its RSID arrive.
536 ASSERT_TRUE(RemoveSink(&sink));
537
538 constexpr uint32_t ssrc = 111;
539 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
540 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
541 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
542 }
543
TEST_F(RtpDemuxerTest,NoCallbackOnRsidSinkRemovedAfterFirstPacket)544 TEST_F(RtpDemuxerTest, NoCallbackOnRsidSinkRemovedAfterFirstPacket) {
545 NiceMock<MockRtpPacketSink> sink;
546 const std::string rsid = "a";
547 AddSinkOnlyRsid(rsid, &sink);
548
549 InSequence sequence;
550 constexpr uint32_t ssrc = 111;
551 for (size_t i = 0; i < 10; i++) {
552 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
553 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet));
554 }
555
556 // Sink removed - it won't get triggers even if packets with its RSID arrive.
557 ASSERT_TRUE(RemoveSink(&sink));
558
559 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
560 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
561 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
562 }
563
TEST_F(RtpDemuxerTest,NoCallbackOnMidSinkRemovedBeforeFirstPacket)564 TEST_F(RtpDemuxerTest, NoCallbackOnMidSinkRemovedBeforeFirstPacket) {
565 const std::string mid = "v";
566 constexpr uint32_t ssrc = 10;
567
568 MockRtpPacketSink sink;
569 AddSinkOnlyMid(mid, &sink);
570 RemoveSink(&sink);
571
572 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
573 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
574 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
575 }
576
TEST_F(RtpDemuxerTest,NoCallbackOnMidSinkRemovedAfterFirstPacket)577 TEST_F(RtpDemuxerTest, NoCallbackOnMidSinkRemovedAfterFirstPacket) {
578 const std::string mid = "v";
579 constexpr uint32_t ssrc = 10;
580
581 NiceMock<MockRtpPacketSink> sink;
582 AddSinkOnlyMid(mid, &sink);
583
584 auto p1 = CreatePacketWithSsrcMid(ssrc, mid);
585 demuxer_.OnRtpPacket(*p1);
586
587 RemoveSink(&sink);
588
589 auto p2 = CreatePacketWithSsrcMid(ssrc, mid);
590 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
591 EXPECT_FALSE(demuxer_.OnRtpPacket(*p2));
592 }
593
TEST_F(RtpDemuxerTest,NoCallbackOnMidRsidSinkRemovedAfterFirstPacket)594 TEST_F(RtpDemuxerTest, NoCallbackOnMidRsidSinkRemovedAfterFirstPacket) {
595 const std::string mid = "v";
596 const std::string rsid = "1";
597 constexpr uint32_t ssrc = 10;
598
599 NiceMock<MockRtpPacketSink> sink;
600 AddSinkBothMidRsid(mid, rsid, &sink);
601
602 auto p1 = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
603 demuxer_.OnRtpPacket(*p1);
604
605 RemoveSink(&sink);
606
607 auto p2 = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
608 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
609 EXPECT_FALSE(demuxer_.OnRtpPacket(*p2));
610 }
611
612 // The RSID to SSRC mapping should be one-to-one. If we end up receiving
613 // two (or more) packets with the same SSRC, but different RSIDs, we guarantee
614 // delivery to one of them but not both.
TEST_F(RtpDemuxerTest,FirstSsrcAssociatedWithAnRsidIsNotForgotten)615 TEST_F(RtpDemuxerTest, FirstSsrcAssociatedWithAnRsidIsNotForgotten) {
616 // Each sink has a distinct RSID.
617 MockRtpPacketSink sink_a;
618 const std::string rsid_a = "a";
619 AddSinkOnlyRsid(rsid_a, &sink_a);
620
621 MockRtpPacketSink sink_b;
622 const std::string rsid_b = "b";
623 AddSinkOnlyRsid(rsid_b, &sink_b);
624
625 InSequence sequence; // Verify that the order of delivery is unchanged.
626
627 constexpr uint32_t shared_ssrc = 100;
628
629 // First a packet with `rsid_a` is received, and `sink_a` is associated with
630 // its SSRC.
631 auto packet_a = CreatePacketWithSsrcRsid(shared_ssrc, rsid_a);
632 EXPECT_CALL(sink_a, OnRtpPacket(SamePacketAs(*packet_a))).Times(1);
633 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_a));
634
635 // Second, a packet with `rsid_b` is received. We guarantee that `sink_b`
636 // receives it.
637 auto packet_b = CreatePacketWithSsrcRsid(shared_ssrc, rsid_b);
638 EXPECT_CALL(sink_a, OnRtpPacket(_)).Times(0);
639 EXPECT_CALL(sink_b, OnRtpPacket(SamePacketAs(*packet_b))).Times(1);
640 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_b));
641
642 // Known edge-case; adding a new RSID association makes us re-examine all
643 // SSRCs. `sink_b` may or may not be associated with the SSRC now; we make
644 // no promises on that. However, since the RSID is specified and it cannot be
645 // found the packet should be dropped.
646 MockRtpPacketSink sink_c;
647 const std::string rsid_c = "c";
648 constexpr uint32_t some_other_ssrc = shared_ssrc + 1;
649 AddSinkOnlySsrc(some_other_ssrc, &sink_c);
650
651 auto packet_c = CreatePacketWithSsrcMid(shared_ssrc, rsid_c);
652 EXPECT_CALL(sink_a, OnRtpPacket(_)).Times(0);
653 EXPECT_CALL(sink_b, OnRtpPacket(_)).Times(0);
654 EXPECT_CALL(sink_c, OnRtpPacket(_)).Times(0);
655 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_c));
656 }
657
TEST_F(RtpDemuxerTest,MultipleRsidsOnSameSink)658 TEST_F(RtpDemuxerTest, MultipleRsidsOnSameSink) {
659 MockRtpPacketSink sink;
660 const std::string rsids[] = {"a", "b", "c"};
661
662 for (const std::string& rsid : rsids) {
663 AddSinkOnlyRsid(rsid, &sink);
664 }
665
666 InSequence sequence;
667 for (size_t i = 0; i < arraysize(rsids); i++) {
668 // Assign different SSRCs and sequence numbers to all packets.
669 const uint32_t ssrc = 1000 + static_cast<uint32_t>(i);
670 const uint16_t sequence_number = 50 + static_cast<uint16_t>(i);
671 auto packet = CreatePacketWithSsrcRsid(ssrc, rsids[i]);
672 packet->SetSequenceNumber(sequence_number);
673 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
674 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
675 }
676 }
677
678 // RSIDs are given higher priority than SSRC because we believe senders are less
679 // likely to mislabel packets with RSID than mislabel them with SSRCs.
TEST_F(RtpDemuxerTest,SinkWithBothRsidAndSsrcAssociations)680 TEST_F(RtpDemuxerTest, SinkWithBothRsidAndSsrcAssociations) {
681 MockRtpPacketSink sink;
682 constexpr uint32_t standalone_ssrc = 10101;
683 constexpr uint32_t rsid_ssrc = 20202;
684 const std::string rsid = "1";
685
686 AddSinkOnlySsrc(standalone_ssrc, &sink);
687 AddSinkOnlyRsid(rsid, &sink);
688
689 InSequence sequence;
690
691 auto ssrc_packet = CreatePacketWithSsrc(standalone_ssrc);
692 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*ssrc_packet))).Times(1);
693 EXPECT_TRUE(demuxer_.OnRtpPacket(*ssrc_packet));
694
695 auto rsid_packet = CreatePacketWithSsrcRsid(rsid_ssrc, rsid);
696 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*rsid_packet))).Times(1);
697 EXPECT_TRUE(demuxer_.OnRtpPacket(*rsid_packet));
698 }
699
700 // Packets are always guaranteed to be routed to only one sink.
TEST_F(RtpDemuxerTest,AssociatingByRsidAndBySsrcCannotTriggerDoubleCall)701 TEST_F(RtpDemuxerTest, AssociatingByRsidAndBySsrcCannotTriggerDoubleCall) {
702 constexpr uint32_t ssrc = 10101;
703 const std::string rsid = "a";
704
705 MockRtpPacketSink sink;
706 AddSinkOnlySsrc(ssrc, &sink);
707 AddSinkOnlyRsid(rsid, &sink);
708
709 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
710 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
711 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
712 }
713
714
715 // If one sink is associated with SSRC x, and another sink with RSID y, then if
716 // we receive a packet with both SSRC x and RSID y, route that to only the sink
717 // for RSID y since we believe RSID tags to be more trustworthy than signaled
718 // SSRCs.
TEST_F(RtpDemuxerTest,PacketFittingBothRsidSinkAndSsrcSinkGivenOnlyToRsidSink)719 TEST_F(RtpDemuxerTest,
720 PacketFittingBothRsidSinkAndSsrcSinkGivenOnlyToRsidSink) {
721 constexpr uint32_t ssrc = 111;
722 MockRtpPacketSink ssrc_sink;
723 AddSinkOnlySsrc(ssrc, &ssrc_sink);
724
725 const std::string rsid = "a";
726 MockRtpPacketSink rsid_sink;
727 AddSinkOnlyRsid(rsid, &rsid_sink);
728
729 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
730
731 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
732 EXPECT_CALL(rsid_sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
733 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
734 }
735
736 // We're not expecting RSIDs to be resolved to SSRCs which were previously
737 // mapped to sinks, and make no guarantees except for graceful handling.
TEST_F(RtpDemuxerTest,GracefullyHandleRsidBeingMappedToPrevouslyAssociatedSsrc)738 TEST_F(RtpDemuxerTest,
739 GracefullyHandleRsidBeingMappedToPrevouslyAssociatedSsrc) {
740 constexpr uint32_t ssrc = 111;
741 NiceMock<MockRtpPacketSink> ssrc_sink;
742 AddSinkOnlySsrc(ssrc, &ssrc_sink);
743
744 const std::string rsid = "a";
745 NiceMock<MockRtpPacketSink> rsid_sink;
746 AddSinkOnlyRsid(rsid, &rsid_sink);
747
748 // The SSRC was mapped to an SSRC sink, but was even active (packets flowed
749 // over it).
750 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
751 demuxer_.OnRtpPacket(*packet);
752
753 // If the SSRC sink is ever removed, the RSID sink *might* receive indications
754 // of packets, and observers *might* be informed. Only graceful handling
755 // is guaranteed.
756 RemoveSink(&ssrc_sink);
757 EXPECT_CALL(rsid_sink, OnRtpPacket(SamePacketAs(*packet))).Times(AtLeast(0));
758 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
759 }
760
761 // Tests that when one MID sink is configured, packets that include the MID
762 // extension will get routed to that sink and any packets that use the same
763 // SSRC as one of those packets later will also get routed to the sink, even
764 // if a new SSRC is introduced for the same MID.
TEST_F(RtpDemuxerTest,RoutedByMidWhenSsrcAdded)765 TEST_F(RtpDemuxerTest, RoutedByMidWhenSsrcAdded) {
766 const std::string mid = "v";
767 NiceMock<MockRtpPacketSink> sink;
768 AddSinkOnlyMid(mid, &sink);
769
770 constexpr uint32_t ssrc1 = 10;
771 constexpr uint32_t ssrc2 = 11;
772
773 auto packet_ssrc1_mid = CreatePacketWithSsrcMid(ssrc1, mid);
774 demuxer_.OnRtpPacket(*packet_ssrc1_mid);
775 auto packet_ssrc2_mid = CreatePacketWithSsrcMid(ssrc2, mid);
776 demuxer_.OnRtpPacket(*packet_ssrc2_mid);
777
778 auto packet_ssrc1_only = CreatePacketWithSsrc(ssrc1);
779 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc1_only))).Times(1);
780 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc1_only));
781
782 auto packet_ssrc2_only = CreatePacketWithSsrc(ssrc2);
783 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc2_only))).Times(1);
784 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc2_only));
785 }
786
TEST_F(RtpDemuxerTest,DontLearnMidSsrcBindingBeforeSinkAdded)787 TEST_F(RtpDemuxerTest, DontLearnMidSsrcBindingBeforeSinkAdded) {
788 const std::string mid = "v";
789 constexpr uint32_t ssrc = 10;
790
791 auto packet_ssrc_mid = CreatePacketWithSsrcMid(ssrc, mid);
792 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_ssrc_mid));
793
794 MockRtpPacketSink sink;
795 AddSinkOnlyMid(mid, &sink);
796
797 auto packet_ssrc_only = CreatePacketWithSsrc(ssrc);
798 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
799 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_ssrc_only));
800 }
801
TEST_F(RtpDemuxerTest,DontForgetMidSsrcBindingWhenSinkRemoved)802 TEST_F(RtpDemuxerTest, DontForgetMidSsrcBindingWhenSinkRemoved) {
803 const std::string mid = "v";
804 constexpr uint32_t ssrc = 10;
805
806 NiceMock<MockRtpPacketSink> sink1;
807 AddSinkOnlyMid(mid, &sink1);
808
809 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
810 demuxer_.OnRtpPacket(*packet_with_mid);
811
812 RemoveSink(&sink1);
813
814 MockRtpPacketSink sink2;
815 AddSinkOnlyMid(mid, &sink2);
816
817 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
818 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
819 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
820 }
821
822 // If a sink is added with only a MID, then any packet with that MID no matter
823 // the RSID should be routed to that sink.
TEST_F(RtpDemuxerTest,RoutedByMidWithAnyRsid)824 TEST_F(RtpDemuxerTest, RoutedByMidWithAnyRsid) {
825 const std::string mid = "v";
826 const std::string rsid1 = "1";
827 const std::string rsid2 = "2";
828 constexpr uint32_t ssrc1 = 10;
829 constexpr uint32_t ssrc2 = 11;
830
831 MockRtpPacketSink sink;
832 AddSinkOnlyMid(mid, &sink);
833
834 InSequence sequence;
835
836 auto packet_ssrc1_rsid1 = CreatePacketWithSsrcMidRsid(ssrc1, mid, rsid1);
837 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc1_rsid1))).Times(1);
838 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc1_rsid1));
839
840 auto packet_ssrc2_rsid2 = CreatePacketWithSsrcMidRsid(ssrc2, mid, rsid2);
841 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc2_rsid2))).Times(1);
842 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc2_rsid2));
843 }
844
845 // These two tests verify that for a sink added with a MID, RSID pair, if the
846 // MID and RSID are learned in separate packets (e.g., because the header
847 // extensions are sent separately), then a later packet with just SSRC will get
848 // routed to that sink.
849 // The first test checks that the functionality works when MID is learned first.
850 // The second test checks that the functionality works when RSID is learned
851 // first.
TEST_F(RtpDemuxerTest,LearnMidThenRsidSeparatelyAndRouteBySsrc)852 TEST_F(RtpDemuxerTest, LearnMidThenRsidSeparatelyAndRouteBySsrc) {
853 const std::string mid = "v";
854 const std::string rsid = "1";
855 constexpr uint32_t ssrc = 10;
856
857 NiceMock<MockRtpPacketSink> sink;
858 AddSinkBothMidRsid(mid, rsid, &sink);
859
860 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
861 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_mid));
862
863 auto packet_with_rsid = CreatePacketWithSsrcRsid(ssrc, rsid);
864 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_rsid));
865
866 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
867 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
868 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
869 }
870
TEST_F(RtpDemuxerTest,LearnRsidThenMidSeparatelyAndRouteBySsrc)871 TEST_F(RtpDemuxerTest, LearnRsidThenMidSeparatelyAndRouteBySsrc) {
872 const std::string mid = "v";
873 const std::string rsid = "1";
874 constexpr uint32_t ssrc = 10;
875
876 NiceMock<MockRtpPacketSink> sink;
877 AddSinkBothMidRsid(mid, rsid, &sink);
878
879 auto packet_with_rsid = CreatePacketWithSsrcRsid(ssrc, rsid);
880 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_rsid));
881
882 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
883 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_mid));
884
885 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
886 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
887 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
888 }
889
TEST_F(RtpDemuxerTest,DontLearnMidRsidBindingBeforeSinkAdded)890 TEST_F(RtpDemuxerTest, DontLearnMidRsidBindingBeforeSinkAdded) {
891 const std::string mid = "v";
892 const std::string rsid = "1";
893 constexpr uint32_t ssrc = 10;
894
895 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
896 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_both));
897
898 MockRtpPacketSink sink;
899 AddSinkBothMidRsid(mid, rsid, &sink);
900
901 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
902 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
903 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_with_ssrc));
904 }
905
TEST_F(RtpDemuxerTest,DontForgetMidRsidBindingWhenSinkRemoved)906 TEST_F(RtpDemuxerTest, DontForgetMidRsidBindingWhenSinkRemoved) {
907 const std::string mid = "v";
908 const std::string rsid = "1";
909 constexpr uint32_t ssrc = 10;
910
911 NiceMock<MockRtpPacketSink> sink1;
912 AddSinkBothMidRsid(mid, rsid, &sink1);
913
914 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
915 demuxer_.OnRtpPacket(*packet_with_both);
916
917 RemoveSink(&sink1);
918
919 MockRtpPacketSink sink2;
920 AddSinkBothMidRsid(mid, rsid, &sink2);
921
922 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
923 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
924 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
925 }
926
TEST_F(RtpDemuxerTest,LearnMidRsidBindingAfterSinkAdded)927 TEST_F(RtpDemuxerTest, LearnMidRsidBindingAfterSinkAdded) {
928 const std::string mid = "v";
929 const std::string rsid = "1";
930 constexpr uint32_t ssrc = 10;
931
932 NiceMock<MockRtpPacketSink> sink;
933 AddSinkBothMidRsid(mid, rsid, &sink);
934
935 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
936 demuxer_.OnRtpPacket(*packet_with_both);
937
938 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
939 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
940 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
941 }
942
TEST_F(RtpDemuxerTest,DropByPayloadTypeIfNoSink)943 TEST_F(RtpDemuxerTest, DropByPayloadTypeIfNoSink) {
944 constexpr uint8_t payload_type = 30;
945 constexpr uint32_t ssrc = 10;
946
947 auto packet = CreatePacketWithSsrc(ssrc);
948 packet->SetPayloadType(payload_type);
949 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
950 }
951
952 // For legacy applications, it's possible for us to demux if the payload type is
953 // unique. But if multiple sinks are registered with different MIDs and the same
954 // payload types, then we cannot route a packet with just payload type because
955 // it is ambiguous which sink it should be sent to.
TEST_F(RtpDemuxerTest,DropByPayloadTypeIfAddedInMultipleSinks)956 TEST_F(RtpDemuxerTest, DropByPayloadTypeIfAddedInMultipleSinks) {
957 const std::string mid1 = "v";
958 const std::string mid2 = "a";
959 constexpr uint8_t payload_type = 30;
960 constexpr uint32_t ssrc = 10;
961
962 RtpDemuxerCriteria mid1_pt(mid1);
963 mid1_pt.payload_types() = {payload_type};
964 MockRtpPacketSink sink1;
965 AddSink(mid1_pt, &sink1);
966
967 RtpDemuxerCriteria mid2_pt(mid2);
968 mid2_pt.payload_types() = {payload_type};
969 MockRtpPacketSink sink2;
970 AddSink(mid2_pt, &sink2);
971
972 auto packet = CreatePacketWithSsrc(ssrc);
973 packet->SetPayloadType(payload_type);
974
975 EXPECT_CALL(sink1, OnRtpPacket(_)).Times(0);
976 EXPECT_CALL(sink2, OnRtpPacket(_)).Times(0);
977 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
978 }
979
980 // If two sinks are added with different MIDs but the same payload types, then
981 // we cannot demux on the payload type only unless one of the sinks is removed.
TEST_F(RtpDemuxerTest,RoutedByPayloadTypeIfAmbiguousSinkRemoved)982 TEST_F(RtpDemuxerTest, RoutedByPayloadTypeIfAmbiguousSinkRemoved) {
983 const std::string mid1 = "v";
984 const std::string mid2 = "a";
985 constexpr uint8_t payload_type = 30;
986 constexpr uint32_t ssrc = 10;
987
988 RtpDemuxerCriteria mid1_pt(mid1);
989 mid1_pt.payload_types().insert(payload_type);
990 MockRtpPacketSink sink1;
991 AddSink(mid1_pt, &sink1);
992
993 RtpDemuxerCriteria mid2_pt(mid2);
994 mid2_pt.payload_types().insert(payload_type);
995 MockRtpPacketSink sink2;
996 AddSink(mid2_pt, &sink2);
997
998 RemoveSink(&sink1);
999
1000 auto packet = CreatePacketWithSsrc(ssrc);
1001 packet->SetPayloadType(payload_type);
1002
1003 EXPECT_CALL(sink1, OnRtpPacket(_)).Times(0);
1004 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1005
1006 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1007 }
1008
TEST_F(RtpDemuxerTest,RoutedByPayloadTypeLatchesSsrc)1009 TEST_F(RtpDemuxerTest, RoutedByPayloadTypeLatchesSsrc) {
1010 constexpr uint8_t payload_type = 30;
1011 constexpr uint32_t ssrc = 10;
1012
1013 RtpDemuxerCriteria pt;
1014 pt.payload_types().insert(payload_type);
1015 NiceMock<MockRtpPacketSink> sink;
1016 AddSink(pt, &sink);
1017
1018 auto packet_with_pt = CreatePacketWithSsrc(ssrc);
1019 packet_with_pt->SetPayloadType(payload_type);
1020 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt));
1021
1022 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1023 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
1024 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1025 }
1026
1027 // RSIDs are scoped within MID, so if two sinks are registered with the same
1028 // RSIDs but different MIDs, then packets containing both extensions should be
1029 // routed to the correct one.
TEST_F(RtpDemuxerTest,PacketWithSameRsidDifferentMidRoutedToProperSink)1030 TEST_F(RtpDemuxerTest, PacketWithSameRsidDifferentMidRoutedToProperSink) {
1031 const std::string mid1 = "mid1";
1032 const std::string mid2 = "mid2";
1033 const std::string rsid = "rsid";
1034 constexpr uint32_t ssrc1 = 10;
1035 constexpr uint32_t ssrc2 = 11;
1036
1037 NiceMock<MockRtpPacketSink> mid1_sink;
1038 AddSinkBothMidRsid(mid1, rsid, &mid1_sink);
1039
1040 MockRtpPacketSink mid2_sink;
1041 AddSinkBothMidRsid(mid2, rsid, &mid2_sink);
1042
1043 auto packet_mid1 = CreatePacketWithSsrcMidRsid(ssrc1, mid1, rsid);
1044 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_mid1));
1045
1046 auto packet_mid2 = CreatePacketWithSsrcMidRsid(ssrc2, mid2, rsid);
1047 EXPECT_CALL(mid2_sink, OnRtpPacket(SamePacketAs(*packet_mid2))).Times(1);
1048 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_mid2));
1049 }
1050
1051 // If a sink is first bound to a given SSRC by signaling but later a new sink is
1052 // bound to a given MID by a later signaling, then when a packet arrives with
1053 // both the SSRC and MID, then the signaled MID sink should take precedence.
TEST_F(RtpDemuxerTest,SignaledMidShouldOverwriteSignaledSsrc)1054 TEST_F(RtpDemuxerTest, SignaledMidShouldOverwriteSignaledSsrc) {
1055 constexpr uint32_t ssrc = 11;
1056 const std::string mid = "mid";
1057
1058 MockRtpPacketSink ssrc_sink;
1059 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1060
1061 MockRtpPacketSink mid_sink;
1062 AddSinkOnlyMid(mid, &mid_sink);
1063
1064 auto p = CreatePacketWithSsrcMid(ssrc, mid);
1065 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
1066 EXPECT_CALL(mid_sink, OnRtpPacket(SamePacketAs(*p))).Times(1);
1067 EXPECT_TRUE(demuxer_.OnRtpPacket(*p));
1068 }
1069
1070 // Extends the previous test to also ensure that later packets that do not
1071 // specify MID are still routed to the MID sink rather than the overwritten SSRC
1072 // sink.
TEST_F(RtpDemuxerTest,SignaledMidShouldOverwriteSignalledSsrcPersistent)1073 TEST_F(RtpDemuxerTest, SignaledMidShouldOverwriteSignalledSsrcPersistent) {
1074 constexpr uint32_t ssrc = 11;
1075 const std::string mid = "mid";
1076
1077 MockRtpPacketSink ssrc_sink;
1078 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1079
1080 NiceMock<MockRtpPacketSink> mid_sink;
1081 AddSinkOnlyMid(mid, &mid_sink);
1082
1083 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
1084
1085 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
1086 demuxer_.OnRtpPacket(*packet_with_mid);
1087
1088 auto packet_without_mid = CreatePacketWithSsrc(ssrc);
1089 EXPECT_CALL(mid_sink, OnRtpPacket(SamePacketAs(*packet_without_mid)))
1090 .Times(1);
1091 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_without_mid));
1092 }
1093
TEST_F(RtpDemuxerTest,RouteByPayloadTypeMultipleMatch)1094 TEST_F(RtpDemuxerTest, RouteByPayloadTypeMultipleMatch) {
1095 constexpr uint32_t ssrc = 10;
1096 constexpr uint8_t pt1 = 30;
1097 constexpr uint8_t pt2 = 31;
1098
1099 MockRtpPacketSink sink;
1100 RtpDemuxerCriteria criteria;
1101 criteria.payload_types() = {pt1, pt2};
1102 AddSink(criteria, &sink);
1103
1104 auto packet_with_pt1 = CreatePacketWithSsrc(ssrc);
1105 packet_with_pt1->SetPayloadType(pt1);
1106 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_pt1)));
1107 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt1));
1108
1109 auto packet_with_pt2 = CreatePacketWithSsrc(ssrc);
1110 packet_with_pt2->SetPayloadType(pt2);
1111 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_pt2)));
1112 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt2));
1113 }
1114
TEST_F(RtpDemuxerTest,DontDemuxOnMidAloneIfAddedWithRsid)1115 TEST_F(RtpDemuxerTest, DontDemuxOnMidAloneIfAddedWithRsid) {
1116 const std::string mid = "v";
1117 const std::string rsid = "1";
1118 constexpr uint32_t ssrc = 10;
1119
1120 MockRtpPacketSink sink;
1121 AddSinkBothMidRsid(mid, rsid, &sink);
1122
1123 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1124
1125 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
1126 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1127 }
1128
TEST_F(RtpDemuxerTest,DemuxBySsrcEvenWithMidAndRsid)1129 TEST_F(RtpDemuxerTest, DemuxBySsrcEvenWithMidAndRsid) {
1130 const std::string mid = "v";
1131 const std::string rsid = "1";
1132 constexpr uint32_t ssrc = 10;
1133
1134 RtpDemuxerCriteria criteria(mid, rsid);
1135 criteria.ssrcs().insert(ssrc);
1136 MockRtpPacketSink sink;
1137 AddSink(criteria, &sink);
1138
1139 auto packet = CreatePacketWithSsrc(ssrc);
1140 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1141 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1142 }
1143
1144 // In slight deviation from the BUNDLE spec, if we match a sink according to
1145 // SSRC, then we do not verify payload type against the criteria and defer to
1146 // the sink to check that it is correct.
TEST_F(RtpDemuxerTest,DoNotCheckPayloadTypeIfMatchedByOtherCriteria)1147 TEST_F(RtpDemuxerTest, DoNotCheckPayloadTypeIfMatchedByOtherCriteria) {
1148 constexpr uint32_t ssrc = 10;
1149 constexpr uint8_t payload_type = 30;
1150 constexpr uint8_t different_payload_type = payload_type + 1;
1151
1152 RtpDemuxerCriteria criteria;
1153 criteria.ssrcs().insert(ssrc);
1154 criteria.payload_types().insert(payload_type);
1155 MockRtpPacketSink sink;
1156 AddSink(criteria, &sink);
1157
1158 auto packet = CreatePacketWithSsrc(ssrc);
1159 packet->SetPayloadType(different_payload_type);
1160 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1161 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1162 }
1163
1164 // If a repair packet includes an RSID it should be ignored and the packet
1165 // should be routed by its RRID.
TEST_F(RtpDemuxerTest,PacketWithRsidAndRridRoutedByRrid)1166 TEST_F(RtpDemuxerTest, PacketWithRsidAndRridRoutedByRrid) {
1167 const std::string rsid = "1";
1168 const std::string rrid = "1r";
1169 constexpr uint32_t ssrc = 10;
1170
1171 MockRtpPacketSink sink_rsid;
1172 AddSinkOnlyRsid(rsid, &sink_rsid);
1173
1174 MockRtpPacketSink sink_rrid;
1175 AddSinkOnlyRsid(rrid, &sink_rrid);
1176
1177 auto packet = CreatePacketWithSsrcRsidRrid(ssrc, rsid, rrid);
1178 EXPECT_CALL(sink_rsid, OnRtpPacket(_)).Times(0);
1179 EXPECT_CALL(sink_rrid, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1180 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1181 }
1182
1183 // Same test as above but checks that the latched SSRC routes to the RRID sink.
TEST_F(RtpDemuxerTest,PacketWithRsidAndRridLatchesSsrcToRrid)1184 TEST_F(RtpDemuxerTest, PacketWithRsidAndRridLatchesSsrcToRrid) {
1185 const std::string rsid = "1";
1186 const std::string rrid = "1r";
1187 constexpr uint32_t ssrc = 10;
1188
1189 MockRtpPacketSink sink_rsid;
1190 AddSinkOnlyRsid(rsid, &sink_rsid);
1191
1192 NiceMock<MockRtpPacketSink> sink_rrid;
1193 AddSinkOnlyRsid(rrid, &sink_rrid);
1194
1195 auto packet_rsid_rrid = CreatePacketWithSsrcRsidRrid(ssrc, rsid, rrid);
1196 demuxer_.OnRtpPacket(*packet_rsid_rrid);
1197
1198 auto packet_ssrc_only = CreatePacketWithSsrc(ssrc);
1199 EXPECT_CALL(sink_rsid, OnRtpPacket(_)).Times(0);
1200 EXPECT_CALL(sink_rrid, OnRtpPacket(SamePacketAs(*packet_ssrc_only))).Times(1);
1201 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc_only));
1202 }
1203
1204 // Tests that a packet which includes MID and RSID is dropped and not routed by
1205 // SSRC if the MID and RSID do not match an added sink.
TEST_F(RtpDemuxerTest,PacketWithMidAndUnknownRsidIsNotRoutedBySsrc)1206 TEST_F(RtpDemuxerTest, PacketWithMidAndUnknownRsidIsNotRoutedBySsrc) {
1207 constexpr uint32_t ssrc = 10;
1208 const std::string mid = "v";
1209 const std::string rsid = "1";
1210 const std::string wrong_rsid = "2";
1211
1212 RtpDemuxerCriteria criteria(mid, rsid);
1213 criteria.ssrcs().insert(ssrc);
1214 MockRtpPacketSink sink;
1215 AddSink(criteria, &sink);
1216
1217 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, wrong_rsid);
1218 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1219 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1220 }
1221
1222 // Tests that a packet which includes MID and RSID is dropped and not routed by
1223 // payload type if the MID and RSID do not match an added sink.
TEST_F(RtpDemuxerTest,PacketWithMidAndUnknownRsidIsNotRoutedByPayloadType)1224 TEST_F(RtpDemuxerTest, PacketWithMidAndUnknownRsidIsNotRoutedByPayloadType) {
1225 constexpr uint32_t ssrc = 10;
1226 const std::string mid = "v";
1227 const std::string rsid = "1";
1228 const std::string wrong_rsid = "2";
1229 constexpr uint8_t payload_type = 30;
1230
1231 RtpDemuxerCriteria criteria(mid, rsid);
1232 criteria.payload_types().insert(payload_type);
1233 MockRtpPacketSink sink;
1234 AddSink(criteria, &sink);
1235
1236 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, wrong_rsid);
1237 packet->SetPayloadType(payload_type);
1238 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1239 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1240 }
1241
TEST_F(RtpDemuxerTest,MidMustNotExceedMaximumLength)1242 TEST_F(RtpDemuxerTest, MidMustNotExceedMaximumLength) {
1243 MockRtpPacketSink sink1;
1244 std::string mid1(BaseRtpStringExtension::kMaxValueSizeBytes + 1, 'a');
1245 // Adding the sink should pass even though the supplied mid is too long.
1246 // The mid will be truncated though.
1247 EXPECT_TRUE(AddSinkOnlyMid(mid1, &sink1));
1248
1249 // Adding a second sink with a mid that matches the truncated mid that was
1250 // just added, should fail.
1251 MockRtpPacketSink sink2;
1252 std::string mid2(mid1.substr(0, BaseRtpStringExtension::kMaxValueSizeBytes));
1253 EXPECT_FALSE(AddSinkOnlyMid(mid2, &sink2));
1254 EXPECT_FALSE(RemoveSink(&sink2));
1255
1256 // Remove the original sink.
1257 EXPECT_TRUE(RemoveSink(&sink1));
1258 }
1259
1260 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
1261
TEST_F(RtpDemuxerDeathTest,CriteriaMustBeNonEmpty)1262 TEST_F(RtpDemuxerDeathTest, CriteriaMustBeNonEmpty) {
1263 MockRtpPacketSink sink;
1264 RtpDemuxerCriteria criteria;
1265 EXPECT_DEATH(AddSink(criteria, &sink), "");
1266 }
1267
TEST_F(RtpDemuxerDeathTest,RsidMustBeAlphaNumeric)1268 TEST_F(RtpDemuxerDeathTest, RsidMustBeAlphaNumeric) {
1269 MockRtpPacketSink sink;
1270 EXPECT_DEATH(AddSinkOnlyRsid("a_3", &sink), "");
1271 }
1272
TEST_F(RtpDemuxerDeathTest,MidMustBeToken)1273 TEST_F(RtpDemuxerDeathTest, MidMustBeToken) {
1274 MockRtpPacketSink sink;
1275 EXPECT_DEATH(AddSinkOnlyMid("a(3)", &sink), "");
1276 }
1277
TEST_F(RtpDemuxerDeathTest,RsidMustNotExceedMaximumLength)1278 TEST_F(RtpDemuxerDeathTest, RsidMustNotExceedMaximumLength) {
1279 MockRtpPacketSink sink;
1280 std::string rsid(BaseRtpStringExtension::kMaxValueSizeBytes + 1, 'a');
1281 EXPECT_DEATH(AddSinkOnlyRsid(rsid, &sink), "");
1282 }
1283
1284 #endif
1285
1286 } // namespace
1287 } // namespace webrtc
1288