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/rsid_resolution_observer.h" |
16 #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" | 17 #include "webrtc/call/rtp_rtcp_demuxer_helper.h" |
18 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" | 18 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" |
19 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" | 19 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" |
20 | 20 |
21 namespace webrtc { | 21 namespace webrtc { |
22 | 22 |
23 namespace { | |
24 constexpr size_t kMaxProcessedSsrcs = 1000; // Prevent memory overuse. | |
25 } // namespace | |
26 | |
27 RtpDemuxer::RtpDemuxer() = default; | 23 RtpDemuxer::RtpDemuxer() = default; |
28 | 24 |
29 RtpDemuxer::~RtpDemuxer() { | 25 RtpDemuxer::~RtpDemuxer() { |
30 RTC_DCHECK(ssrc_sinks_.empty()); | 26 RTC_DCHECK(ssrc_sinks_.empty()); |
31 RTC_DCHECK(rsid_sinks_.empty()); | 27 RTC_DCHECK(rsid_sinks_.empty()); |
32 } | 28 } |
33 | 29 |
34 void RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) { | 30 void RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) { |
35 RTC_DCHECK(sink); | 31 RTC_DCHECK(sink); |
36 RecordSsrcToSinkAssociation(ssrc, sink); | 32 RecordSsrcToSinkAssociation(ssrc, sink); |
37 } | 33 } |
38 | 34 |
39 void RtpDemuxer::AddSink(const std::string& rsid, | 35 void RtpDemuxer::AddSink(const std::string& rsid, |
40 RtpPacketSinkInterface* sink) { | 36 RtpPacketSinkInterface* sink) { |
41 RTC_DCHECK(StreamId::IsLegalName(rsid)); | 37 RTC_DCHECK(StreamId::IsLegalName(rsid)); |
42 RTC_DCHECK(sink); | 38 RTC_DCHECK(sink); |
43 RTC_DCHECK(!MultimapAssociationExists(rsid_sinks_, rsid, sink)); | 39 RTC_DCHECK(!MultimapAssociationExists(rsid_sinks_, rsid, sink)); |
44 | 40 |
45 rsid_sinks_.emplace(rsid, sink); | 41 rsid_sinks_.emplace(rsid, sink); |
46 | |
47 // This RSID might now map to an SSRC which we saw earlier. | |
48 processed_ssrcs_.clear(); | |
49 } | 42 } |
50 | 43 |
51 bool RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) { | 44 bool RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) { |
52 RTC_DCHECK(sink); | 45 RTC_DCHECK(sink); |
53 return (RemoveFromMultimapByValue(&ssrc_sinks_, sink) + | 46 return (RemoveFromMultimapByValue(&ssrc_sinks_, sink) + |
54 RemoveFromMultimapByValue(&rsid_sinks_, sink)) > 0; | 47 RemoveFromMultimapByValue(&rsid_sinks_, sink)) > 0; |
55 } | 48 } |
56 | 49 |
57 void RtpDemuxer::RecordSsrcToSinkAssociation(uint32_t ssrc, | 50 void RtpDemuxer::RecordSsrcToSinkAssociation(uint32_t ssrc, |
58 RtpPacketSinkInterface* sink) { | 51 RtpPacketSinkInterface* sink) { |
59 RTC_DCHECK(sink); | 52 RTC_DCHECK(sink); |
60 // The association might already have been set by a different | 53 // The association might already have been set by a different |
61 // configuration source. | 54 // configuration source. |
62 if (!MultimapAssociationExists(ssrc_sinks_, ssrc, sink)) { | 55 if (!MultimapAssociationExists(ssrc_sinks_, ssrc, sink)) { |
63 ssrc_sinks_.emplace(ssrc, sink); | 56 ssrc_sinks_.emplace(ssrc, sink); |
64 } | 57 } |
65 } | 58 } |
66 | 59 |
67 bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) { | 60 bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) { |
68 ResolveAssociations(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); | |
danilchap
2017/06/28 16:51:34
may be add if (!rsid_sinks_.empty()) as a fast che
eladalon
2017/06/29 08:09:59
Would need to also check for no RSID resolution ob
| |
66 | |
69 auto it_range = ssrc_sinks_.equal_range(packet.Ssrc()); | 67 auto it_range = ssrc_sinks_.equal_range(packet.Ssrc()); |
70 for (auto it = it_range.first; it != it_range.second; ++it) { | 68 for (auto it = it_range.first; it != it_range.second; ++it) { |
71 it->second->OnRtpPacket(packet); | 69 it->second->OnRtpPacket(packet); |
72 } | 70 } |
73 return it_range.first != it_range.second; | 71 return it_range.first != it_range.second; |
74 } | 72 } |
75 | 73 |
76 void RtpDemuxer::RegisterRsidResolutionObserver( | 74 void RtpDemuxer::RegisterRsidResolutionObserver( |
77 RsidResolutionObserver* observer) { | 75 RsidResolutionObserver* observer) { |
78 RTC_DCHECK(observer); | 76 RTC_DCHECK(observer); |
79 RTC_DCHECK(!ContainerHasKey(rsid_resolution_observers_, observer)); | 77 RTC_DCHECK(!ContainerHasKey(rsid_resolution_observers_, observer)); |
80 | 78 |
81 rsid_resolution_observers_.push_back(observer); | 79 rsid_resolution_observers_.push_back(observer); |
82 | |
83 processed_ssrcs_.clear(); // New observer requires new notifications. | |
84 } | 80 } |
85 | 81 |
86 void RtpDemuxer::DeregisterRsidResolutionObserver( | 82 void RtpDemuxer::DeregisterRsidResolutionObserver( |
87 const RsidResolutionObserver* observer) { | 83 const RsidResolutionObserver* observer) { |
88 RTC_DCHECK(observer); | 84 RTC_DCHECK(observer); |
89 auto it = std::find(rsid_resolution_observers_.begin(), | 85 auto it = std::find(rsid_resolution_observers_.begin(), |
90 rsid_resolution_observers_.end(), observer); | 86 rsid_resolution_observers_.end(), observer); |
91 RTC_DCHECK(it != rsid_resolution_observers_.end()); | 87 RTC_DCHECK(it != rsid_resolution_observers_.end()); |
92 rsid_resolution_observers_.erase(it); | 88 rsid_resolution_observers_.erase(it); |
93 } | 89 } |
94 | 90 |
95 void RtpDemuxer::ResolveAssociations(const RtpPacketReceived& packet) { | |
96 // Avoid expensive string comparisons for RSID by looking the sinks up only | |
97 // by SSRC whenever possible. | |
98 if (processed_ssrcs_.find(packet.Ssrc()) != processed_ssrcs_.cend()) { | |
99 return; | |
100 } | |
101 | |
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( | 91 void RtpDemuxer::ResolveRsidToSsrcAssociations( |
114 const RtpPacketReceived& packet) { | 92 const RtpPacketReceived& packet) { |
115 std::string rsid; | 93 std::string rsid; |
116 if (packet.GetExtension<RtpStreamId>(&rsid)) { | 94 if (packet.GetExtension<RtpStreamId>(&rsid)) { |
117 // All streams associated with this RSID need to be marked as associated | 95 // All streams associated with this RSID need to be marked as associated |
118 // with this SSRC (if they aren't already). | 96 // with this SSRC (if they aren't already). |
119 auto it_range = rsid_sinks_.equal_range(rsid); | 97 auto it_range = rsid_sinks_.equal_range(rsid); |
120 for (auto it = it_range.first; it != it_range.second; ++it) { | 98 for (auto it = it_range.first; it != it_range.second; ++it) { |
121 RecordSsrcToSinkAssociation(packet.Ssrc(), it->second); | 99 RecordSsrcToSinkAssociation(packet.Ssrc(), it->second); |
122 } | 100 } |
123 | 101 |
124 NotifyObserversOfRsidResolution(rsid, packet.Ssrc()); | 102 NotifyObserversOfRsidResolution(rsid, packet.Ssrc()); |
nisse-webrtc
2017/06/29 14:59:43
Does this mean we also get one callback to the obs
eladalon
2017/06/29 17:59:07
Yes and yes.
| |
125 | 103 |
126 // To prevent memory-overuse attacks, forget this RSID. Future packets | 104 // To prevent memory-overuse attacks, forget this RSID. Future packets |
127 // with this RSID, but a different SSRC, will not spawn new associations. | 105 // with this RSID, but a different SSRC, will not spawn new associations. |
128 rsid_sinks_.erase(it_range.first, it_range.second); | 106 rsid_sinks_.erase(it_range.first, it_range.second); |
129 } | 107 } |
130 } | 108 } |
131 | 109 |
132 void RtpDemuxer::NotifyObserversOfRsidResolution(const std::string& rsid, | 110 void RtpDemuxer::NotifyObserversOfRsidResolution(const std::string& rsid, |
133 uint32_t ssrc) { | 111 uint32_t ssrc) { |
134 for (auto* observer : rsid_resolution_observers_) { | 112 for (auto* observer : rsid_resolution_observers_) { |
135 observer->OnRsidResolved(rsid, ssrc); | 113 observer->OnRsidResolved(rsid, ssrc); |
136 } | 114 } |
137 } | 115 } |
138 | 116 |
139 } // namespace webrtc | 117 } // namespace webrtc |
OLD | NEW |