| 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/call/rsid_resolution_observer.h" | 13 #include "webrtc/call/rsid_resolution_observer.h" |
| 14 #include "webrtc/call/rtp_packet_sink_interface.h" | 14 #include "webrtc/call/rtp_packet_sink_interface.h" |
| 15 #include "webrtc/call/rtp_rtcp_demuxer_helper.h" | 15 #include "webrtc/call/rtp_rtcp_demuxer_helper.h" |
| 16 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" | 16 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" |
| 17 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" | 17 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" |
| 18 #include "webrtc/rtc_base/checks.h" | 18 #include "webrtc/rtc_base/checks.h" |
| 19 #include "webrtc/rtc_base/logging.h" | 19 #include "webrtc/rtc_base/logging.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 bool RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) { | 30 void RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) { |
| 31 RTC_DCHECK(sink); | 31 RTC_DCHECK(sink); |
| 32 // The association might already have been set by a different | 32 RecordSsrcToSinkAssociation(ssrc, sink); |
| 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; | |
| 38 } | 33 } |
| 39 | 34 |
| 40 void RtpDemuxer::AddSink(const std::string& rsid, | 35 void RtpDemuxer::AddSink(const std::string& rsid, |
| 41 RtpPacketSinkInterface* sink) { | 36 RtpPacketSinkInterface* sink) { |
| 42 RTC_DCHECK(StreamId::IsLegalName(rsid)); | 37 RTC_DCHECK(StreamId::IsLegalName(rsid)); |
| 43 RTC_DCHECK(sink); | 38 RTC_DCHECK(sink); |
| 44 RTC_DCHECK(rsid_sinks_.find(rsid) == rsid_sinks_.cend()); | 39 RTC_DCHECK(!MultimapAssociationExists(rsid_sinks_, rsid, sink)); |
| 45 | 40 |
| 46 rsid_sinks_.emplace(rsid, sink); | 41 rsid_sinks_.emplace(rsid, sink); |
| 47 } | 42 } |
| 48 | 43 |
| 49 bool RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) { | 44 bool RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) { |
| 50 RTC_DCHECK(sink); | 45 RTC_DCHECK(sink); |
| 51 return (RemoveFromMapByValue(&ssrc_sinks_, sink) + | 46 return (RemoveFromMultimapByValue(&ssrc_sinks_, sink) + |
| 52 RemoveFromMapByValue(&rsid_sinks_, sink)) > 0; | 47 RemoveFromMultimapByValue(&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 } |
| 53 } | 58 } |
| 54 | 59 |
| 55 bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) { | 60 bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& 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. |
| 56 ResolveRsidToSsrcAssociations(packet); | 65 ResolveRsidToSsrcAssociations(packet); |
| 57 | 66 |
| 58 auto it_range = ssrc_sinks_.equal_range(packet.Ssrc()); | 67 auto it_range = ssrc_sinks_.equal_range(packet.Ssrc()); |
| 59 for (auto it = it_range.first; it != it_range.second; ++it) { | 68 for (auto it = it_range.first; it != it_range.second; ++it) { |
| 60 it->second->OnRtpPacket(packet); | 69 it->second->OnRtpPacket(packet); |
| 61 } | 70 } |
| 62 return it_range.first != it_range.second; | 71 return it_range.first != it_range.second; |
| 63 } | 72 } |
| 64 | 73 |
| 65 void RtpDemuxer::RegisterRsidResolutionObserver( | 74 void RtpDemuxer::RegisterRsidResolutionObserver( |
| 66 RsidResolutionObserver* observer) { | 75 RsidResolutionObserver* observer) { |
| 67 RTC_DCHECK(observer); | 76 RTC_DCHECK(observer); |
| 68 RTC_DCHECK(!ContainerHasKey(rsid_resolution_observers_, observer)); | 77 RTC_DCHECK(!ContainerHasKey(rsid_resolution_observers_, observer)); |
| 69 | 78 |
| 70 rsid_resolution_observers_.push_back(observer); | 79 rsid_resolution_observers_.push_back(observer); |
| 71 } | 80 } |
| 72 | 81 |
| 73 void RtpDemuxer::DeregisterRsidResolutionObserver( | 82 void RtpDemuxer::DeregisterRsidResolutionObserver( |
| 74 const RsidResolutionObserver* observer) { | 83 const RsidResolutionObserver* observer) { |
| 75 RTC_DCHECK(observer); | 84 RTC_DCHECK(observer); |
| 76 auto it = std::find(rsid_resolution_observers_.begin(), | 85 auto it = std::find(rsid_resolution_observers_.begin(), |
| 77 rsid_resolution_observers_.end(), observer); | 86 rsid_resolution_observers_.end(), observer); |
| 78 RTC_DCHECK(it != rsid_resolution_observers_.end()); | 87 RTC_DCHECK(it != rsid_resolution_observers_.end()); |
| 79 rsid_resolution_observers_.erase(it); | 88 rsid_resolution_observers_.erase(it); |
| 80 } | 89 } |
| 81 | 90 |
| 82 void RtpDemuxer::ResolveRsidToSsrcAssociations( | 91 void RtpDemuxer::ResolveRsidToSsrcAssociations( |
| 83 const RtpPacketReceived& packet) { | 92 const RtpPacketReceived& packet) { |
| 84 std::string rsid; | 93 std::string rsid; |
| 85 if (!packet.GetExtension<RtpStreamId>(&rsid)) { | 94 if (packet.GetExtension<RtpStreamId>(&rsid)) { |
| 86 return; | 95 // All streams associated with this RSID need to be marked as associated |
| 96 // with this SSRC (if they aren't already). |
| 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 |
| 102 NotifyObserversOfRsidResolution(rsid, packet.Ssrc()); |
| 103 |
| 104 // To prevent memory-overuse attacks, forget this RSID. Future packets |
| 105 // with this RSID, but a different SSRC, will not spawn new associations. |
| 106 rsid_sinks_.erase(it_range.first, it_range.second); |
| 87 } | 107 } |
| 88 | |
| 89 auto it = rsid_sinks_.find(rsid); | |
| 90 if (it == rsid_sinks_.end()) { | |
| 91 // Might be unknown, or we might have already associated this RSID | |
| 92 // with a sink. | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 // If a sink is associated with an RSID, we should associate it with | |
| 97 // this SSRC. | |
| 98 if (!AddSink(packet.Ssrc(), it->second)) { | |
| 99 // In the faulty case of RSIDs mapped to SSRCs which are already associated | |
| 100 // with a sink, avoid propagating the problem to the resolution observers. | |
| 101 LOG(LS_WARNING) << "RSID (" << rsid << ") resolved to preconfigured SSRC (" | |
| 102 << packet.Ssrc() << ")."; | |
| 103 return; | |
| 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); | |
| 113 } | 108 } |
| 114 | 109 |
| 115 void RtpDemuxer::NotifyObserversOfRsidResolution(const std::string& rsid, | 110 void RtpDemuxer::NotifyObserversOfRsidResolution(const std::string& rsid, |
| 116 uint32_t ssrc) { | 111 uint32_t ssrc) { |
| 117 for (auto* observer : rsid_resolution_observers_) { | 112 for (auto* observer : rsid_resolution_observers_) { |
| 118 observer->OnRsidResolved(rsid, ssrc); | 113 observer->OnRsidResolved(rsid, ssrc); |
| 119 } | 114 } |
| 120 } | 115 } |
| 121 | 116 |
| 122 } // namespace webrtc | 117 } // namespace webrtc |
| OLD | NEW |