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