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