Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: webrtc/call/rtp_demuxer.cc

Issue 2920993002: Add RSID-based demuxing to RtpDemuxer (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/base/checks.h" 11 #include "webrtc/base/checks.h"
12 #include "webrtc/call/rtp_demuxer.h" 12 #include "webrtc/call/rtp_demuxer.h"
13 #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h"
13 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" 14 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
14 15
15 namespace webrtc { 16 namespace webrtc {
16 17
17 namespace { 18 namespace {
18 19
20 constexpr size_t kMaxProcessedSsrcs = 1000; // Prevent memory overuse.
21
19 template <typename Key, typename Value> 22 template <typename Key, typename Value>
20 bool MultimapAssociationExists(const std::multimap<Key, Value>& multimap, 23 bool MultimapAssociationExists(const std::multimap<Key, Value>& multimap,
21 Key key, 24 Key key,
22 Value val) { 25 Value val) {
23 auto it_range = multimap.equal_range(key); 26 auto it_range = multimap.equal_range(key);
24 using Reference = typename std::multimap<Key, Value>::const_reference; 27 using Reference = typename std::multimap<Key, Value>::const_reference;
25 return std::any_of(it_range.first, it_range.second, 28 return std::any_of(it_range.first, it_range.second,
26 [val](Reference elem) { return elem.second == val; }); 29 [val](Reference elem) { return elem.second == val; });
27 } 30 }
28 31
32 template <typename Key, typename Value>
33 size_t RemoveFromMultimapByValue(std::multimap<Key, Value*>& multimap,
34 const Value* value) {
35 size_t count = 0;
36 for (auto it = multimap.begin(); it != multimap.end();) {
37 if (it->second == value) {
38 it = multimap.erase(it);
39 ++count;
40 } else {
41 ++it;
42 }
43 }
44 return count;
45 }
46
29 } // namespace 47 } // namespace
30 48
31 RtpDemuxer::RtpDemuxer() {} 49 RtpDemuxer::RtpDemuxer() {}
32 50
33 RtpDemuxer::~RtpDemuxer() { 51 RtpDemuxer::~RtpDemuxer() {
34 RTC_DCHECK(sinks_.empty()); 52 RTC_DCHECK(sinks_.empty());
35 } 53 }
36 54
37 void RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) { 55 void RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) {
56 RecordSsrcToSinkAssociation(ssrc, sink);
57 }
58
59 void RtpDemuxer::AddSink(const std::string& rsid,
60 RtpPacketSinkInterface* sink) {
61 RTC_DCHECK(StreamId::IsLegalName(rsid.c_str(), rsid.size()));
38 RTC_DCHECK(sink); 62 RTC_DCHECK(sink);
39 RTC_DCHECK(!MultimapAssociationExists(sinks_, ssrc, sink)); 63 RTC_DCHECK(!MultimapAssociationExists(rsid_sinks_, rsid, sink));
40 sinks_.emplace(ssrc, sink); 64
65 rsid_sinks_.emplace(rsid, sink);
66
67 // This RSID might now map to an SSRC which we saw earlier.
68 processed_ssrcs_.clear();
41 } 69 }
42 70
43 size_t RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) { 71 size_t RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) {
44 RTC_DCHECK(sink); 72 RTC_DCHECK(sink);
45 size_t count = 0; 73 return RemoveFromMultimapByValue(sinks_, sink) +
46 for (auto it = sinks_.begin(); it != sinks_.end(); ) { 74 RemoveFromMultimapByValue(rsid_sinks_, sink);
47 if (it->second == sink) { 75 }
48 it = sinks_.erase(it); 76
49 ++count; 77 void RtpDemuxer::RecordSsrcToSinkAssociation(uint32_t ssrc,
50 } else { 78 RtpPacketSinkInterface* sink) {
51 ++it; 79 RTC_DCHECK(sink);
52 } 80 // The association might already have been set by a different
81 // configuration source.
82 if (!MultimapAssociationExists(sinks_, ssrc, sink)) {
83 sinks_.emplace(ssrc, sink);
53 } 84 }
54 return count;
55 } 85 }
56 86
57 bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) { 87 bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) {
58 bool found = false; 88 FindSsrcAssociations(packet);
59 auto it_range = sinks_.equal_range(packet.Ssrc()); 89 auto it_range = sinks_.equal_range(packet.Ssrc());
60 for (auto it = it_range.first; it != it_range.second; ++it) { 90 for (auto it = it_range.first; it != it_range.second; ++it) {
61 found = true;
62 it->second->OnRtpPacket(packet); 91 it->second->OnRtpPacket(packet);
63 } 92 }
64 return found; 93 return it_range.first != it_range.second;
94 }
95
96 void RtpDemuxer::FindSsrcAssociations(const RtpPacketReceived& packet) {
97 // Avoid expensive string comparisons for RSID by looking the sinks up only
98 // by SSRC whenever possible.
99 if (processed_ssrcs_.find(packet.Ssrc()) != processed_ssrcs_.cend()) {
100 return;
101 }
102
103 // RSID-based associations:
104 std::string rsid;
105 if (packet.GetExtension<RtpStreamId>(&rsid)) {
106 // All streams associated with this RSID need to be marked as associated
107 // with this SSRC (if they aren't already).
108 auto it_range = rsid_sinks_.equal_range(rsid);
109 for (auto it = it_range.first; it != it_range.second; ++it) {
110 RecordSsrcToSinkAssociation(packet.Ssrc(), it->second);
111 }
112
113 // To prevent memory-overuse attacks, forget this RSID. Future packets
114 // with this RSID, but a different SSRC, will not spawn new associations.
115 rsid_sinks_.erase(it_range.first, it_range.second);
116 }
117
118 if (processed_ssrcs_.size() < kMaxProcessedSsrcs) { // Prevent memory overuse
119 processed_ssrcs_.insert(packet.Ssrc()); // Avoid re-examining in-depth.
120 }
65 } 121 }
66 122
67 } // namespace webrtc 123 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698