nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/base/checks.h" |
| 12 | #include "webrtc/call/rtp_demuxer.h" |
nisse | d76b7b2 | 2017-06-01 04:02:35 -0700 | [diff] [blame] | 13 | #include "webrtc/call/rtp_packet_sink_interface.h" |
eladalon | d0244c2 | 2017-06-08 04:19:13 -0700 | [diff] [blame^] | 14 | #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" |
nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 15 | #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" |
| 16 | |
| 17 | namespace webrtc { |
| 18 | |
eladalon | 760a076 | 2017-05-31 09:12:25 -0700 | [diff] [blame] | 19 | namespace { |
| 20 | |
eladalon | d0244c2 | 2017-06-08 04:19:13 -0700 | [diff] [blame^] | 21 | constexpr size_t kMaxProcessedSsrcs = 1000; // Prevent memory overuse. |
| 22 | |
eladalon | 760a076 | 2017-05-31 09:12:25 -0700 | [diff] [blame] | 23 | template <typename Key, typename Value> |
| 24 | bool MultimapAssociationExists(const std::multimap<Key, Value>& multimap, |
| 25 | Key key, |
| 26 | Value val) { |
| 27 | auto it_range = multimap.equal_range(key); |
| 28 | using Reference = typename std::multimap<Key, Value>::const_reference; |
| 29 | return std::any_of(it_range.first, it_range.second, |
| 30 | [val](Reference elem) { return elem.second == val; }); |
| 31 | } |
| 32 | |
eladalon | d0244c2 | 2017-06-08 04:19:13 -0700 | [diff] [blame^] | 33 | template <typename Key, typename Value> |
| 34 | size_t RemoveFromMultimapByValue(std::multimap<Key, Value*>* multimap, |
| 35 | const Value* value) { |
| 36 | size_t count = 0; |
| 37 | for (auto it = multimap->begin(); it != multimap->end();) { |
| 38 | if (it->second == value) { |
| 39 | it = multimap->erase(it); |
| 40 | ++count; |
| 41 | } else { |
| 42 | ++it; |
| 43 | } |
| 44 | } |
| 45 | return count; |
| 46 | } |
| 47 | |
eladalon | 760a076 | 2017-05-31 09:12:25 -0700 | [diff] [blame] | 48 | } // namespace |
| 49 | |
nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 50 | RtpDemuxer::RtpDemuxer() {} |
| 51 | |
| 52 | RtpDemuxer::~RtpDemuxer() { |
| 53 | RTC_DCHECK(sinks_.empty()); |
| 54 | } |
| 55 | |
| 56 | void RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) { |
eladalon | d0244c2 | 2017-06-08 04:19:13 -0700 | [diff] [blame^] | 57 | RecordSsrcToSinkAssociation(ssrc, sink); |
nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 58 | } |
| 59 | |
eladalon | d0244c2 | 2017-06-08 04:19:13 -0700 | [diff] [blame^] | 60 | void RtpDemuxer::AddSink(const std::string& rsid, |
| 61 | RtpPacketSinkInterface* sink) { |
| 62 | RTC_DCHECK(StreamId::IsLegalName(rsid)); |
nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 63 | RTC_DCHECK(sink); |
eladalon | d0244c2 | 2017-06-08 04:19:13 -0700 | [diff] [blame^] | 64 | RTC_DCHECK(!MultimapAssociationExists(rsid_sinks_, rsid, sink)); |
| 65 | |
| 66 | rsid_sinks_.emplace(rsid, sink); |
| 67 | |
| 68 | // This RSID might now map to an SSRC which we saw earlier. |
| 69 | processed_ssrcs_.clear(); |
| 70 | } |
| 71 | |
| 72 | bool RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) { |
| 73 | RTC_DCHECK(sink); |
| 74 | return (RemoveFromMultimapByValue(&sinks_, sink) + |
| 75 | RemoveFromMultimapByValue(&rsid_sinks_, sink)) > 0; |
| 76 | } |
| 77 | |
| 78 | void RtpDemuxer::RecordSsrcToSinkAssociation(uint32_t ssrc, |
| 79 | RtpPacketSinkInterface* sink) { |
| 80 | RTC_DCHECK(sink); |
| 81 | // The association might already have been set by a different |
| 82 | // configuration source. |
| 83 | if (!MultimapAssociationExists(sinks_, ssrc, sink)) { |
| 84 | sinks_.emplace(ssrc, sink); |
nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 85 | } |
nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) { |
eladalon | d0244c2 | 2017-06-08 04:19:13 -0700 | [diff] [blame^] | 89 | FindSsrcAssociations(packet); |
nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 90 | auto it_range = sinks_.equal_range(packet.Ssrc()); |
| 91 | for (auto it = it_range.first; it != it_range.second; ++it) { |
nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 92 | it->second->OnRtpPacket(packet); |
| 93 | } |
eladalon | d0244c2 | 2017-06-08 04:19:13 -0700 | [diff] [blame^] | 94 | return it_range.first != it_range.second; |
| 95 | } |
| 96 | |
| 97 | void RtpDemuxer::FindSsrcAssociations(const RtpPacketReceived& packet) { |
| 98 | // Avoid expensive string comparisons for RSID by looking the sinks up only |
| 99 | // by SSRC whenever possible. |
| 100 | if (processed_ssrcs_.find(packet.Ssrc()) != processed_ssrcs_.cend()) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | // RSID-based associations: |
| 105 | std::string rsid; |
| 106 | if (packet.GetExtension<RtpStreamId>(&rsid)) { |
| 107 | // All streams associated with this RSID need to be marked as associated |
| 108 | // with this SSRC (if they aren't already). |
| 109 | auto it_range = rsid_sinks_.equal_range(rsid); |
| 110 | for (auto it = it_range.first; it != it_range.second; ++it) { |
| 111 | RecordSsrcToSinkAssociation(packet.Ssrc(), it->second); |
| 112 | } |
| 113 | |
| 114 | // To prevent memory-overuse attacks, forget this RSID. Future packets |
| 115 | // with this RSID, but a different SSRC, will not spawn new associations. |
| 116 | rsid_sinks_.erase(it_range.first, it_range.second); |
| 117 | } |
| 118 | |
| 119 | if (processed_ssrcs_.size() < kMaxProcessedSsrcs) { // Prevent memory overuse |
| 120 | processed_ssrcs_.insert(packet.Ssrc()); // Avoid re-examining in-depth. |
| 121 | } |
nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | } // namespace webrtc |