Chromium Code Reviews| 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 if (ssrc_sinks_.find(ssrc) == ssrc_sinks_.cend()) { | |
|
danilchap
2017/06/30 16:45:30
can be done with single lookup instead of two:
ret
eladalon
2017/07/03 08:23:35
That's less readable IMHO, but okay.
| |
| 38 ssrc_sinks_.emplace(ssrc, sink); | |
| 39 return true; | |
| 40 } else { | |
|
danilchap
2017/06/30 16:45:30
fyi (since likely irrelevant due to previous comme
eladalon
2017/07/03 08:23:35
Thanks for letting me know. I don't like this part
| |
| 41 return false; | |
| 42 } | |
| 33 } | 43 } |
| 34 | 44 |
| 35 void RtpDemuxer::AddSink(const std::string& rsid, | 45 void RtpDemuxer::AddSink(const std::string& rsid, |
| 36 RtpPacketSinkInterface* sink) { | 46 RtpPacketSinkInterface* sink) { |
| 37 RTC_DCHECK(StreamId::IsLegalName(rsid)); | 47 RTC_DCHECK(StreamId::IsLegalName(rsid)); |
| 38 RTC_DCHECK(sink); | 48 RTC_DCHECK(sink); |
| 39 RTC_DCHECK(!MultimapAssociationExists(rsid_sinks_, rsid, sink)); | 49 RTC_DCHECK(rsid_sinks_.find(rsid) == rsid_sinks_.cend()); |
| 40 | 50 |
| 41 rsid_sinks_.emplace(rsid, sink); | 51 rsid_sinks_.emplace(rsid, sink); |
| 42 } | 52 } |
| 43 | 53 |
| 44 bool RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) { | 54 bool RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) { |
| 45 RTC_DCHECK(sink); | 55 RTC_DCHECK(sink); |
| 46 return (RemoveFromMultimapByValue(&ssrc_sinks_, sink) + | 56 return (RemoveFromMapByValue(&ssrc_sinks_, sink) + |
| 47 RemoveFromMultimapByValue(&rsid_sinks_, sink)) > 0; | 57 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 } | 58 } |
| 59 | 59 |
| 60 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 | 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 | 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 | 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. | 64 // at most. Then, only packets with an unknown SSRC will be checked for RSID. |
| 65 ResolveRsidToSsrcAssociations(packet); | 65 ResolveRsidToSsrcAssociations(packet); |
| 66 | 66 |
| 67 auto it_range = ssrc_sinks_.equal_range(packet.Ssrc()); | 67 auto it_range = ssrc_sinks_.equal_range(packet.Ssrc()); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 84 RTC_DCHECK(observer); | 84 RTC_DCHECK(observer); |
| 85 auto it = std::find(rsid_resolution_observers_.begin(), | 85 auto it = std::find(rsid_resolution_observers_.begin(), |
| 86 rsid_resolution_observers_.end(), observer); | 86 rsid_resolution_observers_.end(), observer); |
| 87 RTC_DCHECK(it != rsid_resolution_observers_.end()); | 87 RTC_DCHECK(it != rsid_resolution_observers_.end()); |
| 88 rsid_resolution_observers_.erase(it); | 88 rsid_resolution_observers_.erase(it); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void RtpDemuxer::ResolveRsidToSsrcAssociations( | 91 void RtpDemuxer::ResolveRsidToSsrcAssociations( |
| 92 const RtpPacketReceived& packet) { | 92 const RtpPacketReceived& packet) { |
| 93 std::string rsid; | 93 std::string rsid; |
| 94 if (packet.GetExtension<RtpStreamId>(&rsid)) { | 94 if (!packet.GetExtension<RtpStreamId>(&rsid)) { |
| 95 // All streams associated with this RSID need to be marked as associated | 95 return; |
| 96 // with this SSRC (if they aren't already). | 96 } |
| 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 | 97 |
| 102 NotifyObserversOfRsidResolution(rsid, packet.Ssrc()); | 98 auto it = rsid_sinks_.find(rsid); |
| 99 if (it == rsid_sinks_.end()) { | |
| 100 return; | |
| 101 } | |
| 103 | 102 |
| 104 // To prevent memory-overuse attacks, forget this RSID. Future packets | 103 // 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. | 104 // this SSRC. |
| 106 rsid_sinks_.erase(it_range.first, it_range.second); | 105 if (!AddSink(packet.Ssrc(), it->second)) { |
| 106 // In the faulty case of RSIDs mapped to SSRCs which are already associated | |
| 107 // with a sink, avoid propagating the problem to the resolution observers. | |
| 108 return; | |
| 107 } | 109 } |
| 110 | |
| 111 // We make the assumption that observers are only interested in notifications | |
| 112 // for RSIDs which are registered with this module. (RTCP sinks are normally | |
| 113 // created with RTP sinks.) | |
| 114 NotifyObserversOfRsidResolution(rsid, packet.Ssrc()); | |
| 115 | |
| 116 // This RSID cannot later be associated with another SSRC. | |
| 117 rsid_sinks_.erase(it); | |
| 108 } | 118 } |
| 109 | 119 |
| 110 void RtpDemuxer::NotifyObserversOfRsidResolution(const std::string& rsid, | 120 void RtpDemuxer::NotifyObserversOfRsidResolution(const std::string& rsid, |
| 111 uint32_t ssrc) { | 121 uint32_t ssrc) { |
| 112 for (auto* observer : rsid_resolution_observers_) { | 122 for (auto* observer : rsid_resolution_observers_) { |
| 113 observer->OnRsidResolved(rsid, ssrc); | 123 observer->OnRsidResolved(rsid, ssrc); |
| 114 } | 124 } |
| 115 } | 125 } |
| 116 | 126 |
| 117 } // namespace webrtc | 127 } // namespace webrtc |
| OLD | NEW |