tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

rtp_demuxer_unittest.cc (42564B)


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