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/base/checks.h" | 13 #include "webrtc/base/checks.h" |
14 #include "webrtc/base/logging.h" | 14 #include "webrtc/base/logging.h" |
15 #include "webrtc/call/rsid_resolution_observer.h" | |
16 #include "webrtc/call/rtp_packet_sink_interface.h" | 15 #include "webrtc/call/rtp_packet_sink_interface.h" |
17 #include "webrtc/call/rtp_rtcp_demuxer_helper.h" | |
18 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" | 16 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" |
19 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" | 17 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" |
20 | 18 |
21 namespace webrtc { | 19 namespace webrtc { |
22 | 20 |
23 namespace { | 21 namespace { |
| 22 |
24 constexpr size_t kMaxProcessedSsrcs = 1000; // Prevent memory overuse. | 23 constexpr size_t kMaxProcessedSsrcs = 1000; // Prevent memory overuse. |
| 24 |
| 25 template <typename Key, typename Value> |
| 26 bool MultimapAssociationExists(const std::multimap<Key, Value>& multimap, |
| 27 Key key, |
| 28 Value val) { |
| 29 auto it_range = multimap.equal_range(key); |
| 30 using Reference = typename std::multimap<Key, Value>::const_reference; |
| 31 return std::any_of(it_range.first, it_range.second, |
| 32 [val](Reference elem) { return elem.second == val; }); |
| 33 } |
| 34 |
| 35 template <typename Key, typename Value> |
| 36 size_t RemoveFromMultimapByValue(std::multimap<Key, Value*>* multimap, |
| 37 const Value* value) { |
| 38 size_t count = 0; |
| 39 for (auto it = multimap->begin(); it != multimap->end();) { |
| 40 if (it->second == value) { |
| 41 it = multimap->erase(it); |
| 42 ++count; |
| 43 } else { |
| 44 ++it; |
| 45 } |
| 46 } |
| 47 return count; |
| 48 } |
| 49 |
25 } // namespace | 50 } // namespace |
26 | 51 |
27 RtpDemuxer::RtpDemuxer() = default; | 52 RtpDemuxer::RtpDemuxer() {} |
28 | 53 |
29 RtpDemuxer::~RtpDemuxer() { | 54 RtpDemuxer::~RtpDemuxer() { |
30 RTC_DCHECK(sinks_.empty()); | 55 RTC_DCHECK(sinks_.empty()); |
31 RTC_DCHECK(rsid_sinks_.empty()); | |
32 } | 56 } |
33 | 57 |
34 void RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) { | 58 void RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) { |
35 RTC_DCHECK(sink); | |
36 RecordSsrcToSinkAssociation(ssrc, sink); | 59 RecordSsrcToSinkAssociation(ssrc, sink); |
37 } | 60 } |
38 | 61 |
39 void RtpDemuxer::AddSink(const std::string& rsid, | 62 void RtpDemuxer::AddSink(const std::string& rsid, |
40 RtpPacketSinkInterface* sink) { | 63 RtpPacketSinkInterface* sink) { |
41 RTC_DCHECK(StreamId::IsLegalName(rsid)); | 64 RTC_DCHECK(StreamId::IsLegalName(rsid)); |
42 RTC_DCHECK(sink); | 65 RTC_DCHECK(sink); |
43 RTC_DCHECK(!MultimapAssociationExists(rsid_sinks_, rsid, sink)); | 66 RTC_DCHECK(!MultimapAssociationExists(rsid_sinks_, rsid, sink)); |
44 | 67 |
45 rsid_sinks_.emplace(rsid, sink); | 68 rsid_sinks_.emplace(rsid, sink); |
(...skipping 12 matching lines...) Expand all Loading... |
58 RtpPacketSinkInterface* sink) { | 81 RtpPacketSinkInterface* sink) { |
59 RTC_DCHECK(sink); | 82 RTC_DCHECK(sink); |
60 // The association might already have been set by a different | 83 // The association might already have been set by a different |
61 // configuration source. | 84 // configuration source. |
62 if (!MultimapAssociationExists(sinks_, ssrc, sink)) { | 85 if (!MultimapAssociationExists(sinks_, ssrc, sink)) { |
63 sinks_.emplace(ssrc, sink); | 86 sinks_.emplace(ssrc, sink); |
64 } | 87 } |
65 } | 88 } |
66 | 89 |
67 bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) { | 90 bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) { |
68 ResolveAssociations(packet); | 91 FindSsrcAssociations(packet); |
69 auto it_range = sinks_.equal_range(packet.Ssrc()); | 92 auto it_range = sinks_.equal_range(packet.Ssrc()); |
70 for (auto it = it_range.first; it != it_range.second; ++it) { | 93 for (auto it = it_range.first; it != it_range.second; ++it) { |
71 it->second->OnRtpPacket(packet); | 94 it->second->OnRtpPacket(packet); |
72 } | 95 } |
73 return it_range.first != it_range.second; | 96 return it_range.first != it_range.second; |
74 } | 97 } |
75 | 98 |
76 void RtpDemuxer::RegisterRsidResolutionObserver( | 99 void RtpDemuxer::FindSsrcAssociations(const RtpPacketReceived& packet) { |
77 RsidResolutionObserver* observer) { | |
78 RTC_DCHECK(observer); | |
79 RTC_DCHECK(!ContainerHasKey(rsid_resolution_observers_, observer)); | |
80 | |
81 rsid_resolution_observers_.push_back(observer); | |
82 | |
83 processed_ssrcs_.clear(); // New observer requires new notifications. | |
84 } | |
85 | |
86 void RtpDemuxer::DeregisterRsidResolutionObserver( | |
87 const RsidResolutionObserver* observer) { | |
88 RTC_DCHECK(observer); | |
89 auto it = std::find(rsid_resolution_observers_.begin(), | |
90 rsid_resolution_observers_.end(), observer); | |
91 RTC_DCHECK(it != rsid_resolution_observers_.end()); | |
92 rsid_resolution_observers_.erase(it); | |
93 } | |
94 | |
95 void RtpDemuxer::ResolveAssociations(const RtpPacketReceived& packet) { | |
96 // Avoid expensive string comparisons for RSID by looking the sinks up only | 100 // Avoid expensive string comparisons for RSID by looking the sinks up only |
97 // by SSRC whenever possible. | 101 // by SSRC whenever possible. |
98 if (processed_ssrcs_.find(packet.Ssrc()) != processed_ssrcs_.cend()) { | 102 if (processed_ssrcs_.find(packet.Ssrc()) != processed_ssrcs_.cend()) { |
99 return; | 103 return; |
100 } | 104 } |
101 | 105 |
102 ResolveRsidToSsrcAssociations(packet); | 106 // RSID-based associations: |
| 107 std::string rsid; |
| 108 if (packet.GetExtension<RtpStreamId>(&rsid)) { |
| 109 // All streams associated with this RSID need to be marked as associated |
| 110 // with this SSRC (if they aren't already). |
| 111 auto it_range = rsid_sinks_.equal_range(rsid); |
| 112 for (auto it = it_range.first; it != it_range.second; ++it) { |
| 113 RecordSsrcToSinkAssociation(packet.Ssrc(), it->second); |
| 114 } |
| 115 |
| 116 // To prevent memory-overuse attacks, forget this RSID. Future packets |
| 117 // with this RSID, but a different SSRC, will not spawn new associations. |
| 118 rsid_sinks_.erase(it_range.first, it_range.second); |
| 119 } |
103 | 120 |
104 if (processed_ssrcs_.size() < kMaxProcessedSsrcs) { // Prevent memory overuse | 121 if (processed_ssrcs_.size() < kMaxProcessedSsrcs) { // Prevent memory overuse |
105 processed_ssrcs_.insert(packet.Ssrc()); // Avoid re-examining in-depth. | 122 processed_ssrcs_.insert(packet.Ssrc()); // Avoid re-examining in-depth. |
106 } else if (!logged_max_processed_ssrcs_exceeded_) { | 123 } else if (!logged_max_processed_ssrcs_exceeded_) { |
107 LOG(LS_WARNING) << "More than " << kMaxProcessedSsrcs | 124 LOG(LS_WARNING) << "More than " << kMaxProcessedSsrcs |
108 << " different SSRCs seen."; | 125 << " different SSRCs seen."; |
109 logged_max_processed_ssrcs_exceeded_ = true; | 126 logged_max_processed_ssrcs_exceeded_ = true; |
110 } | 127 } |
111 } | 128 } |
112 | 129 |
113 void RtpDemuxer::ResolveRsidToSsrcAssociations( | |
114 const RtpPacketReceived& packet) { | |
115 std::string rsid; | |
116 if (packet.GetExtension<RtpStreamId>(&rsid)) { | |
117 // All streams associated with this RSID need to be marked as associated | |
118 // with this SSRC (if they aren't already). | |
119 auto it_range = rsid_sinks_.equal_range(rsid); | |
120 for (auto it = it_range.first; it != it_range.second; ++it) { | |
121 RecordSsrcToSinkAssociation(packet.Ssrc(), it->second); | |
122 } | |
123 | |
124 NotifyObserversOfRsidResolution(rsid, packet.Ssrc()); | |
125 | |
126 // To prevent memory-overuse attacks, forget this RSID. Future packets | |
127 // with this RSID, but a different SSRC, will not spawn new associations. | |
128 rsid_sinks_.erase(it_range.first, it_range.second); | |
129 } | |
130 } | |
131 | |
132 void RtpDemuxer::NotifyObserversOfRsidResolution(const std::string& rsid, | |
133 uint32_t ssrc) { | |
134 for (auto* observer : rsid_resolution_observers_) { | |
135 observer->OnRsidResolved(rsid, ssrc); | |
136 } | |
137 } | |
138 | |
139 } // namespace webrtc | 130 } // namespace webrtc |
OLD | NEW |