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