blob: e181d3a5557c3bff903b713264986f27269f906f [file] [log] [blame]
eladalona52722f2017-06-26 11:23:54 -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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "call/rtcp_demuxer.h"
eladalona52722f2017-06-26 11:23:54 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
14#include <algorithm>
15#include <utility>
16
17#include "absl/types/optional.h"
Patrik Höglund3e113432017-12-15 14:40:10 +010018#include "api/rtp_headers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "call/rtcp_packet_sink_interface.h"
20#include "call/rtp_rtcp_demuxer_helper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
eladalona52722f2017-06-26 11:23:54 -070022
23namespace webrtc {
24
25RtcpDemuxer::RtcpDemuxer() = default;
26
27RtcpDemuxer::~RtcpDemuxer() {
28 RTC_DCHECK(ssrc_sinks_.empty());
29 RTC_DCHECK(rsid_sinks_.empty());
30 RTC_DCHECK(broadcast_sinks_.empty());
31}
32
33void RtcpDemuxer::AddSink(uint32_t sender_ssrc, RtcpPacketSinkInterface* sink) {
34 RTC_DCHECK(sink);
35 RTC_DCHECK(!ContainerHasKey(broadcast_sinks_, sink));
36 RTC_DCHECK(!MultimapAssociationExists(ssrc_sinks_, sender_ssrc, sink));
37 ssrc_sinks_.emplace(sender_ssrc, sink);
38}
39
40void RtcpDemuxer::AddSink(const std::string& rsid,
41 RtcpPacketSinkInterface* sink) {
Joachim Bauchd3b7ec22018-08-01 10:12:00 +020042 RTC_DCHECK(StreamId::IsLegalRsidName(rsid));
eladalona52722f2017-06-26 11:23:54 -070043 RTC_DCHECK(sink);
44 RTC_DCHECK(!ContainerHasKey(broadcast_sinks_, sink));
45 RTC_DCHECK(!MultimapAssociationExists(rsid_sinks_, rsid, sink));
46 rsid_sinks_.emplace(rsid, sink);
47}
48
49void RtcpDemuxer::AddBroadcastSink(RtcpPacketSinkInterface* sink) {
50 RTC_DCHECK(sink);
51 RTC_DCHECK(!MultimapHasValue(ssrc_sinks_, sink));
52 RTC_DCHECK(!MultimapHasValue(rsid_sinks_, sink));
53 RTC_DCHECK(!ContainerHasKey(broadcast_sinks_, sink));
54 broadcast_sinks_.push_back(sink);
55}
56
57void RtcpDemuxer::RemoveSink(const RtcpPacketSinkInterface* sink) {
58 RTC_DCHECK(sink);
59 size_t removal_count = RemoveFromMultimapByValue(&ssrc_sinks_, sink) +
60 RemoveFromMultimapByValue(&rsid_sinks_, sink);
61 RTC_DCHECK_GT(removal_count, 0);
62}
63
64void RtcpDemuxer::RemoveBroadcastSink(const RtcpPacketSinkInterface* sink) {
65 RTC_DCHECK(sink);
66 auto it = std::find(broadcast_sinks_.begin(), broadcast_sinks_.end(), sink);
67 RTC_DCHECK(it != broadcast_sinks_.end());
68 broadcast_sinks_.erase(it);
69}
70
71void RtcpDemuxer::OnRtcpPacket(rtc::ArrayView<const uint8_t> packet) {
72 // Perform sender-SSRC-based demuxing for packets with a sender-SSRC.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020073 absl::optional<uint32_t> sender_ssrc = ParseRtcpPacketSenderSsrc(packet);
eladalona52722f2017-06-26 11:23:54 -070074 if (sender_ssrc) {
75 auto it_range = ssrc_sinks_.equal_range(*sender_ssrc);
76 for (auto it = it_range.first; it != it_range.second; ++it) {
77 it->second->OnRtcpPacket(packet);
78 }
79 }
80
81 // All packets, even those without a sender-SSRC, are broadcast to sinks
82 // which listen to broadcasts.
83 for (RtcpPacketSinkInterface* sink : broadcast_sinks_) {
84 sink->OnRtcpPacket(packet);
85 }
86}
87
Steve Antonb3329172017-08-17 15:23:51 -070088void RtcpDemuxer::OnSsrcBoundToRsid(const std::string& rsid, uint32_t ssrc) {
eladalona52722f2017-06-26 11:23:54 -070089 // Record the new SSRC association for all of the sinks that were associated
90 // with the RSID.
91 auto it_range = rsid_sinks_.equal_range(rsid);
92 for (auto it = it_range.first; it != it_range.second; ++it) {
93 RtcpPacketSinkInterface* sink = it->second;
94 // Watch out for pre-existing SSRC-based associations.
95 if (!MultimapAssociationExists(ssrc_sinks_, ssrc, sink)) {
96 AddSink(ssrc, sink);
97 }
98 }
99
100 // RSIDs are uniquely associated with SSRCs; no need to keep in memory
101 // the RSID-to-sink association of resolved RSIDs.
102 rsid_sinks_.erase(it_range.first, it_range.second);
103}
104
105} // namespace webrtc