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 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 | 56 // 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 | 57 // 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 | 58 // 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. | 59 // at most. Then, only packets with an unknown SSRC will be checked for RSID. |
65 ResolveRsidToSsrcAssociations(packet); | 60 ResolveRsidToSsrcAssociations(packet); |
66 | 61 |
67 auto it_range = ssrc_sinks_.equal_range(packet.Ssrc()); | 62 auto it_range = ssrc_sinks_.equal_range(packet.Ssrc()); |
(...skipping 16 matching lines...) Expand all Loading... | |
84 RTC_DCHECK(observer); | 79 RTC_DCHECK(observer); |
85 auto it = std::find(rsid_resolution_observers_.begin(), | 80 auto it = std::find(rsid_resolution_observers_.begin(), |
86 rsid_resolution_observers_.end(), observer); | 81 rsid_resolution_observers_.end(), observer); |
87 RTC_DCHECK(it != rsid_resolution_observers_.end()); | 82 RTC_DCHECK(it != rsid_resolution_observers_.end()); |
88 rsid_resolution_observers_.erase(it); | 83 rsid_resolution_observers_.erase(it); |
89 } | 84 } |
90 | 85 |
91 void RtpDemuxer::ResolveRsidToSsrcAssociations( | 86 void RtpDemuxer::ResolveRsidToSsrcAssociations( |
92 const RtpPacketReceived& packet) { | 87 const RtpPacketReceived& packet) { |
93 std::string rsid; | 88 std::string rsid; |
94 if (packet.GetExtension<RtpStreamId>(&rsid)) { | 89 if (!packet.GetExtension<RtpStreamId>(&rsid)) { |
95 // All streams associated with this RSID need to be marked as associated | 90 return; |
holmer
2017/07/03 09:06:14
Log a warning?
eladalon
2017/07/03 11:25:35
Not an error; this packet simply had no RSID.
| |
96 // with this SSRC (if they aren't already). | 91 } |
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 | 92 |
102 NotifyObserversOfRsidResolution(rsid, packet.Ssrc()); | 93 auto it = rsid_sinks_.find(rsid); |
94 if (it == rsid_sinks_.end()) { | |
95 return; | |
holmer
2017/07/03 09:06:14
Should we log warnings when this happens?
eladalon
2017/07/03 11:25:34
Done.
| |
96 } | |
103 | 97 |
104 // To prevent memory-overuse attacks, forget this RSID. Future packets | 98 // 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. | 99 // this SSRC. |
106 rsid_sinks_.erase(it_range.first, it_range.second); | 100 if (!AddSink(packet.Ssrc(), it->second)) { |
101 // In the faulty case of RSIDs mapped to SSRCs which are already associated | |
102 // with a sink, avoid propagating the problem to the resolution observers. | |
holmer
2017/07/03 09:06:14
Warning
eladalon
2017/07/03 11:25:35
Done.
| |
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 |