blob: 62024135946c9faab2c5ab38ac6341a328807389 [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
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "call/ssrc_binding_observer.h"
18#include "call/test/mock_rtp_packet_sink_interface.h"
19#include "common_types.h"
20#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"
24#include "rtc_base/basictypes.h"
25#include "rtc_base/checks.h"
26#include "rtc_base/ptr_util.h"
27#include "rtc_base/safe_conversions.h"
28#include "test/gmock.h"
29#include "test/gtest.h"
eladalon760a0762017-05-31 16:12:2530
31namespace webrtc {
32
33namespace {
34
35using ::testing::_;
eladalond0244c22017-06-08 11:19:1336using ::testing::AtLeast;
Steve Anton53c7ba62017-08-18 17:05:4737using ::testing::AtMost;
eladalond0244c22017-06-08 11:19:1338using ::testing::InSequence;
39using ::testing::NiceMock;
eladalon760a0762017-05-31 16:12:2540
Steve Antonb3329172017-08-17 22:23:5141class MockSsrcBindingObserver : public SsrcBindingObserver {
eladalona52722f2017-06-26 18:23:5442 public:
Steve Antonb3329172017-08-17 22:23:5143 MOCK_METHOD2(OnSsrcBoundToRsid, void(const std::string& rsid, uint32_t ssrc));
Steve Anton53c7ba62017-08-18 17:05:4744 MOCK_METHOD2(OnSsrcBoundToMid, void(const std::string& mid, uint32_t ssrc));
45 MOCK_METHOD3(OnSsrcBoundToMidRsid,
46 void(const std::string& mid,
47 const std::string& rsid,
48 uint32_t ssrc));
49 MOCK_METHOD2(OnSsrcBoundToPayloadType,
50 void(uint8_t payload_type, uint32_t ssrc));
eladalona52722f2017-06-26 18:23:5451};
52
Steve Anton9e0c7422017-08-18 01:59:4753class RtpDemuxerTest : public testing::Test {
54 protected:
55 ~RtpDemuxerTest() {
56 for (auto* sink : sinks_to_tear_down_) {
57 demuxer_.RemoveSink(sink);
58 }
59 for (auto* observer : observers_to_tear_down_) {
60 demuxer_.DeregisterSsrcBindingObserver(observer);
61 }
62 }
63
Steve Anton53c7ba62017-08-18 17:05:4764 // These are convenience methods for calling demuxer.AddSink with different
65 // parameters and will ensure that the sink is automatically removed when the
66 // test case finishes.
67
68 bool AddSink(const RtpDemuxerCriteria& criteria,
69 RtpPacketSinkInterface* sink) {
70 bool added = demuxer_.AddSink(criteria, sink);
Steve Anton9e0c7422017-08-18 01:59:4771 if (added) {
72 sinks_to_tear_down_.insert(sink);
73 }
74 return added;
75 }
76
Steve Anton53c7ba62017-08-18 17:05:4777 bool AddSinkOnlySsrc(uint32_t ssrc, RtpPacketSinkInterface* sink) {
78 RtpDemuxerCriteria criteria;
79 criteria.ssrcs = {ssrc};
80 return AddSink(criteria, sink);
81 }
82
83 bool AddSinkOnlyRsid(const std::string& rsid, RtpPacketSinkInterface* sink) {
84 RtpDemuxerCriteria criteria;
85 criteria.rsid = rsid;
86 return AddSink(criteria, sink);
87 }
88
89 bool AddSinkOnlyMid(const std::string& mid, RtpPacketSinkInterface* sink) {
90 RtpDemuxerCriteria criteria;
91 criteria.mid = mid;
92 return AddSink(criteria, sink);
93 }
94
95 bool AddSinkBothMidRsid(const std::string& mid,
96 const std::string& rsid,
97 RtpPacketSinkInterface* sink) {
98 RtpDemuxerCriteria criteria;
99 criteria.mid = mid;
100 criteria.rsid = rsid;
101 return AddSink(criteria, sink);
Steve Anton9e0c7422017-08-18 01:59:47102 }
103
104 bool RemoveSink(RtpPacketSinkInterface* sink) {
105 sinks_to_tear_down_.erase(sink);
106 return demuxer_.RemoveSink(sink);
107 }
108
Steve Anton53c7ba62017-08-18 17:05:47109 // These are convenience methods for calling
110 // demuxer.{Register|Unregister}SsrcBindingObserver such that observers are
111 // automatically removed when the test finishes.
112
Steve Anton9e0c7422017-08-18 01:59:47113 void RegisterSsrcBindingObserver(SsrcBindingObserver* observer) {
114 demuxer_.RegisterSsrcBindingObserver(observer);
115 observers_to_tear_down_.insert(observer);
116 }
117
118 void DeregisterSsrcBindingObserver(SsrcBindingObserver* observer) {
119 demuxer_.DeregisterSsrcBindingObserver(observer);
120 observers_to_tear_down_.erase(observer);
121 }
122
123 // The CreatePacket* methods are helpers for creating new RTP packets with
124 // various attributes set. Tests should use the helper that provides the
125 // minimum information needed to exercise the behavior under test. Tests also
126 // should not rely on any behavior which is not clearly described in the
127 // helper name/arguments. Any additional settings that are not covered by the
128 // helper should be set manually on the packet once it has been returned.
129 // For example, most tests in this file do not care about the RTP sequence
130 // number, but to ensure that the returned packets are valid the helpers will
131 // auto-increment the sequence number starting with 1. Tests that rely on
132 // specific sequence number behavior should call SetSequenceNumber manually on
133 // the returned packet.
134
135 // Intended for use only by other CreatePacket* helpers.
136 std::unique_ptr<RtpPacketReceived> CreatePacket(
137 uint32_t ssrc,
138 RtpPacketReceived::ExtensionManager* extension_manager) {
139 auto packet = rtc::MakeUnique<RtpPacketReceived>(extension_manager);
140 packet->SetSsrc(ssrc);
141 packet->SetSequenceNumber(next_sequence_number_++);
142 return packet;
143 }
144
145 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrc(uint32_t ssrc) {
146 return CreatePacket(ssrc, nullptr);
147 }
148
Steve Anton53c7ba62017-08-18 17:05:47149 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcMid(
150 uint32_t ssrc,
151 const std::string& mid) {
152 RtpPacketReceived::ExtensionManager extension_manager;
153 extension_manager.Register<RtpMid>(11);
154
155 auto packet = CreatePacket(ssrc, &extension_manager);
156 packet->SetExtension<RtpMid>(mid);
157 return packet;
158 }
159
Steve Anton9e0c7422017-08-18 01:59:47160 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcRsid(
161 uint32_t ssrc,
162 const std::string& rsid) {
163 RtpPacketReceived::ExtensionManager extension_manager;
164 extension_manager.Register<RtpStreamId>(6);
165
166 auto packet = CreatePacket(ssrc, &extension_manager);
167 packet->SetExtension<RtpStreamId>(rsid);
168 return packet;
169 }
170
Steve Anton53c7ba62017-08-18 17:05:47171 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcRrid(
172 uint32_t ssrc,
173 const std::string& rrid) {
174 RtpPacketReceived::ExtensionManager extension_manager;
175 extension_manager.Register<RepairedRtpStreamId>(7);
176
177 auto packet = CreatePacket(ssrc, &extension_manager);
178 packet->SetExtension<RepairedRtpStreamId>(rrid);
179 return packet;
180 }
181
182 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcMidRsid(
183 uint32_t ssrc,
184 const std::string& mid,
185 const std::string& rsid) {
186 RtpPacketReceived::ExtensionManager extension_manager;
187 extension_manager.Register<RtpMid>(11);
188 extension_manager.Register<RtpStreamId>(6);
189
190 auto packet = CreatePacket(ssrc, &extension_manager);
191 packet->SetExtension<RtpMid>(mid);
192 packet->SetExtension<RtpStreamId>(rsid);
193 return packet;
194 }
195
196 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcRsidRrid(
197 uint32_t ssrc,
198 const std::string& rsid,
199 const std::string& rrid) {
200 RtpPacketReceived::ExtensionManager extension_manager;
201 extension_manager.Register<RtpStreamId>(6);
202 extension_manager.Register<RepairedRtpStreamId>(7);
203
204 auto packet = CreatePacket(ssrc, &extension_manager);
205 packet->SetExtension<RtpStreamId>(rsid);
206 packet->SetExtension<RepairedRtpStreamId>(rrid);
207 return packet;
208 }
209
Steve Anton9e0c7422017-08-18 01:59:47210 RtpDemuxer demuxer_;
211 std::set<RtpPacketSinkInterface*> sinks_to_tear_down_;
212 std::set<SsrcBindingObserver*> observers_to_tear_down_;
213 uint16_t next_sequence_number_ = 1;
214};
215
eladalond0244c22017-06-08 11:19:13216MATCHER_P(SamePacketAs, other, "") {
217 return arg.Ssrc() == other.Ssrc() &&
218 arg.SequenceNumber() == other.SequenceNumber();
eladalon760a0762017-05-31 16:12:25219}
220
Steve Anton9e0c7422017-08-18 01:59:47221TEST_F(RtpDemuxerTest, CanAddSinkBySsrc) {
eladalon5daecca2017-08-04 13:34:54222 MockRtpPacketSink sink;
223 constexpr uint32_t ssrc = 1;
224
Steve Anton9e0c7422017-08-18 01:59:47225 EXPECT_TRUE(AddSinkOnlySsrc(ssrc, &sink));
eladalon5daecca2017-08-04 13:34:54226}
227
Steve Anton53c7ba62017-08-18 17:05:47228TEST_F(RtpDemuxerTest, AllowAddSinkWithOverlappingPayloadTypesIfDifferentMid) {
229 const std::string mid1 = "v";
230 const std::string mid2 = "a";
231 constexpr uint8_t pt1 = 30;
232 constexpr uint8_t pt2 = 31;
233 constexpr uint8_t pt3 = 32;
234
235 RtpDemuxerCriteria pt1_pt2;
236 pt1_pt2.mid = mid1;
237 pt1_pt2.payload_types = {pt1, pt2};
238 MockRtpPacketSink sink1;
239 AddSink(pt1_pt2, &sink1);
240
241 RtpDemuxerCriteria pt1_pt3;
242 pt1_pt2.mid = mid2;
243 pt1_pt3.payload_types = {pt1, pt3};
244 MockRtpPacketSink sink2;
245 EXPECT_TRUE(AddSink(pt1_pt3, &sink2));
246}
247
248TEST_F(RtpDemuxerTest, RejectAddSinkForSameMidOnly) {
249 const std::string mid = "mid";
250
251 MockRtpPacketSink sink;
252 AddSinkOnlyMid(mid, &sink);
253 EXPECT_FALSE(AddSinkOnlyMid(mid, &sink));
254}
255
256TEST_F(RtpDemuxerTest, RejectAddSinkForSameMidRsid) {
257 const std::string mid = "v";
258 const std::string rsid = "1";
259
260 MockRtpPacketSink sink1;
261 AddSinkBothMidRsid(mid, rsid, &sink1);
262
263 MockRtpPacketSink sink2;
264 EXPECT_FALSE(AddSinkBothMidRsid(mid, rsid, &sink2));
265}
266
267TEST_F(RtpDemuxerTest, RejectAddSinkForConflictingMidAndMidRsid) {
268 const std::string mid = "v";
269 const std::string rsid = "1";
270
271 MockRtpPacketSink mid_sink;
272 AddSinkOnlyMid(mid, &mid_sink);
273
274 // This sink would never get any packets routed to it because the above sink
275 // would receive them all.
276 MockRtpPacketSink mid_rsid_sink;
277 EXPECT_FALSE(AddSinkBothMidRsid(mid, rsid, &mid_rsid_sink));
278}
279
280TEST_F(RtpDemuxerTest, RejectAddSinkForConflictingMidRsidAndMid) {
281 const std::string mid = "v";
282 const std::string rsid = "";
283
284 MockRtpPacketSink mid_rsid_sink;
285 AddSinkBothMidRsid(mid, rsid, &mid_rsid_sink);
286
287 // This sink would shadow the above sink.
288 MockRtpPacketSink mid_sink;
289 EXPECT_FALSE(AddSinkOnlyMid(mid, &mid_sink));
290}
291
292TEST_F(RtpDemuxerTest, AddSinkFailsIfCalledForTwoSinksWithSameSsrc) {
293 MockRtpPacketSink sink_a;
294 MockRtpPacketSink sink_b;
295 constexpr uint32_t ssrc = 1;
296 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sink_a));
297
298 EXPECT_FALSE(AddSinkOnlySsrc(ssrc, &sink_b));
299}
300
301TEST_F(RtpDemuxerTest, AddSinkFailsIfCalledTwiceEvenIfSameSinkWithSameSsrc) {
302 MockRtpPacketSink sink;
303 constexpr uint32_t ssrc = 1;
304 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sink));
305
306 EXPECT_FALSE(AddSinkOnlySsrc(ssrc, &sink));
307}
308
309// TODO(steveanton): Currently fails because payload type validation is not
310// complete in AddSink (see note in rtp_demuxer.cc).
311TEST_F(RtpDemuxerTest, DISABLED_RejectAddSinkForSamePayloadTypes) {
312 constexpr uint8_t pt1 = 30;
313 constexpr uint8_t pt2 = 31;
314
315 RtpDemuxerCriteria pt1_pt2;
316 pt1_pt2.payload_types = {pt1, pt2};
317 MockRtpPacketSink sink1;
318 AddSink(pt1_pt2, &sink1);
319
320 RtpDemuxerCriteria pt2_pt1;
321 pt2_pt1.payload_types = {pt2, pt1};
322 MockRtpPacketSink sink2;
323 EXPECT_FALSE(AddSink(pt2_pt1, &sink2));
324}
325
326// Routing Tests
327
Steve Anton9e0c7422017-08-18 01:59:47328TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkBySsrc) {
eladalond0244c22017-06-08 11:19:13329 constexpr uint32_t ssrcs[] = {101, 202, 303};
330 MockRtpPacketSink sinks[arraysize(ssrcs)];
331 for (size_t i = 0; i < arraysize(ssrcs); i++) {
Steve Anton9e0c7422017-08-18 01:59:47332 AddSinkOnlySsrc(ssrcs[i], &sinks[i]);
eladalond0244c22017-06-08 11:19:13333 }
334
335 for (size_t i = 0; i < arraysize(ssrcs); i++) {
Steve Anton9e0c7422017-08-18 01:59:47336 auto packet = CreatePacketWithSsrc(ssrcs[i]);
eladalond0244c22017-06-08 11:19:13337 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47338 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon760a0762017-05-31 16:12:25339 }
340}
341
Steve Anton9e0c7422017-08-18 01:59:47342TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByRsid) {
eladalond0244c22017-06-08 11:19:13343 const std::string rsids[] = {"a", "b", "c"};
344 MockRtpPacketSink sinks[arraysize(rsids)];
345 for (size_t i = 0; i < arraysize(rsids); i++) {
Steve Anton9e0c7422017-08-18 01:59:47346 AddSinkOnlyRsid(rsids[i], &sinks[i]);
eladalond0244c22017-06-08 11:19:13347 }
348
349 for (size_t i = 0; i < arraysize(rsids); i++) {
oprypin0826fb22017-08-22 20:57:48350 auto packet = CreatePacketWithSsrcRsid(rtc::checked_cast<uint32_t>(i),
351 rsids[i]);
eladalond0244c22017-06-08 11:19:13352 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47353 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13354 }
355}
356
Steve Anton53c7ba62017-08-18 17:05:47357TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByMid) {
358 const std::string mids[] = {"a", "v", "s"};
359 MockRtpPacketSink sinks[arraysize(mids)];
360 for (size_t i = 0; i < arraysize(mids); i++) {
361 AddSinkOnlyMid(mids[i], &sinks[i]);
362 }
363
364 for (size_t i = 0; i < arraysize(mids); i++) {
oprypin0826fb22017-08-22 20:57:48365 auto packet = CreatePacketWithSsrcMid(rtc::checked_cast<uint32_t>(i),
366 mids[i]);
Steve Anton53c7ba62017-08-18 17:05:47367 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
368 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
369 }
370}
371
372TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByMidAndRsid) {
373 const std::string mid = "v";
374 const std::string rsid = "1";
375 constexpr uint32_t ssrc = 10;
376
377 MockRtpPacketSink sink;
378 AddSinkBothMidRsid(mid, rsid, &sink);
379
380 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
381 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
382 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
383}
384
385TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByRepairedRsid) {
386 const std::string rrid = "1";
387 constexpr uint32_t ssrc = 10;
388
389 MockRtpPacketSink sink;
390 AddSinkOnlyRsid(rrid, &sink);
391
392 auto packet_with_rrid = CreatePacketWithSsrcRrid(ssrc, rrid);
393 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_rrid))).Times(1);
394 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_rrid));
395}
396
397TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByPayloadType) {
398 constexpr uint32_t ssrc = 10;
399 constexpr uint8_t payload_type = 30;
400
401 MockRtpPacketSink sink;
402 RtpDemuxerCriteria criteria;
403 criteria.payload_types = {payload_type};
404 AddSink(criteria, &sink);
405
406 auto packet = CreatePacketWithSsrc(ssrc);
407 packet->SetPayloadType(payload_type);
408 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
409 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
410}
411
Steve Anton9e0c7422017-08-18 01:59:47412TEST_F(RtpDemuxerTest, PacketsDeliveredInRightOrder) {
eladalona52722f2017-06-26 18:23:54413 constexpr uint32_t ssrc = 101;
414 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47415 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13416
417 std::unique_ptr<RtpPacketReceived> packets[5];
418 for (size_t i = 0; i < arraysize(packets); i++) {
Steve Anton9e0c7422017-08-18 01:59:47419 packets[i] = CreatePacketWithSsrc(ssrc);
oprypin0826fb22017-08-22 20:57:48420 packets[i]->SetSequenceNumber(rtc::checked_cast<uint16_t>(i));
eladalond0244c22017-06-08 11:19:13421 }
422
423 InSequence sequence;
424 for (const auto& packet : packets) {
eladalona52722f2017-06-26 18:23:54425 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
eladalond0244c22017-06-08 11:19:13426 }
427
428 for (const auto& packet : packets) {
Steve Anton9e0c7422017-08-18 01:59:47429 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13430 }
eladalond0244c22017-06-08 11:19:13431}
432
Steve Anton9e0c7422017-08-18 01:59:47433TEST_F(RtpDemuxerTest, SinkMappedToMultipleSsrcs) {
eladalond0244c22017-06-08 11:19:13434 constexpr uint32_t ssrcs[] = {404, 505, 606};
435 MockRtpPacketSink sink;
436 for (uint32_t ssrc : ssrcs) {
Steve Anton9e0c7422017-08-18 01:59:47437 AddSinkOnlySsrc(ssrc, &sink);
eladalon760a0762017-05-31 16:12:25438 }
439
440 // The sink which is associated with multiple SSRCs gets the callback
441 // triggered for each of those SSRCs.
eladalond0244c22017-06-08 11:19:13442 for (uint32_t ssrc : ssrcs) {
Steve Anton9e0c7422017-08-18 01:59:47443 auto packet = CreatePacketWithSsrc(ssrc);
Steve Anton53c7ba62017-08-18 17:05:47444 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47445 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon760a0762017-05-31 16:12:25446 }
eladalon760a0762017-05-31 16:12:25447}
448
Steve Anton9e0c7422017-08-18 01:59:47449TEST_F(RtpDemuxerTest, NoCallbackOnSsrcSinkRemovedBeforeFirstPacket) {
eladalond0244c22017-06-08 11:19:13450 constexpr uint32_t ssrc = 404;
451 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47452 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13453
Steve Anton9e0c7422017-08-18 01:59:47454 ASSERT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13455
456 // The removed sink does not get callbacks.
Steve Anton9e0c7422017-08-18 01:59:47457 auto packet = CreatePacketWithSsrc(ssrc);
eladalond0244c22017-06-08 11:19:13458 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47459 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13460}
461
Steve Anton9e0c7422017-08-18 01:59:47462TEST_F(RtpDemuxerTest, NoCallbackOnSsrcSinkRemovedAfterFirstPacket) {
eladalond0244c22017-06-08 11:19:13463 constexpr uint32_t ssrc = 404;
464 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:47465 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13466
467 InSequence sequence;
Steve Anton9e0c7422017-08-18 01:59:47468 for (size_t i = 0; i < 10; i++) {
469 ASSERT_TRUE(demuxer_.OnRtpPacket(*CreatePacketWithSsrc(ssrc)));
eladalon760a0762017-05-31 16:12:25470 }
471
Steve Anton9e0c7422017-08-18 01:59:47472 ASSERT_TRUE(RemoveSink(&sink));
eladalon760a0762017-05-31 16:12:25473
eladalond0244c22017-06-08 11:19:13474 // The removed sink does not get callbacks.
Steve Anton9e0c7422017-08-18 01:59:47475 auto packet = CreatePacketWithSsrc(ssrc);
eladalond0244c22017-06-08 11:19:13476 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47477 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13478}
479
eladalon5daecca2017-08-04 13:34:54480// An SSRC may only be mapped to a single sink. However, since configuration
481// of this associations might come from the network, we need to fail gracefully.
Steve Anton9e0c7422017-08-18 01:59:47482TEST_F(RtpDemuxerTest, OnlyOneSinkPerSsrcGetsOnRtpPacketTriggered) {
eladalon5daecca2017-08-04 13:34:54483 MockRtpPacketSink sinks[3];
484 constexpr uint32_t ssrc = 404;
Steve Anton9e0c7422017-08-18 01:59:47485 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sinks[0]));
486 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sinks[1]));
487 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sinks[2]));
eladalon5daecca2017-08-04 13:34:54488
489 // The first sink associated with the SSRC remains active; other sinks
490 // were not really added, and so do not get OnRtpPacket() called.
Steve Anton9e0c7422017-08-18 01:59:47491 auto packet = CreatePacketWithSsrc(ssrc);
eladalon5daecca2017-08-04 13:34:54492 EXPECT_CALL(sinks[0], OnRtpPacket(SamePacketAs(*packet))).Times(1);
493 EXPECT_CALL(sinks[1], OnRtpPacket(_)).Times(0);
494 EXPECT_CALL(sinks[2], OnRtpPacket(_)).Times(0);
Steve Anton9e0c7422017-08-18 01:59:47495 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54496}
497
Steve Anton9e0c7422017-08-18 01:59:47498TEST_F(RtpDemuxerTest, NoRepeatedCallbackOnRepeatedAddSinkForSameSink) {
eladalond0244c22017-06-08 11:19:13499 constexpr uint32_t ssrc = 111;
500 MockRtpPacketSink sink;
501
Steve Anton9e0c7422017-08-18 01:59:47502 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sink));
503 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sink));
eladalond0244c22017-06-08 11:19:13504
Steve Anton9e0c7422017-08-18 01:59:47505 auto packet = CreatePacketWithSsrc(ssrc);
eladalond0244c22017-06-08 11:19:13506 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47507 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13508}
509
Steve Anton9e0c7422017-08-18 01:59:47510TEST_F(RtpDemuxerTest, RemoveSinkReturnsFalseForNeverAddedSink) {
eladalond0244c22017-06-08 11:19:13511 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47512 EXPECT_FALSE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13513}
514
Steve Anton9e0c7422017-08-18 01:59:47515TEST_F(RtpDemuxerTest, RemoveSinkReturnsTrueForPreviouslyAddedSsrcSink) {
eladalond0244c22017-06-08 11:19:13516 constexpr uint32_t ssrc = 101;
517 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47518 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13519
Steve Anton9e0c7422017-08-18 01:59:47520 EXPECT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13521}
522
Steve Anton9e0c7422017-08-18 01:59:47523TEST_F(RtpDemuxerTest,
524 RemoveSinkReturnsTrueForUnresolvedPreviouslyAddedRsidSink) {
eladalond0244c22017-06-08 11:19:13525 const std::string rsid = "a";
526 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47527 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13528
Steve Anton9e0c7422017-08-18 01:59:47529 EXPECT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13530}
531
Steve Anton9e0c7422017-08-18 01:59:47532TEST_F(RtpDemuxerTest,
533 RemoveSinkReturnsTrueForResolvedPreviouslyAddedRsidSink) {
eladalond0244c22017-06-08 11:19:13534 const std::string rsid = "a";
535 constexpr uint32_t ssrc = 101;
536 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:47537 AddSinkOnlyRsid(rsid, &sink);
538 ASSERT_TRUE(demuxer_.OnRtpPacket(*CreatePacketWithSsrcRsid(ssrc, rsid)));
eladalond0244c22017-06-08 11:19:13539
Steve Anton9e0c7422017-08-18 01:59:47540 EXPECT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13541}
542
Steve Anton53c7ba62017-08-18 17:05:47543TEST_F(RtpDemuxerTest, RsidLearnedAndLaterPacketsDeliveredWithOnlySsrc) {
eladalond0244c22017-06-08 11:19:13544 MockRtpPacketSink sink;
545 const std::string rsid = "a";
Steve Anton9e0c7422017-08-18 01:59:47546 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13547
548 // Create a sequence of RTP packets, where only the first one actually
549 // mentions the RSID.
550 std::unique_ptr<RtpPacketReceived> packets[5];
551 constexpr uint32_t rsid_ssrc = 111;
Steve Anton9e0c7422017-08-18 01:59:47552 packets[0] = CreatePacketWithSsrcRsid(rsid_ssrc, rsid);
eladalond0244c22017-06-08 11:19:13553 for (size_t i = 1; i < arraysize(packets); i++) {
Steve Anton9e0c7422017-08-18 01:59:47554 packets[i] = CreatePacketWithSsrc(rsid_ssrc);
eladalon760a0762017-05-31 16:12:25555 }
eladalond0244c22017-06-08 11:19:13556
557 // The first packet associates the RSID with the SSRC, thereby allowing the
558 // demuxer to correctly demux all of the packets.
559 InSequence sequence;
560 for (const auto& packet : packets) {
561 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
562 }
563 for (const auto& packet : packets) {
Steve Anton9e0c7422017-08-18 01:59:47564 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13565 }
eladalond0244c22017-06-08 11:19:13566}
567
Steve Anton9e0c7422017-08-18 01:59:47568TEST_F(RtpDemuxerTest, NoCallbackOnRsidSinkRemovedBeforeFirstPacket) {
eladalond0244c22017-06-08 11:19:13569 MockRtpPacketSink sink;
570 const std::string rsid = "a";
Steve Anton9e0c7422017-08-18 01:59:47571 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13572
573 // Sink removed - it won't get triggers even if packets with its RSID arrive.
Steve Anton9e0c7422017-08-18 01:59:47574 ASSERT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13575
576 constexpr uint32_t ssrc = 111;
Steve Anton9e0c7422017-08-18 01:59:47577 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalond0244c22017-06-08 11:19:13578 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47579 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13580}
581
Steve Anton9e0c7422017-08-18 01:59:47582TEST_F(RtpDemuxerTest, NoCallbackOnRsidSinkRemovedAfterFirstPacket) {
eladalond0244c22017-06-08 11:19:13583 NiceMock<MockRtpPacketSink> sink;
584 const std::string rsid = "a";
Steve Anton9e0c7422017-08-18 01:59:47585 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13586
587 InSequence sequence;
588 constexpr uint32_t ssrc = 111;
Steve Anton9e0c7422017-08-18 01:59:47589 for (size_t i = 0; i < 10; i++) {
590 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
591 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13592 }
593
594 // Sink removed - it won't get triggers even if packets with its RSID arrive.
Steve Anton9e0c7422017-08-18 01:59:47595 ASSERT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13596
Steve Anton9e0c7422017-08-18 01:59:47597 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalond0244c22017-06-08 11:19:13598 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47599 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13600}
601
Steve Anton53c7ba62017-08-18 17:05:47602TEST_F(RtpDemuxerTest, NoCallbackOnMidSinkRemovedBeforeFirstPacket) {
603 const std::string mid = "v";
604 constexpr uint32_t ssrc = 10;
605
606 MockRtpPacketSink sink;
607 AddSinkOnlyMid(mid, &sink);
608 RemoveSink(&sink);
609
610 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
611 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
612 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
613}
614
615TEST_F(RtpDemuxerTest, NoCallbackOnMidSinkRemovedAfterFirstPacket) {
616 const std::string mid = "v";
617 constexpr uint32_t ssrc = 10;
618
619 NiceMock<MockRtpPacketSink> sink;
620 AddSinkOnlyMid(mid, &sink);
621
622 auto p1 = CreatePacketWithSsrcMid(ssrc, mid);
623 demuxer_.OnRtpPacket(*p1);
624
625 RemoveSink(&sink);
626
627 auto p2 = CreatePacketWithSsrcMid(ssrc, mid);
628 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
629 EXPECT_FALSE(demuxer_.OnRtpPacket(*p2));
630}
631
632TEST_F(RtpDemuxerTest, NoCallbackOnMidRsidSinkRemovedAfterFirstPacket) {
633 const std::string mid = "v";
634 const std::string rsid = "1";
635 constexpr uint32_t ssrc = 10;
636
637 NiceMock<MockRtpPacketSink> sink;
638 AddSinkBothMidRsid(mid, rsid, &sink);
639
640 auto p1 = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
641 demuxer_.OnRtpPacket(*p1);
642
643 RemoveSink(&sink);
644
645 auto p2 = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
646 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
647 EXPECT_FALSE(demuxer_.OnRtpPacket(*p2));
648}
649
eladalond0244c22017-06-08 11:19:13650// The RSID to SSRC mapping should be one-to-one. If we end up receiving
651// two (or more) packets with the same SSRC, but different RSIDs, we guarantee
Steve Anton53c7ba62017-08-18 17:05:47652// delivery to one of them but not both.
Steve Anton9e0c7422017-08-18 01:59:47653TEST_F(RtpDemuxerTest, FirstSsrcAssociatedWithAnRsidIsNotForgotten) {
eladalond0244c22017-06-08 11:19:13654 // Each sink has a distinct RSID.
655 MockRtpPacketSink sink_a;
656 const std::string rsid_a = "a";
Steve Anton9e0c7422017-08-18 01:59:47657 AddSinkOnlyRsid(rsid_a, &sink_a);
eladalond0244c22017-06-08 11:19:13658
659 MockRtpPacketSink sink_b;
660 const std::string rsid_b = "b";
Steve Anton9e0c7422017-08-18 01:59:47661 AddSinkOnlyRsid(rsid_b, &sink_b);
eladalond0244c22017-06-08 11:19:13662
663 InSequence sequence; // Verify that the order of delivery is unchanged.
664
665 constexpr uint32_t shared_ssrc = 100;
666
667 // First a packet with |rsid_a| is received, and |sink_a| is associated with
668 // its SSRC.
Steve Anton9e0c7422017-08-18 01:59:47669 auto packet_a = CreatePacketWithSsrcRsid(shared_ssrc, rsid_a);
eladalond0244c22017-06-08 11:19:13670 EXPECT_CALL(sink_a, OnRtpPacket(SamePacketAs(*packet_a))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47671 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_a));
eladalond0244c22017-06-08 11:19:13672
Steve Anton53c7ba62017-08-18 17:05:47673 // Second, a packet with |rsid_b| is received. We guarantee that |sink_b|
674 // receives it.
Steve Anton9e0c7422017-08-18 01:59:47675 auto packet_b = CreatePacketWithSsrcRsid(shared_ssrc, rsid_b);
Steve Anton53c7ba62017-08-18 17:05:47676 EXPECT_CALL(sink_a, OnRtpPacket(_)).Times(0);
677 EXPECT_CALL(sink_b, OnRtpPacket(SamePacketAs(*packet_b))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47678 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_b));
eladalond0244c22017-06-08 11:19:13679
680 // Known edge-case; adding a new RSID association makes us re-examine all
681 // SSRCs. |sink_b| may or may not be associated with the SSRC now; we make
Steve Anton53c7ba62017-08-18 17:05:47682 // no promises on that. However, since the RSID is specified and it cannot be
683 // found the packet should be dropped.
eladalon9addbeb2017-06-30 13:26:54684 MockRtpPacketSink sink_c;
685 const std::string rsid_c = "c";
686 constexpr uint32_t some_other_ssrc = shared_ssrc + 1;
Steve Anton9e0c7422017-08-18 01:59:47687 AddSinkOnlySsrc(some_other_ssrc, &sink_c);
Steve Anton53c7ba62017-08-18 17:05:47688
689 auto packet_c = CreatePacketWithSsrcMid(shared_ssrc, rsid_c);
690 EXPECT_CALL(sink_a, OnRtpPacket(_)).Times(0);
691 EXPECT_CALL(sink_b, OnRtpPacket(_)).Times(0);
692 EXPECT_CALL(sink_c, OnRtpPacket(_)).Times(0);
693 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_c));
eladalond0244c22017-06-08 11:19:13694}
695
Steve Anton9e0c7422017-08-18 01:59:47696TEST_F(RtpDemuxerTest, MultipleRsidsOnSameSink) {
eladalond0244c22017-06-08 11:19:13697 MockRtpPacketSink sink;
698 const std::string rsids[] = {"a", "b", "c"};
699
700 for (const std::string& rsid : rsids) {
Steve Anton9e0c7422017-08-18 01:59:47701 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13702 }
703
704 InSequence sequence;
705 for (size_t i = 0; i < arraysize(rsids); i++) {
706 // Assign different SSRCs and sequence numbers to all packets.
707 const uint32_t ssrc = 1000 + static_cast<uint32_t>(i);
Steve Anton53c7ba62017-08-18 17:05:47708 const uint16_t sequence_number = 50 + static_cast<uint16_t>(i);
Steve Anton9e0c7422017-08-18 01:59:47709 auto packet = CreatePacketWithSsrcRsid(ssrc, rsids[i]);
Steve Anton53c7ba62017-08-18 17:05:47710 packet->SetSequenceNumber(sequence_number);
eladalond0244c22017-06-08 11:19:13711 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47712 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13713 }
eladalond0244c22017-06-08 11:19:13714}
715
Steve Anton53c7ba62017-08-18 17:05:47716// RSIDs are given higher priority than SSRC because we believe senders are less
717// likely to mislabel packets with RSID than mislabel them with SSRCs.
Steve Anton9e0c7422017-08-18 01:59:47718TEST_F(RtpDemuxerTest, SinkWithBothRsidAndSsrcAssociations) {
Steve Anton53c7ba62017-08-18 17:05:47719 MockRtpPacketSink sink;
eladalond0244c22017-06-08 11:19:13720 constexpr uint32_t standalone_ssrc = 10101;
721 constexpr uint32_t rsid_ssrc = 20202;
Steve Anton53c7ba62017-08-18 17:05:47722 const std::string rsid = "1";
eladalond0244c22017-06-08 11:19:13723
Steve Anton9e0c7422017-08-18 01:59:47724 AddSinkOnlySsrc(standalone_ssrc, &sink);
725 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13726
727 InSequence sequence;
728
Steve Anton9e0c7422017-08-18 01:59:47729 auto ssrc_packet = CreatePacketWithSsrc(standalone_ssrc);
eladalond0244c22017-06-08 11:19:13730 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*ssrc_packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47731 EXPECT_TRUE(demuxer_.OnRtpPacket(*ssrc_packet));
eladalond0244c22017-06-08 11:19:13732
Steve Anton9e0c7422017-08-18 01:59:47733 auto rsid_packet = CreatePacketWithSsrcRsid(rsid_ssrc, rsid);
eladalond0244c22017-06-08 11:19:13734 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*rsid_packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47735 EXPECT_TRUE(demuxer_.OnRtpPacket(*rsid_packet));
eladalond0244c22017-06-08 11:19:13736}
737
Steve Anton53c7ba62017-08-18 17:05:47738// Packets are always guaranteed to be routed to only one sink.
Steve Anton9e0c7422017-08-18 01:59:47739TEST_F(RtpDemuxerTest, AssociatingByRsidAndBySsrcCannotTriggerDoubleCall) {
eladalond0244c22017-06-08 11:19:13740 constexpr uint32_t ssrc = 10101;
eladalond0244c22017-06-08 11:19:13741 const std::string rsid = "a";
eladalond0244c22017-06-08 11:19:13742
Steve Anton9e0c7422017-08-18 01:59:47743 MockRtpPacketSink sink;
744 AddSinkOnlySsrc(ssrc, &sink);
745 AddSinkOnlyRsid(rsid, &sink);
746
747 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalond0244c22017-06-08 11:19:13748 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47749 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon760a0762017-05-31 16:12:25750}
751
Steve Anton53c7ba62017-08-18 17:05:47752TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToMid) {
753 const std::string mid = "v";
754 constexpr uint32_t ssrc = 10;
755
756 NiceMock<MockRtpPacketSink> sink;
757 AddSinkOnlyMid(mid, &sink);
758
759 MockSsrcBindingObserver observer;
760 RegisterSsrcBindingObserver(&observer);
761
762 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
763 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc));
764 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
765}
766
767TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToRsid) {
768 const std::string rsid = "1";
eladalona52722f2017-06-26 18:23:54769 constexpr uint32_t ssrc = 111;
eladalona52722f2017-06-26 18:23:54770
eladalon5daecca2017-08-04 13:34:54771 // Only RSIDs which the demuxer knows may be resolved.
772 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:47773 AddSinkOnlyRsid(rsid, &sink);
eladalon5daecca2017-08-04 13:34:54774
Steve Anton53c7ba62017-08-18 17:05:47775 NiceMock<MockSsrcBindingObserver> rsid_resolution_observers[3];
eladalona52722f2017-06-26 18:23:54776 for (auto& observer : rsid_resolution_observers) {
Steve Anton9e0c7422017-08-18 01:59:47777 RegisterSsrcBindingObserver(&observer);
Steve Antonb3329172017-08-17 22:23:51778 EXPECT_CALL(observer, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
eladalona52722f2017-06-26 18:23:54779 }
780
Steve Antonb3329172017-08-17 22:23:51781 // The expected calls to OnSsrcBoundToRsid() will be triggered by this.
Steve Anton53c7ba62017-08-18 17:05:47782 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
783 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54784}
785
Steve Anton53c7ba62017-08-18 17:05:47786TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToMidRsid) {
787 const std::string mid = "v";
788 const std::string rsid = "1";
789 constexpr uint32_t ssrc = 10;
eladalon5daecca2017-08-04 13:34:54790
Steve Anton53c7ba62017-08-18 17:05:47791 NiceMock<MockRtpPacketSink> sink;
792 AddSinkBothMidRsid(mid, rsid, &sink);
eladalon5daecca2017-08-04 13:34:54793
Steve Anton53c7ba62017-08-18 17:05:47794 MockSsrcBindingObserver observer;
795 RegisterSsrcBindingObserver(&observer);
796
797 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
798 EXPECT_CALL(observer, OnSsrcBoundToMidRsid(mid, rsid, ssrc));
799 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54800}
801
Steve Anton53c7ba62017-08-18 17:05:47802TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToPayloadType) {
803 constexpr uint8_t payload_type = 3;
804 constexpr uint32_t ssrc = 10;
805
806 RtpDemuxerCriteria criteria;
807 criteria.payload_types = {payload_type};
808 NiceMock<MockRtpPacketSink> sink;
809 AddSink(criteria, &sink);
810
811 MockSsrcBindingObserver observer;
812 RegisterSsrcBindingObserver(&observer);
813
814 auto packet = CreatePacketWithSsrc(ssrc);
815 packet->SetPayloadType(payload_type);
816 EXPECT_CALL(observer, OnSsrcBoundToPayloadType(payload_type, ssrc));
817 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
818}
819
820// If one sink is associated with SSRC x, and another sink with RSID y, then if
821// we receive a packet with both SSRC x and RSID y, route that to only the sink
822// for RSID y since we believe RSID tags to be more trustworthy than signaled
823// SSRCs.
Steve Anton9e0c7422017-08-18 01:59:47824TEST_F(RtpDemuxerTest,
Steve Anton53c7ba62017-08-18 17:05:47825 PacketFittingBothRsidSinkAndSsrcSinkGivenOnlyToRsidSink) {
eladalon5daecca2017-08-04 13:34:54826 constexpr uint32_t ssrc = 111;
827 MockRtpPacketSink ssrc_sink;
Steve Anton9e0c7422017-08-18 01:59:47828 AddSinkOnlySsrc(ssrc, &ssrc_sink);
eladalon5daecca2017-08-04 13:34:54829
830 const std::string rsid = "a";
831 MockRtpPacketSink rsid_sink;
Steve Anton9e0c7422017-08-18 01:59:47832 AddSinkOnlyRsid(rsid, &rsid_sink);
eladalon5daecca2017-08-04 13:34:54833
Steve Anton9e0c7422017-08-18 01:59:47834 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalon5daecca2017-08-04 13:34:54835
Steve Anton53c7ba62017-08-18 17:05:47836 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
837 EXPECT_CALL(rsid_sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
838 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54839}
840
841// We're not expecting RSIDs to be resolved to SSRCs which were previously
842// mapped to sinks, and make no guarantees except for graceful handling.
Steve Anton9e0c7422017-08-18 01:59:47843TEST_F(RtpDemuxerTest,
844 GracefullyHandleRsidBeingMappedToPrevouslyAssociatedSsrc) {
eladalon5daecca2017-08-04 13:34:54845 constexpr uint32_t ssrc = 111;
846 NiceMock<MockRtpPacketSink> ssrc_sink;
Steve Anton9e0c7422017-08-18 01:59:47847 AddSinkOnlySsrc(ssrc, &ssrc_sink);
eladalon5daecca2017-08-04 13:34:54848
849 const std::string rsid = "a";
Steve Anton53c7ba62017-08-18 17:05:47850 NiceMock<MockRtpPacketSink> rsid_sink;
Steve Anton9e0c7422017-08-18 01:59:47851 AddSinkOnlyRsid(rsid, &rsid_sink);
eladalon5daecca2017-08-04 13:34:54852
Steve Anton53c7ba62017-08-18 17:05:47853 NiceMock<MockSsrcBindingObserver> observer;
Steve Anton9e0c7422017-08-18 01:59:47854 RegisterSsrcBindingObserver(&observer);
eladalon5daecca2017-08-04 13:34:54855
856 // The SSRC was mapped to an SSRC sink, but was even active (packets flowed
857 // over it).
Steve Anton9e0c7422017-08-18 01:59:47858 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
859 demuxer_.OnRtpPacket(*packet);
eladalon5daecca2017-08-04 13:34:54860
861 // If the SSRC sink is ever removed, the RSID sink *might* receive indications
862 // of packets, and observers *might* be informed. Only graceful handling
863 // is guaranteed.
Steve Anton9e0c7422017-08-18 01:59:47864 RemoveSink(&ssrc_sink);
eladalon5daecca2017-08-04 13:34:54865 EXPECT_CALL(rsid_sink, OnRtpPacket(SamePacketAs(*packet))).Times(AtLeast(0));
Steve Antonb3329172017-08-17 22:23:51866 EXPECT_CALL(observer, OnSsrcBoundToRsid(rsid, ssrc)).Times(AtLeast(0));
Steve Anton53c7ba62017-08-18 17:05:47867 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
868}
869
870// Tests that when one MID sink is configured, packets that include the MID
871// extension will get routed to that sink and any packets that use the same
872// SSRC as one of those packets later will also get routed to the sink, even
873// if a new SSRC is introduced for the same MID.
874TEST_F(RtpDemuxerTest, RoutedByMidWhenSsrcAdded) {
875 const std::string mid = "v";
876 NiceMock<MockRtpPacketSink> sink;
877 AddSinkOnlyMid(mid, &sink);
878
879 constexpr uint32_t ssrc1 = 10;
880 constexpr uint32_t ssrc2 = 11;
881
882 auto packet_ssrc1_mid = CreatePacketWithSsrcMid(ssrc1, mid);
883 demuxer_.OnRtpPacket(*packet_ssrc1_mid);
884 auto packet_ssrc2_mid = CreatePacketWithSsrcMid(ssrc2, mid);
885 demuxer_.OnRtpPacket(*packet_ssrc2_mid);
886
887 auto packet_ssrc1_only = CreatePacketWithSsrc(ssrc1);
888 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc1_only))).Times(1);
889 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc1_only));
890
891 auto packet_ssrc2_only = CreatePacketWithSsrc(ssrc2);
892 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc2_only))).Times(1);
893 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc2_only));
894}
895
896TEST_F(RtpDemuxerTest, DontLearnMidSsrcBindingBeforeSinkAdded) {
897 const std::string mid = "v";
898 constexpr uint32_t ssrc = 10;
899
900 auto packet_ssrc_mid = CreatePacketWithSsrcMid(ssrc, mid);
901 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_ssrc_mid));
902
903 MockRtpPacketSink sink;
904 AddSinkOnlyMid(mid, &sink);
905
906 auto packet_ssrc_only = CreatePacketWithSsrc(ssrc);
907 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
908 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_ssrc_only));
909}
910
911TEST_F(RtpDemuxerTest, DontForgetMidSsrcBindingWhenSinkRemoved) {
912 const std::string mid = "v";
913 constexpr uint32_t ssrc = 10;
914
915 NiceMock<MockRtpPacketSink> sink1;
916 AddSinkOnlyMid(mid, &sink1);
917
918 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
919 demuxer_.OnRtpPacket(*packet_with_mid);
920
921 RemoveSink(&sink1);
922
923 MockRtpPacketSink sink2;
924 AddSinkOnlyMid(mid, &sink2);
925
926 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
927 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
928 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
929}
930
931// If a sink is added with only a MID, then any packet with that MID no matter
932// the RSID should be routed to that sink.
933TEST_F(RtpDemuxerTest, RoutedByMidWithAnyRsid) {
934 const std::string mid = "v";
935 const std::string rsid1 = "1";
936 const std::string rsid2 = "2";
937 constexpr uint32_t ssrc1 = 10;
938 constexpr uint32_t ssrc2 = 11;
939
940 MockRtpPacketSink sink;
941 AddSinkOnlyMid(mid, &sink);
942
943 InSequence sequence;
944
945 auto packet_ssrc1_rsid1 = CreatePacketWithSsrcMidRsid(ssrc1, mid, rsid1);
946 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc1_rsid1))).Times(1);
947 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc1_rsid1));
948
949 auto packet_ssrc2_rsid2 = CreatePacketWithSsrcMidRsid(ssrc2, mid, rsid2);
950 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc2_rsid2))).Times(1);
951 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc2_rsid2));
952}
953
954// These two tests verify that for a sink added with a MID, RSID pair, if the
955// MID and RSID are learned in separate packets (e.g., because the header
956// extensions are sent separately), then a later packet with just SSRC will get
957// routed to that sink.
958// The first test checks that the functionality works when MID is learned first.
959// The second test checks that the functionality works when RSID is learned
960// first.
961TEST_F(RtpDemuxerTest, LearnMidThenRsidSeparatelyAndRouteBySsrc) {
962 const std::string mid = "v";
963 const std::string rsid = "1";
964 constexpr uint32_t ssrc = 10;
965
966 NiceMock<MockRtpPacketSink> sink;
967 AddSinkBothMidRsid(mid, rsid, &sink);
968
969 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
970 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_mid));
971
972 auto packet_with_rsid = CreatePacketWithSsrcRsid(ssrc, rsid);
973 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_rsid));
974
975 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
976 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
977 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
978}
979
980TEST_F(RtpDemuxerTest, LearnRsidThenMidSeparatelyAndRouteBySsrc) {
981 const std::string mid = "v";
982 const std::string rsid = "1";
983 constexpr uint32_t ssrc = 10;
984
985 NiceMock<MockRtpPacketSink> sink;
986 AddSinkBothMidRsid(mid, rsid, &sink);
987
988 auto packet_with_rsid = CreatePacketWithSsrcRsid(ssrc, rsid);
989 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_rsid));
990
991 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
992 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_mid));
993
994 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
995 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
996 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
997}
998
999TEST_F(RtpDemuxerTest, DontLearnMidRsidBindingBeforeSinkAdded) {
1000 const std::string mid = "v";
1001 const std::string rsid = "1";
1002 constexpr uint32_t ssrc = 10;
1003
1004 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
1005 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_both));
1006
1007 MockRtpPacketSink sink;
1008 AddSinkBothMidRsid(mid, rsid, &sink);
1009
1010 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1011 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1012 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1013}
1014
1015TEST_F(RtpDemuxerTest, DontForgetMidRsidBindingWhenSinkRemoved) {
1016 const std::string mid = "v";
1017 const std::string rsid = "1";
1018 constexpr uint32_t ssrc = 10;
1019
1020 NiceMock<MockRtpPacketSink> sink1;
1021 AddSinkBothMidRsid(mid, rsid, &sink1);
1022
1023 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
1024 demuxer_.OnRtpPacket(*packet_with_both);
1025
1026 RemoveSink(&sink1);
1027
1028 MockRtpPacketSink sink2;
1029 AddSinkBothMidRsid(mid, rsid, &sink2);
1030
1031 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1032 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
1033 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1034}
1035
1036TEST_F(RtpDemuxerTest, LearnMidRsidBindingAfterSinkAdded) {
1037 const std::string mid = "v";
1038 const std::string rsid = "1";
1039 constexpr uint32_t ssrc = 10;
1040
1041 NiceMock<MockRtpPacketSink> sink;
1042 AddSinkBothMidRsid(mid, rsid, &sink);
1043
1044 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
1045 demuxer_.OnRtpPacket(*packet_with_both);
1046
1047 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1048 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
1049 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1050}
1051
1052TEST_F(RtpDemuxerTest, DropByPayloadTypeIfNoSink) {
1053 constexpr uint8_t payload_type = 30;
1054 constexpr uint32_t ssrc = 10;
1055
1056 auto packet = CreatePacketWithSsrc(ssrc);
1057 packet->SetPayloadType(payload_type);
1058 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1059}
1060
1061// For legacy applications, it's possible for us to demux if the payload type is
1062// unique. But if multiple sinks are registered with different MIDs and the same
1063// payload types, then we cannot route a packet with just payload type because
1064// it is ambiguous which sink it should be sent to.
1065TEST_F(RtpDemuxerTest, DropByPayloadTypeIfAddedInMultipleSinks) {
1066 const std::string mid1 = "v";
1067 const std::string mid2 = "a";
1068 constexpr uint8_t payload_type = 30;
1069 constexpr uint32_t ssrc = 10;
1070
1071 RtpDemuxerCriteria mid1_pt;
1072 mid1_pt.mid = mid1;
1073 mid1_pt.payload_types = {payload_type};
1074 MockRtpPacketSink sink1;
1075 AddSink(mid1_pt, &sink1);
1076
1077 RtpDemuxerCriteria mid2_pt;
1078 mid2_pt.mid = mid2;
1079 mid2_pt.payload_types = {payload_type};
1080 MockRtpPacketSink sink2;
1081 AddSink(mid2_pt, &sink2);
1082
1083 auto packet = CreatePacketWithSsrc(ssrc);
1084 packet->SetPayloadType(payload_type);
1085
1086 EXPECT_CALL(sink1, OnRtpPacket(_)).Times(0);
1087 EXPECT_CALL(sink2, OnRtpPacket(_)).Times(0);
1088 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1089}
1090
1091// If two sinks are added with different MIDs but the same payload types, then
1092// we cannot demux on the payload type only unless one of the sinks is removed.
1093TEST_F(RtpDemuxerTest, RoutedByPayloadTypeIfAmbiguousSinkRemoved) {
1094 const std::string mid1 = "v";
1095 const std::string mid2 = "a";
1096 constexpr uint8_t payload_type = 30;
1097 constexpr uint32_t ssrc = 10;
1098
1099 RtpDemuxerCriteria mid1_pt;
1100 mid1_pt.mid = mid1;
1101 mid1_pt.payload_types = {payload_type};
1102 MockRtpPacketSink sink1;
1103 AddSink(mid1_pt, &sink1);
1104
1105 RtpDemuxerCriteria mid2_pt;
1106 mid2_pt.mid = mid2;
1107 mid2_pt.payload_types = {payload_type};
1108 MockRtpPacketSink sink2;
1109 AddSink(mid2_pt, &sink2);
1110
1111 RemoveSink(&sink1);
1112
1113 auto packet = CreatePacketWithSsrc(ssrc);
1114 packet->SetPayloadType(payload_type);
1115
1116 EXPECT_CALL(sink1, OnRtpPacket(_)).Times(0);
1117 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1118
1119 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1120}
1121
1122TEST_F(RtpDemuxerTest, RoutedByPayloadTypeLatchesSsrc) {
1123 constexpr uint8_t payload_type = 30;
1124 constexpr uint32_t ssrc = 10;
1125
1126 RtpDemuxerCriteria pt;
1127 pt.payload_types = {payload_type};
1128 NiceMock<MockRtpPacketSink> sink;
1129 AddSink(pt, &sink);
1130
1131 auto packet_with_pt = CreatePacketWithSsrc(ssrc);
1132 packet_with_pt->SetPayloadType(payload_type);
1133 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt));
1134
1135 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1136 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
1137 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1138}
1139
1140// RSIDs are scoped within MID, so if two sinks are registered with the same
1141// RSIDs but different MIDs, then packets containing both extensions should be
1142// routed to the correct one.
1143TEST_F(RtpDemuxerTest, PacketWithSameRsidDifferentMidRoutedToProperSink) {
1144 const std::string mid1 = "mid1";
1145 const std::string mid2 = "mid2";
1146 const std::string rsid = "rsid";
1147 constexpr uint32_t ssrc1 = 10;
1148 constexpr uint32_t ssrc2 = 11;
1149
1150 NiceMock<MockRtpPacketSink> mid1_sink;
1151 AddSinkBothMidRsid(mid1, rsid, &mid1_sink);
1152
1153 MockRtpPacketSink mid2_sink;
1154 AddSinkBothMidRsid(mid2, rsid, &mid2_sink);
1155
1156 auto packet_mid1 = CreatePacketWithSsrcMidRsid(ssrc1, mid1, rsid);
1157 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_mid1));
1158
1159 auto packet_mid2 = CreatePacketWithSsrcMidRsid(ssrc2, mid2, rsid);
1160 EXPECT_CALL(mid2_sink, OnRtpPacket(SamePacketAs(*packet_mid2))).Times(1);
1161 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_mid2));
1162}
1163
1164// If a sink is first bound to a given SSRC by signaling but later a new sink is
1165// bound to a given MID by a later signaling, then when a packet arrives with
1166// both the SSRC and MID, then the signaled MID sink should take precedence.
1167TEST_F(RtpDemuxerTest, SignaledMidShouldOverwriteSignaledSsrc) {
1168 constexpr uint32_t ssrc = 11;
1169 const std::string mid = "mid";
1170
1171 MockRtpPacketSink ssrc_sink;
1172 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1173
1174 MockRtpPacketSink mid_sink;
1175 AddSinkOnlyMid(mid, &mid_sink);
1176
1177 auto p = CreatePacketWithSsrcMid(ssrc, mid);
1178 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
1179 EXPECT_CALL(mid_sink, OnRtpPacket(SamePacketAs(*p))).Times(1);
1180 EXPECT_TRUE(demuxer_.OnRtpPacket(*p));
1181}
1182
1183// Extends the previous test to also ensure that later packets that do not
1184// specify MID are still routed to the MID sink rather than the overwritten SSRC
1185// sink.
1186TEST_F(RtpDemuxerTest, SignaledMidShouldOverwriteSignalledSsrcPersistent) {
1187 constexpr uint32_t ssrc = 11;
1188 const std::string mid = "mid";
1189
1190 MockRtpPacketSink ssrc_sink;
1191 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1192
1193 NiceMock<MockRtpPacketSink> mid_sink;
1194 AddSinkOnlyMid(mid, &mid_sink);
1195
1196 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
1197
1198 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
1199 demuxer_.OnRtpPacket(*packet_with_mid);
1200
1201 auto packet_without_mid = CreatePacketWithSsrc(ssrc);
1202 EXPECT_CALL(mid_sink, OnRtpPacket(SamePacketAs(*packet_without_mid)))
1203 .Times(1);
1204 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_without_mid));
1205}
1206
1207TEST_F(RtpDemuxerTest, RouteByPayloadTypeMultipleMatch) {
1208 constexpr uint32_t ssrc = 10;
1209 constexpr uint8_t pt1 = 30;
1210 constexpr uint8_t pt2 = 31;
1211
1212 MockRtpPacketSink sink;
1213 RtpDemuxerCriteria criteria;
1214 criteria.payload_types = {pt1, pt2};
1215 AddSink(criteria, &sink);
1216
1217 auto packet_with_pt1 = CreatePacketWithSsrc(ssrc);
1218 packet_with_pt1->SetPayloadType(pt1);
1219 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_pt1)));
1220 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt1));
1221
1222 auto packet_with_pt2 = CreatePacketWithSsrc(ssrc);
1223 packet_with_pt2->SetPayloadType(pt2);
1224 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_pt2)));
1225 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt2));
1226}
1227
1228TEST_F(RtpDemuxerTest, DontDemuxOnMidAloneIfAddedWithRsid) {
1229 const std::string mid = "v";
1230 const std::string rsid = "1";
1231 constexpr uint32_t ssrc = 10;
1232
1233 MockRtpPacketSink sink;
1234 AddSinkBothMidRsid(mid, rsid, &sink);
1235
1236 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1237
1238 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
1239 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1240}
1241
1242TEST_F(RtpDemuxerTest, DemuxBySsrcEvenWithMidAndRsid) {
1243 const std::string mid = "v";
1244 const std::string rsid = "1";
1245 constexpr uint32_t ssrc = 10;
1246
1247 RtpDemuxerCriteria criteria;
1248 criteria.rsid = rsid;
1249 criteria.mid = mid;
1250 criteria.ssrcs = {ssrc};
1251 MockRtpPacketSink sink;
1252 AddSink(criteria, &sink);
1253
1254 auto packet = CreatePacketWithSsrc(ssrc);
1255 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1256 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1257}
1258
1259// In slight deviation from the BUNDLE spec, if we match a sink according to
1260// SSRC, then we do not verify payload type against the criteria and defer to
1261// the sink to check that it is correct.
1262TEST_F(RtpDemuxerTest, DoNotCheckPayloadTypeIfMatchedByOtherCriteria) {
1263 constexpr uint32_t ssrc = 10;
1264 constexpr uint8_t payload_type = 30;
1265 constexpr uint8_t different_payload_type = payload_type + 1;
1266
1267 RtpDemuxerCriteria criteria;
1268 criteria.ssrcs = {ssrc};
1269 criteria.payload_types = {payload_type};
1270 MockRtpPacketSink sink;
1271 AddSink(criteria, &sink);
1272
1273 auto packet = CreatePacketWithSsrc(ssrc);
1274 packet->SetPayloadType(different_payload_type);
1275 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1276 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1277}
1278
1279// If a repair packet includes an RSID it should be ignored and the packet
1280// should be routed by its RRID.
1281TEST_F(RtpDemuxerTest, PacketWithRsidAndRridRoutedByRrid) {
1282 const std::string rsid = "1";
1283 const std::string rrid = "1r";
1284 constexpr uint32_t ssrc = 10;
1285
1286 MockRtpPacketSink sink_rsid;
1287 AddSinkOnlyRsid(rsid, &sink_rsid);
1288
1289 MockRtpPacketSink sink_rrid;
1290 AddSinkOnlyRsid(rrid, &sink_rrid);
1291
1292 auto packet = CreatePacketWithSsrcRsidRrid(ssrc, rsid, rrid);
1293 EXPECT_CALL(sink_rsid, OnRtpPacket(_)).Times(0);
1294 EXPECT_CALL(sink_rrid, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1295 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1296}
1297
1298// Same test as above but checks that the latched SSRC routes to the RRID sink.
1299TEST_F(RtpDemuxerTest, PacketWithRsidAndRridLatchesSsrcToRrid) {
1300 const std::string rsid = "1";
1301 const std::string rrid = "1r";
1302 constexpr uint32_t ssrc = 10;
1303
1304 MockRtpPacketSink sink_rsid;
1305 AddSinkOnlyRsid(rsid, &sink_rsid);
1306
1307 NiceMock<MockRtpPacketSink> sink_rrid;
1308 AddSinkOnlyRsid(rrid, &sink_rrid);
1309
1310 auto packet_rsid_rrid = CreatePacketWithSsrcRsidRrid(ssrc, rsid, rrid);
1311 demuxer_.OnRtpPacket(*packet_rsid_rrid);
1312
1313 auto packet_ssrc_only = CreatePacketWithSsrc(ssrc);
1314 EXPECT_CALL(sink_rsid, OnRtpPacket(_)).Times(0);
1315 EXPECT_CALL(sink_rrid, OnRtpPacket(SamePacketAs(*packet_ssrc_only))).Times(1);
1316 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc_only));
1317}
1318
1319// Tests that a packet which includes MID and RSID is dropped and not routed by
1320// SSRC if the MID and RSID do not match an added sink.
1321TEST_F(RtpDemuxerTest, PacketWithMidAndUnknownRsidIsNotRoutedBySsrc) {
1322 constexpr uint32_t ssrc = 10;
1323 const std::string mid = "v";
1324 const std::string rsid = "1";
1325 const std::string wrong_rsid = "2";
1326
1327 RtpDemuxerCriteria criteria;
1328 criteria.mid = mid;
1329 criteria.rsid = rsid;
1330 criteria.ssrcs = {ssrc};
1331 MockRtpPacketSink sink;
1332 AddSink(criteria, &sink);
1333
1334 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, wrong_rsid);
1335 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1336 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1337}
1338
1339// Tests that a packet which includes MID and RSID is dropped and not routed by
1340// payload type if the MID and RSID do not match an added sink.
1341TEST_F(RtpDemuxerTest, PacketWithMidAndUnknownRsidIsNotRoutedByPayloadType) {
1342 constexpr uint32_t ssrc = 10;
1343 const std::string mid = "v";
1344 const std::string rsid = "1";
1345 const std::string wrong_rsid = "2";
1346 constexpr uint8_t payload_type = 30;
1347
1348 RtpDemuxerCriteria criteria;
1349 criteria.mid = mid;
1350 criteria.rsid = rsid;
1351 criteria.payload_types = {payload_type};
1352 MockRtpPacketSink sink;
1353 AddSink(criteria, &sink);
1354
1355 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, wrong_rsid);
1356 packet->SetPayloadType(payload_type);
1357 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1358 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1359}
1360
1361// Observers are only notified of an SSRC binding to an RSID if we care about
1362// the RSID (i.e., have a sink added for that RSID).
1363TEST_F(RtpDemuxerTest, ObserversNotNotifiedOfUntrackedRsids) {
1364 const std::string rsid = "1";
1365 constexpr uint32_t ssrc = 111;
1366
1367 MockSsrcBindingObserver rsid_resolution_observers[3];
1368 for (auto& observer : rsid_resolution_observers) {
1369 RegisterSsrcBindingObserver(&observer);
1370 EXPECT_CALL(observer, OnSsrcBoundToRsid(_, _)).Times(0);
1371 }
1372
1373 // Since no sink is registered for this SSRC/RSID, expect the packet to not be
1374 // routed and no observers notified of the SSRC -> RSID binding.
1375 EXPECT_FALSE(demuxer_.OnRtpPacket(*CreatePacketWithSsrcRsid(ssrc, rsid)));
1376}
1377
1378// Ensure that observers are notified of SSRC bindings only once per unique
1379// binding source (e.g., SSRC -> MID, SSRC -> RSID, etc.)
1380TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundtoMidOnlyOnce) {
1381 const std::string mid = "v";
1382 constexpr uint32_t ssrc = 10;
1383
1384 NiceMock<MockRtpPacketSink> sink;
1385 AddSinkOnlyMid(mid, &sink);
1386
1387 MockSsrcBindingObserver observer;
1388 RegisterSsrcBindingObserver(&observer);
1389
1390 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc)).Times(1);
1391
1392 demuxer_.OnRtpPacket(*CreatePacketWithSsrcMid(ssrc, mid));
1393 demuxer_.OnRtpPacket(*CreatePacketWithSsrcMid(ssrc, mid));
1394}
1395
1396// Ensure that when a new SSRC -> MID binding is discovered observers are also
1397// notified of that, even if there has already been an SSRC bound to the MID.
1398TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundtoMidWhenSsrcChanges) {
1399 const std::string mid = "v";
1400 constexpr uint32_t ssrc1 = 10;
1401 constexpr uint32_t ssrc2 = 11;
1402
1403 NiceMock<MockRtpPacketSink> sink;
1404 AddSinkOnlyMid(mid, &sink);
1405
1406 MockSsrcBindingObserver observer;
1407 RegisterSsrcBindingObserver(&observer);
1408
1409 InSequence seq;
1410 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc1)).Times(1);
1411 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc2)).Times(1);
1412
1413 auto p1 = CreatePacketWithSsrcMid(ssrc1, mid);
1414 demuxer_.OnRtpPacket(*p1);
1415
1416 auto p2 = CreatePacketWithSsrcMid(ssrc2, mid);
1417 demuxer_.OnRtpPacket(*p2);
eladalona52722f2017-06-26 18:23:541418}
1419
Steve Anton9e0c7422017-08-18 01:59:471420TEST_F(RtpDemuxerTest, DeregisteredRsidObserversNotInformedOfResolutions) {
eladalona52722f2017-06-26 18:23:541421 constexpr uint32_t ssrc = 111;
1422 const std::string rsid = "a";
1423 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:471424 AddSinkOnlyRsid(rsid, &sink);
eladalona52722f2017-06-26 18:23:541425
1426 // Register several, then deregister only one, to show that not all of the
1427 // observers had been forgotten when one was removed.
Steve Antonb3329172017-08-17 22:23:511428 MockSsrcBindingObserver observer_1;
1429 MockSsrcBindingObserver observer_2_removed;
1430 MockSsrcBindingObserver observer_3;
eladalona52722f2017-06-26 18:23:541431
Steve Anton9e0c7422017-08-18 01:59:471432 RegisterSsrcBindingObserver(&observer_1);
1433 RegisterSsrcBindingObserver(&observer_2_removed);
1434 RegisterSsrcBindingObserver(&observer_3);
eladalona52722f2017-06-26 18:23:541435
Steve Anton9e0c7422017-08-18 01:59:471436 DeregisterSsrcBindingObserver(&observer_2_removed);
eladalona52722f2017-06-26 18:23:541437
Steve Antonb3329172017-08-17 22:23:511438 EXPECT_CALL(observer_1, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
1439 EXPECT_CALL(observer_2_removed, OnSsrcBoundToRsid(_, _)).Times(0);
1440 EXPECT_CALL(observer_3, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
eladalona52722f2017-06-26 18:23:541441
Steve Antonb3329172017-08-17 22:23:511442 // The expected calls to OnSsrcBoundToRsid() will be triggered by this.
Steve Anton9e0c7422017-08-18 01:59:471443 demuxer_.OnRtpPacket(*CreatePacketWithSsrcRsid(ssrc, rsid));
eladalona52722f2017-06-26 18:23:541444}
1445
Steve Anton53c7ba62017-08-18 17:05:471446TEST_F(RtpDemuxerTest,
1447 PacketFittingBothRsidSinkAndSsrcSinkTriggersResolutionCallbacks) {
1448 constexpr uint32_t ssrc = 111;
1449 NiceMock<MockRtpPacketSink> ssrc_sink;
1450 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1451
1452 const std::string rsid = "a";
1453 NiceMock<MockRtpPacketSink> rsid_sink;
1454 AddSinkOnlyRsid(rsid, &rsid_sink);
1455
1456 MockSsrcBindingObserver observer;
1457 RegisterSsrcBindingObserver(&observer);
1458
1459 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
1460 EXPECT_CALL(observer, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
1461 demuxer_.OnRtpPacket(*packet);
1462}
1463
1464TEST_F(RtpDemuxerTest, MaliciousPeerCannotCauseMemoryOveruse) {
1465 const std::string mid = "v";
1466
1467 NiceMock<MockRtpPacketSink> sink;
1468 AddSinkOnlyMid(mid, &sink);
1469
1470 MockSsrcBindingObserver observer;
1471 RegisterSsrcBindingObserver(&observer);
1472
1473 EXPECT_CALL(observer, OnSsrcBoundToMid(_, _))
1474 .Times(AtMost(RtpDemuxer::kMaxSsrcBindings));
1475
1476 for (int i = 0; i < RtpDemuxer::kMaxSsrcBindings + 1; i++) {
1477 auto packet = CreatePacketWithSsrcMid(i, mid);
1478 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1479 }
1480}
1481
eladalon760a0762017-05-31 16:12:251482#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
Steve Anton53c7ba62017-08-18 17:05:471483
1484TEST_F(RtpDemuxerTest, CriteriaMustBeNonEmpty) {
eladalond0244c22017-06-08 11:19:131485 MockRtpPacketSink sink;
Steve Anton53c7ba62017-08-18 17:05:471486 RtpDemuxerCriteria criteria;
1487 EXPECT_DEATH(AddSink(criteria, &sink), "");
eladalond0244c22017-06-08 11:19:131488}
1489
Steve Anton9e0c7422017-08-18 01:59:471490TEST_F(RtpDemuxerTest, RsidMustBeAlphaNumeric) {
eladalond0244c22017-06-08 11:19:131491 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:471492 EXPECT_DEATH(AddSinkOnlyRsid("a_3", &sink), "");
eladalond0244c22017-06-08 11:19:131493}
1494
Steve Anton53c7ba62017-08-18 17:05:471495TEST_F(RtpDemuxerTest, MidMustBeAlphaNumeric) {
1496 MockRtpPacketSink sink;
1497 EXPECT_DEATH(AddSinkOnlyMid("a_3", &sink), "");
1498}
1499
Steve Anton9e0c7422017-08-18 01:59:471500TEST_F(RtpDemuxerTest, RsidMustNotExceedMaximumLength) {
eladalond0244c22017-06-08 11:19:131501 MockRtpPacketSink sink;
1502 std::string rsid(StreamId::kMaxSize + 1, 'a');
Steve Anton9e0c7422017-08-18 01:59:471503 EXPECT_DEATH(AddSinkOnlyRsid(rsid, &sink), "");
eladalond0244c22017-06-08 11:19:131504}
1505
Steve Anton53c7ba62017-08-18 17:05:471506TEST_F(RtpDemuxerTest, MidMustNotExceedMaximumLength) {
Steve Anton9e0c7422017-08-18 01:59:471507 MockRtpPacketSink sink;
Steve Anton53c7ba62017-08-18 17:05:471508 std::string mid(Mid::kMaxSize + 1, 'a');
1509 EXPECT_DEATH(AddSinkOnlyMid(mid, &sink), "");
eladalon760a0762017-05-31 16:12:251510}
eladalona52722f2017-06-26 18:23:541511
Steve Anton53c7ba62017-08-18 17:05:471512TEST_F(RtpDemuxerTest, DoubleRegisterationOfSsrcBindingObserverDisallowed) {
Steve Antonb3329172017-08-17 22:23:511513 MockSsrcBindingObserver observer;
Steve Anton9e0c7422017-08-18 01:59:471514 RegisterSsrcBindingObserver(&observer);
1515 EXPECT_DEATH(RegisterSsrcBindingObserver(&observer), "");
eladalona52722f2017-06-26 18:23:541516}
1517
Steve Anton9e0c7422017-08-18 01:59:471518TEST_F(RtpDemuxerTest,
Steve Anton53c7ba62017-08-18 17:05:471519 DregisterationOfNeverRegisteredSsrcBindingObserverDisallowed) {
Steve Antonb3329172017-08-17 22:23:511520 MockSsrcBindingObserver observer;
Steve Anton9e0c7422017-08-18 01:59:471521 EXPECT_DEATH(DeregisterSsrcBindingObserver(&observer), "");
eladalona52722f2017-06-26 18:23:541522}
1523
eladalon760a0762017-05-31 16:12:251524#endif
1525
1526} // namespace
1527} // namespace webrtc