OLD | NEW |
(Empty) | |
| 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 "webrtc/call/rtp_demuxer.h" |
| 12 |
| 13 #include <memory> |
| 14 |
| 15 #include "webrtc/base/arraysize.h" |
| 16 #include "webrtc/base/checks.h" |
| 17 #include "webrtc/base/ptr_util.h" |
| 18 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" |
| 19 #include "webrtc/test/gmock.h" |
| 20 #include "webrtc/test/gtest.h" |
| 21 |
| 22 namespace webrtc { |
| 23 |
| 24 namespace { |
| 25 |
| 26 using ::testing::_; |
| 27 |
| 28 class MockRtpPacketSink : public RtpPacketSinkInterface { |
| 29 public: |
| 30 MOCK_METHOD1(OnRtpPacket, void(const RtpPacketReceived&)); |
| 31 }; |
| 32 |
| 33 constexpr uint32_t kSsrcs[] = {101, 202, 303}; |
| 34 |
| 35 MATCHER_P(SsrcSameAsIn, other, "") { |
| 36 return arg.Ssrc() == other.Ssrc(); |
| 37 } |
| 38 |
| 39 std::unique_ptr<RtpPacketReceived> CreateRtpPacketReceived(uint32_t ssrc) { |
| 40 auto packet = rtc::MakeUnique<RtpPacketReceived>(); |
| 41 packet->SetSsrc(ssrc); |
| 42 return packet; |
| 43 } |
| 44 |
| 45 class RtpDemuxerTest : public ::testing::Test { |
| 46 protected: |
| 47 RtpDemuxerTest() { |
| 48 for (size_t i = 0; i < arraysize(sinks); i++) { |
| 49 demuxer.AddSink(kSsrcs[i], &sinks[i]); |
| 50 } |
| 51 } |
| 52 |
| 53 ~RtpDemuxerTest() override { |
| 54 for (auto& sink : sinks) { |
| 55 EXPECT_EQ(demuxer.RemoveSink(&sink), 1u); |
| 56 } |
| 57 } |
| 58 |
| 59 RtpDemuxer demuxer; |
| 60 MockRtpPacketSink sinks[arraysize(kSsrcs)]; |
| 61 }; |
| 62 |
| 63 TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSink) { |
| 64 for (size_t i = 0; i < arraysize(sinks); i++) { |
| 65 auto packet = CreateRtpPacketReceived(kSsrcs[i]); |
| 66 EXPECT_CALL(sinks[i], OnRtpPacket(SsrcSameAsIn(*packet))); |
| 67 demuxer.OnRtpPacket(*packet); |
| 68 } |
| 69 } |
| 70 |
| 71 TEST_F(RtpDemuxerTest, MultipleSinksMappedToSameSsrc) { |
| 72 // |sinks| associated with different SSRCs each. Add a few additional sinks |
| 73 // that are all associated with one new, distinct SSRC. |
| 74 MockRtpPacketSink same_ssrc_sinks[arraysize(sinks)]; |
| 75 constexpr uint32_t kSharedSsrc = 404; |
| 76 for (auto& sink : same_ssrc_sinks) { |
| 77 demuxer.AddSink(kSharedSsrc, &sink); |
| 78 } |
| 79 |
| 80 // Reception of an RTP packet associated with the shared SSRC triggers the |
| 81 // callback on all of the interfaces associated with it. |
| 82 auto packet = CreateRtpPacketReceived(kSharedSsrc); |
| 83 for (auto& sink : same_ssrc_sinks) { |
| 84 EXPECT_CALL(sink, OnRtpPacket(SsrcSameAsIn(*packet))); |
| 85 } |
| 86 demuxer.OnRtpPacket(*packet); |
| 87 |
| 88 // Test-specific tear-down |
| 89 for (auto& sink : same_ssrc_sinks) { |
| 90 EXPECT_EQ(demuxer.RemoveSink(&sink), 1u); |
| 91 } |
| 92 } |
| 93 |
| 94 TEST_F(RtpDemuxerTest, SinkMappedToMultipleSsrcs) { |
| 95 // |sinks| associated with different SSRCs each. We set one of them to also |
| 96 // be mapped to additional SSRCs. |
| 97 constexpr uint32_t kSsrcsOfMultiSsrcSink[] = {404, 505, 606}; |
| 98 MockRtpPacketSink multi_ssrc_sink; |
| 99 for (uint32_t ssrc : kSsrcsOfMultiSsrcSink) { |
| 100 demuxer.AddSink(ssrc, &multi_ssrc_sink); |
| 101 } |
| 102 |
| 103 // The sink which is associated with multiple SSRCs gets the callback |
| 104 // triggered for each of those SSRCs. |
| 105 for (uint32_t ssrc : kSsrcsOfMultiSsrcSink) { |
| 106 auto packet = CreateRtpPacketReceived(ssrc); |
| 107 EXPECT_CALL(multi_ssrc_sink, OnRtpPacket(SsrcSameAsIn(*packet))); |
| 108 demuxer.OnRtpPacket(*packet); |
| 109 } |
| 110 |
| 111 // Test-specific tear-down |
| 112 EXPECT_EQ(demuxer.RemoveSink(&multi_ssrc_sink), |
| 113 arraysize(kSsrcsOfMultiSsrcSink)); |
| 114 } |
| 115 |
| 116 TEST_F(RtpDemuxerTest, OnRtpPacketNotCalledOnRemovedSinks) { |
| 117 // |sinks| associated with different SSRCs each. We set one of them to also |
| 118 // be mapped to additional SSRCs. |
| 119 constexpr uint32_t kSsrcsOfMultiSsrcSink[] = {404, 505, 606}; |
| 120 MockRtpPacketSink multi_ssrc_sink; |
| 121 for (uint32_t ssrc : kSsrcsOfMultiSsrcSink) { |
| 122 demuxer.AddSink(ssrc, &multi_ssrc_sink); |
| 123 } |
| 124 |
| 125 // Remove the sink. |
| 126 EXPECT_EQ(demuxer.RemoveSink(&multi_ssrc_sink), |
| 127 arraysize(kSsrcsOfMultiSsrcSink)); |
| 128 |
| 129 // The removed sink does not get callbacks triggered for any of the SSRCs |
| 130 // with which it was previously associated. |
| 131 EXPECT_CALL(multi_ssrc_sink, OnRtpPacket(_)).Times(0); |
| 132 for (uint32_t ssrc : kSsrcsOfMultiSsrcSink) { |
| 133 auto packet = CreateRtpPacketReceived(ssrc); |
| 134 demuxer.OnRtpPacket(*packet); |
| 135 } |
| 136 } |
| 137 |
| 138 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 139 TEST_F(RtpDemuxerTest, RepeatedAssociationsForbidden) { |
| 140 // Set-up already associated sinks[0] with kSsrcs[0]. Repeating the |
| 141 // association is an error. |
| 142 EXPECT_DEATH(demuxer.AddSink(kSsrcs[0], &sinks[0]), ""); |
| 143 } |
| 144 #endif |
| 145 |
| 146 } // namespace |
| 147 } // namespace webrtc |
OLD | NEW |