blob: 39dc67b0a17b7ef20f726a6ff699238a280403ac [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
11#include "webrtc/call/rtp_demuxer.h"
12
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
Steve Antonb3329172017-08-17 22:23:5117#include "webrtc/call/ssrc_binding_observer.h"
eladalone2173d92017-07-28 17:05:4518#include "webrtc/call/test/mock_rtp_packet_sink_interface.h"
eladalona52722f2017-06-26 18:23:5419#include "webrtc/common_types.h"
eladalond0244c22017-06-08 11:19:1320#include "webrtc/modules/rtp_rtcp/include/rtp_header_extension_map.h"
21#include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h"
eladalon760a0762017-05-31 16:12:2522#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
Edward Lemurc20978e2017-07-06 17:44:3423#include "webrtc/rtc_base/arraysize.h"
24#include "webrtc/rtc_base/basictypes.h"
25#include "webrtc/rtc_base/checks.h"
26#include "webrtc/rtc_base/ptr_util.h"
eladalon760a0762017-05-31 16:12:2527#include "webrtc/test/gmock.h"
28#include "webrtc/test/gtest.h"
29
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++) {
Steve Anton9e0c7422017-08-18 01:59:47349 auto packet = CreatePacketWithSsrcRsid(i, rsids[i]);
eladalond0244c22017-06-08 11:19:13350 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47351 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13352 }
353}
354
Steve Anton53c7ba62017-08-18 17:05:47355TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByMid) {
356 const std::string mids[] = {"a", "v", "s"};
357 MockRtpPacketSink sinks[arraysize(mids)];
358 for (size_t i = 0; i < arraysize(mids); i++) {
359 AddSinkOnlyMid(mids[i], &sinks[i]);
360 }
361
362 for (size_t i = 0; i < arraysize(mids); i++) {
363 auto packet = CreatePacketWithSsrcMid(i, mids[i]);
364 EXPECT_CALL(sinks[i], OnRtpPacket(SamePacketAs(*packet))).Times(1);
365 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
366 }
367}
368
369TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByMidAndRsid) {
370 const std::string mid = "v";
371 const std::string rsid = "1";
372 constexpr uint32_t ssrc = 10;
373
374 MockRtpPacketSink sink;
375 AddSinkBothMidRsid(mid, rsid, &sink);
376
377 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
378 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
379 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
380}
381
382TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByRepairedRsid) {
383 const std::string rrid = "1";
384 constexpr uint32_t ssrc = 10;
385
386 MockRtpPacketSink sink;
387 AddSinkOnlyRsid(rrid, &sink);
388
389 auto packet_with_rrid = CreatePacketWithSsrcRrid(ssrc, rrid);
390 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_rrid))).Times(1);
391 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_rrid));
392}
393
394TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSinkByPayloadType) {
395 constexpr uint32_t ssrc = 10;
396 constexpr uint8_t payload_type = 30;
397
398 MockRtpPacketSink sink;
399 RtpDemuxerCriteria criteria;
400 criteria.payload_types = {payload_type};
401 AddSink(criteria, &sink);
402
403 auto packet = CreatePacketWithSsrc(ssrc);
404 packet->SetPayloadType(payload_type);
405 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
406 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
407}
408
Steve Anton9e0c7422017-08-18 01:59:47409TEST_F(RtpDemuxerTest, PacketsDeliveredInRightOrder) {
eladalona52722f2017-06-26 18:23:54410 constexpr uint32_t ssrc = 101;
411 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47412 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13413
414 std::unique_ptr<RtpPacketReceived> packets[5];
415 for (size_t i = 0; i < arraysize(packets); i++) {
Steve Anton9e0c7422017-08-18 01:59:47416 packets[i] = CreatePacketWithSsrc(ssrc);
417 packets[i]->SetSequenceNumber(i);
eladalond0244c22017-06-08 11:19:13418 }
419
420 InSequence sequence;
421 for (const auto& packet : packets) {
eladalona52722f2017-06-26 18:23:54422 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
eladalond0244c22017-06-08 11:19:13423 }
424
425 for (const auto& packet : packets) {
Steve Anton9e0c7422017-08-18 01:59:47426 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13427 }
eladalond0244c22017-06-08 11:19:13428}
429
Steve Anton9e0c7422017-08-18 01:59:47430TEST_F(RtpDemuxerTest, SinkMappedToMultipleSsrcs) {
eladalond0244c22017-06-08 11:19:13431 constexpr uint32_t ssrcs[] = {404, 505, 606};
432 MockRtpPacketSink sink;
433 for (uint32_t ssrc : ssrcs) {
Steve Anton9e0c7422017-08-18 01:59:47434 AddSinkOnlySsrc(ssrc, &sink);
eladalon760a0762017-05-31 16:12:25435 }
436
437 // The sink which is associated with multiple SSRCs gets the callback
438 // triggered for each of those SSRCs.
eladalond0244c22017-06-08 11:19:13439 for (uint32_t ssrc : ssrcs) {
Steve Anton9e0c7422017-08-18 01:59:47440 auto packet = CreatePacketWithSsrc(ssrc);
Steve Anton53c7ba62017-08-18 17:05:47441 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47442 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon760a0762017-05-31 16:12:25443 }
eladalon760a0762017-05-31 16:12:25444}
445
Steve Anton9e0c7422017-08-18 01:59:47446TEST_F(RtpDemuxerTest, NoCallbackOnSsrcSinkRemovedBeforeFirstPacket) {
eladalond0244c22017-06-08 11:19:13447 constexpr uint32_t ssrc = 404;
448 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47449 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13450
Steve Anton9e0c7422017-08-18 01:59:47451 ASSERT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13452
453 // The removed sink does not get callbacks.
Steve Anton9e0c7422017-08-18 01:59:47454 auto packet = CreatePacketWithSsrc(ssrc);
eladalond0244c22017-06-08 11:19:13455 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47456 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13457}
458
Steve Anton9e0c7422017-08-18 01:59:47459TEST_F(RtpDemuxerTest, NoCallbackOnSsrcSinkRemovedAfterFirstPacket) {
eladalond0244c22017-06-08 11:19:13460 constexpr uint32_t ssrc = 404;
461 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:47462 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13463
464 InSequence sequence;
Steve Anton9e0c7422017-08-18 01:59:47465 for (size_t i = 0; i < 10; i++) {
466 ASSERT_TRUE(demuxer_.OnRtpPacket(*CreatePacketWithSsrc(ssrc)));
eladalon760a0762017-05-31 16:12:25467 }
468
Steve Anton9e0c7422017-08-18 01:59:47469 ASSERT_TRUE(RemoveSink(&sink));
eladalon760a0762017-05-31 16:12:25470
eladalond0244c22017-06-08 11:19:13471 // The removed sink does not get callbacks.
Steve Anton9e0c7422017-08-18 01:59:47472 auto packet = CreatePacketWithSsrc(ssrc);
eladalond0244c22017-06-08 11:19:13473 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47474 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13475}
476
eladalon5daecca2017-08-04 13:34:54477// An SSRC may only be mapped to a single sink. However, since configuration
478// of this associations might come from the network, we need to fail gracefully.
Steve Anton9e0c7422017-08-18 01:59:47479TEST_F(RtpDemuxerTest, OnlyOneSinkPerSsrcGetsOnRtpPacketTriggered) {
eladalon5daecca2017-08-04 13:34:54480 MockRtpPacketSink sinks[3];
481 constexpr uint32_t ssrc = 404;
Steve Anton9e0c7422017-08-18 01:59:47482 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sinks[0]));
483 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sinks[1]));
484 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sinks[2]));
eladalon5daecca2017-08-04 13:34:54485
486 // The first sink associated with the SSRC remains active; other sinks
487 // were not really added, and so do not get OnRtpPacket() called.
Steve Anton9e0c7422017-08-18 01:59:47488 auto packet = CreatePacketWithSsrc(ssrc);
eladalon5daecca2017-08-04 13:34:54489 EXPECT_CALL(sinks[0], OnRtpPacket(SamePacketAs(*packet))).Times(1);
490 EXPECT_CALL(sinks[1], OnRtpPacket(_)).Times(0);
491 EXPECT_CALL(sinks[2], OnRtpPacket(_)).Times(0);
Steve Anton9e0c7422017-08-18 01:59:47492 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54493}
494
Steve Anton9e0c7422017-08-18 01:59:47495TEST_F(RtpDemuxerTest, NoRepeatedCallbackOnRepeatedAddSinkForSameSink) {
eladalond0244c22017-06-08 11:19:13496 constexpr uint32_t ssrc = 111;
497 MockRtpPacketSink sink;
498
Steve Anton9e0c7422017-08-18 01:59:47499 ASSERT_TRUE(AddSinkOnlySsrc(ssrc, &sink));
500 ASSERT_FALSE(AddSinkOnlySsrc(ssrc, &sink));
eladalond0244c22017-06-08 11:19:13501
Steve Anton9e0c7422017-08-18 01:59:47502 auto packet = CreatePacketWithSsrc(ssrc);
eladalond0244c22017-06-08 11:19:13503 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47504 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13505}
506
Steve Anton9e0c7422017-08-18 01:59:47507TEST_F(RtpDemuxerTest, RemoveSinkReturnsFalseForNeverAddedSink) {
eladalond0244c22017-06-08 11:19:13508 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47509 EXPECT_FALSE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13510}
511
Steve Anton9e0c7422017-08-18 01:59:47512TEST_F(RtpDemuxerTest, RemoveSinkReturnsTrueForPreviouslyAddedSsrcSink) {
eladalond0244c22017-06-08 11:19:13513 constexpr uint32_t ssrc = 101;
514 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47515 AddSinkOnlySsrc(ssrc, &sink);
eladalond0244c22017-06-08 11:19:13516
Steve Anton9e0c7422017-08-18 01:59:47517 EXPECT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13518}
519
Steve Anton9e0c7422017-08-18 01:59:47520TEST_F(RtpDemuxerTest,
521 RemoveSinkReturnsTrueForUnresolvedPreviouslyAddedRsidSink) {
eladalond0244c22017-06-08 11:19:13522 const std::string rsid = "a";
523 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:47524 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13525
Steve Anton9e0c7422017-08-18 01:59:47526 EXPECT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13527}
528
Steve Anton9e0c7422017-08-18 01:59:47529TEST_F(RtpDemuxerTest,
530 RemoveSinkReturnsTrueForResolvedPreviouslyAddedRsidSink) {
eladalond0244c22017-06-08 11:19:13531 const std::string rsid = "a";
532 constexpr uint32_t ssrc = 101;
533 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:47534 AddSinkOnlyRsid(rsid, &sink);
535 ASSERT_TRUE(demuxer_.OnRtpPacket(*CreatePacketWithSsrcRsid(ssrc, rsid)));
eladalond0244c22017-06-08 11:19:13536
Steve Anton9e0c7422017-08-18 01:59:47537 EXPECT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13538}
539
Steve Anton53c7ba62017-08-18 17:05:47540TEST_F(RtpDemuxerTest, RsidLearnedAndLaterPacketsDeliveredWithOnlySsrc) {
eladalond0244c22017-06-08 11:19:13541 MockRtpPacketSink sink;
542 const std::string rsid = "a";
Steve Anton9e0c7422017-08-18 01:59:47543 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13544
545 // Create a sequence of RTP packets, where only the first one actually
546 // mentions the RSID.
547 std::unique_ptr<RtpPacketReceived> packets[5];
548 constexpr uint32_t rsid_ssrc = 111;
Steve Anton9e0c7422017-08-18 01:59:47549 packets[0] = CreatePacketWithSsrcRsid(rsid_ssrc, rsid);
eladalond0244c22017-06-08 11:19:13550 for (size_t i = 1; i < arraysize(packets); i++) {
Steve Anton9e0c7422017-08-18 01:59:47551 packets[i] = CreatePacketWithSsrc(rsid_ssrc);
eladalon760a0762017-05-31 16:12:25552 }
eladalond0244c22017-06-08 11:19:13553
554 // The first packet associates the RSID with the SSRC, thereby allowing the
555 // demuxer to correctly demux all of the packets.
556 InSequence sequence;
557 for (const auto& packet : packets) {
558 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
559 }
560 for (const auto& packet : packets) {
Steve Anton9e0c7422017-08-18 01:59:47561 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13562 }
eladalond0244c22017-06-08 11:19:13563}
564
Steve Anton9e0c7422017-08-18 01:59:47565TEST_F(RtpDemuxerTest, NoCallbackOnRsidSinkRemovedBeforeFirstPacket) {
eladalond0244c22017-06-08 11:19:13566 MockRtpPacketSink sink;
567 const std::string rsid = "a";
Steve Anton9e0c7422017-08-18 01:59:47568 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13569
570 // Sink removed - it won't get triggers even if packets with its RSID arrive.
Steve Anton9e0c7422017-08-18 01:59:47571 ASSERT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13572
573 constexpr uint32_t ssrc = 111;
Steve Anton9e0c7422017-08-18 01:59:47574 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalond0244c22017-06-08 11:19:13575 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47576 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13577}
578
Steve Anton9e0c7422017-08-18 01:59:47579TEST_F(RtpDemuxerTest, NoCallbackOnRsidSinkRemovedAfterFirstPacket) {
eladalond0244c22017-06-08 11:19:13580 NiceMock<MockRtpPacketSink> sink;
581 const std::string rsid = "a";
Steve Anton9e0c7422017-08-18 01:59:47582 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13583
584 InSequence sequence;
585 constexpr uint32_t ssrc = 111;
Steve Anton9e0c7422017-08-18 01:59:47586 for (size_t i = 0; i < 10; i++) {
587 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
588 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13589 }
590
591 // Sink removed - it won't get triggers even if packets with its RSID arrive.
Steve Anton9e0c7422017-08-18 01:59:47592 ASSERT_TRUE(RemoveSink(&sink));
eladalond0244c22017-06-08 11:19:13593
Steve Anton9e0c7422017-08-18 01:59:47594 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalond0244c22017-06-08 11:19:13595 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0); // Not called.
Steve Anton9e0c7422017-08-18 01:59:47596 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13597}
598
Steve Anton53c7ba62017-08-18 17:05:47599TEST_F(RtpDemuxerTest, NoCallbackOnMidSinkRemovedBeforeFirstPacket) {
600 const std::string mid = "v";
601 constexpr uint32_t ssrc = 10;
602
603 MockRtpPacketSink sink;
604 AddSinkOnlyMid(mid, &sink);
605 RemoveSink(&sink);
606
607 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
608 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
609 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
610}
611
612TEST_F(RtpDemuxerTest, NoCallbackOnMidSinkRemovedAfterFirstPacket) {
613 const std::string mid = "v";
614 constexpr uint32_t ssrc = 10;
615
616 NiceMock<MockRtpPacketSink> sink;
617 AddSinkOnlyMid(mid, &sink);
618
619 auto p1 = CreatePacketWithSsrcMid(ssrc, mid);
620 demuxer_.OnRtpPacket(*p1);
621
622 RemoveSink(&sink);
623
624 auto p2 = CreatePacketWithSsrcMid(ssrc, mid);
625 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
626 EXPECT_FALSE(demuxer_.OnRtpPacket(*p2));
627}
628
629TEST_F(RtpDemuxerTest, NoCallbackOnMidRsidSinkRemovedAfterFirstPacket) {
630 const std::string mid = "v";
631 const std::string rsid = "1";
632 constexpr uint32_t ssrc = 10;
633
634 NiceMock<MockRtpPacketSink> sink;
635 AddSinkBothMidRsid(mid, rsid, &sink);
636
637 auto p1 = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
638 demuxer_.OnRtpPacket(*p1);
639
640 RemoveSink(&sink);
641
642 auto p2 = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
643 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
644 EXPECT_FALSE(demuxer_.OnRtpPacket(*p2));
645}
646
eladalond0244c22017-06-08 11:19:13647// The RSID to SSRC mapping should be one-to-one. If we end up receiving
648// two (or more) packets with the same SSRC, but different RSIDs, we guarantee
Steve Anton53c7ba62017-08-18 17:05:47649// delivery to one of them but not both.
Steve Anton9e0c7422017-08-18 01:59:47650TEST_F(RtpDemuxerTest, FirstSsrcAssociatedWithAnRsidIsNotForgotten) {
eladalond0244c22017-06-08 11:19:13651 // Each sink has a distinct RSID.
652 MockRtpPacketSink sink_a;
653 const std::string rsid_a = "a";
Steve Anton9e0c7422017-08-18 01:59:47654 AddSinkOnlyRsid(rsid_a, &sink_a);
eladalond0244c22017-06-08 11:19:13655
656 MockRtpPacketSink sink_b;
657 const std::string rsid_b = "b";
Steve Anton9e0c7422017-08-18 01:59:47658 AddSinkOnlyRsid(rsid_b, &sink_b);
eladalond0244c22017-06-08 11:19:13659
660 InSequence sequence; // Verify that the order of delivery is unchanged.
661
662 constexpr uint32_t shared_ssrc = 100;
663
664 // First a packet with |rsid_a| is received, and |sink_a| is associated with
665 // its SSRC.
Steve Anton9e0c7422017-08-18 01:59:47666 auto packet_a = CreatePacketWithSsrcRsid(shared_ssrc, rsid_a);
eladalond0244c22017-06-08 11:19:13667 EXPECT_CALL(sink_a, OnRtpPacket(SamePacketAs(*packet_a))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47668 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_a));
eladalond0244c22017-06-08 11:19:13669
Steve Anton53c7ba62017-08-18 17:05:47670 // Second, a packet with |rsid_b| is received. We guarantee that |sink_b|
671 // receives it.
Steve Anton9e0c7422017-08-18 01:59:47672 auto packet_b = CreatePacketWithSsrcRsid(shared_ssrc, rsid_b);
Steve Anton53c7ba62017-08-18 17:05:47673 EXPECT_CALL(sink_a, OnRtpPacket(_)).Times(0);
674 EXPECT_CALL(sink_b, OnRtpPacket(SamePacketAs(*packet_b))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47675 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_b));
eladalond0244c22017-06-08 11:19:13676
677 // Known edge-case; adding a new RSID association makes us re-examine all
678 // SSRCs. |sink_b| may or may not be associated with the SSRC now; we make
Steve Anton53c7ba62017-08-18 17:05:47679 // no promises on that. However, since the RSID is specified and it cannot be
680 // found the packet should be dropped.
eladalon9addbeb2017-06-30 13:26:54681 MockRtpPacketSink sink_c;
682 const std::string rsid_c = "c";
683 constexpr uint32_t some_other_ssrc = shared_ssrc + 1;
Steve Anton9e0c7422017-08-18 01:59:47684 AddSinkOnlySsrc(some_other_ssrc, &sink_c);
Steve Anton53c7ba62017-08-18 17:05:47685
686 auto packet_c = CreatePacketWithSsrcMid(shared_ssrc, rsid_c);
687 EXPECT_CALL(sink_a, OnRtpPacket(_)).Times(0);
688 EXPECT_CALL(sink_b, OnRtpPacket(_)).Times(0);
689 EXPECT_CALL(sink_c, OnRtpPacket(_)).Times(0);
690 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_c));
eladalond0244c22017-06-08 11:19:13691}
692
Steve Anton9e0c7422017-08-18 01:59:47693TEST_F(RtpDemuxerTest, MultipleRsidsOnSameSink) {
eladalond0244c22017-06-08 11:19:13694 MockRtpPacketSink sink;
695 const std::string rsids[] = {"a", "b", "c"};
696
697 for (const std::string& rsid : rsids) {
Steve Anton9e0c7422017-08-18 01:59:47698 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13699 }
700
701 InSequence sequence;
702 for (size_t i = 0; i < arraysize(rsids); i++) {
703 // Assign different SSRCs and sequence numbers to all packets.
704 const uint32_t ssrc = 1000 + static_cast<uint32_t>(i);
Steve Anton53c7ba62017-08-18 17:05:47705 const uint16_t sequence_number = 50 + static_cast<uint16_t>(i);
Steve Anton9e0c7422017-08-18 01:59:47706 auto packet = CreatePacketWithSsrcRsid(ssrc, rsids[i]);
Steve Anton53c7ba62017-08-18 17:05:47707 packet->SetSequenceNumber(sequence_number);
eladalond0244c22017-06-08 11:19:13708 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47709 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalond0244c22017-06-08 11:19:13710 }
eladalond0244c22017-06-08 11:19:13711}
712
Steve Anton53c7ba62017-08-18 17:05:47713// RSIDs are given higher priority than SSRC because we believe senders are less
714// likely to mislabel packets with RSID than mislabel them with SSRCs.
Steve Anton9e0c7422017-08-18 01:59:47715TEST_F(RtpDemuxerTest, SinkWithBothRsidAndSsrcAssociations) {
Steve Anton53c7ba62017-08-18 17:05:47716 MockRtpPacketSink sink;
eladalond0244c22017-06-08 11:19:13717 constexpr uint32_t standalone_ssrc = 10101;
718 constexpr uint32_t rsid_ssrc = 20202;
Steve Anton53c7ba62017-08-18 17:05:47719 const std::string rsid = "1";
eladalond0244c22017-06-08 11:19:13720
Steve Anton9e0c7422017-08-18 01:59:47721 AddSinkOnlySsrc(standalone_ssrc, &sink);
722 AddSinkOnlyRsid(rsid, &sink);
eladalond0244c22017-06-08 11:19:13723
724 InSequence sequence;
725
Steve Anton9e0c7422017-08-18 01:59:47726 auto ssrc_packet = CreatePacketWithSsrc(standalone_ssrc);
eladalond0244c22017-06-08 11:19:13727 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*ssrc_packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47728 EXPECT_TRUE(demuxer_.OnRtpPacket(*ssrc_packet));
eladalond0244c22017-06-08 11:19:13729
Steve Anton9e0c7422017-08-18 01:59:47730 auto rsid_packet = CreatePacketWithSsrcRsid(rsid_ssrc, rsid);
eladalond0244c22017-06-08 11:19:13731 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*rsid_packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47732 EXPECT_TRUE(demuxer_.OnRtpPacket(*rsid_packet));
eladalond0244c22017-06-08 11:19:13733}
734
Steve Anton53c7ba62017-08-18 17:05:47735// Packets are always guaranteed to be routed to only one sink.
Steve Anton9e0c7422017-08-18 01:59:47736TEST_F(RtpDemuxerTest, AssociatingByRsidAndBySsrcCannotTriggerDoubleCall) {
eladalond0244c22017-06-08 11:19:13737 constexpr uint32_t ssrc = 10101;
eladalond0244c22017-06-08 11:19:13738 const std::string rsid = "a";
eladalond0244c22017-06-08 11:19:13739
Steve Anton9e0c7422017-08-18 01:59:47740 MockRtpPacketSink sink;
741 AddSinkOnlySsrc(ssrc, &sink);
742 AddSinkOnlyRsid(rsid, &sink);
743
744 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalond0244c22017-06-08 11:19:13745 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
Steve Anton9e0c7422017-08-18 01:59:47746 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon760a0762017-05-31 16:12:25747}
748
Steve Anton53c7ba62017-08-18 17:05:47749TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToMid) {
750 const std::string mid = "v";
751 constexpr uint32_t ssrc = 10;
752
753 NiceMock<MockRtpPacketSink> sink;
754 AddSinkOnlyMid(mid, &sink);
755
756 MockSsrcBindingObserver observer;
757 RegisterSsrcBindingObserver(&observer);
758
759 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
760 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc));
761 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
762}
763
764TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToRsid) {
765 const std::string rsid = "1";
eladalona52722f2017-06-26 18:23:54766 constexpr uint32_t ssrc = 111;
eladalona52722f2017-06-26 18:23:54767
eladalon5daecca2017-08-04 13:34:54768 // Only RSIDs which the demuxer knows may be resolved.
769 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:47770 AddSinkOnlyRsid(rsid, &sink);
eladalon5daecca2017-08-04 13:34:54771
Steve Anton53c7ba62017-08-18 17:05:47772 NiceMock<MockSsrcBindingObserver> rsid_resolution_observers[3];
eladalona52722f2017-06-26 18:23:54773 for (auto& observer : rsid_resolution_observers) {
Steve Anton9e0c7422017-08-18 01:59:47774 RegisterSsrcBindingObserver(&observer);
Steve Antonb3329172017-08-17 22:23:51775 EXPECT_CALL(observer, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
eladalona52722f2017-06-26 18:23:54776 }
777
Steve Antonb3329172017-08-17 22:23:51778 // The expected calls to OnSsrcBoundToRsid() will be triggered by this.
Steve Anton53c7ba62017-08-18 17:05:47779 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
780 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54781}
782
Steve Anton53c7ba62017-08-18 17:05:47783TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToMidRsid) {
784 const std::string mid = "v";
785 const std::string rsid = "1";
786 constexpr uint32_t ssrc = 10;
eladalon5daecca2017-08-04 13:34:54787
Steve Anton53c7ba62017-08-18 17:05:47788 NiceMock<MockRtpPacketSink> sink;
789 AddSinkBothMidRsid(mid, rsid, &sink);
eladalon5daecca2017-08-04 13:34:54790
Steve Anton53c7ba62017-08-18 17:05:47791 MockSsrcBindingObserver observer;
792 RegisterSsrcBindingObserver(&observer);
793
794 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
795 EXPECT_CALL(observer, OnSsrcBoundToMidRsid(mid, rsid, ssrc));
796 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54797}
798
Steve Anton53c7ba62017-08-18 17:05:47799TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundToPayloadType) {
800 constexpr uint8_t payload_type = 3;
801 constexpr uint32_t ssrc = 10;
802
803 RtpDemuxerCriteria criteria;
804 criteria.payload_types = {payload_type};
805 NiceMock<MockRtpPacketSink> sink;
806 AddSink(criteria, &sink);
807
808 MockSsrcBindingObserver observer;
809 RegisterSsrcBindingObserver(&observer);
810
811 auto packet = CreatePacketWithSsrc(ssrc);
812 packet->SetPayloadType(payload_type);
813 EXPECT_CALL(observer, OnSsrcBoundToPayloadType(payload_type, ssrc));
814 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
815}
816
817// If one sink is associated with SSRC x, and another sink with RSID y, then if
818// we receive a packet with both SSRC x and RSID y, route that to only the sink
819// for RSID y since we believe RSID tags to be more trustworthy than signaled
820// SSRCs.
Steve Anton9e0c7422017-08-18 01:59:47821TEST_F(RtpDemuxerTest,
Steve Anton53c7ba62017-08-18 17:05:47822 PacketFittingBothRsidSinkAndSsrcSinkGivenOnlyToRsidSink) {
eladalon5daecca2017-08-04 13:34:54823 constexpr uint32_t ssrc = 111;
824 MockRtpPacketSink ssrc_sink;
Steve Anton9e0c7422017-08-18 01:59:47825 AddSinkOnlySsrc(ssrc, &ssrc_sink);
eladalon5daecca2017-08-04 13:34:54826
827 const std::string rsid = "a";
828 MockRtpPacketSink rsid_sink;
Steve Anton9e0c7422017-08-18 01:59:47829 AddSinkOnlyRsid(rsid, &rsid_sink);
eladalon5daecca2017-08-04 13:34:54830
Steve Anton9e0c7422017-08-18 01:59:47831 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
eladalon5daecca2017-08-04 13:34:54832
Steve Anton53c7ba62017-08-18 17:05:47833 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
834 EXPECT_CALL(rsid_sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
835 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
eladalon5daecca2017-08-04 13:34:54836}
837
838// We're not expecting RSIDs to be resolved to SSRCs which were previously
839// mapped to sinks, and make no guarantees except for graceful handling.
Steve Anton9e0c7422017-08-18 01:59:47840TEST_F(RtpDemuxerTest,
841 GracefullyHandleRsidBeingMappedToPrevouslyAssociatedSsrc) {
eladalon5daecca2017-08-04 13:34:54842 constexpr uint32_t ssrc = 111;
843 NiceMock<MockRtpPacketSink> ssrc_sink;
Steve Anton9e0c7422017-08-18 01:59:47844 AddSinkOnlySsrc(ssrc, &ssrc_sink);
eladalon5daecca2017-08-04 13:34:54845
846 const std::string rsid = "a";
Steve Anton53c7ba62017-08-18 17:05:47847 NiceMock<MockRtpPacketSink> rsid_sink;
Steve Anton9e0c7422017-08-18 01:59:47848 AddSinkOnlyRsid(rsid, &rsid_sink);
eladalon5daecca2017-08-04 13:34:54849
Steve Anton53c7ba62017-08-18 17:05:47850 NiceMock<MockSsrcBindingObserver> observer;
Steve Anton9e0c7422017-08-18 01:59:47851 RegisterSsrcBindingObserver(&observer);
eladalon5daecca2017-08-04 13:34:54852
853 // The SSRC was mapped to an SSRC sink, but was even active (packets flowed
854 // over it).
Steve Anton9e0c7422017-08-18 01:59:47855 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
856 demuxer_.OnRtpPacket(*packet);
eladalon5daecca2017-08-04 13:34:54857
858 // If the SSRC sink is ever removed, the RSID sink *might* receive indications
859 // of packets, and observers *might* be informed. Only graceful handling
860 // is guaranteed.
Steve Anton9e0c7422017-08-18 01:59:47861 RemoveSink(&ssrc_sink);
eladalon5daecca2017-08-04 13:34:54862 EXPECT_CALL(rsid_sink, OnRtpPacket(SamePacketAs(*packet))).Times(AtLeast(0));
Steve Antonb3329172017-08-17 22:23:51863 EXPECT_CALL(observer, OnSsrcBoundToRsid(rsid, ssrc)).Times(AtLeast(0));
Steve Anton53c7ba62017-08-18 17:05:47864 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
865}
866
867// Tests that when one MID sink is configured, packets that include the MID
868// extension will get routed to that sink and any packets that use the same
869// SSRC as one of those packets later will also get routed to the sink, even
870// if a new SSRC is introduced for the same MID.
871TEST_F(RtpDemuxerTest, RoutedByMidWhenSsrcAdded) {
872 const std::string mid = "v";
873 NiceMock<MockRtpPacketSink> sink;
874 AddSinkOnlyMid(mid, &sink);
875
876 constexpr uint32_t ssrc1 = 10;
877 constexpr uint32_t ssrc2 = 11;
878
879 auto packet_ssrc1_mid = CreatePacketWithSsrcMid(ssrc1, mid);
880 demuxer_.OnRtpPacket(*packet_ssrc1_mid);
881 auto packet_ssrc2_mid = CreatePacketWithSsrcMid(ssrc2, mid);
882 demuxer_.OnRtpPacket(*packet_ssrc2_mid);
883
884 auto packet_ssrc1_only = CreatePacketWithSsrc(ssrc1);
885 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc1_only))).Times(1);
886 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc1_only));
887
888 auto packet_ssrc2_only = CreatePacketWithSsrc(ssrc2);
889 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc2_only))).Times(1);
890 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc2_only));
891}
892
893TEST_F(RtpDemuxerTest, DontLearnMidSsrcBindingBeforeSinkAdded) {
894 const std::string mid = "v";
895 constexpr uint32_t ssrc = 10;
896
897 auto packet_ssrc_mid = CreatePacketWithSsrcMid(ssrc, mid);
898 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_ssrc_mid));
899
900 MockRtpPacketSink sink;
901 AddSinkOnlyMid(mid, &sink);
902
903 auto packet_ssrc_only = CreatePacketWithSsrc(ssrc);
904 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
905 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_ssrc_only));
906}
907
908TEST_F(RtpDemuxerTest, DontForgetMidSsrcBindingWhenSinkRemoved) {
909 const std::string mid = "v";
910 constexpr uint32_t ssrc = 10;
911
912 NiceMock<MockRtpPacketSink> sink1;
913 AddSinkOnlyMid(mid, &sink1);
914
915 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
916 demuxer_.OnRtpPacket(*packet_with_mid);
917
918 RemoveSink(&sink1);
919
920 MockRtpPacketSink sink2;
921 AddSinkOnlyMid(mid, &sink2);
922
923 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
924 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
925 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
926}
927
928// If a sink is added with only a MID, then any packet with that MID no matter
929// the RSID should be routed to that sink.
930TEST_F(RtpDemuxerTest, RoutedByMidWithAnyRsid) {
931 const std::string mid = "v";
932 const std::string rsid1 = "1";
933 const std::string rsid2 = "2";
934 constexpr uint32_t ssrc1 = 10;
935 constexpr uint32_t ssrc2 = 11;
936
937 MockRtpPacketSink sink;
938 AddSinkOnlyMid(mid, &sink);
939
940 InSequence sequence;
941
942 auto packet_ssrc1_rsid1 = CreatePacketWithSsrcMidRsid(ssrc1, mid, rsid1);
943 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc1_rsid1))).Times(1);
944 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc1_rsid1));
945
946 auto packet_ssrc2_rsid2 = CreatePacketWithSsrcMidRsid(ssrc2, mid, rsid2);
947 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_ssrc2_rsid2))).Times(1);
948 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc2_rsid2));
949}
950
951// These two tests verify that for a sink added with a MID, RSID pair, if the
952// MID and RSID are learned in separate packets (e.g., because the header
953// extensions are sent separately), then a later packet with just SSRC will get
954// routed to that sink.
955// The first test checks that the functionality works when MID is learned first.
956// The second test checks that the functionality works when RSID is learned
957// first.
958TEST_F(RtpDemuxerTest, LearnMidThenRsidSeparatelyAndRouteBySsrc) {
959 const std::string mid = "v";
960 const std::string rsid = "1";
961 constexpr uint32_t ssrc = 10;
962
963 NiceMock<MockRtpPacketSink> sink;
964 AddSinkBothMidRsid(mid, rsid, &sink);
965
966 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
967 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_mid));
968
969 auto packet_with_rsid = CreatePacketWithSsrcRsid(ssrc, rsid);
970 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_rsid));
971
972 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
973 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
974 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
975}
976
977TEST_F(RtpDemuxerTest, LearnRsidThenMidSeparatelyAndRouteBySsrc) {
978 const std::string mid = "v";
979 const std::string rsid = "1";
980 constexpr uint32_t ssrc = 10;
981
982 NiceMock<MockRtpPacketSink> sink;
983 AddSinkBothMidRsid(mid, rsid, &sink);
984
985 auto packet_with_rsid = CreatePacketWithSsrcRsid(ssrc, rsid);
986 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_rsid));
987
988 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
989 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_mid));
990
991 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
992 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
993 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
994}
995
996TEST_F(RtpDemuxerTest, DontLearnMidRsidBindingBeforeSinkAdded) {
997 const std::string mid = "v";
998 const std::string rsid = "1";
999 constexpr uint32_t ssrc = 10;
1000
1001 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
1002 ASSERT_FALSE(demuxer_.OnRtpPacket(*packet_with_both));
1003
1004 MockRtpPacketSink sink;
1005 AddSinkBothMidRsid(mid, rsid, &sink);
1006
1007 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1008 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1009 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1010}
1011
1012TEST_F(RtpDemuxerTest, DontForgetMidRsidBindingWhenSinkRemoved) {
1013 const std::string mid = "v";
1014 const std::string rsid = "1";
1015 constexpr uint32_t ssrc = 10;
1016
1017 NiceMock<MockRtpPacketSink> sink1;
1018 AddSinkBothMidRsid(mid, rsid, &sink1);
1019
1020 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
1021 demuxer_.OnRtpPacket(*packet_with_both);
1022
1023 RemoveSink(&sink1);
1024
1025 MockRtpPacketSink sink2;
1026 AddSinkBothMidRsid(mid, rsid, &sink2);
1027
1028 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1029 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
1030 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1031}
1032
1033TEST_F(RtpDemuxerTest, LearnMidRsidBindingAfterSinkAdded) {
1034 const std::string mid = "v";
1035 const std::string rsid = "1";
1036 constexpr uint32_t ssrc = 10;
1037
1038 NiceMock<MockRtpPacketSink> sink;
1039 AddSinkBothMidRsid(mid, rsid, &sink);
1040
1041 auto packet_with_both = CreatePacketWithSsrcMidRsid(ssrc, mid, rsid);
1042 demuxer_.OnRtpPacket(*packet_with_both);
1043
1044 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1045 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc)));
1046 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1047}
1048
1049TEST_F(RtpDemuxerTest, DropByPayloadTypeIfNoSink) {
1050 constexpr uint8_t payload_type = 30;
1051 constexpr uint32_t ssrc = 10;
1052
1053 auto packet = CreatePacketWithSsrc(ssrc);
1054 packet->SetPayloadType(payload_type);
1055 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1056}
1057
1058// For legacy applications, it's possible for us to demux if the payload type is
1059// unique. But if multiple sinks are registered with different MIDs and the same
1060// payload types, then we cannot route a packet with just payload type because
1061// it is ambiguous which sink it should be sent to.
1062TEST_F(RtpDemuxerTest, DropByPayloadTypeIfAddedInMultipleSinks) {
1063 const std::string mid1 = "v";
1064 const std::string mid2 = "a";
1065 constexpr uint8_t payload_type = 30;
1066 constexpr uint32_t ssrc = 10;
1067
1068 RtpDemuxerCriteria mid1_pt;
1069 mid1_pt.mid = mid1;
1070 mid1_pt.payload_types = {payload_type};
1071 MockRtpPacketSink sink1;
1072 AddSink(mid1_pt, &sink1);
1073
1074 RtpDemuxerCriteria mid2_pt;
1075 mid2_pt.mid = mid2;
1076 mid2_pt.payload_types = {payload_type};
1077 MockRtpPacketSink sink2;
1078 AddSink(mid2_pt, &sink2);
1079
1080 auto packet = CreatePacketWithSsrc(ssrc);
1081 packet->SetPayloadType(payload_type);
1082
1083 EXPECT_CALL(sink1, OnRtpPacket(_)).Times(0);
1084 EXPECT_CALL(sink2, OnRtpPacket(_)).Times(0);
1085 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1086}
1087
1088// If two sinks are added with different MIDs but the same payload types, then
1089// we cannot demux on the payload type only unless one of the sinks is removed.
1090TEST_F(RtpDemuxerTest, RoutedByPayloadTypeIfAmbiguousSinkRemoved) {
1091 const std::string mid1 = "v";
1092 const std::string mid2 = "a";
1093 constexpr uint8_t payload_type = 30;
1094 constexpr uint32_t ssrc = 10;
1095
1096 RtpDemuxerCriteria mid1_pt;
1097 mid1_pt.mid = mid1;
1098 mid1_pt.payload_types = {payload_type};
1099 MockRtpPacketSink sink1;
1100 AddSink(mid1_pt, &sink1);
1101
1102 RtpDemuxerCriteria mid2_pt;
1103 mid2_pt.mid = mid2;
1104 mid2_pt.payload_types = {payload_type};
1105 MockRtpPacketSink sink2;
1106 AddSink(mid2_pt, &sink2);
1107
1108 RemoveSink(&sink1);
1109
1110 auto packet = CreatePacketWithSsrc(ssrc);
1111 packet->SetPayloadType(payload_type);
1112
1113 EXPECT_CALL(sink1, OnRtpPacket(_)).Times(0);
1114 EXPECT_CALL(sink2, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1115
1116 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1117}
1118
1119TEST_F(RtpDemuxerTest, RoutedByPayloadTypeLatchesSsrc) {
1120 constexpr uint8_t payload_type = 30;
1121 constexpr uint32_t ssrc = 10;
1122
1123 RtpDemuxerCriteria pt;
1124 pt.payload_types = {payload_type};
1125 NiceMock<MockRtpPacketSink> sink;
1126 AddSink(pt, &sink);
1127
1128 auto packet_with_pt = CreatePacketWithSsrc(ssrc);
1129 packet_with_pt->SetPayloadType(payload_type);
1130 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt));
1131
1132 auto packet_with_ssrc = CreatePacketWithSsrc(ssrc);
1133 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_ssrc))).Times(1);
1134 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_ssrc));
1135}
1136
1137// RSIDs are scoped within MID, so if two sinks are registered with the same
1138// RSIDs but different MIDs, then packets containing both extensions should be
1139// routed to the correct one.
1140TEST_F(RtpDemuxerTest, PacketWithSameRsidDifferentMidRoutedToProperSink) {
1141 const std::string mid1 = "mid1";
1142 const std::string mid2 = "mid2";
1143 const std::string rsid = "rsid";
1144 constexpr uint32_t ssrc1 = 10;
1145 constexpr uint32_t ssrc2 = 11;
1146
1147 NiceMock<MockRtpPacketSink> mid1_sink;
1148 AddSinkBothMidRsid(mid1, rsid, &mid1_sink);
1149
1150 MockRtpPacketSink mid2_sink;
1151 AddSinkBothMidRsid(mid2, rsid, &mid2_sink);
1152
1153 auto packet_mid1 = CreatePacketWithSsrcMidRsid(ssrc1, mid1, rsid);
1154 ASSERT_TRUE(demuxer_.OnRtpPacket(*packet_mid1));
1155
1156 auto packet_mid2 = CreatePacketWithSsrcMidRsid(ssrc2, mid2, rsid);
1157 EXPECT_CALL(mid2_sink, OnRtpPacket(SamePacketAs(*packet_mid2))).Times(1);
1158 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_mid2));
1159}
1160
1161// If a sink is first bound to a given SSRC by signaling but later a new sink is
1162// bound to a given MID by a later signaling, then when a packet arrives with
1163// both the SSRC and MID, then the signaled MID sink should take precedence.
1164TEST_F(RtpDemuxerTest, SignaledMidShouldOverwriteSignaledSsrc) {
1165 constexpr uint32_t ssrc = 11;
1166 const std::string mid = "mid";
1167
1168 MockRtpPacketSink ssrc_sink;
1169 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1170
1171 MockRtpPacketSink mid_sink;
1172 AddSinkOnlyMid(mid, &mid_sink);
1173
1174 auto p = CreatePacketWithSsrcMid(ssrc, mid);
1175 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
1176 EXPECT_CALL(mid_sink, OnRtpPacket(SamePacketAs(*p))).Times(1);
1177 EXPECT_TRUE(demuxer_.OnRtpPacket(*p));
1178}
1179
1180// Extends the previous test to also ensure that later packets that do not
1181// specify MID are still routed to the MID sink rather than the overwritten SSRC
1182// sink.
1183TEST_F(RtpDemuxerTest, SignaledMidShouldOverwriteSignalledSsrcPersistent) {
1184 constexpr uint32_t ssrc = 11;
1185 const std::string mid = "mid";
1186
1187 MockRtpPacketSink ssrc_sink;
1188 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1189
1190 NiceMock<MockRtpPacketSink> mid_sink;
1191 AddSinkOnlyMid(mid, &mid_sink);
1192
1193 EXPECT_CALL(ssrc_sink, OnRtpPacket(_)).Times(0);
1194
1195 auto packet_with_mid = CreatePacketWithSsrcMid(ssrc, mid);
1196 demuxer_.OnRtpPacket(*packet_with_mid);
1197
1198 auto packet_without_mid = CreatePacketWithSsrc(ssrc);
1199 EXPECT_CALL(mid_sink, OnRtpPacket(SamePacketAs(*packet_without_mid)))
1200 .Times(1);
1201 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_without_mid));
1202}
1203
1204TEST_F(RtpDemuxerTest, RouteByPayloadTypeMultipleMatch) {
1205 constexpr uint32_t ssrc = 10;
1206 constexpr uint8_t pt1 = 30;
1207 constexpr uint8_t pt2 = 31;
1208
1209 MockRtpPacketSink sink;
1210 RtpDemuxerCriteria criteria;
1211 criteria.payload_types = {pt1, pt2};
1212 AddSink(criteria, &sink);
1213
1214 auto packet_with_pt1 = CreatePacketWithSsrc(ssrc);
1215 packet_with_pt1->SetPayloadType(pt1);
1216 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_pt1)));
1217 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt1));
1218
1219 auto packet_with_pt2 = CreatePacketWithSsrc(ssrc);
1220 packet_with_pt2->SetPayloadType(pt2);
1221 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet_with_pt2)));
1222 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_with_pt2));
1223}
1224
1225TEST_F(RtpDemuxerTest, DontDemuxOnMidAloneIfAddedWithRsid) {
1226 const std::string mid = "v";
1227 const std::string rsid = "1";
1228 constexpr uint32_t ssrc = 10;
1229
1230 MockRtpPacketSink sink;
1231 AddSinkBothMidRsid(mid, rsid, &sink);
1232
1233 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1234
1235 auto packet = CreatePacketWithSsrcMid(ssrc, mid);
1236 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1237}
1238
1239TEST_F(RtpDemuxerTest, DemuxBySsrcEvenWithMidAndRsid) {
1240 const std::string mid = "v";
1241 const std::string rsid = "1";
1242 constexpr uint32_t ssrc = 10;
1243
1244 RtpDemuxerCriteria criteria;
1245 criteria.rsid = rsid;
1246 criteria.mid = mid;
1247 criteria.ssrcs = {ssrc};
1248 MockRtpPacketSink sink;
1249 AddSink(criteria, &sink);
1250
1251 auto packet = CreatePacketWithSsrc(ssrc);
1252 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1253 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1254}
1255
1256// In slight deviation from the BUNDLE spec, if we match a sink according to
1257// SSRC, then we do not verify payload type against the criteria and defer to
1258// the sink to check that it is correct.
1259TEST_F(RtpDemuxerTest, DoNotCheckPayloadTypeIfMatchedByOtherCriteria) {
1260 constexpr uint32_t ssrc = 10;
1261 constexpr uint8_t payload_type = 30;
1262 constexpr uint8_t different_payload_type = payload_type + 1;
1263
1264 RtpDemuxerCriteria criteria;
1265 criteria.ssrcs = {ssrc};
1266 criteria.payload_types = {payload_type};
1267 MockRtpPacketSink sink;
1268 AddSink(criteria, &sink);
1269
1270 auto packet = CreatePacketWithSsrc(ssrc);
1271 packet->SetPayloadType(different_payload_type);
1272 EXPECT_CALL(sink, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1273 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1274}
1275
1276// If a repair packet includes an RSID it should be ignored and the packet
1277// should be routed by its RRID.
1278TEST_F(RtpDemuxerTest, PacketWithRsidAndRridRoutedByRrid) {
1279 const std::string rsid = "1";
1280 const std::string rrid = "1r";
1281 constexpr uint32_t ssrc = 10;
1282
1283 MockRtpPacketSink sink_rsid;
1284 AddSinkOnlyRsid(rsid, &sink_rsid);
1285
1286 MockRtpPacketSink sink_rrid;
1287 AddSinkOnlyRsid(rrid, &sink_rrid);
1288
1289 auto packet = CreatePacketWithSsrcRsidRrid(ssrc, rsid, rrid);
1290 EXPECT_CALL(sink_rsid, OnRtpPacket(_)).Times(0);
1291 EXPECT_CALL(sink_rrid, OnRtpPacket(SamePacketAs(*packet))).Times(1);
1292 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1293}
1294
1295// Same test as above but checks that the latched SSRC routes to the RRID sink.
1296TEST_F(RtpDemuxerTest, PacketWithRsidAndRridLatchesSsrcToRrid) {
1297 const std::string rsid = "1";
1298 const std::string rrid = "1r";
1299 constexpr uint32_t ssrc = 10;
1300
1301 MockRtpPacketSink sink_rsid;
1302 AddSinkOnlyRsid(rsid, &sink_rsid);
1303
1304 NiceMock<MockRtpPacketSink> sink_rrid;
1305 AddSinkOnlyRsid(rrid, &sink_rrid);
1306
1307 auto packet_rsid_rrid = CreatePacketWithSsrcRsidRrid(ssrc, rsid, rrid);
1308 demuxer_.OnRtpPacket(*packet_rsid_rrid);
1309
1310 auto packet_ssrc_only = CreatePacketWithSsrc(ssrc);
1311 EXPECT_CALL(sink_rsid, OnRtpPacket(_)).Times(0);
1312 EXPECT_CALL(sink_rrid, OnRtpPacket(SamePacketAs(*packet_ssrc_only))).Times(1);
1313 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet_ssrc_only));
1314}
1315
1316// Tests that a packet which includes MID and RSID is dropped and not routed by
1317// SSRC if the MID and RSID do not match an added sink.
1318TEST_F(RtpDemuxerTest, PacketWithMidAndUnknownRsidIsNotRoutedBySsrc) {
1319 constexpr uint32_t ssrc = 10;
1320 const std::string mid = "v";
1321 const std::string rsid = "1";
1322 const std::string wrong_rsid = "2";
1323
1324 RtpDemuxerCriteria criteria;
1325 criteria.mid = mid;
1326 criteria.rsid = rsid;
1327 criteria.ssrcs = {ssrc};
1328 MockRtpPacketSink sink;
1329 AddSink(criteria, &sink);
1330
1331 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, wrong_rsid);
1332 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1333 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1334}
1335
1336// Tests that a packet which includes MID and RSID is dropped and not routed by
1337// payload type if the MID and RSID do not match an added sink.
1338TEST_F(RtpDemuxerTest, PacketWithMidAndUnknownRsidIsNotRoutedByPayloadType) {
1339 constexpr uint32_t ssrc = 10;
1340 const std::string mid = "v";
1341 const std::string rsid = "1";
1342 const std::string wrong_rsid = "2";
1343 constexpr uint8_t payload_type = 30;
1344
1345 RtpDemuxerCriteria criteria;
1346 criteria.mid = mid;
1347 criteria.rsid = rsid;
1348 criteria.payload_types = {payload_type};
1349 MockRtpPacketSink sink;
1350 AddSink(criteria, &sink);
1351
1352 auto packet = CreatePacketWithSsrcMidRsid(ssrc, mid, wrong_rsid);
1353 packet->SetPayloadType(payload_type);
1354 EXPECT_CALL(sink, OnRtpPacket(_)).Times(0);
1355 EXPECT_FALSE(demuxer_.OnRtpPacket(*packet));
1356}
1357
1358// Observers are only notified of an SSRC binding to an RSID if we care about
1359// the RSID (i.e., have a sink added for that RSID).
1360TEST_F(RtpDemuxerTest, ObserversNotNotifiedOfUntrackedRsids) {
1361 const std::string rsid = "1";
1362 constexpr uint32_t ssrc = 111;
1363
1364 MockSsrcBindingObserver rsid_resolution_observers[3];
1365 for (auto& observer : rsid_resolution_observers) {
1366 RegisterSsrcBindingObserver(&observer);
1367 EXPECT_CALL(observer, OnSsrcBoundToRsid(_, _)).Times(0);
1368 }
1369
1370 // Since no sink is registered for this SSRC/RSID, expect the packet to not be
1371 // routed and no observers notified of the SSRC -> RSID binding.
1372 EXPECT_FALSE(demuxer_.OnRtpPacket(*CreatePacketWithSsrcRsid(ssrc, rsid)));
1373}
1374
1375// Ensure that observers are notified of SSRC bindings only once per unique
1376// binding source (e.g., SSRC -> MID, SSRC -> RSID, etc.)
1377TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundtoMidOnlyOnce) {
1378 const std::string mid = "v";
1379 constexpr uint32_t ssrc = 10;
1380
1381 NiceMock<MockRtpPacketSink> sink;
1382 AddSinkOnlyMid(mid, &sink);
1383
1384 MockSsrcBindingObserver observer;
1385 RegisterSsrcBindingObserver(&observer);
1386
1387 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc)).Times(1);
1388
1389 demuxer_.OnRtpPacket(*CreatePacketWithSsrcMid(ssrc, mid));
1390 demuxer_.OnRtpPacket(*CreatePacketWithSsrcMid(ssrc, mid));
1391}
1392
1393// Ensure that when a new SSRC -> MID binding is discovered observers are also
1394// notified of that, even if there has already been an SSRC bound to the MID.
1395TEST_F(RtpDemuxerTest, ObserversNotifiedOfSsrcBoundtoMidWhenSsrcChanges) {
1396 const std::string mid = "v";
1397 constexpr uint32_t ssrc1 = 10;
1398 constexpr uint32_t ssrc2 = 11;
1399
1400 NiceMock<MockRtpPacketSink> sink;
1401 AddSinkOnlyMid(mid, &sink);
1402
1403 MockSsrcBindingObserver observer;
1404 RegisterSsrcBindingObserver(&observer);
1405
1406 InSequence seq;
1407 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc1)).Times(1);
1408 EXPECT_CALL(observer, OnSsrcBoundToMid(mid, ssrc2)).Times(1);
1409
1410 auto p1 = CreatePacketWithSsrcMid(ssrc1, mid);
1411 demuxer_.OnRtpPacket(*p1);
1412
1413 auto p2 = CreatePacketWithSsrcMid(ssrc2, mid);
1414 demuxer_.OnRtpPacket(*p2);
eladalona52722f2017-06-26 18:23:541415}
1416
Steve Anton9e0c7422017-08-18 01:59:471417TEST_F(RtpDemuxerTest, DeregisteredRsidObserversNotInformedOfResolutions) {
eladalona52722f2017-06-26 18:23:541418 constexpr uint32_t ssrc = 111;
1419 const std::string rsid = "a";
1420 NiceMock<MockRtpPacketSink> sink;
Steve Anton9e0c7422017-08-18 01:59:471421 AddSinkOnlyRsid(rsid, &sink);
eladalona52722f2017-06-26 18:23:541422
1423 // Register several, then deregister only one, to show that not all of the
1424 // observers had been forgotten when one was removed.
Steve Antonb3329172017-08-17 22:23:511425 MockSsrcBindingObserver observer_1;
1426 MockSsrcBindingObserver observer_2_removed;
1427 MockSsrcBindingObserver observer_3;
eladalona52722f2017-06-26 18:23:541428
Steve Anton9e0c7422017-08-18 01:59:471429 RegisterSsrcBindingObserver(&observer_1);
1430 RegisterSsrcBindingObserver(&observer_2_removed);
1431 RegisterSsrcBindingObserver(&observer_3);
eladalona52722f2017-06-26 18:23:541432
Steve Anton9e0c7422017-08-18 01:59:471433 DeregisterSsrcBindingObserver(&observer_2_removed);
eladalona52722f2017-06-26 18:23:541434
Steve Antonb3329172017-08-17 22:23:511435 EXPECT_CALL(observer_1, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
1436 EXPECT_CALL(observer_2_removed, OnSsrcBoundToRsid(_, _)).Times(0);
1437 EXPECT_CALL(observer_3, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
eladalona52722f2017-06-26 18:23:541438
Steve Antonb3329172017-08-17 22:23:511439 // The expected calls to OnSsrcBoundToRsid() will be triggered by this.
Steve Anton9e0c7422017-08-18 01:59:471440 demuxer_.OnRtpPacket(*CreatePacketWithSsrcRsid(ssrc, rsid));
eladalona52722f2017-06-26 18:23:541441}
1442
Steve Anton53c7ba62017-08-18 17:05:471443TEST_F(RtpDemuxerTest,
1444 PacketFittingBothRsidSinkAndSsrcSinkTriggersResolutionCallbacks) {
1445 constexpr uint32_t ssrc = 111;
1446 NiceMock<MockRtpPacketSink> ssrc_sink;
1447 AddSinkOnlySsrc(ssrc, &ssrc_sink);
1448
1449 const std::string rsid = "a";
1450 NiceMock<MockRtpPacketSink> rsid_sink;
1451 AddSinkOnlyRsid(rsid, &rsid_sink);
1452
1453 MockSsrcBindingObserver observer;
1454 RegisterSsrcBindingObserver(&observer);
1455
1456 auto packet = CreatePacketWithSsrcRsid(ssrc, rsid);
1457 EXPECT_CALL(observer, OnSsrcBoundToRsid(rsid, ssrc)).Times(1);
1458 demuxer_.OnRtpPacket(*packet);
1459}
1460
1461TEST_F(RtpDemuxerTest, MaliciousPeerCannotCauseMemoryOveruse) {
1462 const std::string mid = "v";
1463
1464 NiceMock<MockRtpPacketSink> sink;
1465 AddSinkOnlyMid(mid, &sink);
1466
1467 MockSsrcBindingObserver observer;
1468 RegisterSsrcBindingObserver(&observer);
1469
1470 EXPECT_CALL(observer, OnSsrcBoundToMid(_, _))
1471 .Times(AtMost(RtpDemuxer::kMaxSsrcBindings));
1472
1473 for (int i = 0; i < RtpDemuxer::kMaxSsrcBindings + 1; i++) {
1474 auto packet = CreatePacketWithSsrcMid(i, mid);
1475 EXPECT_TRUE(demuxer_.OnRtpPacket(*packet));
1476 }
1477}
1478
eladalon760a0762017-05-31 16:12:251479#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
Steve Anton53c7ba62017-08-18 17:05:471480
1481TEST_F(RtpDemuxerTest, CriteriaMustBeNonEmpty) {
eladalond0244c22017-06-08 11:19:131482 MockRtpPacketSink sink;
Steve Anton53c7ba62017-08-18 17:05:471483 RtpDemuxerCriteria criteria;
1484 EXPECT_DEATH(AddSink(criteria, &sink), "");
eladalond0244c22017-06-08 11:19:131485}
1486
Steve Anton9e0c7422017-08-18 01:59:471487TEST_F(RtpDemuxerTest, RsidMustBeAlphaNumeric) {
eladalond0244c22017-06-08 11:19:131488 MockRtpPacketSink sink;
Steve Anton9e0c7422017-08-18 01:59:471489 EXPECT_DEATH(AddSinkOnlyRsid("a_3", &sink), "");
eladalond0244c22017-06-08 11:19:131490}
1491
Steve Anton53c7ba62017-08-18 17:05:471492TEST_F(RtpDemuxerTest, MidMustBeAlphaNumeric) {
1493 MockRtpPacketSink sink;
1494 EXPECT_DEATH(AddSinkOnlyMid("a_3", &sink), "");
1495}
1496
Steve Anton9e0c7422017-08-18 01:59:471497TEST_F(RtpDemuxerTest, RsidMustNotExceedMaximumLength) {
eladalond0244c22017-06-08 11:19:131498 MockRtpPacketSink sink;
1499 std::string rsid(StreamId::kMaxSize + 1, 'a');
Steve Anton9e0c7422017-08-18 01:59:471500 EXPECT_DEATH(AddSinkOnlyRsid(rsid, &sink), "");
eladalond0244c22017-06-08 11:19:131501}
1502
Steve Anton53c7ba62017-08-18 17:05:471503TEST_F(RtpDemuxerTest, MidMustNotExceedMaximumLength) {
Steve Anton9e0c7422017-08-18 01:59:471504 MockRtpPacketSink sink;
Steve Anton53c7ba62017-08-18 17:05:471505 std::string mid(Mid::kMaxSize + 1, 'a');
1506 EXPECT_DEATH(AddSinkOnlyMid(mid, &sink), "");
eladalon760a0762017-05-31 16:12:251507}
eladalona52722f2017-06-26 18:23:541508
Steve Anton53c7ba62017-08-18 17:05:471509TEST_F(RtpDemuxerTest, DoubleRegisterationOfSsrcBindingObserverDisallowed) {
Steve Antonb3329172017-08-17 22:23:511510 MockSsrcBindingObserver observer;
Steve Anton9e0c7422017-08-18 01:59:471511 RegisterSsrcBindingObserver(&observer);
1512 EXPECT_DEATH(RegisterSsrcBindingObserver(&observer), "");
eladalona52722f2017-06-26 18:23:541513}
1514
Steve Anton9e0c7422017-08-18 01:59:471515TEST_F(RtpDemuxerTest,
Steve Anton53c7ba62017-08-18 17:05:471516 DregisterationOfNeverRegisteredSsrcBindingObserverDisallowed) {
Steve Antonb3329172017-08-17 22:23:511517 MockSsrcBindingObserver observer;
Steve Anton9e0c7422017-08-18 01:59:471518 EXPECT_DEATH(DeregisterSsrcBindingObserver(&observer), "");
eladalona52722f2017-06-26 18:23:541519}
1520
eladalon760a0762017-05-31 16:12:251521#endif
1522
1523} // namespace
1524} // namespace webrtc