blob: b2c9510ccabc3905074afd64263487079b2c04eb [file] [log] [blame]
nissee4bcd6d2017-05-16 04:47:04 -07001/*
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"
nissed76b7b22017-06-01 04:02:35 -070013#include "webrtc/call/rtp_packet_sink_interface.h"
eladalond0244c22017-06-08 04:19:13 -070014#include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h"
nissee4bcd6d2017-05-16 04:47:04 -070015#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
16
17namespace webrtc {
18
eladalon760a0762017-05-31 09:12:25 -070019namespace {
20
eladalond0244c22017-06-08 04:19:13 -070021constexpr size_t kMaxProcessedSsrcs = 1000; // Prevent memory overuse.
22
eladalon760a0762017-05-31 09:12:25 -070023template <typename Key, typename Value>
24bool 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
eladalond0244c22017-06-08 04:19:13 -070033template <typename Key, typename Value>
34size_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
eladalon760a0762017-05-31 09:12:25 -070048} // namespace
49
nissee4bcd6d2017-05-16 04:47:04 -070050RtpDemuxer::RtpDemuxer() {}
51
52RtpDemuxer::~RtpDemuxer() {
53 RTC_DCHECK(sinks_.empty());
54}
55
56void RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) {
eladalond0244c22017-06-08 04:19:13 -070057 RecordSsrcToSinkAssociation(ssrc, sink);
nissee4bcd6d2017-05-16 04:47:04 -070058}
59
eladalond0244c22017-06-08 04:19:13 -070060void RtpDemuxer::AddSink(const std::string& rsid,
61 RtpPacketSinkInterface* sink) {
62 RTC_DCHECK(StreamId::IsLegalName(rsid));
nissee4bcd6d2017-05-16 04:47:04 -070063 RTC_DCHECK(sink);
eladalond0244c22017-06-08 04:19:13 -070064 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
72bool RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) {
73 RTC_DCHECK(sink);
74 return (RemoveFromMultimapByValue(&sinks_, sink) +
75 RemoveFromMultimapByValue(&rsid_sinks_, sink)) > 0;
76}
77
78void 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);
nissee4bcd6d2017-05-16 04:47:04 -070085 }
nissee4bcd6d2017-05-16 04:47:04 -070086}
87
88bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) {
eladalond0244c22017-06-08 04:19:13 -070089 FindSsrcAssociations(packet);
nissee4bcd6d2017-05-16 04:47:04 -070090 auto it_range = sinks_.equal_range(packet.Ssrc());
91 for (auto it = it_range.first; it != it_range.second; ++it) {
nissee4bcd6d2017-05-16 04:47:04 -070092 it->second->OnRtpPacket(packet);
93 }
eladalond0244c22017-06-08 04:19:13 -070094 return it_range.first != it_range.second;
95}
96
97void 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 }
nissee4bcd6d2017-05-16 04:47:04 -0700122}
123
124} // namespace webrtc