blob: af6cbee3e9672ed733dcfff036c426397e831ee2 [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"
Mirko Bonadei71207422017-09-15 11:58:0919#include "common_types.h" // NOLINT(build/include)
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 "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3127#include "test/gmock.h"
28#include "test/gtest.h"
eladalon760a0762017-05-31 16:12:2529
30namespace webrtc {
31
32namespace {
33
34using ::testing::_;
eladalond0244c22017-06-08 11:19:1335using ::testing::AtLeast;
Steve Anton53c7ba62017-08-18 17:05:4736using ::testing::AtMost;
eladalond0244c22017-06-08 11:19:1337using ::testing::InSequence;
38using ::testing::NiceMock;
eladalon760a0762017-05-31 16:12:2539
Steve Antonb3329172017-08-17 22:23:5140class MockSsrcBindingObserver : public SsrcBindingObserver {
eladalona52722f2017-06-26 18:23:5441 public:
Steve Antonb3329172017-08-17 22:23:5142 MOCK_METHOD2(OnSsrcBoundToRsid, void(const std::string& rsid, uint32_t ssrc));
Steve Anton53c7ba62017-08-18 17:05:4743 MOCK_METHOD2(OnSsrcBoundToMid, void(const std::string& mid, uint32_t ssrc));
44 MOCK_METHOD3(OnSsrcBoundToMidRsid,
45 void(const std::string& mid,
46 const std::string& rsid,
47 uint32_t ssrc));
48 MOCK_METHOD2(OnSsrcBoundToPayloadType,
49 void(uint8_t payload_type, uint32_t ssrc));
eladalona52722f2017-06-26 18:23:5450};
51
Steve Anton9e0c7422017-08-18 01:59:4752class RtpDemuxerTest : public testing::Test {
53 protected:
54 ~RtpDemuxerTest() {
55 for (auto* sink : sinks_to_tear_down_) {
56 demuxer_.RemoveSink(sink);
57 }
58 for (auto* observer : observers_to_tear_down_) {
59 demuxer_.DeregisterSsrcBindingObserver(observer);
60 }
61 }
62
Steve Anton53c7ba62017-08-18 17:05:4763 // These are convenience methods for calling demuxer.AddSink with different
64 // parameters and will ensure that the sink is automatically removed when the
65 // test case finishes.
66
67 bool AddSink(const RtpDemuxerCriteria& criteria,
68 RtpPacketSinkInterface* sink) {
69 bool added = demuxer_.AddSink(criteria, sink);
Steve Anton9e0c7422017-08-18 01:59:4770 if (added) {
71 sinks_to_tear_down_.insert(sink);
72 }
73 return added;
74 }
75
Steve Anton53c7ba62017-08-18 17:05:4776 bool AddSinkOnlySsrc(uint32_t ssrc, RtpPacketSinkInterface* sink) {
77 RtpDemuxerCriteria criteria;
78 criteria.ssrcs = {ssrc};
79 return AddSink(criteria, sink);
80 }
81
82 bool AddSinkOnlyRsid(const std::string& rsid, RtpPacketSinkInterface* sink) {
83 RtpDemuxerCriteria criteria;
84 criteria.rsid = rsid;
85 return AddSink(criteria, sink);
86 }
87
88 bool AddSinkOnlyMid(const std::string& mid, RtpPacketSinkInterface* sink) {
89 RtpDemuxerCriteria criteria;
90 criteria.mid = mid;
91 return AddSink(criteria, sink);
92 }
93
94 bool AddSinkBothMidRsid(const std::string& mid,
95 const std::string& rsid,
96 RtpPacketSinkInterface* sink) {
97 RtpDemuxerCriteria criteria;
98 criteria.mid = mid;
99 criteria.rsid = rsid;
100 return AddSink(criteria, sink);
Steve Anton9e0c7422017-08-18 01:59:47101 }
102
103 bool RemoveSink(RtpPacketSinkInterface* sink) {
104 sinks_to_tear_down_.erase(sink);
105 return demuxer_.RemoveSink(sink);
106 }
107
Steve Anton53c7ba62017-08-18 17:05:47108 // These are convenience methods for calling
109 // demuxer.{Register|Unregister}SsrcBindingObserver such that observers are
110 // automatically removed when the test finishes.
111
Steve Anton9e0c7422017-08-18 01:59:47112 void RegisterSsrcBindingObserver(SsrcBindingObserver* observer) {
113 demuxer_.RegisterSsrcBindingObserver(observer);
114 observers_to_tear_down_.insert(observer);
115 }
116
117 void DeregisterSsrcBindingObserver(SsrcBindingObserver* observer) {
118 demuxer_.DeregisterSsrcBindingObserver(observer);
119 observers_to_tear_down_.erase(observer);
120 }
121
122 // The CreatePacket* methods are helpers for creating new RTP packets with
123 // various attributes set. Tests should use the helper that provides the
124 // minimum information needed to exercise the behavior under test. Tests also
125 // should not rely on any behavior which is not clearly described in the
126 // helper name/arguments. Any additional settings that are not covered by the
127 // helper should be set manually on the packet once it has been returned.
128 // For example, most tests in this file do not care about the RTP sequence
129 // number, but to ensure that the returned packets are valid the helpers will
130 // auto-increment the sequence number starting with 1. Tests that rely on
131 // specific sequence number behavior should call SetSequenceNumber manually on
132 // the returned packet.
133
134 // Intended for use only by other CreatePacket* helpers.
135 std::unique_ptr<RtpPacketReceived> CreatePacket(
136 uint32_t ssrc,
137 RtpPacketReceived::ExtensionManager* extension_manager) {
138 auto packet = rtc::MakeUnique<RtpPacketReceived>(extension_manager);
139 packet->SetSsrc(ssrc);
140 packet->SetSequenceNumber(next_sequence_number_++);
141 return packet;
142 }
143
144 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrc(uint32_t ssrc) {
145 return CreatePacket(ssrc, nullptr);
146 }
147
Steve Anton53c7ba62017-08-18 17:05:47148 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcMid(
149 uint32_t ssrc,
150 const std::string& mid) {
151 RtpPacketReceived::ExtensionManager extension_manager;
152 extension_manager.Register<RtpMid>(11);
153
154 auto packet = CreatePacket(ssrc, &extension_manager);
155 packet->SetExtension<RtpMid>(mid);
156 return packet;
157 }
158
Steve Anton9e0c7422017-08-18 01:59:47159 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcRsid(
160 uint32_t ssrc,
161 const std::string& rsid) {
162 RtpPacketReceived::ExtensionManager extension_manager;
163 extension_manager.Register<RtpStreamId>(6);
164
165 auto packet = CreatePacket(ssrc, &extension_manager);
166 packet->SetExtension<RtpStreamId>(rsid);
167 return packet;
168 }
169
Steve Anton53c7ba62017-08-18 17:05:47170 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcRrid(
171 uint32_t ssrc,
172 const std::string& rrid) {
173 RtpPacketReceived::ExtensionManager extension_manager;
174 extension_manager.Register<RepairedRtpStreamId>(7);
175
176 auto packet = CreatePacket(ssrc, &extension_manager);
177 packet->SetExtension<RepairedRtpStreamId>(rrid);
178 return packet;
179 }
180
181 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcMidRsid(
182 uint32_t ssrc,
183 const std::string& mid,
184 const std::string& rsid) {
185 RtpPacketReceived::ExtensionManager extension_manager;
186 extension_manager.Register<RtpMid>(11);
187 extension_manager.Register<RtpStreamId>(6);
188
189 auto packet = CreatePacket(ssrc, &extension_manager);
190 packet->SetExtension<RtpMid>(mid);
191 packet->SetExtension<RtpStreamId>(rsid);
192 return packet;
193 }
194
195 std::unique_ptr<RtpPacketReceived> CreatePacketWithSsrcRsidRrid(
196 uint32_t ssrc,
197 const std::string& rsid,
198 const std::string& rrid) {
199 RtpPacketReceived::ExtensionManager extension_manager;
200 extension_manager.Register<RtpStreamId>(6);
201 extension_manager.Register<RepairedRtpStreamId>(7);
202
203 auto packet = CreatePacket(ssrc, &extension_manager);
204 packet->SetExtension<RtpStreamId>(rsid);
205 packet->SetExtension<RepairedRtpStreamId>(rrid);
206 return packet;
207 }
208
Steve Anton9e0c7422017-08-18 01:59:47209 RtpDemuxer demuxer_;
210 std::set<RtpPacketSinkInterface*> sinks_to_tear_down_;
211 std::set<SsrcBindingObserver*> observers_to_tear_down_;
212 uint16_t next_sequence_number_ = 1;
213};
214
eladalond0244c22017-06-08 11:19:13215MATCHER_P(SamePacketAs, other, "") {
216 return arg.Ssrc() == other.Ssrc() &&
217 arg.SequenceNumber() == other.SequenceNumber();
eladalon760a0762017-05-31 16:12:25218}
219
Steve Anton9e0c7422017-08-18 01:59:47220TEST_F(RtpDemuxerTest, CanAddSinkBySsrc) {
eladalon5daecca2017-08-04 13:34:54221 MockRtpPacketSink sink;
222 constexpr uint32_t ssrc = 1;
223
Steve Anton9e0c7422017-08-18 01:59:47224 EXPECT_TRUE(AddSinkOnlySsrc(ssrc, &sink));
eladalon5daecca2017-08-04 13:34:54225}
226
Steve Anton53c7ba62017-08-18 17:05:47227TEST_F(RtpDemuxerTest, AllowAddSinkWithOverlappingPayloadTypesIfDifferentMid) {
228 const std::string mid1 = "v";
229 const std::string mid2 = "a";
230 constexpr uint8_t pt1 = 30;
231 constexpr uint8_t pt2 = 31;
232 constexpr uint8_t pt3 = 32;
233
234 RtpDemuxerCriteria pt1_pt2;
235 pt1_pt2.mid = mid1;
236 pt1_pt2.payload_types = {pt1, pt2};
237 MockRtpPacketSink sink1;
238 AddSink(pt1_pt2, &sink1);
239
240 RtpDemuxerCriteria pt1_pt3;
241 pt1_pt2.mid = mid2;
242 pt1_pt3.payload_types = {pt1, pt3};
243 MockRtpPacketSink sink2;
244 EXPECT_TRUE(AddSink(pt1_pt3, &sink2));
245}
246
247TEST_F(RtpDemuxerTest, RejectAddSinkForSameMidOnly) {
248 const std::string mid = "mid";
249
250 MockRtpPacketSink sink;
251 AddSinkOnlyMid(mid, &sink);
252 EXPECT_FALSE(AddSinkOnlyMid(mid, &sink));
253}
254
255TEST_F(RtpDemuxerTest, RejectAddSinkForSameMidRsid) {
256 const std::string mid = "v";
257 const std::string rsid = "1";
258
259 MockRtpPacketSink sink1;
260 AddSinkBothMidRsid(mid, rsid, &sink1);
261
262 MockRtpPacketSink sink2;
263 EXPECT_FALSE(AddSinkBothMidRsid(mid, rsid, &sink2));
264}
265
266TEST_F(RtpDemuxerTest, RejectAddSinkForConflictingMidAndMidRsid) {
267 const std::string mid = "v";
268 const std::string rsid = "1";
269
270 MockRtpPacketSink mid_sink;
271 AddSinkOnlyMid(mid, &mid_sink);
272
273 // This sink would never get any packets routed to it because the above sink
274 // would receive them all.
275 MockRtpPacketSink mid_rsid_sink;
276 EXPECT_FALSE(AddSinkBothMidRsid(mid, rsid, &mid_rsid_sink));
277}
278
279TEST_F(RtpDemuxerTest, RejectAddSinkForConflictingMidRsidAndMid) {
280 const std::string mid = "v";
281 const std::string rsid = "";
282
283 MockRtpPacketSink mid_rsid_sink;
284 AddSinkBothMidRsid(mid, rsid, &mid_rsid_sink);
285
286 // This sink would shadow the above sink.
287 MockRtpPacketSink mid_sink;
288 EXPECT_FALSE(AddSinkOnlyMid(mid, &mid_sink));
289}
290
291TEST_F(RtpDemuxerTest, AddSinkFailsIfCalledForTwoSinksWithSameSsrc) {
292 MockRtpPacketSink sink_a;
293 MockRtpPacketSink sink_b;
294 constexpr uint32_t ssrc = 1;
295 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sink_a));
296
297 EXPECT_FALSE(AddSinkOnlySsrc(ssrc, &sink_b));
298}
299
300TEST_F(RtpDemuxerTest, AddSinkFailsIfCalledTwiceEvenIfSameSinkWithSameSsrc) {
301 MockRtpPacketSink sink;
302 constexpr uint32_t ssrc = 1;
303 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sink));
304
305 EXPECT_FALSE(AddSinkOnlySsrc(ssrc, &sink));
306}
307
308// TODO(steveanton): Currently fails because payload type validation is not
309// complete in AddSink (see note in rtp_demuxer.cc).
310TEST_F(RtpDemuxerTest, DISABLED_RejectAddSinkForSamePayloadTypes) {
311 constexpr uint8_t pt1 = 30;
312 constexpr uint8_t pt2 = 31;
313
314 RtpDemuxerCriteria pt1_pt2;
315 pt1_pt2.payload_types = {pt1, pt2};
316 MockRtpPacketSink sink1;
317 AddSink(pt1_pt2, &sink1);
318
319 RtpDemuxerCriteria pt2_pt1;
320 pt2_pt1.payload_types = {pt2, pt1};
321 MockRtpPacketSink sink2;
322 EXPECT_FALSE(AddSink(pt2_pt1, &sink2));
323}
324
325// Routing Tests
326
Steve Anton9e0c7422017-08-18 01:59:47327TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkBySsrc) {
eladalond0244c22017-06-08 11:19:13328 constexpr uint32_t ssrcs[] = {101, 202, 303};
329 MockRtpPacketSink sinks[arraysize(ssrcs)];
330 for (size_t i = 0; i < arraysize(ssrcs); i++) {
Steve Anton9e0c7422017-08-18 01:59:47331 AddSinkOnlySsrc(ssrcs[i], &sinks[i]);
eladalond0244c22017-06-08 11:19:13332 }
333
334 for (size_t i = 0; i < arraysize(ssrcs); i++) {
Steve Anton9e0c7422017-08-18 01:59:47335 auto packet = CreatePacketWithSsrc(ssrcs[i]);
eladalond0244c22017-06-08 11:19:13336 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47337 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon760a0762017-05-31 16:12:25338 }
339}
340
Steve Anton9e0c7422017-08-18 01:59:47341TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByRsid) {
eladalond0244c22017-06-08 11:19:13342 const std::string rsids[] = {"a", "b", "c"};
343 MockRtpPacketSink sinks[arraysize(rsids)];
344 for (size_t i = 0; i < arraysize(rsids); i++) {
Steve Anton9e0c7422017-08-18 01:59:47345 AddSinkOnlyRsid(rsids[i], &sinks[i]);
eladalond0244c22017-06-08 11:19:13346 }
347
348 for (size_t i = 0; i < arraysize(rsids); i++) {
oprypin0826fb22017-08-22 20:57:48349 auto packet = CreatePacketWithSsrcRsid(rtc::checked_cast<uint32_t>(i),
350 rsids[i]);
eladalond0244c22017-06-08 11:19:13351 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47352 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13353 }
354}
355
Steve Anton53c7ba62017-08-18 17:05:47356TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByMid) {
357 const std::string mids[] = {"a", "v", "s"};
358 MockRtpPacketSink sinks[arraysize(mids)];
359 for (size_t i = 0; i < arraysize(mids); i++) {
360 AddSinkOnlyMid(mids[i], &sinks[i]);
361 }
362
363 for (size_t i = 0; i < arraysize(mids); i++) {
oprypin0826fb22017-08-22 20:57:48364 auto packet = CreatePacketWithSsrcMid(rtc::checked_cast<uint32_t>(i),
365 mids[i]);
Steve Anton53c7ba62017-08-18 17:05:47366 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
367 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
368 }
369}
370
371TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByMidAndRsid) {
372 const std::string mid = "v";
373 const std::string rsid = "1";
374 constexpr uint32_t ssrc = 10;
375
376 MockRtpPacketSink sink;
377 AddSinkBothMidRsid(mid, rsid, &sink);
378
379 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
380 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
381 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
382}
383
384TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByRepairedRsid) {
385 const std::string rrid = "1";
386 constexpr uint32_t ssrc = 10;
387
388 MockRtpPacketSink sink;
389 AddSinkOnlyRsid(rrid, &sink);
390
391 auto packet_with_rrid = CreatePacketWithSsrcRrid(ssrc, rrid);
392 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_rrid))).Times(1);
393 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_rrid));
394}
395
396TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByPayloadType) {
397 constexpr uint32_t ssrc = 10;
398 constexpr uint8_t payload_type = 30;
399
400 MockRtpPacketSink sink;
401 RtpDemuxerCriteria criteria;
402 criteria.payload_types = {payload_type};
403 AddSink(criteria, &sink);
404
405 auto packet = CreatePacketWithSsrc(ssrc);
406 packet->SetPayloadType(payload_type);
407 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
408 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
409}
410
Steve Anton9e0c7422017-08-18 01:59:47411TEST_F(RtpDemuxerTest, PacketsDeliveredInRightOrder) {
eladalona52722f2017-06-26 18:23:54412 constexpr uint32_t ssrc = 101;
413 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47414 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13415
416 std::unique_ptr<RtpPacketReceived> packets[5];
417 for (size_t i = 0; i < arraysize(packets); i++) {
Steve Anton9e0c7422017-08-18 01:59:47418 packets[i] = CreatePacketWithSsrc(ssrc);
oprypin0826fb22017-08-22 20:57:48419 packets[i]->SetSequenceNumber(rtc::checked_cast<uint16_t>(i));
eladalond0244c22017-06-08 11:19:13420 }
421
422 InSequence sequence;
423 for (const auto& packet : packets) {
eladalona52722f2017-06-26 18:23:54424 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
eladalond0244c22017-06-08 11:19:13425 }
426
427 for (const auto& packet : packets) {
Steve Anton9e0c7422017-08-18 01:59:47428 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13429 }
eladalond0244c22017-06-08 11:19:13430}
431
Steve Anton9e0c7422017-08-18 01:59:47432TEST_F(RtpDemuxerTest, SinkMappedToMultipleSsrcs) {
eladalond0244c22017-06-08 11:19:13433 constexpr uint32_t ssrcs[] = {404, 505, 606};
434 MockRtpPacketSink sink;
435 for (uint32_t ssrc : ssrcs) {
Steve Anton9e0c7422017-08-18 01:59:47436 AddSinkOnlySsrc(ssrc, &sink);
eladalon760a0762017-05-31 16:12:25437 }
438
439 // The sink which is associated with multiple SSRCs gets the callback
440 // triggered for each of those SSRCs.
eladalond0244c22017-06-08 11:19:13441 for (uint32_t ssrc : ssrcs) {
Steve Anton9e0c7422017-08-18 01:59:47442 auto packet = CreatePacketWithSsrc(ssrc);
Steve Anton53c7ba62017-08-18 17:05:47443 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47444 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon760a0762017-05-31 16:12:25445 }
eladalon760a0762017-05-31 16:12:25446}
447
Steve Anton9e0c7422017-08-18 01:59:47448TEST_F(RtpDemuxerTest, NoCallbackOnSsrcSinkRemovedBeforeFirstPacket) {
eladalond0244c22017-06-08 11:19:13449 constexpr uint32_t ssrc = 404;
450 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47451 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13452
Steve Anton9e0c7422017-08-18 01:59:47453 ASSERT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13454
455 // The removed sink does not get callbacks.
Steve Anton9e0c7422017-08-18 01:59:47456 auto packet = CreatePacketWithSsrc(ssrc);
eladalond0244c22017-06-08 11:19:13457 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47458 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13459}
460
Steve Anton9e0c7422017-08-18 01:59:47461TEST_F(RtpDemuxerTest, NoCallbackOnSsrcSinkRemovedAfterFirstPacket) {
eladalond0244c22017-06-08 11:19:13462 constexpr uint32_t ssrc = 404;
463 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:47464 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13465
466 InSequence sequence;
Steve Anton9e0c7422017-08-18 01:59:47467 for (size_t i = 0; i < 10; i++) {
468 ASSERT_TRUE(demuxer_.OnRtpPacket(*CreatePacketWithSsrc(ssrc)));
eladalon760a0762017-05-31 16:12:25469 }
470
Steve Anton9e0c7422017-08-18 01:59:47471 ASSERT_TRUE(RemoveSink(&sink));
eladalon760a0762017-05-31 16:12:25472
eladalond0244c22017-06-08 11:19:13473 // The removed sink does not get callbacks.
Steve Anton9e0c7422017-08-18 01:59:47474 auto packet = CreatePacketWithSsrc(ssrc);
eladalond0244c22017-06-08 11:19:13475 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47476 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13477}
478
eladalon5daecca2017-08-04 13:34:54479// An SSRC may only be mapped to a single sink. However, since configuration
480// of this associations might come from the network, we need to fail gracefully.
Steve Anton9e0c7422017-08-18 01:59:47481TEST_F(RtpDemuxerTest, OnlyOneSinkPerSsrcGetsOnRtpPacketTriggered) {
eladalon5daecca2017-08-04 13:34:54482 MockRtpPacketSink sinks[3];
483 constexpr uint32_t ssrc = 404;
Steve Anton9e0c7422017-08-18 01:59:47484 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sinks[0]));
485 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sinks[1]));
486 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sinks[2]));
eladalon5daecca2017-08-04 13:34:54487
488 // The first sink associated with the SSRC remains active; other sinks
489 // were not really added, and so do not get OnRtpPacket() called.
Steve Anton9e0c7422017-08-18 01:59:47490 auto packet = CreatePacketWithSsrc(ssrc);
eladalon5daecca2017-08-04 13:34:54491 EXPECT_CALL(sinks[0], OnRtpPacket(SamePacketAs(*packet))).Times(1);
492 EXPECT_CALL(sinks[1], OnRtpPacket(_)).Times(0);
493 EXPECT_CALL(sinks[2], OnRtpPacket(_)).Times(0);
Steve Anton9e0c7422017-08-18 01:59:47494 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54495}
496
Steve Anton9e0c7422017-08-18 01:59:47497TEST_F(RtpDemuxerTest, NoRepeatedCallbackOnRepeatedAddSinkForSameSink) {
eladalond0244c22017-06-08 11:19:13498 constexpr uint32_t ssrc = 111;
499 MockRtpPacketSink sink;
500
Steve Anton9e0c7422017-08-18 01:59:47501 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sink));
502 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sink));
eladalond0244c22017-06-08 11:19:13503
Steve Anton9e0c7422017-08-18 01:59:47504 auto packet = CreatePacketWithSsrc(ssrc);
eladalond0244c22017-06-08 11:19:13505 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47506 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13507}
508
Steve Anton9e0c7422017-08-18 01:59:47509TEST_F(RtpDemuxerTest, RemoveSinkReturnsFalseForNeverAddedSink) {
eladalond0244c22017-06-08 11:19:13510 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47511 EXPECT_FALSE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13512}
513
Steve Anton9e0c7422017-08-18 01:59:47514TEST_F(RtpDemuxerTest, RemoveSinkReturnsTrueForPreviouslyAddedSsrcSink) {
eladalond0244c22017-06-08 11:19:13515 constexpr uint32_t ssrc = 101;
516 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47517 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13518
Steve Anton9e0c7422017-08-18 01:59:47519 EXPECT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13520}
521
Steve Anton9e0c7422017-08-18 01:59:47522TEST_F(RtpDemuxerTest,
523 RemoveSinkReturnsTrueForUnresolvedPreviouslyAddedRsidSink) {
eladalond0244c22017-06-08 11:19:13524 const std::string rsid = "a";
525 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47526 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13527
Steve Anton9e0c7422017-08-18 01:59:47528 EXPECT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13529}
530
Steve Anton9e0c7422017-08-18 01:59:47531TEST_F(RtpDemuxerTest,
532 RemoveSinkReturnsTrueForResolvedPreviouslyAddedRsidSink) {
eladalond0244c22017-06-08 11:19:13533 const std::string rsid = "a";
534 constexpr uint32_t ssrc = 101;
535 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:47536 AddSinkOnlyRsid(rsid, &sink);
537 ASSERT_TRUE(demuxer_.OnRtpPacket(*CreatePacketWithSsrcRsid(ssrc, rsid)));
eladalond0244c22017-06-08 11:19:13538
Steve Anton9e0c7422017-08-18 01:59:47539 EXPECT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13540}
541
Steve Anton53c7ba62017-08-18 17:05:47542TEST_F(RtpDemuxerTest, RsidLearnedAndLaterPacketsDeliveredWithOnlySsrc) {
eladalond0244c22017-06-08 11:19:13543 MockRtpPacketSink sink;
544 const std::string rsid = "a";
Steve Anton9e0c7422017-08-18 01:59:47545 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13546
547 // Create a sequence of RTP packets, where only the first one actually
548 // mentions the RSID.
549 std::unique_ptr<RtpPacketReceived> packets[5];
550 constexpr uint32_t rsid_ssrc = 111;
Steve Anton9e0c7422017-08-18 01:59:47551 packets[0] = CreatePacketWithSsrcRsid(rsid_ssrc, rsid);
eladalond0244c22017-06-08 11:19:13552 for (size_t i = 1; i < arraysize(packets); i++) {
Steve Anton9e0c7422017-08-18 01:59:47553 packets[i] = CreatePacketWithSsrc(rsid_ssrc);
eladalon760a0762017-05-31 16:12:25554 }
eladalond0244c22017-06-08 11:19:13555
556 // The first packet associates the RSID with the SSRC, thereby allowing the
557 // demuxer to correctly demux all of the packets.
558 InSequence sequence;
559 for (const auto& packet : packets) {
560 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
561 }
562 for (const auto& packet : packets) {
Steve Anton9e0c7422017-08-18 01:59:47563 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13564 }
eladalond0244c22017-06-08 11:19:13565}
566
Steve Anton9e0c7422017-08-18 01:59:47567TEST_F(RtpDemuxerTest, NoCallbackOnRsidSinkRemovedBeforeFirstPacket) {
eladalond0244c22017-06-08 11:19:13568 MockRtpPacketSink sink;
569 const std::string rsid = "a";
Steve Anton9e0c7422017-08-18 01:59:47570 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13571
572 // Sink removed - it won't get triggers even if packets with its RSID arrive.
Steve Anton9e0c7422017-08-18 01:59:47573 ASSERT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13574
575 constexpr uint32_t ssrc = 111;
Steve Anton9e0c7422017-08-18 01:59:47576 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalond0244c22017-06-08 11:19:13577 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47578 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13579}
580
Steve Anton9e0c7422017-08-18 01:59:47581TEST_F(RtpDemuxerTest, NoCallbackOnRsidSinkRemovedAfterFirstPacket) {
eladalond0244c22017-06-08 11:19:13582 NiceMock<MockRtpPacketSink> sink;
583 const std::string rsid = "a";
Steve Anton9e0c7422017-08-18 01:59:47584 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13585
586 InSequence sequence;
587 constexpr uint32_t ssrc = 111;
Steve Anton9e0c7422017-08-18 01:59:47588 for (size_t i = 0; i < 10; i++) {
589 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
590 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13591 }
592
593 // Sink removed - it won't get triggers even if packets with its RSID arrive.
Steve Anton9e0c7422017-08-18 01:59:47594 ASSERT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13595
Steve Anton9e0c7422017-08-18 01:59:47596 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalond0244c22017-06-08 11:19:13597 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47598 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13599}
600
Steve Anton53c7ba62017-08-18 17:05:47601TEST_F(RtpDemuxerTest, NoCallbackOnMidSinkRemovedBeforeFirstPacket) {
602 const std::string mid = "v";
603 constexpr uint32_t ssrc = 10;
604
605 MockRtpPacketSink sink;
606 AddSinkOnlyMid(mid, &sink);
607 RemoveSink(&sink);
608
609 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
610 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
611 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
612}
613
614TEST_F(RtpDemuxerTest, NoCallbackOnMidSinkRemovedAfterFirstPacket) {
615 const std::string mid = "v";
616 constexpr uint32_t ssrc = 10;
617
618 NiceMock<MockRtpPacketSink> sink;
619 AddSinkOnlyMid(mid, &sink);
620
621 auto p1 = CreatePacketWithSsrcMid(ssrc, mid);
622 demuxer_.OnRtpPacket(*p1);
623
624 RemoveSink(&sink);
625
626 auto p2 = CreatePacketWithSsrcMid(ssrc, mid);
627 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
628 EXPECT_FALSE(demuxer_.OnRtpPacket(*p2));
629}
630
631TEST_F(RtpDemuxerTest, NoCallbackOnMidRsidSinkRemovedAfterFirstPacket) {
632 const std::string mid = "v";
633 const std::string rsid = "1";
634 constexpr uint32_t ssrc = 10;
635
636 NiceMock<MockRtpPacketSink> sink;
637 AddSinkBothMidRsid(mid, rsid, &sink);
638
639 auto p1 = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
640 demuxer_.OnRtpPacket(*p1);
641
642 RemoveSink(&sink);
643
644 auto p2 = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
645 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
646 EXPECT_FALSE(demuxer_.OnRtpPacket(*p2));
647}
648
eladalond0244c22017-06-08 11:19:13649// The RSID to SSRC mapping should be one-to-one. If we end up receiving
650// two (or more) packets with the same SSRC, but different RSIDs, we guarantee
Steve Anton53c7ba62017-08-18 17:05:47651// delivery to one of them but not both.
Steve Anton9e0c7422017-08-18 01:59:47652TEST_F(RtpDemuxerTest, FirstSsrcAssociatedWithAnRsidIsNotForgotten) {
eladalond0244c22017-06-08 11:19:13653 // Each sink has a distinct RSID.
654 MockRtpPacketSink sink_a;
655 const std::string rsid_a = "a";
Steve Anton9e0c7422017-08-18 01:59:47656 AddSinkOnlyRsid(rsid_a, &sink_a);
eladalond0244c22017-06-08 11:19:13657
658 MockRtpPacketSink sink_b;
659 const std::string rsid_b = "b";
Steve Anton9e0c7422017-08-18 01:59:47660 AddSinkOnlyRsid(rsid_b, &sink_b);
eladalond0244c22017-06-08 11:19:13661
662 InSequence sequence; // Verify that the order of delivery is unchanged.
663
664 constexpr uint32_t shared_ssrc = 100;
665
666 // First a packet with |rsid_a| is received, and |sink_a| is associated with
667 // its SSRC.
Steve Anton9e0c7422017-08-18 01:59:47668 auto packet_a = CreatePacketWithSsrcRsid(shared_ssrc, rsid_a);
eladalond0244c22017-06-08 11:19:13669 EXPECT_CALL(sink_a, OnRtpPacket(SamePacketAs(*packet_a))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47670 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_a));
eladalond0244c22017-06-08 11:19:13671
Steve Anton53c7ba62017-08-18 17:05:47672 // Second, a packet with |rsid_b| is received. We guarantee that |sink_b|
673 // receives it.
Steve Anton9e0c7422017-08-18 01:59:47674 auto packet_b = CreatePacketWithSsrcRsid(shared_ssrc, rsid_b);
Steve Anton53c7ba62017-08-18 17:05:47675 EXPECT_CALL(sink_a, OnRtpPacket(_)).Times(0);
676 EXPECT_CALL(sink_b, OnRtpPacket(SamePacketAs(*packet_b))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47677 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_b));
eladalond0244c22017-06-08 11:19:13678
679 // Known edge-case; adding a new RSID association makes us re-examine all
680 // SSRCs. |sink_b| may or may not be associated with the SSRC now; we make
Steve Anton53c7ba62017-08-18 17:05:47681 // no promises on that. However, since the RSID is specified and it cannot be
682 // found the packet should be dropped.
eladalon9addbeb2017-06-30 13:26:54683 MockRtpPacketSink sink_c;
684 const std::string rsid_c = "c";
685 constexpr uint32_t some_other_ssrc = shared_ssrc + 1;
Steve Anton9e0c7422017-08-18 01:59:47686 AddSinkOnlySsrc(some_other_ssrc, &sink_c);
Steve Anton53c7ba62017-08-18 17:05:47687
688 auto packet_c = CreatePacketWithSsrcMid(shared_ssrc, rsid_c);
689 EXPECT_CALL(sink_a, OnRtpPacket(_)).Times(0);
690 EXPECT_CALL(sink_b, OnRtpPacket(_)).Times(0);
691 EXPECT_CALL(sink_c, OnRtpPacket(_)).Times(0);
692 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_c));
eladalond0244c22017-06-08 11:19:13693}
694
Steve Anton9e0c7422017-08-18 01:59:47695TEST_F(RtpDemuxerTest, MultipleRsidsOnSameSink) {
eladalond0244c22017-06-08 11:19:13696 MockRtpPacketSink sink;
697 const std::string rsids[] = {"a", "b", "c"};
698
699 for (const std::string& rsid : rsids) {
Steve Anton9e0c7422017-08-18 01:59:47700 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13701 }
702
703 InSequence sequence;
704 for (size_t i = 0; i < arraysize(rsids); i++) {
705 // Assign different SSRCs and sequence numbers to all packets.
706 const uint32_t ssrc = 1000 + static_cast<uint32_t>(i);
Steve Anton53c7ba62017-08-18 17:05:47707 const uint16_t sequence_number = 50 + static_cast<uint16_t>(i);
Steve Anton9e0c7422017-08-18 01:59:47708 auto packet = CreatePacketWithSsrcRsid(ssrc, rsids[i]);
Steve Anton53c7ba62017-08-18 17:05:47709 packet->SetSequenceNumber(sequence_number);
eladalond0244c22017-06-08 11:19:13710 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47711 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13712 }
eladalond0244c22017-06-08 11:19:13713}
714
Steve Anton53c7ba62017-08-18 17:05:47715// RSIDs are given higher priority than SSRC because we believe senders are less
716// likely to mislabel packets with RSID than mislabel them with SSRCs.
Steve Anton9e0c7422017-08-18 01:59:47717TEST_F(RtpDemuxerTest, SinkWithBothRsidAndSsrcAssociations) {
Steve Anton53c7ba62017-08-18 17:05:47718 MockRtpPacketSink sink;
eladalond0244c22017-06-08 11:19:13719 constexpr uint32_t standalone_ssrc = 10101;
720 constexpr uint32_t rsid_ssrc = 20202;
Steve Anton53c7ba62017-08-18 17:05:47721 const std::string rsid = "1";
eladalond0244c22017-06-08 11:19:13722
Steve Anton9e0c7422017-08-18 01:59:47723 AddSinkOnlySsrc(standalone_ssrc, &sink);
724 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13725
726 InSequence sequence;
727
Steve Anton9e0c7422017-08-18 01:59:47728 auto ssrc_packet = CreatePacketWithSsrc(standalone_ssrc);
eladalond0244c22017-06-08 11:19:13729 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*ssrc_packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47730 EXPECT_TRUE(demuxer_.OnRtpPacket(*ssrc_packet));
eladalond0244c22017-06-08 11:19:13731
Steve Anton9e0c7422017-08-18 01:59:47732 auto rsid_packet = CreatePacketWithSsrcRsid(rsid_ssrc, rsid);
eladalond0244c22017-06-08 11:19:13733 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*rsid_packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47734 EXPECT_TRUE(demuxer_.OnRtpPacket(*rsid_packet));
eladalond0244c22017-06-08 11:19:13735}
736
Steve Anton53c7ba62017-08-18 17:05:47737// Packets are always guaranteed to be routed to only one sink.
Steve Anton9e0c7422017-08-18 01:59:47738TEST_F(RtpDemuxerTest, AssociatingByRsidAndBySsrcCannotTriggerDoubleCall) {
eladalond0244c22017-06-08 11:19:13739 constexpr uint32_t ssrc = 10101;
eladalond0244c22017-06-08 11:19:13740 const std::string rsid = "a";
eladalond0244c22017-06-08 11:19:13741
Steve Anton9e0c7422017-08-18 01:59:47742 MockRtpPacketSink sink;
743 AddSinkOnlySsrc(ssrc, &sink);
744 AddSinkOnlyRsid(rsid, &sink);
745
746 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalond0244c22017-06-08 11:19:13747 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47748 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon760a0762017-05-31 16:12:25749}
750
Steve Anton53c7ba62017-08-18 17:05:47751TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToMid) {
752 const std::string mid = "v";
753 constexpr uint32_t ssrc = 10;
754
755 NiceMock<MockRtpPacketSink> sink;
756 AddSinkOnlyMid(mid, &sink);
757
758 MockSsrcBindingObserver observer;
759 RegisterSsrcBindingObserver(&observer);
760
761 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
762 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc));
763 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
764}
765
766TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToRsid) {
767 const std::string rsid = "1";
eladalona52722f2017-06-26 18:23:54768 constexpr uint32_t ssrc = 111;
eladalona52722f2017-06-26 18:23:54769
eladalon5daecca2017-08-04 13:34:54770 // Only RSIDs which the demuxer knows may be resolved.
771 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:47772 AddSinkOnlyRsid(rsid, &sink);
eladalon5daecca2017-08-04 13:34:54773
Steve Anton53c7ba62017-08-18 17:05:47774 NiceMock<MockSsrcBindingObserver> rsid_resolution_observers[3];
eladalona52722f2017-06-26 18:23:54775 for (auto& observer : rsid_resolution_observers) {
Steve Anton9e0c7422017-08-18 01:59:47776 RegisterSsrcBindingObserver(&observer);
Steve Antonb3329172017-08-17 22:23:51777 EXPECT_CALL(observer, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
eladalona52722f2017-06-26 18:23:54778 }
779
Steve Antonb3329172017-08-17 22:23:51780 // The expected calls to OnSsrcBoundToRsid() will be triggered by this.
Steve Anton53c7ba62017-08-18 17:05:47781 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
782 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54783}
784
Steve Anton53c7ba62017-08-18 17:05:47785TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToMidRsid) {
786 const std::string mid = "v";
787 const std::string rsid = "1";
788 constexpr uint32_t ssrc = 10;
eladalon5daecca2017-08-04 13:34:54789
Steve Anton53c7ba62017-08-18 17:05:47790 NiceMock<MockRtpPacketSink> sink;
791 AddSinkBothMidRsid(mid, rsid, &sink);
eladalon5daecca2017-08-04 13:34:54792
Steve Anton53c7ba62017-08-18 17:05:47793 MockSsrcBindingObserver observer;
794 RegisterSsrcBindingObserver(&observer);
795
796 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
797 EXPECT_CALL(observer, OnSsrcBoundToMidRsid(mid, rsid, ssrc));
798 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54799}
800
Steve Anton53c7ba62017-08-18 17:05:47801TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToPayloadType) {
802 constexpr uint8_t payload_type = 3;
803 constexpr uint32_t ssrc = 10;
804
805 RtpDemuxerCriteria criteria;
806 criteria.payload_types = {payload_type};
807 NiceMock<MockRtpPacketSink> sink;
808 AddSink(criteria, &sink);
809
810 MockSsrcBindingObserver observer;
811 RegisterSsrcBindingObserver(&observer);
812
813 auto packet = CreatePacketWithSsrc(ssrc);
814 packet->SetPayloadType(payload_type);
815 EXPECT_CALL(observer, OnSsrcBoundToPayloadType(payload_type, ssrc));
816 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
817}
818
819// If one sink is associated with SSRC x, and another sink with RSID y, then if
820// we receive a packet with both SSRC x and RSID y, route that to only the sink
821// for RSID y since we believe RSID tags to be more trustworthy than signaled
822// SSRCs.
Steve Anton9e0c7422017-08-18 01:59:47823TEST_F(RtpDemuxerTest,
Steve Anton53c7ba62017-08-18 17:05:47824 PacketFittingBothRsidSinkAndSsrcSinkGivenOnlyToRsidSink) {
eladalon5daecca2017-08-04 13:34:54825 constexpr uint32_t ssrc = 111;
826 MockRtpPacketSink ssrc_sink;
Steve Anton9e0c7422017-08-18 01:59:47827 AddSinkOnlySsrc(ssrc, &ssrc_sink);
eladalon5daecca2017-08-04 13:34:54828
829 const std::string rsid = "a";
830 MockRtpPacketSink rsid_sink;
Steve Anton9e0c7422017-08-18 01:59:47831 AddSinkOnlyRsid(rsid, &rsid_sink);
eladalon5daecca2017-08-04 13:34:54832
Steve Anton9e0c7422017-08-18 01:59:47833 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalon5daecca2017-08-04 13:34:54834
Steve Anton53c7ba62017-08-18 17:05:47835 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
836 EXPECT_CALL(rsid_sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
837 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54838}
839
840// We're not expecting RSIDs to be resolved to SSRCs which were previously
841// mapped to sinks, and make no guarantees except for graceful handling.
Steve Anton9e0c7422017-08-18 01:59:47842TEST_F(RtpDemuxerTest,
843 GracefullyHandleRsidBeingMappedToPrevouslyAssociatedSsrc) {
eladalon5daecca2017-08-04 13:34:54844 constexpr uint32_t ssrc = 111;
845 NiceMock<MockRtpPacketSink> ssrc_sink;
Steve Anton9e0c7422017-08-18 01:59:47846 AddSinkOnlySsrc(ssrc, &ssrc_sink);
eladalon5daecca2017-08-04 13:34:54847
848 const std::string rsid = "a";
Steve Anton53c7ba62017-08-18 17:05:47849 NiceMock<MockRtpPacketSink> rsid_sink;
Steve Anton9e0c7422017-08-18 01:59:47850 AddSinkOnlyRsid(rsid, &rsid_sink);
eladalon5daecca2017-08-04 13:34:54851
Steve Anton53c7ba62017-08-18 17:05:47852 NiceMock<MockSsrcBindingObserver> observer;
Steve Anton9e0c7422017-08-18 01:59:47853 RegisterSsrcBindingObserver(&observer);
eladalon5daecca2017-08-04 13:34:54854
855 // The SSRC was mapped to an SSRC sink, but was even active (packets flowed
856 // over it).
Steve Anton9e0c7422017-08-18 01:59:47857 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
858 demuxer_.OnRtpPacket(*packet);
eladalon5daecca2017-08-04 13:34:54859
860 // If the SSRC sink is ever removed, the RSID sink *might* receive indications
861 // of packets, and observers *might* be informed. Only graceful handling
862 // is guaranteed.
Steve Anton9e0c7422017-08-18 01:59:47863 RemoveSink(&ssrc_sink);
eladalon5daecca2017-08-04 13:34:54864 EXPECT_CALL(rsid_sink, OnRtpPacket(SamePacketAs(*packet))).Times(AtLeast(0));
Steve Antonb3329172017-08-17 22:23:51865 EXPECT_CALL(observer, OnSsrcBoundToRsid(rsid, ssrc)).Times(AtLeast(0));
Steve Anton53c7ba62017-08-18 17:05:47866 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
867}
868
869// Tests that when one MID sink is configured, packets that include the MID
870// extension will get routed to that sink and any packets that use the same
871// SSRC as one of those packets later will also get routed to the sink, even
872// if a new SSRC is introduced for the same MID.
873TEST_F(RtpDemuxerTest, RoutedByMidWhenSsrcAdded) {
874 const std::string mid = "v";
875 NiceMock<MockRtpPacketSink> sink;
876 AddSinkOnlyMid(mid, &sink);
877
878 constexpr uint32_t ssrc1 = 10;
879 constexpr uint32_t ssrc2 = 11;
880
881 auto packet_ssrc1_mid = CreatePacketWithSsrcMid(ssrc1, mid);
882 demuxer_.OnRtpPacket(*packet_ssrc1_mid);
883 auto packet_ssrc2_mid = CreatePacketWithSsrcMid(ssrc2, mid);
884 demuxer_.OnRtpPacket(*packet_ssrc2_mid);
885
886 auto packet_ssrc1_only = CreatePacketWithSsrc(ssrc1);
887 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc1_only))).Times(1);
888 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc1_only));
889
890 auto packet_ssrc2_only = CreatePacketWithSsrc(ssrc2);
891 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc2_only))).Times(1);
892 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc2_only));
893}
894
895TEST_F(RtpDemuxerTest, DontLearnMidSsrcBindingBeforeSinkAdded) {
896 const std::string mid = "v";
897 constexpr uint32_t ssrc = 10;
898
899 auto packet_ssrc_mid = CreatePacketWithSsrcMid(ssrc, mid);
900 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_ssrc_mid));
901
902 MockRtpPacketSink sink;
903 AddSinkOnlyMid(mid, &sink);
904
905 auto packet_ssrc_only = CreatePacketWithSsrc(ssrc);
906 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
907 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_ssrc_only));
908}
909
910TEST_F(RtpDemuxerTest, DontForgetMidSsrcBindingWhenSinkRemoved) {
911 const std::string mid = "v";
912 constexpr uint32_t ssrc = 10;
913
914 NiceMock<MockRtpPacketSink> sink1;
915 AddSinkOnlyMid(mid, &sink1);
916
917 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
918 demuxer_.OnRtpPacket(*packet_with_mid);
919
920 RemoveSink(&sink1);
921
922 MockRtpPacketSink sink2;
923 AddSinkOnlyMid(mid, &sink2);
924
925 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
926 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
927 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
928}
929
930// If a sink is added with only a MID, then any packet with that MID no matter
931// the RSID should be routed to that sink.
932TEST_F(RtpDemuxerTest, RoutedByMidWithAnyRsid) {
933 const std::string mid = "v";
934 const std::string rsid1 = "1";
935 const std::string rsid2 = "2";
936 constexpr uint32_t ssrc1 = 10;
937 constexpr uint32_t ssrc2 = 11;
938
939 MockRtpPacketSink sink;
940 AddSinkOnlyMid(mid, &sink);
941
942 InSequence sequence;
943
944 auto packet_ssrc1_rsid1 = CreatePacketWithSsrcMidRsid(ssrc1, mid, rsid1);
945 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc1_rsid1))).Times(1);
946 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc1_rsid1));
947
948 auto packet_ssrc2_rsid2 = CreatePacketWithSsrcMidRsid(ssrc2, mid, rsid2);
949 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc2_rsid2))).Times(1);
950 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc2_rsid2));
951}
952
953// These two tests verify that for a sink added with a MID, RSID pair, if the
954// MID and RSID are learned in separate packets (e.g., because the header
955// extensions are sent separately), then a later packet with just SSRC will get
956// routed to that sink.
957// The first test checks that the functionality works when MID is learned first.
958// The second test checks that the functionality works when RSID is learned
959// first.
960TEST_F(RtpDemuxerTest, LearnMidThenRsidSeparatelyAndRouteBySsrc) {
961 const std::string mid = "v";
962 const std::string rsid = "1";
963 constexpr uint32_t ssrc = 10;
964
965 NiceMock<MockRtpPacketSink> sink;
966 AddSinkBothMidRsid(mid, rsid, &sink);
967
968 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
969 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_mid));
970
971 auto packet_with_rsid = CreatePacketWithSsrcRsid(ssrc, rsid);
972 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_rsid));
973
974 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
975 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
976 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
977}
978
979TEST_F(RtpDemuxerTest, LearnRsidThenMidSeparatelyAndRouteBySsrc) {
980 const std::string mid = "v";
981 const std::string rsid = "1";
982 constexpr uint32_t ssrc = 10;
983
984 NiceMock<MockRtpPacketSink> sink;
985 AddSinkBothMidRsid(mid, rsid, &sink);
986
987 auto packet_with_rsid = CreatePacketWithSsrcRsid(ssrc, rsid);
988 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_rsid));
989
990 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
991 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_mid));
992
993 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
994 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
995 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
996}
997
998TEST_F(RtpDemuxerTest, DontLearnMidRsidBindingBeforeSinkAdded) {
999 const std::string mid = "v";
1000 const std::string rsid = "1";
1001 constexpr uint32_t ssrc = 10;
1002
1003 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
1004 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_both));
1005
1006 MockRtpPacketSink sink;
1007 AddSinkBothMidRsid(mid, rsid, &sink);
1008
1009 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1010 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1011 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1012}
1013
1014TEST_F(RtpDemuxerTest, DontForgetMidRsidBindingWhenSinkRemoved) {
1015 const std::string mid = "v";
1016 const std::string rsid = "1";
1017 constexpr uint32_t ssrc = 10;
1018
1019 NiceMock<MockRtpPacketSink> sink1;
1020 AddSinkBothMidRsid(mid, rsid, &sink1);
1021
1022 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
1023 demuxer_.OnRtpPacket(*packet_with_both);
1024
1025 RemoveSink(&sink1);
1026
1027 MockRtpPacketSink sink2;
1028 AddSinkBothMidRsid(mid, rsid, &sink2);
1029
1030 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1031 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
1032 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1033}
1034
1035TEST_F(RtpDemuxerTest, LearnMidRsidBindingAfterSinkAdded) {
1036 const std::string mid = "v";
1037 const std::string rsid = "1";
1038 constexpr uint32_t ssrc = 10;
1039
1040 NiceMock<MockRtpPacketSink> sink;
1041 AddSinkBothMidRsid(mid, rsid, &sink);
1042
1043 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
1044 demuxer_.OnRtpPacket(*packet_with_both);
1045
1046 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1047 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
1048 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1049}
1050
1051TEST_F(RtpDemuxerTest, DropByPayloadTypeIfNoSink) {
1052 constexpr uint8_t payload_type = 30;
1053 constexpr uint32_t ssrc = 10;
1054
1055 auto packet = CreatePacketWithSsrc(ssrc);
1056 packet->SetPayloadType(payload_type);
1057 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1058}
1059
1060// For legacy applications, it's possible for us to demux if the payload type is
1061// unique. But if multiple sinks are registered with different MIDs and the same
1062// payload types, then we cannot route a packet with just payload type because
1063// it is ambiguous which sink it should be sent to.
1064TEST_F(RtpDemuxerTest, DropByPayloadTypeIfAddedInMultipleSinks) {
1065 const std::string mid1 = "v";
1066 const std::string mid2 = "a";
1067 constexpr uint8_t payload_type = 30;
1068 constexpr uint32_t ssrc = 10;
1069
1070 RtpDemuxerCriteria mid1_pt;
1071 mid1_pt.mid = mid1;
1072 mid1_pt.payload_types = {payload_type};
1073 MockRtpPacketSink sink1;
1074 AddSink(mid1_pt, &sink1);
1075
1076 RtpDemuxerCriteria mid2_pt;
1077 mid2_pt.mid = mid2;
1078 mid2_pt.payload_types = {payload_type};
1079 MockRtpPacketSink sink2;
1080 AddSink(mid2_pt, &sink2);
1081
1082 auto packet = CreatePacketWithSsrc(ssrc);
1083 packet->SetPayloadType(payload_type);
1084
1085 EXPECT_CALL(sink1, OnRtpPacket(_)).Times(0);
1086 EXPECT_CALL(sink2, OnRtpPacket(_)).Times(0);
1087 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1088}
1089
1090// If two sinks are added with different MIDs but the same payload types, then
1091// we cannot demux on the payload type only unless one of the sinks is removed.
1092TEST_F(RtpDemuxerTest, RoutedByPayloadTypeIfAmbiguousSinkRemoved) {
1093 const std::string mid1 = "v";
1094 const std::string mid2 = "a";
1095 constexpr uint8_t payload_type = 30;
1096 constexpr uint32_t ssrc = 10;
1097
1098 RtpDemuxerCriteria mid1_pt;
1099 mid1_pt.mid = mid1;
1100 mid1_pt.payload_types = {payload_type};
1101 MockRtpPacketSink sink1;
1102 AddSink(mid1_pt, &sink1);
1103
1104 RtpDemuxerCriteria mid2_pt;
1105 mid2_pt.mid = mid2;
1106 mid2_pt.payload_types = {payload_type};
1107 MockRtpPacketSink sink2;
1108 AddSink(mid2_pt, &sink2);
1109
1110 RemoveSink(&sink1);
1111
1112 auto packet = CreatePacketWithSsrc(ssrc);
1113 packet->SetPayloadType(payload_type);
1114
1115 EXPECT_CALL(sink1, OnRtpPacket(_)).Times(0);
1116 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1117
1118 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1119}
1120
1121TEST_F(RtpDemuxerTest, RoutedByPayloadTypeLatchesSsrc) {
1122 constexpr uint8_t payload_type = 30;
1123 constexpr uint32_t ssrc = 10;
1124
1125 RtpDemuxerCriteria pt;
1126 pt.payload_types = {payload_type};
1127 NiceMock<MockRtpPacketSink> sink;
1128 AddSink(pt, &sink);
1129
1130 auto packet_with_pt = CreatePacketWithSsrc(ssrc);
1131 packet_with_pt->SetPayloadType(payload_type);
1132 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt));
1133
1134 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1135 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
1136 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1137}
1138
1139// RSIDs are scoped within MID, so if two sinks are registered with the same
1140// RSIDs but different MIDs, then packets containing both extensions should be
1141// routed to the correct one.
1142TEST_F(RtpDemuxerTest, PacketWithSameRsidDifferentMidRoutedToProperSink) {
1143 const std::string mid1 = "mid1";
1144 const std::string mid2 = "mid2";
1145 const std::string rsid = "rsid";
1146 constexpr uint32_t ssrc1 = 10;
1147 constexpr uint32_t ssrc2 = 11;
1148
1149 NiceMock<MockRtpPacketSink> mid1_sink;
1150 AddSinkBothMidRsid(mid1, rsid, &mid1_sink);
1151
1152 MockRtpPacketSink mid2_sink;
1153 AddSinkBothMidRsid(mid2, rsid, &mid2_sink);
1154
1155 auto packet_mid1 = CreatePacketWithSsrcMidRsid(ssrc1, mid1, rsid);
1156 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_mid1));
1157
1158 auto packet_mid2 = CreatePacketWithSsrcMidRsid(ssrc2, mid2, rsid);
1159 EXPECT_CALL(mid2_sink, OnRtpPacket(SamePacketAs(*packet_mid2))).Times(1);
1160 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_mid2));
1161}
1162
1163// If a sink is first bound to a given SSRC by signaling but later a new sink is
1164// bound to a given MID by a later signaling, then when a packet arrives with
1165// both the SSRC and MID, then the signaled MID sink should take precedence.
1166TEST_F(RtpDemuxerTest, SignaledMidShouldOverwriteSignaledSsrc) {
1167 constexpr uint32_t ssrc = 11;
1168 const std::string mid = "mid";
1169
1170 MockRtpPacketSink ssrc_sink;
1171 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1172
1173 MockRtpPacketSink mid_sink;
1174 AddSinkOnlyMid(mid, &mid_sink);
1175
1176 auto p = CreatePacketWithSsrcMid(ssrc, mid);
1177 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
1178 EXPECT_CALL(mid_sink, OnRtpPacket(SamePacketAs(*p))).Times(1);
1179 EXPECT_TRUE(demuxer_.OnRtpPacket(*p));
1180}
1181
1182// Extends the previous test to also ensure that later packets that do not
1183// specify MID are still routed to the MID sink rather than the overwritten SSRC
1184// sink.
1185TEST_F(RtpDemuxerTest, SignaledMidShouldOverwriteSignalledSsrcPersistent) {
1186 constexpr uint32_t ssrc = 11;
1187 const std::string mid = "mid";
1188
1189 MockRtpPacketSink ssrc_sink;
1190 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1191
1192 NiceMock<MockRtpPacketSink> mid_sink;
1193 AddSinkOnlyMid(mid, &mid_sink);
1194
1195 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
1196
1197 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
1198 demuxer_.OnRtpPacket(*packet_with_mid);
1199
1200 auto packet_without_mid = CreatePacketWithSsrc(ssrc);
1201 EXPECT_CALL(mid_sink, OnRtpPacket(SamePacketAs(*packet_without_mid)))
1202 .Times(1);
1203 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_without_mid));
1204}
1205
1206TEST_F(RtpDemuxerTest, RouteByPayloadTypeMultipleMatch) {
1207 constexpr uint32_t ssrc = 10;
1208 constexpr uint8_t pt1 = 30;
1209 constexpr uint8_t pt2 = 31;
1210
1211 MockRtpPacketSink sink;
1212 RtpDemuxerCriteria criteria;
1213 criteria.payload_types = {pt1, pt2};
1214 AddSink(criteria, &sink);
1215
1216 auto packet_with_pt1 = CreatePacketWithSsrc(ssrc);
1217 packet_with_pt1->SetPayloadType(pt1);
1218 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_pt1)));
1219 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt1));
1220
1221 auto packet_with_pt2 = CreatePacketWithSsrc(ssrc);
1222 packet_with_pt2->SetPayloadType(pt2);
1223 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_pt2)));
1224 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt2));
1225}
1226
1227TEST_F(RtpDemuxerTest, DontDemuxOnMidAloneIfAddedWithRsid) {
1228 const std::string mid = "v";
1229 const std::string rsid = "1";
1230 constexpr uint32_t ssrc = 10;
1231
1232 MockRtpPacketSink sink;
1233 AddSinkBothMidRsid(mid, rsid, &sink);
1234
1235 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1236
1237 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
1238 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1239}
1240
1241TEST_F(RtpDemuxerTest, DemuxBySsrcEvenWithMidAndRsid) {
1242 const std::string mid = "v";
1243 const std::string rsid = "1";
1244 constexpr uint32_t ssrc = 10;
1245
1246 RtpDemuxerCriteria criteria;
1247 criteria.rsid = rsid;
1248 criteria.mid = mid;
1249 criteria.ssrcs = {ssrc};
1250 MockRtpPacketSink sink;
1251 AddSink(criteria, &sink);
1252
1253 auto packet = CreatePacketWithSsrc(ssrc);
1254 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1255 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1256}
1257
1258// In slight deviation from the BUNDLE spec, if we match a sink according to
1259// SSRC, then we do not verify payload type against the criteria and defer to
1260// the sink to check that it is correct.
1261TEST_F(RtpDemuxerTest, DoNotCheckPayloadTypeIfMatchedByOtherCriteria) {
1262 constexpr uint32_t ssrc = 10;
1263 constexpr uint8_t payload_type = 30;
1264 constexpr uint8_t different_payload_type = payload_type + 1;
1265
1266 RtpDemuxerCriteria criteria;
1267 criteria.ssrcs = {ssrc};
1268 criteria.payload_types = {payload_type};
1269 MockRtpPacketSink sink;
1270 AddSink(criteria, &sink);
1271
1272 auto packet = CreatePacketWithSsrc(ssrc);
1273 packet->SetPayloadType(different_payload_type);
1274 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1275 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1276}
1277
1278// If a repair packet includes an RSID it should be ignored and the packet
1279// should be routed by its RRID.
1280TEST_F(RtpDemuxerTest, PacketWithRsidAndRridRoutedByRrid) {
1281 const std::string rsid = "1";
1282 const std::string rrid = "1r";
1283 constexpr uint32_t ssrc = 10;
1284
1285 MockRtpPacketSink sink_rsid;
1286 AddSinkOnlyRsid(rsid, &sink_rsid);
1287
1288 MockRtpPacketSink sink_rrid;
1289 AddSinkOnlyRsid(rrid, &sink_rrid);
1290
1291 auto packet = CreatePacketWithSsrcRsidRrid(ssrc, rsid, rrid);
1292 EXPECT_CALL(sink_rsid, OnRtpPacket(_)).Times(0);
1293 EXPECT_CALL(sink_rrid, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1294 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1295}
1296
1297// Same test as above but checks that the latched SSRC routes to the RRID sink.
1298TEST_F(RtpDemuxerTest, PacketWithRsidAndRridLatchesSsrcToRrid) {
1299 const std::string rsid = "1";
1300 const std::string rrid = "1r";
1301 constexpr uint32_t ssrc = 10;
1302
1303 MockRtpPacketSink sink_rsid;
1304 AddSinkOnlyRsid(rsid, &sink_rsid);
1305
1306 NiceMock<MockRtpPacketSink> sink_rrid;
1307 AddSinkOnlyRsid(rrid, &sink_rrid);
1308
1309 auto packet_rsid_rrid = CreatePacketWithSsrcRsidRrid(ssrc, rsid, rrid);
1310 demuxer_.OnRtpPacket(*packet_rsid_rrid);
1311
1312 auto packet_ssrc_only = CreatePacketWithSsrc(ssrc);
1313 EXPECT_CALL(sink_rsid, OnRtpPacket(_)).Times(0);
1314 EXPECT_CALL(sink_rrid, OnRtpPacket(SamePacketAs(*packet_ssrc_only))).Times(1);
1315 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc_only));
1316}
1317
1318// Tests that a packet which includes MID and RSID is dropped and not routed by
1319// SSRC if the MID and RSID do not match an added sink.
1320TEST_F(RtpDemuxerTest, PacketWithMidAndUnknownRsidIsNotRoutedBySsrc) {
1321 constexpr uint32_t ssrc = 10;
1322 const std::string mid = "v";
1323 const std::string rsid = "1";
1324 const std::string wrong_rsid = "2";
1325
1326 RtpDemuxerCriteria criteria;
1327 criteria.mid = mid;
1328 criteria.rsid = rsid;
1329 criteria.ssrcs = {ssrc};
1330 MockRtpPacketSink sink;
1331 AddSink(criteria, &sink);
1332
1333 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, wrong_rsid);
1334 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1335 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1336}
1337
1338// Tests that a packet which includes MID and RSID is dropped and not routed by
1339// payload type if the MID and RSID do not match an added sink.
1340TEST_F(RtpDemuxerTest, PacketWithMidAndUnknownRsidIsNotRoutedByPayloadType) {
1341 constexpr uint32_t ssrc = 10;
1342 const std::string mid = "v";
1343 const std::string rsid = "1";
1344 const std::string wrong_rsid = "2";
1345 constexpr uint8_t payload_type = 30;
1346
1347 RtpDemuxerCriteria criteria;
1348 criteria.mid = mid;
1349 criteria.rsid = rsid;
1350 criteria.payload_types = {payload_type};
1351 MockRtpPacketSink sink;
1352 AddSink(criteria, &sink);
1353
1354 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, wrong_rsid);
1355 packet->SetPayloadType(payload_type);
1356 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1357 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1358}
1359
1360// Observers are only notified of an SSRC binding to an RSID if we care about
1361// the RSID (i.e., have a sink added for that RSID).
1362TEST_F(RtpDemuxerTest, ObserversNotNotifiedOfUntrackedRsids) {
1363 const std::string rsid = "1";
1364 constexpr uint32_t ssrc = 111;
1365
1366 MockSsrcBindingObserver rsid_resolution_observers[3];
1367 for (auto& observer : rsid_resolution_observers) {
1368 RegisterSsrcBindingObserver(&observer);
1369 EXPECT_CALL(observer, OnSsrcBoundToRsid(_, _)).Times(0);
1370 }
1371
1372 // Since no sink is registered for this SSRC/RSID, expect the packet to not be
1373 // routed and no observers notified of the SSRC -> RSID binding.
1374 EXPECT_FALSE(demuxer_.OnRtpPacket(*CreatePacketWithSsrcRsid(ssrc, rsid)));
1375}
1376
1377// Ensure that observers are notified of SSRC bindings only once per unique
1378// binding source (e.g., SSRC -> MID, SSRC -> RSID, etc.)
1379TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundtoMidOnlyOnce) {
1380 const std::string mid = "v";
1381 constexpr uint32_t ssrc = 10;
1382
1383 NiceMock<MockRtpPacketSink> sink;
1384 AddSinkOnlyMid(mid, &sink);
1385
1386 MockSsrcBindingObserver observer;
1387 RegisterSsrcBindingObserver(&observer);
1388
1389 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc)).Times(1);
1390
1391 demuxer_.OnRtpPacket(*CreatePacketWithSsrcMid(ssrc, mid));
1392 demuxer_.OnRtpPacket(*CreatePacketWithSsrcMid(ssrc, mid));
1393}
1394
1395// Ensure that when a new SSRC -> MID binding is discovered observers are also
1396// notified of that, even if there has already been an SSRC bound to the MID.
1397TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundtoMidWhenSsrcChanges) {
1398 const std::string mid = "v";
1399 constexpr uint32_t ssrc1 = 10;
1400 constexpr uint32_t ssrc2 = 11;
1401
1402 NiceMock<MockRtpPacketSink> sink;
1403 AddSinkOnlyMid(mid, &sink);
1404
1405 MockSsrcBindingObserver observer;
1406 RegisterSsrcBindingObserver(&observer);
1407
1408 InSequence seq;
1409 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc1)).Times(1);
1410 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc2)).Times(1);
1411
1412 auto p1 = CreatePacketWithSsrcMid(ssrc1, mid);
1413 demuxer_.OnRtpPacket(*p1);
1414
1415 auto p2 = CreatePacketWithSsrcMid(ssrc2, mid);
1416 demuxer_.OnRtpPacket(*p2);
eladalona52722f2017-06-26 18:23:541417}
1418
Steve Anton9e0c7422017-08-18 01:59:471419TEST_F(RtpDemuxerTest, DeregisteredRsidObserversNotInformedOfResolutions) {
eladalona52722f2017-06-26 18:23:541420 constexpr uint32_t ssrc = 111;
1421 const std::string rsid = "a";
1422 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:471423 AddSinkOnlyRsid(rsid, &sink);
eladalona52722f2017-06-26 18:23:541424
1425 // Register several, then deregister only one, to show that not all of the
1426 // observers had been forgotten when one was removed.
Steve Antonb3329172017-08-17 22:23:511427 MockSsrcBindingObserver observer_1;
1428 MockSsrcBindingObserver observer_2_removed;
1429 MockSsrcBindingObserver observer_3;
eladalona52722f2017-06-26 18:23:541430
Steve Anton9e0c7422017-08-18 01:59:471431 RegisterSsrcBindingObserver(&observer_1);
1432 RegisterSsrcBindingObserver(&observer_2_removed);
1433 RegisterSsrcBindingObserver(&observer_3);
eladalona52722f2017-06-26 18:23:541434
Steve Anton9e0c7422017-08-18 01:59:471435 DeregisterSsrcBindingObserver(&observer_2_removed);
eladalona52722f2017-06-26 18:23:541436
Steve Antonb3329172017-08-17 22:23:511437 EXPECT_CALL(observer_1, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
1438 EXPECT_CALL(observer_2_removed, OnSsrcBoundToRsid(_, _)).Times(0);
1439 EXPECT_CALL(observer_3, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
eladalona52722f2017-06-26 18:23:541440
Steve Antonb3329172017-08-17 22:23:511441 // The expected calls to OnSsrcBoundToRsid() will be triggered by this.
Steve Anton9e0c7422017-08-18 01:59:471442 demuxer_.OnRtpPacket(*CreatePacketWithSsrcRsid(ssrc, rsid));
eladalona52722f2017-06-26 18:23:541443}
1444
Steve Anton53c7ba62017-08-18 17:05:471445TEST_F(RtpDemuxerTest,
1446 PacketFittingBothRsidSinkAndSsrcSinkTriggersResolutionCallbacks) {
1447 constexpr uint32_t ssrc = 111;
1448 NiceMock<MockRtpPacketSink> ssrc_sink;
1449 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1450
1451 const std::string rsid = "a";
1452 NiceMock<MockRtpPacketSink> rsid_sink;
1453 AddSinkOnlyRsid(rsid, &rsid_sink);
1454
1455 MockSsrcBindingObserver observer;
1456 RegisterSsrcBindingObserver(&observer);
1457
1458 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
1459 EXPECT_CALL(observer, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
1460 demuxer_.OnRtpPacket(*packet);
1461}
1462
1463TEST_F(RtpDemuxerTest, MaliciousPeerCannotCauseMemoryOveruse) {
1464 const std::string mid = "v";
1465
1466 NiceMock<MockRtpPacketSink> sink;
1467 AddSinkOnlyMid(mid, &sink);
1468
1469 MockSsrcBindingObserver observer;
1470 RegisterSsrcBindingObserver(&observer);
1471
1472 EXPECT_CALL(observer, OnSsrcBoundToMid(_, _))
1473 .Times(AtMost(RtpDemuxer::kMaxSsrcBindings));
1474
1475 for (int i = 0; i < RtpDemuxer::kMaxSsrcBindings + 1; i++) {
1476 auto packet = CreatePacketWithSsrcMid(i, mid);
1477 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1478 }
1479}
1480
eladalon760a0762017-05-31 16:12:251481#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
Steve Anton53c7ba62017-08-18 17:05:471482
1483TEST_F(RtpDemuxerTest, CriteriaMustBeNonEmpty) {
eladalond0244c22017-06-08 11:19:131484 MockRtpPacketSink sink;
Steve Anton53c7ba62017-08-18 17:05:471485 RtpDemuxerCriteria criteria;
1486 EXPECT_DEATH(AddSink(criteria, &sink), "");
eladalond0244c22017-06-08 11:19:131487}
1488
Steve Anton9e0c7422017-08-18 01:59:471489TEST_F(RtpDemuxerTest, RsidMustBeAlphaNumeric) {
eladalond0244c22017-06-08 11:19:131490 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:471491 EXPECT_DEATH(AddSinkOnlyRsid("a_3", &sink), "");
eladalond0244c22017-06-08 11:19:131492}
1493
Steve Anton53c7ba62017-08-18 17:05:471494TEST_F(RtpDemuxerTest, MidMustBeAlphaNumeric) {
1495 MockRtpPacketSink sink;
1496 EXPECT_DEATH(AddSinkOnlyMid("a_3", &sink), "");
1497}
1498
Steve Anton9e0c7422017-08-18 01:59:471499TEST_F(RtpDemuxerTest, RsidMustNotExceedMaximumLength) {
eladalond0244c22017-06-08 11:19:131500 MockRtpPacketSink sink;
1501 std::string rsid(StreamId::kMaxSize + 1, 'a');
Steve Anton9e0c7422017-08-18 01:59:471502 EXPECT_DEATH(AddSinkOnlyRsid(rsid, &sink), "");
eladalond0244c22017-06-08 11:19:131503}
1504
Steve Anton53c7ba62017-08-18 17:05:471505TEST_F(RtpDemuxerTest, MidMustNotExceedMaximumLength) {
Steve Anton9e0c7422017-08-18 01:59:471506 MockRtpPacketSink sink;
Steve Anton53c7ba62017-08-18 17:05:471507 std::string mid(Mid::kMaxSize + 1, 'a');
1508 EXPECT_DEATH(AddSinkOnlyMid(mid, &sink), "");
eladalon760a0762017-05-31 16:12:251509}
eladalona52722f2017-06-26 18:23:541510
Steve Anton53c7ba62017-08-18 17:05:471511TEST_F(RtpDemuxerTest, DoubleRegisterationOfSsrcBindingObserverDisallowed) {
Steve Antonb3329172017-08-17 22:23:511512 MockSsrcBindingObserver observer;
Steve Anton9e0c7422017-08-18 01:59:471513 RegisterSsrcBindingObserver(&observer);
1514 EXPECT_DEATH(RegisterSsrcBindingObserver(&observer), "");
eladalona52722f2017-06-26 18:23:541515}
1516
Steve Anton9e0c7422017-08-18 01:59:471517TEST_F(RtpDemuxerTest,
Steve Anton53c7ba62017-08-18 17:05:471518 DregisterationOfNeverRegisteredSsrcBindingObserverDisallowed) {
Steve Antonb3329172017-08-17 22:23:511519 MockSsrcBindingObserver observer;
Steve Anton9e0c7422017-08-18 01:59:471520 EXPECT_DEATH(DeregisterSsrcBindingObserver(&observer), "");
eladalona52722f2017-06-26 18:23:541521}
1522
eladalon760a0762017-05-31 16:12:251523#endif
1524
1525} // namespace
1526} // namespace webrtc