blob: 0a08c8698dbc7a1b0ab9b60d2817cb3958cbf678 [file] [log] [blame]
eladalon760a0762017-05-31 16:12:251/*
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 Bonadei92ea95e2017-09-15 04:47:3111#include "call/rtp_demuxer.h"
eladalon760a0762017-05-31 16:12:2512
13#include <memory>
Steve Anton9e0c7422017-08-18 01:59:4714#include <set>
eladalond0244c22017-06-08 11:19:1315#include <string>
eladalon760a0762017-05-31 16:12:2516
Karl Wiberg918f50c2018-07-05 09:40:3317#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "call/ssrc_binding_observer.h"
19#include "call/test/mock_rtp_packet_sink_interface.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
21#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
22#include "modules/rtp_rtcp/source/rtp_packet_received.h"
23#include "rtc_base/arraysize.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3124#include "rtc_base/checks.h"
Karl Wiberge40468b2017-11-22 09:42:2625#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3126#include "test/gmock.h"
27#include "test/gtest.h"
eladalon760a0762017-05-31 16:12:2528
29namespace webrtc {
30
31namespace {
32
33using ::testing::_;
eladalond0244c22017-06-08 11:19:1334using ::testing::AtLeast;
Steve Anton53c7ba62017-08-18 17:05:4735using ::testing::AtMost;
eladalond0244c22017-06-08 11:19:1336using ::testing::InSequence;
37using ::testing::NiceMock;
eladalon760a0762017-05-31 16:12:2538
Steve Antonb3329172017-08-17 22:23:5139class MockSsrcBindingObserver : public SsrcBindingObserver {
eladalona52722f2017-06-26 18:23:5440 public:
Steve Antonb3329172017-08-17 22:23:5141 MOCK_METHOD2(OnSsrcBoundToRsid, void(const std::string& rsid, uint32_t ssrc));
Steve Anton53c7ba62017-08-18 17:05:4742 MOCK_METHOD2(OnSsrcBoundToMid, void(const std::string& mid, uint32_t ssrc));
43 MOCK_METHOD3(OnSsrcBoundToMidRsid,
44 void(const std::string& mid,
45 const std::string& rsid,
46 uint32_t ssrc));
47 MOCK_METHOD2(OnSsrcBoundToPayloadType,
48 void(uint8_t payload_type, uint32_t ssrc));
eladalona52722f2017-06-26 18:23:5449};
50
Mirko Bonadei6a489f22019-04-09 13:11:1251class RtpDemuxerTest : public ::testing::Test {
Steve Anton9e0c7422017-08-18 01:59:4752 protected:
53 ~RtpDemuxerTest() {
54 for (auto* sink : sinks_to_tear_down_) {
55 demuxer_.RemoveSink(sink);
56 }
57 for (auto* observer : observers_to_tear_down_) {
58 demuxer_.DeregisterSsrcBindingObserver(observer);
59 }
60 }
61
Steve Anton53c7ba62017-08-18 17:05:4762 // These are convenience methods for calling demuxer.AddSink with different
63 // parameters and will ensure that the sink is automatically removed when the
64 // test case finishes.
65
66 bool AddSink(const RtpDemuxerCriteria& criteria,
67 RtpPacketSinkInterface* sink) {
68 bool added = demuxer_.AddSink(criteria, sink);
Steve Anton9e0c7422017-08-18 01:59:4769 if (added) {
70 sinks_to_tear_down_.insert(sink);
71 }
72 return added;
73 }
74
Steve Anton53c7ba62017-08-18 17:05:4775 bool AddSinkOnlySsrc(uint32_t ssrc, RtpPacketSinkInterface* sink) {
76 RtpDemuxerCriteria criteria;
77 criteria.ssrcs = {ssrc};
78 return AddSink(criteria, sink);
79 }
80
81 bool AddSinkOnlyRsid(const std::string& rsid, RtpPacketSinkInterface* sink) {
82 RtpDemuxerCriteria criteria;
83 criteria.rsid = rsid;
84 return AddSink(criteria, sink);
85 }
86
87 bool AddSinkOnlyMid(const std::string& mid, RtpPacketSinkInterface* sink) {
88 RtpDemuxerCriteria criteria;
89 criteria.mid = mid;
90 return AddSink(criteria, sink);
91 }
92
93 bool AddSinkBothMidRsid(const std::string& mid,
94 const std::string& rsid,
95 RtpPacketSinkInterface* sink) {
96 RtpDemuxerCriteria criteria;
97 criteria.mid = mid;
98 criteria.rsid = rsid;
99 return AddSink(criteria, sink);
Steve Anton9e0c7422017-08-18 01:59:47100 }
101
102 bool RemoveSink(RtpPacketSinkInterface* sink) {
103 sinks_to_tear_down_.erase(sink);
104 return demuxer_.RemoveSink(sink);
105 }
106
Steve Anton53c7ba62017-08-18 17:05:47107 // These are convenience methods for calling
108 // demuxer.{Register|Unregister}SsrcBindingObserver such that observers are
109 // automatically removed when the test finishes.
110
Steve Anton9e0c7422017-08-18 01:59:47111 void RegisterSsrcBindingObserver(SsrcBindingObserver* observer) {
112 demuxer_.RegisterSsrcBindingObserver(observer);
113 observers_to_tear_down_.insert(observer);
114 }
115
116 void DeregisterSsrcBindingObserver(SsrcBindingObserver* observer) {
117 demuxer_.DeregisterSsrcBindingObserver(observer);
118 observers_to_tear_down_.erase(observer);
119 }
120
121 // The CreatePacket* methods are helpers for creating new RTP packets with
122 // various attributes set. Tests should use the helper that provides the
123 // minimum information needed to exercise the behavior under test. Tests also
124 // should not rely on any behavior which is not clearly described in the
125 // helper name/arguments. Any additional settings that are not covered by the
126 // helper should be set manually on the packet once it has been returned.
127 // For example, most tests in this file do not care about the RTP sequence
128 // number, but to ensure that the returned packets are valid the helpers will
129 // auto-increment the sequence number starting with 1. Tests that rely on
130 // specific sequence number behavior should call SetSequenceNumber manually on
131 // the returned packet.
132
133 // Intended for use only by other CreatePacket* helpers.
134 std::unique_ptr<RtpPacketReceived> CreatePacket(
135 uint32_t ssrc,
136 RtpPacketReceived::ExtensionManager* extension_manager) {
Karl Wiberg918f50c2018-07-05 09:40:33137 auto packet = absl::make_unique<RtpPacketReceived>(extension_manager);
Steve Anton9e0c7422017-08-18 01:59:47138 packet->SetSsrc(ssrc);
139 packet->SetSequenceNumber(next_sequence_number_++);
140 return packet;
141 }
142
143 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrc(uint32_t ssrc) {
144 return CreatePacket(ssrc, nullptr);
145 }
146
Steve Anton53c7ba62017-08-18 17:05:47147 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcMid(
148 uint32_t ssrc,
149 const std::string& mid) {
150 RtpPacketReceived::ExtensionManager extension_manager;
151 extension_manager.Register<RtpMid>(11);
152
153 auto packet = CreatePacket(ssrc, &extension_manager);
154 packet->SetExtension<RtpMid>(mid);
155 return packet;
156 }
157
Steve Anton9e0c7422017-08-18 01:59:47158 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcRsid(
159 uint32_t ssrc,
160 const std::string& rsid) {
161 RtpPacketReceived::ExtensionManager extension_manager;
162 extension_manager.Register<RtpStreamId>(6);
163
164 auto packet = CreatePacket(ssrc, &extension_manager);
165 packet->SetExtension<RtpStreamId>(rsid);
166 return packet;
167 }
168
Steve Anton53c7ba62017-08-18 17:05:47169 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcRrid(
170 uint32_t ssrc,
171 const std::string& rrid) {
172 RtpPacketReceived::ExtensionManager extension_manager;
173 extension_manager.Register<RepairedRtpStreamId>(7);
174
175 auto packet = CreatePacket(ssrc, &extension_manager);
176 packet->SetExtension<RepairedRtpStreamId>(rrid);
177 return packet;
178 }
179
180 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcMidRsid(
181 uint32_t ssrc,
182 const std::string& mid,
183 const std::string& rsid) {
184 RtpPacketReceived::ExtensionManager extension_manager;
185 extension_manager.Register<RtpMid>(11);
186 extension_manager.Register<RtpStreamId>(6);
187
188 auto packet = CreatePacket(ssrc, &extension_manager);
189 packet->SetExtension<RtpMid>(mid);
190 packet->SetExtension<RtpStreamId>(rsid);
191 return packet;
192 }
193
194 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcRsidRrid(
195 uint32_t ssrc,
196 const std::string& rsid,
197 const std::string& rrid) {
198 RtpPacketReceived::ExtensionManager extension_manager;
199 extension_manager.Register<RtpStreamId>(6);
200 extension_manager.Register<RepairedRtpStreamId>(7);
201
202 auto packet = CreatePacket(ssrc, &extension_manager);
203 packet->SetExtension<RtpStreamId>(rsid);
204 packet->SetExtension<RepairedRtpStreamId>(rrid);
205 return packet;
206 }
207
Steve Anton9e0c7422017-08-18 01:59:47208 RtpDemuxer demuxer_;
209 std::set<RtpPacketSinkInterface*> sinks_to_tear_down_;
210 std::set<SsrcBindingObserver*> observers_to_tear_down_;
211 uint16_t next_sequence_number_ = 1;
212};
213
eladalond0244c22017-06-08 11:19:13214MATCHER_P(SamePacketAs, other, "") {
215 return arg.Ssrc() == other.Ssrc() &&
216 arg.SequenceNumber() == other.SequenceNumber();
eladalon760a0762017-05-31 16:12:25217}
218
Steve Anton9e0c7422017-08-18 01:59:47219TEST_F(RtpDemuxerTest, CanAddSinkBySsrc) {
eladalon5daecca2017-08-04 13:34:54220 MockRtpPacketSink sink;
221 constexpr uint32_t ssrc = 1;
222
Steve Anton9e0c7422017-08-18 01:59:47223 EXPECT_TRUE(AddSinkOnlySsrc(ssrc, &sink));
eladalon5daecca2017-08-04 13:34:54224}
225
Steve Anton53c7ba62017-08-18 17:05:47226TEST_F(RtpDemuxerTest, AllowAddSinkWithOverlappingPayloadTypesIfDifferentMid) {
227 const std::string mid1 = "v";
228 const std::string mid2 = "a";
229 constexpr uint8_t pt1 = 30;
230 constexpr uint8_t pt2 = 31;
231 constexpr uint8_t pt3 = 32;
232
233 RtpDemuxerCriteria pt1_pt2;
234 pt1_pt2.mid = mid1;
235 pt1_pt2.payload_types = {pt1, pt2};
236 MockRtpPacketSink sink1;
237 AddSink(pt1_pt2, &sink1);
238
239 RtpDemuxerCriteria pt1_pt3;
240 pt1_pt2.mid = mid2;
241 pt1_pt3.payload_types = {pt1, pt3};
242 MockRtpPacketSink sink2;
243 EXPECT_TRUE(AddSink(pt1_pt3, &sink2));
244}
245
246TEST_F(RtpDemuxerTest, RejectAddSinkForSameMidOnly) {
247 const std::string mid = "mid";
248
249 MockRtpPacketSink sink;
250 AddSinkOnlyMid(mid, &sink);
251 EXPECT_FALSE(AddSinkOnlyMid(mid, &sink));
252}
253
254TEST_F(RtpDemuxerTest, RejectAddSinkForSameMidRsid) {
255 const std::string mid = "v";
256 const std::string rsid = "1";
257
258 MockRtpPacketSink sink1;
259 AddSinkBothMidRsid(mid, rsid, &sink1);
260
261 MockRtpPacketSink sink2;
262 EXPECT_FALSE(AddSinkBothMidRsid(mid, rsid, &sink2));
263}
264
265TEST_F(RtpDemuxerTest, RejectAddSinkForConflictingMidAndMidRsid) {
266 const std::string mid = "v";
267 const std::string rsid = "1";
268
269 MockRtpPacketSink mid_sink;
270 AddSinkOnlyMid(mid, &mid_sink);
271
272 // This sink would never get any packets routed to it because the above sink
273 // would receive them all.
274 MockRtpPacketSink mid_rsid_sink;
275 EXPECT_FALSE(AddSinkBothMidRsid(mid, rsid, &mid_rsid_sink));
276}
277
278TEST_F(RtpDemuxerTest, RejectAddSinkForConflictingMidRsidAndMid) {
279 const std::string mid = "v";
280 const std::string rsid = "";
281
282 MockRtpPacketSink mid_rsid_sink;
283 AddSinkBothMidRsid(mid, rsid, &mid_rsid_sink);
284
285 // This sink would shadow the above sink.
286 MockRtpPacketSink mid_sink;
287 EXPECT_FALSE(AddSinkOnlyMid(mid, &mid_sink));
288}
289
290TEST_F(RtpDemuxerTest, AddSinkFailsIfCalledForTwoSinksWithSameSsrc) {
291 MockRtpPacketSink sink_a;
292 MockRtpPacketSink sink_b;
293 constexpr uint32_t ssrc = 1;
294 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sink_a));
295
296 EXPECT_FALSE(AddSinkOnlySsrc(ssrc, &sink_b));
297}
298
299TEST_F(RtpDemuxerTest, AddSinkFailsIfCalledTwiceEvenIfSameSinkWithSameSsrc) {
300 MockRtpPacketSink sink;
301 constexpr uint32_t ssrc = 1;
302 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sink));
303
304 EXPECT_FALSE(AddSinkOnlySsrc(ssrc, &sink));
305}
306
307// TODO(steveanton): Currently fails because payload type validation is not
308// complete in AddSink (see note in rtp_demuxer.cc).
309TEST_F(RtpDemuxerTest, DISABLED_RejectAddSinkForSamePayloadTypes) {
310 constexpr uint8_t pt1 = 30;
311 constexpr uint8_t pt2 = 31;
312
313 RtpDemuxerCriteria pt1_pt2;
314 pt1_pt2.payload_types = {pt1, pt2};
315 MockRtpPacketSink sink1;
316 AddSink(pt1_pt2, &sink1);
317
318 RtpDemuxerCriteria pt2_pt1;
319 pt2_pt1.payload_types = {pt2, pt1};
320 MockRtpPacketSink sink2;
321 EXPECT_FALSE(AddSink(pt2_pt1, &sink2));
322}
323
324// Routing Tests
325
Steve Anton9e0c7422017-08-18 01:59:47326TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkBySsrc) {
eladalond0244c22017-06-08 11:19:13327 constexpr uint32_t ssrcs[] = {101, 202, 303};
328 MockRtpPacketSink sinks[arraysize(ssrcs)];
329 for (size_t i = 0; i < arraysize(ssrcs); i++) {
Steve Anton9e0c7422017-08-18 01:59:47330 AddSinkOnlySsrc(ssrcs[i], &sinks[i]);
eladalond0244c22017-06-08 11:19:13331 }
332
333 for (size_t i = 0; i < arraysize(ssrcs); i++) {
Steve Anton9e0c7422017-08-18 01:59:47334 auto packet = CreatePacketWithSsrc(ssrcs[i]);
eladalond0244c22017-06-08 11:19:13335 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47336 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon760a0762017-05-31 16:12:25337 }
338}
339
Steve Anton9e0c7422017-08-18 01:59:47340TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByRsid) {
eladalond0244c22017-06-08 11:19:13341 const std::string rsids[] = {"a", "b", "c"};
342 MockRtpPacketSink sinks[arraysize(rsids)];
343 for (size_t i = 0; i < arraysize(rsids); i++) {
Steve Anton9e0c7422017-08-18 01:59:47344 AddSinkOnlyRsid(rsids[i], &sinks[i]);
eladalond0244c22017-06-08 11:19:13345 }
346
347 for (size_t i = 0; i < arraysize(rsids); i++) {
Yves Gerey665174f2018-06-19 13:03:05348 auto packet =
349 CreatePacketWithSsrcRsid(rtc::checked_cast<uint32_t>(i), rsids[i]);
eladalond0244c22017-06-08 11:19:13350 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47351 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13352 }
353}
354
Steve Anton53c7ba62017-08-18 17:05:47355TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByMid) {
356 const std::string mids[] = {"a", "v", "s"};
357 MockRtpPacketSink sinks[arraysize(mids)];
358 for (size_t i = 0; i < arraysize(mids); i++) {
359 AddSinkOnlyMid(mids[i], &sinks[i]);
360 }
361
362 for (size_t i = 0; i < arraysize(mids); i++) {
Yves Gerey665174f2018-06-19 13:03:05363 auto packet =
364 CreatePacketWithSsrcMid(rtc::checked_cast<uint32_t>(i), mids[i]);
Steve Anton53c7ba62017-08-18 17:05:47365 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
366 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
367 }
368}
369
370TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByMidAndRsid) {
371 const std::string mid = "v";
372 const std::string rsid = "1";
373 constexpr uint32_t ssrc = 10;
374
375 MockRtpPacketSink sink;
376 AddSinkBothMidRsid(mid, rsid, &sink);
377
378 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
379 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
380 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
381}
382
383TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByRepairedRsid) {
384 const std::string rrid = "1";
385 constexpr uint32_t ssrc = 10;
386
387 MockRtpPacketSink sink;
388 AddSinkOnlyRsid(rrid, &sink);
389
390 auto packet_with_rrid = CreatePacketWithSsrcRrid(ssrc, rrid);
391 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_rrid))).Times(1);
392 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_rrid));
393}
394
395TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByPayloadType) {
396 constexpr uint32_t ssrc = 10;
397 constexpr uint8_t payload_type = 30;
398
399 MockRtpPacketSink sink;
400 RtpDemuxerCriteria criteria;
401 criteria.payload_types = {payload_type};
402 AddSink(criteria, &sink);
403
404 auto packet = CreatePacketWithSsrc(ssrc);
405 packet->SetPayloadType(payload_type);
406 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
407 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
408}
409
Steve Anton9e0c7422017-08-18 01:59:47410TEST_F(RtpDemuxerTest, PacketsDeliveredInRightOrder) {
eladalona52722f2017-06-26 18:23:54411 constexpr uint32_t ssrc = 101;
412 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47413 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13414
415 std::unique_ptr<RtpPacketReceived> packets[5];
416 for (size_t i = 0; i < arraysize(packets); i++) {
Steve Anton9e0c7422017-08-18 01:59:47417 packets[i] = CreatePacketWithSsrc(ssrc);
oprypin0826fb22017-08-22 20:57:48418 packets[i]->SetSequenceNumber(rtc::checked_cast<uint16_t>(i));
eladalond0244c22017-06-08 11:19:13419 }
420
421 InSequence sequence;
422 for (const auto& packet : packets) {
eladalona52722f2017-06-26 18:23:54423 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
eladalond0244c22017-06-08 11:19:13424 }
425
426 for (const auto& packet : packets) {
Steve Anton9e0c7422017-08-18 01:59:47427 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13428 }
eladalond0244c22017-06-08 11:19:13429}
430
Steve Anton9e0c7422017-08-18 01:59:47431TEST_F(RtpDemuxerTest, SinkMappedToMultipleSsrcs) {
eladalond0244c22017-06-08 11:19:13432 constexpr uint32_t ssrcs[] = {404, 505, 606};
433 MockRtpPacketSink sink;
434 for (uint32_t ssrc : ssrcs) {
Steve Anton9e0c7422017-08-18 01:59:47435 AddSinkOnlySsrc(ssrc, &sink);
eladalon760a0762017-05-31 16:12:25436 }
437
438 // The sink which is associated with multiple SSRCs gets the callback
439 // triggered for each of those SSRCs.
eladalond0244c22017-06-08 11:19:13440 for (uint32_t ssrc : ssrcs) {
Steve Anton9e0c7422017-08-18 01:59:47441 auto packet = CreatePacketWithSsrc(ssrc);
Steve Anton53c7ba62017-08-18 17:05:47442 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47443 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon760a0762017-05-31 16:12:25444 }
eladalon760a0762017-05-31 16:12:25445}
446
Steve Anton9e0c7422017-08-18 01:59:47447TEST_F(RtpDemuxerTest, NoCallbackOnSsrcSinkRemovedBeforeFirstPacket) {
eladalond0244c22017-06-08 11:19:13448 constexpr uint32_t ssrc = 404;
449 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47450 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13451
Steve Anton9e0c7422017-08-18 01:59:47452 ASSERT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13453
454 // The removed sink does not get callbacks.
Steve Anton9e0c7422017-08-18 01:59:47455 auto packet = CreatePacketWithSsrc(ssrc);
eladalond0244c22017-06-08 11:19:13456 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47457 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13458}
459
Steve Anton9e0c7422017-08-18 01:59:47460TEST_F(RtpDemuxerTest, NoCallbackOnSsrcSinkRemovedAfterFirstPacket) {
eladalond0244c22017-06-08 11:19:13461 constexpr uint32_t ssrc = 404;
462 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:47463 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13464
465 InSequence sequence;
Steve Anton9e0c7422017-08-18 01:59:47466 for (size_t i = 0; i < 10; i++) {
467 ASSERT_TRUE(demuxer_.OnRtpPacket(*CreatePacketWithSsrc(ssrc)));
eladalon760a0762017-05-31 16:12:25468 }
469
Steve Anton9e0c7422017-08-18 01:59:47470 ASSERT_TRUE(RemoveSink(&sink));
eladalon760a0762017-05-31 16:12:25471
eladalond0244c22017-06-08 11:19:13472 // The removed sink does not get callbacks.
Steve Anton9e0c7422017-08-18 01:59:47473 auto packet = CreatePacketWithSsrc(ssrc);
eladalond0244c22017-06-08 11:19:13474 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47475 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13476}
477
eladalon5daecca2017-08-04 13:34:54478// An SSRC may only be mapped to a single sink. However, since configuration
479// of this associations might come from the network, we need to fail gracefully.
Steve Anton9e0c7422017-08-18 01:59:47480TEST_F(RtpDemuxerTest, OnlyOneSinkPerSsrcGetsOnRtpPacketTriggered) {
eladalon5daecca2017-08-04 13:34:54481 MockRtpPacketSink sinks[3];
482 constexpr uint32_t ssrc = 404;
Steve Anton9e0c7422017-08-18 01:59:47483 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sinks[0]));
484 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sinks[1]));
485 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sinks[2]));
eladalon5daecca2017-08-04 13:34:54486
487 // The first sink associated with the SSRC remains active; other sinks
488 // were not really added, and so do not get OnRtpPacket() called.
Steve Anton9e0c7422017-08-18 01:59:47489 auto packet = CreatePacketWithSsrc(ssrc);
eladalon5daecca2017-08-04 13:34:54490 EXPECT_CALL(sinks[0], OnRtpPacket(SamePacketAs(*packet))).Times(1);
491 EXPECT_CALL(sinks[1], OnRtpPacket(_)).Times(0);
492 EXPECT_CALL(sinks[2], OnRtpPacket(_)).Times(0);
Steve Anton9e0c7422017-08-18 01:59:47493 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54494}
495
Steve Anton9e0c7422017-08-18 01:59:47496TEST_F(RtpDemuxerTest, NoRepeatedCallbackOnRepeatedAddSinkForSameSink) {
eladalond0244c22017-06-08 11:19:13497 constexpr uint32_t ssrc = 111;
498 MockRtpPacketSink sink;
499
Steve Anton9e0c7422017-08-18 01:59:47500 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sink));
501 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sink));
eladalond0244c22017-06-08 11:19:13502
Steve Anton9e0c7422017-08-18 01:59:47503 auto packet = CreatePacketWithSsrc(ssrc);
eladalond0244c22017-06-08 11:19:13504 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47505 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13506}
507
Steve Anton9e0c7422017-08-18 01:59:47508TEST_F(RtpDemuxerTest, RemoveSinkReturnsFalseForNeverAddedSink) {
eladalond0244c22017-06-08 11:19:13509 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47510 EXPECT_FALSE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13511}
512
Steve Anton9e0c7422017-08-18 01:59:47513TEST_F(RtpDemuxerTest, RemoveSinkReturnsTrueForPreviouslyAddedSsrcSink) {
eladalond0244c22017-06-08 11:19:13514 constexpr uint32_t ssrc = 101;
515 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47516 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13517
Steve Anton9e0c7422017-08-18 01:59:47518 EXPECT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13519}
520
Steve Anton9e0c7422017-08-18 01:59:47521TEST_F(RtpDemuxerTest,
522 RemoveSinkReturnsTrueForUnresolvedPreviouslyAddedRsidSink) {
eladalond0244c22017-06-08 11:19:13523 const std::string rsid = "a";
524 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47525 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13526
Steve Anton9e0c7422017-08-18 01:59:47527 EXPECT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13528}
529
Steve Anton9e0c7422017-08-18 01:59:47530TEST_F(RtpDemuxerTest,
531 RemoveSinkReturnsTrueForResolvedPreviouslyAddedRsidSink) {
eladalond0244c22017-06-08 11:19:13532 const std::string rsid = "a";
533 constexpr uint32_t ssrc = 101;
534 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:47535 AddSinkOnlyRsid(rsid, &sink);
536 ASSERT_TRUE(demuxer_.OnRtpPacket(*CreatePacketWithSsrcRsid(ssrc, rsid)));
eladalond0244c22017-06-08 11:19:13537
Steve Anton9e0c7422017-08-18 01:59:47538 EXPECT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13539}
540
Steve Anton53c7ba62017-08-18 17:05:47541TEST_F(RtpDemuxerTest, RsidLearnedAndLaterPacketsDeliveredWithOnlySsrc) {
eladalond0244c22017-06-08 11:19:13542 MockRtpPacketSink sink;
543 const std::string rsid = "a";
Steve Anton9e0c7422017-08-18 01:59:47544 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13545
546 // Create a sequence of RTP packets, where only the first one actually
547 // mentions the RSID.
548 std::unique_ptr<RtpPacketReceived> packets[5];
549 constexpr uint32_t rsid_ssrc = 111;
Steve Anton9e0c7422017-08-18 01:59:47550 packets[0] = CreatePacketWithSsrcRsid(rsid_ssrc, rsid);
eladalond0244c22017-06-08 11:19:13551 for (size_t i = 1; i < arraysize(packets); i++) {
Steve Anton9e0c7422017-08-18 01:59:47552 packets[i] = CreatePacketWithSsrc(rsid_ssrc);
eladalon760a0762017-05-31 16:12:25553 }
eladalond0244c22017-06-08 11:19:13554
555 // The first packet associates the RSID with the SSRC, thereby allowing the
556 // demuxer to correctly demux all of the packets.
557 InSequence sequence;
558 for (const auto& packet : packets) {
559 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
560 }
561 for (const auto& packet : packets) {
Steve Anton9e0c7422017-08-18 01:59:47562 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13563 }
eladalond0244c22017-06-08 11:19:13564}
565
Steve Anton9e0c7422017-08-18 01:59:47566TEST_F(RtpDemuxerTest, NoCallbackOnRsidSinkRemovedBeforeFirstPacket) {
eladalond0244c22017-06-08 11:19:13567 MockRtpPacketSink sink;
568 const std::string rsid = "a";
Steve Anton9e0c7422017-08-18 01:59:47569 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13570
571 // Sink removed - it won't get triggers even if packets with its RSID arrive.
Steve Anton9e0c7422017-08-18 01:59:47572 ASSERT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13573
574 constexpr uint32_t ssrc = 111;
Steve Anton9e0c7422017-08-18 01:59:47575 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalond0244c22017-06-08 11:19:13576 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47577 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13578}
579
Steve Anton9e0c7422017-08-18 01:59:47580TEST_F(RtpDemuxerTest, NoCallbackOnRsidSinkRemovedAfterFirstPacket) {
eladalond0244c22017-06-08 11:19:13581 NiceMock<MockRtpPacketSink> sink;
582 const std::string rsid = "a";
Steve Anton9e0c7422017-08-18 01:59:47583 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13584
585 InSequence sequence;
586 constexpr uint32_t ssrc = 111;
Steve Anton9e0c7422017-08-18 01:59:47587 for (size_t i = 0; i < 10; i++) {
588 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
589 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13590 }
591
592 // Sink removed - it won't get triggers even if packets with its RSID arrive.
Steve Anton9e0c7422017-08-18 01:59:47593 ASSERT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13594
Steve Anton9e0c7422017-08-18 01:59:47595 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalond0244c22017-06-08 11:19:13596 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47597 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13598}
599
Steve Anton53c7ba62017-08-18 17:05:47600TEST_F(RtpDemuxerTest, NoCallbackOnMidSinkRemovedBeforeFirstPacket) {
601 const std::string mid = "v";
602 constexpr uint32_t ssrc = 10;
603
604 MockRtpPacketSink sink;
605 AddSinkOnlyMid(mid, &sink);
606 RemoveSink(&sink);
607
608 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
609 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
610 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
611}
612
613TEST_F(RtpDemuxerTest, NoCallbackOnMidSinkRemovedAfterFirstPacket) {
614 const std::string mid = "v";
615 constexpr uint32_t ssrc = 10;
616
617 NiceMock<MockRtpPacketSink> sink;
618 AddSinkOnlyMid(mid, &sink);
619
620 auto p1 = CreatePacketWithSsrcMid(ssrc, mid);
621 demuxer_.OnRtpPacket(*p1);
622
623 RemoveSink(&sink);
624
625 auto p2 = CreatePacketWithSsrcMid(ssrc, mid);
626 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
627 EXPECT_FALSE(demuxer_.OnRtpPacket(*p2));
628}
629
630TEST_F(RtpDemuxerTest, NoCallbackOnMidRsidSinkRemovedAfterFirstPacket) {
631 const std::string mid = "v";
632 const std::string rsid = "1";
633 constexpr uint32_t ssrc = 10;
634
635 NiceMock<MockRtpPacketSink> sink;
636 AddSinkBothMidRsid(mid, rsid, &sink);
637
638 auto p1 = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
639 demuxer_.OnRtpPacket(*p1);
640
641 RemoveSink(&sink);
642
643 auto p2 = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
644 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
645 EXPECT_FALSE(demuxer_.OnRtpPacket(*p2));
646}
647
eladalond0244c22017-06-08 11:19:13648// The RSID to SSRC mapping should be one-to-one. If we end up receiving
649// two (or more) packets with the same SSRC, but different RSIDs, we guarantee
Steve Anton53c7ba62017-08-18 17:05:47650// delivery to one of them but not both.
Steve Anton9e0c7422017-08-18 01:59:47651TEST_F(RtpDemuxerTest, FirstSsrcAssociatedWithAnRsidIsNotForgotten) {
eladalond0244c22017-06-08 11:19:13652 // Each sink has a distinct RSID.
653 MockRtpPacketSink sink_a;
654 const std::string rsid_a = "a";
Steve Anton9e0c7422017-08-18 01:59:47655 AddSinkOnlyRsid(rsid_a, &sink_a);
eladalond0244c22017-06-08 11:19:13656
657 MockRtpPacketSink sink_b;
658 const std::string rsid_b = "b";
Steve Anton9e0c7422017-08-18 01:59:47659 AddSinkOnlyRsid(rsid_b, &sink_b);
eladalond0244c22017-06-08 11:19:13660
661 InSequence sequence; // Verify that the order of delivery is unchanged.
662
663 constexpr uint32_t shared_ssrc = 100;
664
665 // First a packet with |rsid_a| is received, and |sink_a| is associated with
666 // its SSRC.
Steve Anton9e0c7422017-08-18 01:59:47667 auto packet_a = CreatePacketWithSsrcRsid(shared_ssrc, rsid_a);
eladalond0244c22017-06-08 11:19:13668 EXPECT_CALL(sink_a, OnRtpPacket(SamePacketAs(*packet_a))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47669 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_a));
eladalond0244c22017-06-08 11:19:13670
Steve Anton53c7ba62017-08-18 17:05:47671 // Second, a packet with |rsid_b| is received. We guarantee that |sink_b|
672 // receives it.
Steve Anton9e0c7422017-08-18 01:59:47673 auto packet_b = CreatePacketWithSsrcRsid(shared_ssrc, rsid_b);
Steve Anton53c7ba62017-08-18 17:05:47674 EXPECT_CALL(sink_a, OnRtpPacket(_)).Times(0);
675 EXPECT_CALL(sink_b, OnRtpPacket(SamePacketAs(*packet_b))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47676 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_b));
eladalond0244c22017-06-08 11:19:13677
678 // Known edge-case; adding a new RSID association makes us re-examine all
679 // SSRCs. |sink_b| may or may not be associated with the SSRC now; we make
Steve Anton53c7ba62017-08-18 17:05:47680 // no promises on that. However, since the RSID is specified and it cannot be
681 // found the packet should be dropped.
eladalon9addbeb2017-06-30 13:26:54682 MockRtpPacketSink sink_c;
683 const std::string rsid_c = "c";
684 constexpr uint32_t some_other_ssrc = shared_ssrc + 1;
Steve Anton9e0c7422017-08-18 01:59:47685 AddSinkOnlySsrc(some_other_ssrc, &sink_c);
Steve Anton53c7ba62017-08-18 17:05:47686
687 auto packet_c = CreatePacketWithSsrcMid(shared_ssrc, rsid_c);
688 EXPECT_CALL(sink_a, OnRtpPacket(_)).Times(0);
689 EXPECT_CALL(sink_b, OnRtpPacket(_)).Times(0);
690 EXPECT_CALL(sink_c, OnRtpPacket(_)).Times(0);
691 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_c));
eladalond0244c22017-06-08 11:19:13692}
693
Steve Anton9e0c7422017-08-18 01:59:47694TEST_F(RtpDemuxerTest, MultipleRsidsOnSameSink) {
eladalond0244c22017-06-08 11:19:13695 MockRtpPacketSink sink;
696 const std::string rsids[] = {"a", "b", "c"};
697
698 for (const std::string& rsid : rsids) {
Steve Anton9e0c7422017-08-18 01:59:47699 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13700 }
701
702 InSequence sequence;
703 for (size_t i = 0; i < arraysize(rsids); i++) {
704 // Assign different SSRCs and sequence numbers to all packets.
705 const uint32_t ssrc = 1000 + static_cast<uint32_t>(i);
Steve Anton53c7ba62017-08-18 17:05:47706 const uint16_t sequence_number = 50 + static_cast<uint16_t>(i);
Steve Anton9e0c7422017-08-18 01:59:47707 auto packet = CreatePacketWithSsrcRsid(ssrc, rsids[i]);
Steve Anton53c7ba62017-08-18 17:05:47708 packet->SetSequenceNumber(sequence_number);
eladalond0244c22017-06-08 11:19:13709 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47710 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13711 }
eladalond0244c22017-06-08 11:19:13712}
713
Steve Anton53c7ba62017-08-18 17:05:47714// RSIDs are given higher priority than SSRC because we believe senders are less
715// likely to mislabel packets with RSID than mislabel them with SSRCs.
Steve Anton9e0c7422017-08-18 01:59:47716TEST_F(RtpDemuxerTest, SinkWithBothRsidAndSsrcAssociations) {
Steve Anton53c7ba62017-08-18 17:05:47717 MockRtpPacketSink sink;
eladalond0244c22017-06-08 11:19:13718 constexpr uint32_t standalone_ssrc = 10101;
719 constexpr uint32_t rsid_ssrc = 20202;
Steve Anton53c7ba62017-08-18 17:05:47720 const std::string rsid = "1";
eladalond0244c22017-06-08 11:19:13721
Steve Anton9e0c7422017-08-18 01:59:47722 AddSinkOnlySsrc(standalone_ssrc, &sink);
723 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13724
725 InSequence sequence;
726
Steve Anton9e0c7422017-08-18 01:59:47727 auto ssrc_packet = CreatePacketWithSsrc(standalone_ssrc);
eladalond0244c22017-06-08 11:19:13728 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*ssrc_packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47729 EXPECT_TRUE(demuxer_.OnRtpPacket(*ssrc_packet));
eladalond0244c22017-06-08 11:19:13730
Steve Anton9e0c7422017-08-18 01:59:47731 auto rsid_packet = CreatePacketWithSsrcRsid(rsid_ssrc, rsid);
eladalond0244c22017-06-08 11:19:13732 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*rsid_packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47733 EXPECT_TRUE(demuxer_.OnRtpPacket(*rsid_packet));
eladalond0244c22017-06-08 11:19:13734}
735
Steve Anton53c7ba62017-08-18 17:05:47736// Packets are always guaranteed to be routed to only one sink.
Steve Anton9e0c7422017-08-18 01:59:47737TEST_F(RtpDemuxerTest, AssociatingByRsidAndBySsrcCannotTriggerDoubleCall) {
eladalond0244c22017-06-08 11:19:13738 constexpr uint32_t ssrc = 10101;
eladalond0244c22017-06-08 11:19:13739 const std::string rsid = "a";
eladalond0244c22017-06-08 11:19:13740
Steve Anton9e0c7422017-08-18 01:59:47741 MockRtpPacketSink sink;
742 AddSinkOnlySsrc(ssrc, &sink);
743 AddSinkOnlyRsid(rsid, &sink);
744
745 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalond0244c22017-06-08 11:19:13746 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47747 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon760a0762017-05-31 16:12:25748}
749
Steve Anton53c7ba62017-08-18 17:05:47750TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToMid) {
751 const std::string mid = "v";
752 constexpr uint32_t ssrc = 10;
753
754 NiceMock<MockRtpPacketSink> sink;
755 AddSinkOnlyMid(mid, &sink);
756
757 MockSsrcBindingObserver observer;
758 RegisterSsrcBindingObserver(&observer);
759
760 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
761 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc));
762 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
763}
764
765TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToRsid) {
766 const std::string rsid = "1";
eladalona52722f2017-06-26 18:23:54767 constexpr uint32_t ssrc = 111;
eladalona52722f2017-06-26 18:23:54768
eladalon5daecca2017-08-04 13:34:54769 // Only RSIDs which the demuxer knows may be resolved.
770 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:47771 AddSinkOnlyRsid(rsid, &sink);
eladalon5daecca2017-08-04 13:34:54772
Steve Anton53c7ba62017-08-18 17:05:47773 NiceMock<MockSsrcBindingObserver> rsid_resolution_observers[3];
eladalona52722f2017-06-26 18:23:54774 for (auto& observer : rsid_resolution_observers) {
Steve Anton9e0c7422017-08-18 01:59:47775 RegisterSsrcBindingObserver(&observer);
Steve Antonb3329172017-08-17 22:23:51776 EXPECT_CALL(observer, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
eladalona52722f2017-06-26 18:23:54777 }
778
Steve Antonb3329172017-08-17 22:23:51779 // The expected calls to OnSsrcBoundToRsid() will be triggered by this.
Steve Anton53c7ba62017-08-18 17:05:47780 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
781 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54782}
783
Steve Anton53c7ba62017-08-18 17:05:47784TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToMidRsid) {
785 const std::string mid = "v";
786 const std::string rsid = "1";
787 constexpr uint32_t ssrc = 10;
eladalon5daecca2017-08-04 13:34:54788
Steve Anton53c7ba62017-08-18 17:05:47789 NiceMock<MockRtpPacketSink> sink;
790 AddSinkBothMidRsid(mid, rsid, &sink);
eladalon5daecca2017-08-04 13:34:54791
Steve Anton53c7ba62017-08-18 17:05:47792 MockSsrcBindingObserver observer;
793 RegisterSsrcBindingObserver(&observer);
794
795 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
796 EXPECT_CALL(observer, OnSsrcBoundToMidRsid(mid, rsid, ssrc));
797 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54798}
799
Steve Anton53c7ba62017-08-18 17:05:47800TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToPayloadType) {
801 constexpr uint8_t payload_type = 3;
802 constexpr uint32_t ssrc = 10;
803
804 RtpDemuxerCriteria criteria;
805 criteria.payload_types = {payload_type};
806 NiceMock<MockRtpPacketSink> sink;
807 AddSink(criteria, &sink);
808
809 MockSsrcBindingObserver observer;
810 RegisterSsrcBindingObserver(&observer);
811
812 auto packet = CreatePacketWithSsrc(ssrc);
813 packet->SetPayloadType(payload_type);
814 EXPECT_CALL(observer, OnSsrcBoundToPayloadType(payload_type, ssrc));
815 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
816}
817
818// If one sink is associated with SSRC x, and another sink with RSID y, then if
819// we receive a packet with both SSRC x and RSID y, route that to only the sink
820// for RSID y since we believe RSID tags to be more trustworthy than signaled
821// SSRCs.
Steve Anton9e0c7422017-08-18 01:59:47822TEST_F(RtpDemuxerTest,
Steve Anton53c7ba62017-08-18 17:05:47823 PacketFittingBothRsidSinkAndSsrcSinkGivenOnlyToRsidSink) {
eladalon5daecca2017-08-04 13:34:54824 constexpr uint32_t ssrc = 111;
825 MockRtpPacketSink ssrc_sink;
Steve Anton9e0c7422017-08-18 01:59:47826 AddSinkOnlySsrc(ssrc, &ssrc_sink);
eladalon5daecca2017-08-04 13:34:54827
828 const std::string rsid = "a";
829 MockRtpPacketSink rsid_sink;
Steve Anton9e0c7422017-08-18 01:59:47830 AddSinkOnlyRsid(rsid, &rsid_sink);
eladalon5daecca2017-08-04 13:34:54831
Steve Anton9e0c7422017-08-18 01:59:47832 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalon5daecca2017-08-04 13:34:54833
Steve Anton53c7ba62017-08-18 17:05:47834 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
835 EXPECT_CALL(rsid_sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
836 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54837}
838
839// We're not expecting RSIDs to be resolved to SSRCs which were previously
840// mapped to sinks, and make no guarantees except for graceful handling.
Steve Anton9e0c7422017-08-18 01:59:47841TEST_F(RtpDemuxerTest,
842 GracefullyHandleRsidBeingMappedToPrevouslyAssociatedSsrc) {
eladalon5daecca2017-08-04 13:34:54843 constexpr uint32_t ssrc = 111;
844 NiceMock<MockRtpPacketSink> ssrc_sink;
Steve Anton9e0c7422017-08-18 01:59:47845 AddSinkOnlySsrc(ssrc, &ssrc_sink);
eladalon5daecca2017-08-04 13:34:54846
847 const std::string rsid = "a";
Steve Anton53c7ba62017-08-18 17:05:47848 NiceMock<MockRtpPacketSink> rsid_sink;
Steve Anton9e0c7422017-08-18 01:59:47849 AddSinkOnlyRsid(rsid, &rsid_sink);
eladalon5daecca2017-08-04 13:34:54850
Steve Anton53c7ba62017-08-18 17:05:47851 NiceMock<MockSsrcBindingObserver> observer;
Steve Anton9e0c7422017-08-18 01:59:47852 RegisterSsrcBindingObserver(&observer);
eladalon5daecca2017-08-04 13:34:54853
854 // The SSRC was mapped to an SSRC sink, but was even active (packets flowed
855 // over it).
Steve Anton9e0c7422017-08-18 01:59:47856 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
857 demuxer_.OnRtpPacket(*packet);
eladalon5daecca2017-08-04 13:34:54858
859 // If the SSRC sink is ever removed, the RSID sink *might* receive indications
860 // of packets, and observers *might* be informed. Only graceful handling
861 // is guaranteed.
Steve Anton9e0c7422017-08-18 01:59:47862 RemoveSink(&ssrc_sink);
eladalon5daecca2017-08-04 13:34:54863 EXPECT_CALL(rsid_sink, OnRtpPacket(SamePacketAs(*packet))).Times(AtLeast(0));
Steve Antonb3329172017-08-17 22:23:51864 EXPECT_CALL(observer, OnSsrcBoundToRsid(rsid, ssrc)).Times(AtLeast(0));
Steve Anton53c7ba62017-08-18 17:05:47865 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
866}
867
868// Tests that when one MID sink is configured, packets that include the MID
869// extension will get routed to that sink and any packets that use the same
870// SSRC as one of those packets later will also get routed to the sink, even
871// if a new SSRC is introduced for the same MID.
872TEST_F(RtpDemuxerTest, RoutedByMidWhenSsrcAdded) {
873 const std::string mid = "v";
874 NiceMock<MockRtpPacketSink> sink;
875 AddSinkOnlyMid(mid, &sink);
876
877 constexpr uint32_t ssrc1 = 10;
878 constexpr uint32_t ssrc2 = 11;
879
880 auto packet_ssrc1_mid = CreatePacketWithSsrcMid(ssrc1, mid);
881 demuxer_.OnRtpPacket(*packet_ssrc1_mid);
882 auto packet_ssrc2_mid = CreatePacketWithSsrcMid(ssrc2, mid);
883 demuxer_.OnRtpPacket(*packet_ssrc2_mid);
884
885 auto packet_ssrc1_only = CreatePacketWithSsrc(ssrc1);
886 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc1_only))).Times(1);
887 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc1_only));
888
889 auto packet_ssrc2_only = CreatePacketWithSsrc(ssrc2);
890 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc2_only))).Times(1);
891 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc2_only));
892}
893
894TEST_F(RtpDemuxerTest, DontLearnMidSsrcBindingBeforeSinkAdded) {
895 const std::string mid = "v";
896 constexpr uint32_t ssrc = 10;
897
898 auto packet_ssrc_mid = CreatePacketWithSsrcMid(ssrc, mid);
899 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_ssrc_mid));
900
901 MockRtpPacketSink sink;
902 AddSinkOnlyMid(mid, &sink);
903
904 auto packet_ssrc_only = CreatePacketWithSsrc(ssrc);
905 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
906 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_ssrc_only));
907}
908
909TEST_F(RtpDemuxerTest, DontForgetMidSsrcBindingWhenSinkRemoved) {
910 const std::string mid = "v";
911 constexpr uint32_t ssrc = 10;
912
913 NiceMock<MockRtpPacketSink> sink1;
914 AddSinkOnlyMid(mid, &sink1);
915
916 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
917 demuxer_.OnRtpPacket(*packet_with_mid);
918
919 RemoveSink(&sink1);
920
921 MockRtpPacketSink sink2;
922 AddSinkOnlyMid(mid, &sink2);
923
924 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
925 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
926 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
927}
928
929// If a sink is added with only a MID, then any packet with that MID no matter
930// the RSID should be routed to that sink.
931TEST_F(RtpDemuxerTest, RoutedByMidWithAnyRsid) {
932 const std::string mid = "v";
933 const std::string rsid1 = "1";
934 const std::string rsid2 = "2";
935 constexpr uint32_t ssrc1 = 10;
936 constexpr uint32_t ssrc2 = 11;
937
938 MockRtpPacketSink sink;
939 AddSinkOnlyMid(mid, &sink);
940
941 InSequence sequence;
942
943 auto packet_ssrc1_rsid1 = CreatePacketWithSsrcMidRsid(ssrc1, mid, rsid1);
944 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc1_rsid1))).Times(1);
945 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc1_rsid1));
946
947 auto packet_ssrc2_rsid2 = CreatePacketWithSsrcMidRsid(ssrc2, mid, rsid2);
948 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc2_rsid2))).Times(1);
949 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc2_rsid2));
950}
951
952// These two tests verify that for a sink added with a MID, RSID pair, if the
953// MID and RSID are learned in separate packets (e.g., because the header
954// extensions are sent separately), then a later packet with just SSRC will get
955// routed to that sink.
956// The first test checks that the functionality works when MID is learned first.
957// The second test checks that the functionality works when RSID is learned
958// first.
959TEST_F(RtpDemuxerTest, LearnMidThenRsidSeparatelyAndRouteBySsrc) {
960 const std::string mid = "v";
961 const std::string rsid = "1";
962 constexpr uint32_t ssrc = 10;
963
964 NiceMock<MockRtpPacketSink> sink;
965 AddSinkBothMidRsid(mid, rsid, &sink);
966
967 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
968 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_mid));
969
970 auto packet_with_rsid = CreatePacketWithSsrcRsid(ssrc, rsid);
971 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_rsid));
972
973 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
974 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
975 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
976}
977
978TEST_F(RtpDemuxerTest, LearnRsidThenMidSeparatelyAndRouteBySsrc) {
979 const std::string mid = "v";
980 const std::string rsid = "1";
981 constexpr uint32_t ssrc = 10;
982
983 NiceMock<MockRtpPacketSink> sink;
984 AddSinkBothMidRsid(mid, rsid, &sink);
985
986 auto packet_with_rsid = CreatePacketWithSsrcRsid(ssrc, rsid);
987 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_rsid));
988
989 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
990 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_mid));
991
992 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
993 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
994 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
995}
996
997TEST_F(RtpDemuxerTest, DontLearnMidRsidBindingBeforeSinkAdded) {
998 const std::string mid = "v";
999 const std::string rsid = "1";
1000 constexpr uint32_t ssrc = 10;
1001
1002 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
1003 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_both));
1004
1005 MockRtpPacketSink sink;
1006 AddSinkBothMidRsid(mid, rsid, &sink);
1007
1008 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1009 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1010 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1011}
1012
1013TEST_F(RtpDemuxerTest, DontForgetMidRsidBindingWhenSinkRemoved) {
1014 const std::string mid = "v";
1015 const std::string rsid = "1";
1016 constexpr uint32_t ssrc = 10;
1017
1018 NiceMock<MockRtpPacketSink> sink1;
1019 AddSinkBothMidRsid(mid, rsid, &sink1);
1020
1021 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
1022 demuxer_.OnRtpPacket(*packet_with_both);
1023
1024 RemoveSink(&sink1);
1025
1026 MockRtpPacketSink sink2;
1027 AddSinkBothMidRsid(mid, rsid, &sink2);
1028
1029 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1030 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
1031 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1032}
1033
1034TEST_F(RtpDemuxerTest, LearnMidRsidBindingAfterSinkAdded) {
1035 const std::string mid = "v";
1036 const std::string rsid = "1";
1037 constexpr uint32_t ssrc = 10;
1038
1039 NiceMock<MockRtpPacketSink> sink;
1040 AddSinkBothMidRsid(mid, rsid, &sink);
1041
1042 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
1043 demuxer_.OnRtpPacket(*packet_with_both);
1044
1045 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1046 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
1047 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1048}
1049
1050TEST_F(RtpDemuxerTest, DropByPayloadTypeIfNoSink) {
1051 constexpr uint8_t payload_type = 30;
1052 constexpr uint32_t ssrc = 10;
1053
1054 auto packet = CreatePacketWithSsrc(ssrc);
1055 packet->SetPayloadType(payload_type);
1056 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1057}
1058
1059// For legacy applications, it's possible for us to demux if the payload type is
1060// unique. But if multiple sinks are registered with different MIDs and the same
1061// payload types, then we cannot route a packet with just payload type because
1062// it is ambiguous which sink it should be sent to.
1063TEST_F(RtpDemuxerTest, DropByPayloadTypeIfAddedInMultipleSinks) {
1064 const std::string mid1 = "v";
1065 const std::string mid2 = "a";
1066 constexpr uint8_t payload_type = 30;
1067 constexpr uint32_t ssrc = 10;
1068
1069 RtpDemuxerCriteria mid1_pt;
1070 mid1_pt.mid = mid1;
1071 mid1_pt.payload_types = {payload_type};
1072 MockRtpPacketSink sink1;
1073 AddSink(mid1_pt, &sink1);
1074
1075 RtpDemuxerCriteria mid2_pt;
1076 mid2_pt.mid = mid2;
1077 mid2_pt.payload_types = {payload_type};
1078 MockRtpPacketSink sink2;
1079 AddSink(mid2_pt, &sink2);
1080
1081 auto packet = CreatePacketWithSsrc(ssrc);
1082 packet->SetPayloadType(payload_type);
1083
1084 EXPECT_CALL(sink1, OnRtpPacket(_)).Times(0);
1085 EXPECT_CALL(sink2, OnRtpPacket(_)).Times(0);
1086 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1087}
1088
1089// If two sinks are added with different MIDs but the same payload types, then
1090// we cannot demux on the payload type only unless one of the sinks is removed.
1091TEST_F(RtpDemuxerTest, RoutedByPayloadTypeIfAmbiguousSinkRemoved) {
1092 const std::string mid1 = "v";
1093 const std::string mid2 = "a";
1094 constexpr uint8_t payload_type = 30;
1095 constexpr uint32_t ssrc = 10;
1096
1097 RtpDemuxerCriteria mid1_pt;
1098 mid1_pt.mid = mid1;
1099 mid1_pt.payload_types = {payload_type};
1100 MockRtpPacketSink sink1;
1101 AddSink(mid1_pt, &sink1);
1102
1103 RtpDemuxerCriteria mid2_pt;
1104 mid2_pt.mid = mid2;
1105 mid2_pt.payload_types = {payload_type};
1106 MockRtpPacketSink sink2;
1107 AddSink(mid2_pt, &sink2);
1108
1109 RemoveSink(&sink1);
1110
1111 auto packet = CreatePacketWithSsrc(ssrc);
1112 packet->SetPayloadType(payload_type);
1113
1114 EXPECT_CALL(sink1, OnRtpPacket(_)).Times(0);
1115 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1116
1117 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1118}
1119
1120TEST_F(RtpDemuxerTest, RoutedByPayloadTypeLatchesSsrc) {
1121 constexpr uint8_t payload_type = 30;
1122 constexpr uint32_t ssrc = 10;
1123
1124 RtpDemuxerCriteria pt;
1125 pt.payload_types = {payload_type};
1126 NiceMock<MockRtpPacketSink> sink;
1127 AddSink(pt, &sink);
1128
1129 auto packet_with_pt = CreatePacketWithSsrc(ssrc);
1130 packet_with_pt->SetPayloadType(payload_type);
1131 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt));
1132
1133 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1134 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
1135 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1136}
1137
1138// RSIDs are scoped within MID, so if two sinks are registered with the same
1139// RSIDs but different MIDs, then packets containing both extensions should be
1140// routed to the correct one.
1141TEST_F(RtpDemuxerTest, PacketWithSameRsidDifferentMidRoutedToProperSink) {
1142 const std::string mid1 = "mid1";
1143 const std::string mid2 = "mid2";
1144 const std::string rsid = "rsid";
1145 constexpr uint32_t ssrc1 = 10;
1146 constexpr uint32_t ssrc2 = 11;
1147
1148 NiceMock<MockRtpPacketSink> mid1_sink;
1149 AddSinkBothMidRsid(mid1, rsid, &mid1_sink);
1150
1151 MockRtpPacketSink mid2_sink;
1152 AddSinkBothMidRsid(mid2, rsid, &mid2_sink);
1153
1154 auto packet_mid1 = CreatePacketWithSsrcMidRsid(ssrc1, mid1, rsid);
1155 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_mid1));
1156
1157 auto packet_mid2 = CreatePacketWithSsrcMidRsid(ssrc2, mid2, rsid);
1158 EXPECT_CALL(mid2_sink, OnRtpPacket(SamePacketAs(*packet_mid2))).Times(1);
1159 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_mid2));
1160}
1161
1162// If a sink is first bound to a given SSRC by signaling but later a new sink is
1163// bound to a given MID by a later signaling, then when a packet arrives with
1164// both the SSRC and MID, then the signaled MID sink should take precedence.
1165TEST_F(RtpDemuxerTest, SignaledMidShouldOverwriteSignaledSsrc) {
1166 constexpr uint32_t ssrc = 11;
1167 const std::string mid = "mid";
1168
1169 MockRtpPacketSink ssrc_sink;
1170 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1171
1172 MockRtpPacketSink mid_sink;
1173 AddSinkOnlyMid(mid, &mid_sink);
1174
1175 auto p = CreatePacketWithSsrcMid(ssrc, mid);
1176 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
1177 EXPECT_CALL(mid_sink, OnRtpPacket(SamePacketAs(*p))).Times(1);
1178 EXPECT_TRUE(demuxer_.OnRtpPacket(*p));
1179}
1180
1181// Extends the previous test to also ensure that later packets that do not
1182// specify MID are still routed to the MID sink rather than the overwritten SSRC
1183// sink.
1184TEST_F(RtpDemuxerTest, SignaledMidShouldOverwriteSignalledSsrcPersistent) {
1185 constexpr uint32_t ssrc = 11;
1186 const std::string mid = "mid";
1187
1188 MockRtpPacketSink ssrc_sink;
1189 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1190
1191 NiceMock<MockRtpPacketSink> mid_sink;
1192 AddSinkOnlyMid(mid, &mid_sink);
1193
1194 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
1195
1196 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
1197 demuxer_.OnRtpPacket(*packet_with_mid);
1198
1199 auto packet_without_mid = CreatePacketWithSsrc(ssrc);
1200 EXPECT_CALL(mid_sink, OnRtpPacket(SamePacketAs(*packet_without_mid)))
1201 .Times(1);
1202 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_without_mid));
1203}
1204
1205TEST_F(RtpDemuxerTest, RouteByPayloadTypeMultipleMatch) {
1206 constexpr uint32_t ssrc = 10;
1207 constexpr uint8_t pt1 = 30;
1208 constexpr uint8_t pt2 = 31;
1209
1210 MockRtpPacketSink sink;
1211 RtpDemuxerCriteria criteria;
1212 criteria.payload_types = {pt1, pt2};
1213 AddSink(criteria, &sink);
1214
1215 auto packet_with_pt1 = CreatePacketWithSsrc(ssrc);
1216 packet_with_pt1->SetPayloadType(pt1);
1217 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_pt1)));
1218 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt1));
1219
1220 auto packet_with_pt2 = CreatePacketWithSsrc(ssrc);
1221 packet_with_pt2->SetPayloadType(pt2);
1222 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_pt2)));
1223 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt2));
1224}
1225
1226TEST_F(RtpDemuxerTest, DontDemuxOnMidAloneIfAddedWithRsid) {
1227 const std::string mid = "v";
1228 const std::string rsid = "1";
1229 constexpr uint32_t ssrc = 10;
1230
1231 MockRtpPacketSink sink;
1232 AddSinkBothMidRsid(mid, rsid, &sink);
1233
1234 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1235
1236 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
1237 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1238}
1239
1240TEST_F(RtpDemuxerTest, DemuxBySsrcEvenWithMidAndRsid) {
1241 const std::string mid = "v";
1242 const std::string rsid = "1";
1243 constexpr uint32_t ssrc = 10;
1244
1245 RtpDemuxerCriteria criteria;
1246 criteria.rsid = rsid;
1247 criteria.mid = mid;
1248 criteria.ssrcs = {ssrc};
1249 MockRtpPacketSink sink;
1250 AddSink(criteria, &sink);
1251
1252 auto packet = CreatePacketWithSsrc(ssrc);
1253 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1254 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1255}
1256
1257// In slight deviation from the BUNDLE spec, if we match a sink according to
1258// SSRC, then we do not verify payload type against the criteria and defer to
1259// the sink to check that it is correct.
1260TEST_F(RtpDemuxerTest, DoNotCheckPayloadTypeIfMatchedByOtherCriteria) {
1261 constexpr uint32_t ssrc = 10;
1262 constexpr uint8_t payload_type = 30;
1263 constexpr uint8_t different_payload_type = payload_type + 1;
1264
1265 RtpDemuxerCriteria criteria;
1266 criteria.ssrcs = {ssrc};
1267 criteria.payload_types = {payload_type};
1268 MockRtpPacketSink sink;
1269 AddSink(criteria, &sink);
1270
1271 auto packet = CreatePacketWithSsrc(ssrc);
1272 packet->SetPayloadType(different_payload_type);
1273 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1274 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1275}
1276
1277// If a repair packet includes an RSID it should be ignored and the packet
1278// should be routed by its RRID.
1279TEST_F(RtpDemuxerTest, PacketWithRsidAndRridRoutedByRrid) {
1280 const std::string rsid = "1";
1281 const std::string rrid = "1r";
1282 constexpr uint32_t ssrc = 10;
1283
1284 MockRtpPacketSink sink_rsid;
1285 AddSinkOnlyRsid(rsid, &sink_rsid);
1286
1287 MockRtpPacketSink sink_rrid;
1288 AddSinkOnlyRsid(rrid, &sink_rrid);
1289
1290 auto packet = CreatePacketWithSsrcRsidRrid(ssrc, rsid, rrid);
1291 EXPECT_CALL(sink_rsid, OnRtpPacket(_)).Times(0);
1292 EXPECT_CALL(sink_rrid, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1293 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1294}
1295
1296// Same test as above but checks that the latched SSRC routes to the RRID sink.
1297TEST_F(RtpDemuxerTest, PacketWithRsidAndRridLatchesSsrcToRrid) {
1298 const std::string rsid = "1";
1299 const std::string rrid = "1r";
1300 constexpr uint32_t ssrc = 10;
1301
1302 MockRtpPacketSink sink_rsid;
1303 AddSinkOnlyRsid(rsid, &sink_rsid);
1304
1305 NiceMock<MockRtpPacketSink> sink_rrid;
1306 AddSinkOnlyRsid(rrid, &sink_rrid);
1307
1308 auto packet_rsid_rrid = CreatePacketWithSsrcRsidRrid(ssrc, rsid, rrid);
1309 demuxer_.OnRtpPacket(*packet_rsid_rrid);
1310
1311 auto packet_ssrc_only = CreatePacketWithSsrc(ssrc);
1312 EXPECT_CALL(sink_rsid, OnRtpPacket(_)).Times(0);
1313 EXPECT_CALL(sink_rrid, OnRtpPacket(SamePacketAs(*packet_ssrc_only))).Times(1);
1314 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc_only));
1315}
1316
1317// Tests that a packet which includes MID and RSID is dropped and not routed by
1318// SSRC if the MID and RSID do not match an added sink.
1319TEST_F(RtpDemuxerTest, PacketWithMidAndUnknownRsidIsNotRoutedBySsrc) {
1320 constexpr uint32_t ssrc = 10;
1321 const std::string mid = "v";
1322 const std::string rsid = "1";
1323 const std::string wrong_rsid = "2";
1324
1325 RtpDemuxerCriteria criteria;
1326 criteria.mid = mid;
1327 criteria.rsid = rsid;
1328 criteria.ssrcs = {ssrc};
1329 MockRtpPacketSink sink;
1330 AddSink(criteria, &sink);
1331
1332 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, wrong_rsid);
1333 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1334 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1335}
1336
1337// Tests that a packet which includes MID and RSID is dropped and not routed by
1338// payload type if the MID and RSID do not match an added sink.
1339TEST_F(RtpDemuxerTest, PacketWithMidAndUnknownRsidIsNotRoutedByPayloadType) {
1340 constexpr uint32_t ssrc = 10;
1341 const std::string mid = "v";
1342 const std::string rsid = "1";
1343 const std::string wrong_rsid = "2";
1344 constexpr uint8_t payload_type = 30;
1345
1346 RtpDemuxerCriteria criteria;
1347 criteria.mid = mid;
1348 criteria.rsid = rsid;
1349 criteria.payload_types = {payload_type};
1350 MockRtpPacketSink sink;
1351 AddSink(criteria, &sink);
1352
1353 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, wrong_rsid);
1354 packet->SetPayloadType(payload_type);
1355 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1356 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1357}
1358
1359// Observers are only notified of an SSRC binding to an RSID if we care about
1360// the RSID (i.e., have a sink added for that RSID).
1361TEST_F(RtpDemuxerTest, ObserversNotNotifiedOfUntrackedRsids) {
1362 const std::string rsid = "1";
1363 constexpr uint32_t ssrc = 111;
1364
1365 MockSsrcBindingObserver rsid_resolution_observers[3];
1366 for (auto& observer : rsid_resolution_observers) {
1367 RegisterSsrcBindingObserver(&observer);
1368 EXPECT_CALL(observer, OnSsrcBoundToRsid(_, _)).Times(0);
1369 }
1370
1371 // Since no sink is registered for this SSRC/RSID, expect the packet to not be
1372 // routed and no observers notified of the SSRC -> RSID binding.
1373 EXPECT_FALSE(demuxer_.OnRtpPacket(*CreatePacketWithSsrcRsid(ssrc, rsid)));
1374}
1375
1376// Ensure that observers are notified of SSRC bindings only once per unique
1377// binding source (e.g., SSRC -> MID, SSRC -> RSID, etc.)
1378TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundtoMidOnlyOnce) {
1379 const std::string mid = "v";
1380 constexpr uint32_t ssrc = 10;
1381
1382 NiceMock<MockRtpPacketSink> sink;
1383 AddSinkOnlyMid(mid, &sink);
1384
1385 MockSsrcBindingObserver observer;
1386 RegisterSsrcBindingObserver(&observer);
1387
1388 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc)).Times(1);
1389
1390 demuxer_.OnRtpPacket(*CreatePacketWithSsrcMid(ssrc, mid));
1391 demuxer_.OnRtpPacket(*CreatePacketWithSsrcMid(ssrc, mid));
1392}
1393
1394// Ensure that when a new SSRC -> MID binding is discovered observers are also
1395// notified of that, even if there has already been an SSRC bound to the MID.
1396TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundtoMidWhenSsrcChanges) {
1397 const std::string mid = "v";
1398 constexpr uint32_t ssrc1 = 10;
1399 constexpr uint32_t ssrc2 = 11;
1400
1401 NiceMock<MockRtpPacketSink> sink;
1402 AddSinkOnlyMid(mid, &sink);
1403
1404 MockSsrcBindingObserver observer;
1405 RegisterSsrcBindingObserver(&observer);
1406
1407 InSequence seq;
1408 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc1)).Times(1);
1409 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc2)).Times(1);
1410
1411 auto p1 = CreatePacketWithSsrcMid(ssrc1, mid);
1412 demuxer_.OnRtpPacket(*p1);
1413
1414 auto p2 = CreatePacketWithSsrcMid(ssrc2, mid);
1415 demuxer_.OnRtpPacket(*p2);
eladalona52722f2017-06-26 18:23:541416}
1417
Steve Anton9e0c7422017-08-18 01:59:471418TEST_F(RtpDemuxerTest, DeregisteredRsidObserversNotInformedOfResolutions) {
eladalona52722f2017-06-26 18:23:541419 constexpr uint32_t ssrc = 111;
1420 const std::string rsid = "a";
1421 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:471422 AddSinkOnlyRsid(rsid, &sink);
eladalona52722f2017-06-26 18:23:541423
1424 // Register several, then deregister only one, to show that not all of the
1425 // observers had been forgotten when one was removed.
Steve Antonb3329172017-08-17 22:23:511426 MockSsrcBindingObserver observer_1;
1427 MockSsrcBindingObserver observer_2_removed;
1428 MockSsrcBindingObserver observer_3;
eladalona52722f2017-06-26 18:23:541429
Steve Anton9e0c7422017-08-18 01:59:471430 RegisterSsrcBindingObserver(&observer_1);
1431 RegisterSsrcBindingObserver(&observer_2_removed);
1432 RegisterSsrcBindingObserver(&observer_3);
eladalona52722f2017-06-26 18:23:541433
Steve Anton9e0c7422017-08-18 01:59:471434 DeregisterSsrcBindingObserver(&observer_2_removed);
eladalona52722f2017-06-26 18:23:541435
Steve Antonb3329172017-08-17 22:23:511436 EXPECT_CALL(observer_1, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
1437 EXPECT_CALL(observer_2_removed, OnSsrcBoundToRsid(_, _)).Times(0);
1438 EXPECT_CALL(observer_3, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
eladalona52722f2017-06-26 18:23:541439
Steve Antonb3329172017-08-17 22:23:511440 // The expected calls to OnSsrcBoundToRsid() will be triggered by this.
Steve Anton9e0c7422017-08-18 01:59:471441 demuxer_.OnRtpPacket(*CreatePacketWithSsrcRsid(ssrc, rsid));
eladalona52722f2017-06-26 18:23:541442}
1443
Steve Anton53c7ba62017-08-18 17:05:471444TEST_F(RtpDemuxerTest,
1445 PacketFittingBothRsidSinkAndSsrcSinkTriggersResolutionCallbacks) {
1446 constexpr uint32_t ssrc = 111;
1447 NiceMock<MockRtpPacketSink> ssrc_sink;
1448 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1449
1450 const std::string rsid = "a";
1451 NiceMock<MockRtpPacketSink> rsid_sink;
1452 AddSinkOnlyRsid(rsid, &rsid_sink);
1453
1454 MockSsrcBindingObserver observer;
1455 RegisterSsrcBindingObserver(&observer);
1456
1457 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
1458 EXPECT_CALL(observer, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
1459 demuxer_.OnRtpPacket(*packet);
1460}
1461
1462TEST_F(RtpDemuxerTest, MaliciousPeerCannotCauseMemoryOveruse) {
1463 const std::string mid = "v";
1464
1465 NiceMock<MockRtpPacketSink> sink;
1466 AddSinkOnlyMid(mid, &sink);
1467
1468 MockSsrcBindingObserver observer;
1469 RegisterSsrcBindingObserver(&observer);
1470
1471 EXPECT_CALL(observer, OnSsrcBoundToMid(_, _))
1472 .Times(AtMost(RtpDemuxer::kMaxSsrcBindings));
1473
1474 for (int i = 0; i < RtpDemuxer::kMaxSsrcBindings + 1; i++) {
1475 auto packet = CreatePacketWithSsrcMid(i, mid);
1476 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1477 }
1478}
1479
eladalon760a0762017-05-31 16:12:251480#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
Steve Anton53c7ba62017-08-18 17:05:471481
1482TEST_F(RtpDemuxerTest, CriteriaMustBeNonEmpty) {
eladalond0244c22017-06-08 11:19:131483 MockRtpPacketSink sink;
Steve Anton53c7ba62017-08-18 17:05:471484 RtpDemuxerCriteria criteria;
1485 EXPECT_DEATH(AddSink(criteria, &sink), "");
eladalond0244c22017-06-08 11:19:131486}
1487
Steve Anton9e0c7422017-08-18 01:59:471488TEST_F(RtpDemuxerTest, RsidMustBeAlphaNumeric) {
eladalond0244c22017-06-08 11:19:131489 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:471490 EXPECT_DEATH(AddSinkOnlyRsid("a_3", &sink), "");
eladalond0244c22017-06-08 11:19:131491}
1492
Joachim Bauchd3b7ec22018-08-01 08:12:001493TEST_F(RtpDemuxerTest, MidMustBeToken) {
Steve Anton53c7ba62017-08-18 17:05:471494 MockRtpPacketSink sink;
Joachim Bauchd3b7ec22018-08-01 08:12:001495 EXPECT_DEATH(AddSinkOnlyMid("a(3)", &sink), "");
Steve Anton53c7ba62017-08-18 17:05:471496}
1497
Steve Anton9e0c7422017-08-18 01:59:471498TEST_F(RtpDemuxerTest, RsidMustNotExceedMaximumLength) {
eladalond0244c22017-06-08 11:19:131499 MockRtpPacketSink sink;
Niels Möllerd57efc12019-03-22 13:02:111500 std::string rsid(BaseRtpStringExtension::kMaxValueSizeBytes + 1, 'a');
Steve Anton9e0c7422017-08-18 01:59:471501 EXPECT_DEATH(AddSinkOnlyRsid(rsid, &sink), "");
eladalond0244c22017-06-08 11:19:131502}
1503
Steve Anton53c7ba62017-08-18 17:05:471504TEST_F(RtpDemuxerTest, MidMustNotExceedMaximumLength) {
Steve Anton9e0c7422017-08-18 01:59:471505 MockRtpPacketSink sink;
Niels Möllerd57efc12019-03-22 13:02:111506 std::string mid(BaseRtpStringExtension::kMaxValueSizeBytes + 1, 'a');
Steve Anton53c7ba62017-08-18 17:05:471507 EXPECT_DEATH(AddSinkOnlyMid(mid, &sink), "");
eladalon760a0762017-05-31 16:12:251508}
eladalona52722f2017-06-26 18:23:541509
Steve Anton53c7ba62017-08-18 17:05:471510TEST_F(RtpDemuxerTest, DoubleRegisterationOfSsrcBindingObserverDisallowed) {
Steve Antonb3329172017-08-17 22:23:511511 MockSsrcBindingObserver observer;
Steve Anton9e0c7422017-08-18 01:59:471512 RegisterSsrcBindingObserver(&observer);
1513 EXPECT_DEATH(RegisterSsrcBindingObserver(&observer), "");
eladalona52722f2017-06-26 18:23:541514}
1515
Steve Anton9e0c7422017-08-18 01:59:471516TEST_F(RtpDemuxerTest,
Steve Anton53c7ba62017-08-18 17:05:471517 DregisterationOfNeverRegisteredSsrcBindingObserverDisallowed) {
Steve Antonb3329172017-08-17 22:23:511518 MockSsrcBindingObserver observer;
Steve Anton9e0c7422017-08-18 01:59:471519 EXPECT_DEATH(DeregisterSsrcBindingObserver(&observer), "");
eladalona52722f2017-06-26 18:23:541520}
1521
eladalon760a0762017-05-31 16:12:251522#endif
1523
1524} // namespace
1525} // namespace webrtc