OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/call/rtp_demuxer.h" | 11 #include "webrtc/call/rtp_demuxer.h" |
12 | 12 |
13 #include "webrtc/call/rsid_resolution_observer.h" | 13 #include "webrtc/call/rsid_resolution_observer.h" |
14 #include "webrtc/call/rtp_packet_sink_interface.h" | 14 #include "webrtc/call/rtp_packet_sink_interface.h" |
15 #include "webrtc/call/rtp_rtcp_demuxer_helper.h" | 15 #include "webrtc/call/rtp_rtcp_demuxer_helper.h" |
16 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" | 16 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" |
17 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" | 17 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" |
18 #include "webrtc/rtc_base/checks.h" | 18 #include "webrtc/rtc_base/checks.h" |
19 #include "webrtc/rtc_base/logging.h" | 19 #include "webrtc/rtc_base/logging.h" |
20 | 20 |
21 namespace webrtc { | 21 namespace webrtc { |
22 | 22 |
23 RtpDemuxer::RtpDemuxer() = default; | 23 RtpDemuxer::RtpDemuxer() = default; |
24 | 24 |
25 RtpDemuxer::~RtpDemuxer() { | 25 RtpDemuxer::~RtpDemuxer() { |
26 RTC_DCHECK(ssrc_sinks_.empty()); | 26 RTC_DCHECK(ssrc_sinks_.empty()); |
27 RTC_DCHECK(rsid_sinks_.empty()); | 27 RTC_DCHECK(rsid_sinks_.empty()); |
28 } | 28 } |
29 | 29 |
30 void RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) { | 30 bool RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) { |
31 RTC_DCHECK(sink); | 31 RTC_DCHECK(sink); |
32 RecordSsrcToSinkAssociation(ssrc, sink); | 32 // The association might already have been set by a different |
| 33 // configuration source. |
| 34 // We cannot RTC_DCHECK against an attempt to remap an SSRC, because |
| 35 // such a configuration might have come from the network (1. resolution |
| 36 // of an RSID or 2. RTCP messages with RSID resolutions). |
| 37 return ssrc_sinks_.emplace(ssrc, sink).second; |
33 } | 38 } |
34 | 39 |
35 void RtpDemuxer::AddSink(const std::string& rsid, | 40 void RtpDemuxer::AddSink(const std::string& rsid, |
36 RtpPacketSinkInterface* sink) { | 41 RtpPacketSinkInterface* sink) { |
37 RTC_DCHECK(StreamId::IsLegalName(rsid)); | 42 RTC_DCHECK(StreamId::IsLegalName(rsid)); |
38 RTC_DCHECK(sink); | 43 RTC_DCHECK(sink); |
39 RTC_DCHECK(!MultimapAssociationExists(rsid_sinks_, rsid, sink)); | 44 RTC_DCHECK(rsid_sinks_.find(rsid) == rsid_sinks_.cend()); |
40 | 45 |
41 rsid_sinks_.emplace(rsid, sink); | 46 rsid_sinks_.emplace(rsid, sink); |
42 } | 47 } |
43 | 48 |
44 bool RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) { | 49 bool RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) { |
45 RTC_DCHECK(sink); | 50 RTC_DCHECK(sink); |
46 return (RemoveFromMultimapByValue(&ssrc_sinks_, sink) + | 51 return (RemoveFromMapByValue(&ssrc_sinks_, sink) + |
47 RemoveFromMultimapByValue(&rsid_sinks_, sink)) > 0; | 52 RemoveFromMapByValue(&rsid_sinks_, sink)) > 0; |
48 } | |
49 | |
50 void RtpDemuxer::RecordSsrcToSinkAssociation(uint32_t ssrc, | |
51 RtpPacketSinkInterface* sink) { | |
52 RTC_DCHECK(sink); | |
53 // The association might already have been set by a different | |
54 // configuration source. | |
55 if (!MultimapAssociationExists(ssrc_sinks_, ssrc, sink)) { | |
56 ssrc_sinks_.emplace(ssrc, sink); | |
57 } | |
58 } | 53 } |
59 | 54 |
60 bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) { | 55 bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) { |
61 // TODO(eladalon): This will now check every single packet, but soon a CL will | |
62 // be added which will change the many-to-many association of packets to sinks | |
63 // to a many-to-one, meaning each packet will be associated with one sink | |
64 // at most. Then, only packets with an unknown SSRC will be checked for RSID. | |
65 ResolveRsidToSsrcAssociations(packet); | 56 ResolveRsidToSsrcAssociations(packet); |
66 | 57 |
67 auto it_range = ssrc_sinks_.equal_range(packet.Ssrc()); | 58 auto it_range = ssrc_sinks_.equal_range(packet.Ssrc()); |
68 for (auto it = it_range.first; it != it_range.second; ++it) { | 59 for (auto it = it_range.first; it != it_range.second; ++it) { |
69 it->second->OnRtpPacket(packet); | 60 it->second->OnRtpPacket(packet); |
70 } | 61 } |
71 return it_range.first != it_range.second; | 62 return it_range.first != it_range.second; |
72 } | 63 } |
73 | 64 |
74 void RtpDemuxer::RegisterRsidResolutionObserver( | 65 void RtpDemuxer::RegisterRsidResolutionObserver( |
75 RsidResolutionObserver* observer) { | 66 RsidResolutionObserver* observer) { |
76 RTC_DCHECK(observer); | 67 RTC_DCHECK(observer); |
77 RTC_DCHECK(!ContainerHasKey(rsid_resolution_observers_, observer)); | 68 RTC_DCHECK(!ContainerHasKey(rsid_resolution_observers_, observer)); |
78 | 69 |
79 rsid_resolution_observers_.push_back(observer); | 70 rsid_resolution_observers_.push_back(observer); |
80 } | 71 } |
81 | 72 |
82 void RtpDemuxer::DeregisterRsidResolutionObserver( | 73 void RtpDemuxer::DeregisterRsidResolutionObserver( |
83 const RsidResolutionObserver* observer) { | 74 const RsidResolutionObserver* observer) { |
84 RTC_DCHECK(observer); | 75 RTC_DCHECK(observer); |
85 auto it = std::find(rsid_resolution_observers_.begin(), | 76 auto it = std::find(rsid_resolution_observers_.begin(), |
86 rsid_resolution_observers_.end(), observer); | 77 rsid_resolution_observers_.end(), observer); |
87 RTC_DCHECK(it != rsid_resolution_observers_.end()); | 78 RTC_DCHECK(it != rsid_resolution_observers_.end()); |
88 rsid_resolution_observers_.erase(it); | 79 rsid_resolution_observers_.erase(it); |
89 } | 80 } |
90 | 81 |
91 void RtpDemuxer::ResolveRsidToSsrcAssociations( | 82 void RtpDemuxer::ResolveRsidToSsrcAssociations( |
92 const RtpPacketReceived& packet) { | 83 const RtpPacketReceived& packet) { |
93 std::string rsid; | 84 std::string rsid; |
94 if (packet.GetExtension<RtpStreamId>(&rsid)) { | 85 if (!packet.GetExtension<RtpStreamId>(&rsid)) { |
95 // All streams associated with this RSID need to be marked as associated | 86 return; |
96 // with this SSRC (if they aren't already). | 87 } |
97 auto it_range = rsid_sinks_.equal_range(rsid); | |
98 for (auto it = it_range.first; it != it_range.second; ++it) { | |
99 RecordSsrcToSinkAssociation(packet.Ssrc(), it->second); | |
100 } | |
101 | 88 |
102 NotifyObserversOfRsidResolution(rsid, packet.Ssrc()); | 89 auto it = rsid_sinks_.find(rsid); |
| 90 if (it == rsid_sinks_.end()) { |
| 91 // Might be unknown, or we might have already associated this RSID |
| 92 // with a sink. |
| 93 return; |
| 94 } |
103 | 95 |
104 // To prevent memory-overuse attacks, forget this RSID. Future packets | 96 // If a sink is associated with an RSID, we should associate it with |
105 // with this RSID, but a different SSRC, will not spawn new associations. | 97 // this SSRC. |
106 rsid_sinks_.erase(it_range.first, it_range.second); | 98 if (!AddSink(packet.Ssrc(), it->second)) { |
| 99 // In the faulty case of RSIDs mapped to SSRCs which are already associated |
| 100 // with a sink, avoid propagating the problem to the resolution observers. |
| 101 LOG(LS_WARNING) << "RSID (" << rsid << ") resolved to preconfigured SSRC (" |
| 102 << packet.Ssrc() << ")."; |
| 103 return; |
107 } | 104 } |
| 105 |
| 106 // We make the assumption that observers are only interested in notifications |
| 107 // for RSIDs which are registered with this module. (RTCP sinks are normally |
| 108 // created with RTP sinks.) |
| 109 NotifyObserversOfRsidResolution(rsid, packet.Ssrc()); |
| 110 |
| 111 // This RSID cannot later be associated with another SSRC. |
| 112 rsid_sinks_.erase(it); |
108 } | 113 } |
109 | 114 |
110 void RtpDemuxer::NotifyObserversOfRsidResolution(const std::string& rsid, | 115 void RtpDemuxer::NotifyObserversOfRsidResolution(const std::string& rsid, |
111 uint32_t ssrc) { | 116 uint32_t ssrc) { |
112 for (auto* observer : rsid_resolution_observers_) { | 117 for (auto* observer : rsid_resolution_observers_) { |
113 observer->OnRsidResolved(rsid, ssrc); | 118 observer->OnRsidResolved(rsid, ssrc); |
114 } | 119 } |
115 } | 120 } |
116 | 121 |
117 } // namespace webrtc | 122 } // namespace webrtc |
OLD | NEW |