blob: d35548d4cbd9ed82fe463700826c1abcf51f936d [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"
Niels Möllera7de6982019-03-21 15:48:49 +010021#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
eladalona52722f2017-06-26 11:23:54 -070023
24namespace webrtc {
25
26RtcpDemuxer::RtcpDemuxer() = default;
27
28RtcpDemuxer::~RtcpDemuxer() {
29 RTC_DCHECK(ssrc_sinks_.empty());
30 RTC_DCHECK(rsid_sinks_.empty());
31 RTC_DCHECK(broadcast_sinks_.empty());
32}
33
34void RtcpDemuxer::AddSink(uint32_t sender_ssrc, RtcpPacketSinkInterface* sink) {
35 RTC_DCHECK(sink);
36 RTC_DCHECK(!ContainerHasKey(broadcast_sinks_, sink));
37 RTC_DCHECK(!MultimapAssociationExists(ssrc_sinks_, sender_ssrc, sink));
38 ssrc_sinks_.emplace(sender_ssrc, sink);
39}
40
41void RtcpDemuxer::AddSink(const std::string& rsid,
42 RtcpPacketSinkInterface* sink) {
Niels Möllera7de6982019-03-21 15:48:49 +010043 RTC_DCHECK(IsLegalRsidName(rsid));
eladalona52722f2017-06-26 11:23:54 -070044 RTC_DCHECK(sink);
45 RTC_DCHECK(!ContainerHasKey(broadcast_sinks_, sink));
46 RTC_DCHECK(!MultimapAssociationExists(rsid_sinks_, rsid, sink));
47 rsid_sinks_.emplace(rsid, sink);
48}
49
50void RtcpDemuxer::AddBroadcastSink(RtcpPacketSinkInterface* sink) {
51 RTC_DCHECK(sink);
52 RTC_DCHECK(!MultimapHasValue(ssrc_sinks_, sink));
53 RTC_DCHECK(!MultimapHasValue(rsid_sinks_, sink));
54 RTC_DCHECK(!ContainerHasKey(broadcast_sinks_, sink));
55 broadcast_sinks_.push_back(sink);
56}
57
58void RtcpDemuxer::RemoveSink(const RtcpPacketSinkInterface* sink) {
59 RTC_DCHECK(sink);
60 size_t removal_count = RemoveFromMultimapByValue(&ssrc_sinks_, sink) +
61 RemoveFromMultimapByValue(&rsid_sinks_, sink);
62 RTC_DCHECK_GT(removal_count, 0);
63}
64
65void RtcpDemuxer::RemoveBroadcastSink(const RtcpPacketSinkInterface* sink) {
66 RTC_DCHECK(sink);
67 auto it = std::find(broadcast_sinks_.begin(), broadcast_sinks_.end(), sink);
68 RTC_DCHECK(it != broadcast_sinks_.end());
69 broadcast_sinks_.erase(it);
70}
71
72void RtcpDemuxer::OnRtcpPacket(rtc::ArrayView<const uint8_t> packet) {
73 // Perform sender-SSRC-based demuxing for packets with a sender-SSRC.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020074 absl::optional<uint32_t> sender_ssrc = ParseRtcpPacketSenderSsrc(packet);
eladalona52722f2017-06-26 11:23:54 -070075 if (sender_ssrc) {
76 auto it_range = ssrc_sinks_.equal_range(*sender_ssrc);
77 for (auto it = it_range.first; it != it_range.second; ++it) {
78 it->second->OnRtcpPacket(packet);
79 }
80 }
81
82 // All packets, even those without a sender-SSRC, are broadcast to sinks
83 // which listen to broadcasts.
84 for (RtcpPacketSinkInterface* sink : broadcast_sinks_) {
85 sink->OnRtcpPacket(packet);
86 }
87}
88
Steve Antonb3329172017-08-17 15:23:51 -070089void RtcpDemuxer::OnSsrcBoundToRsid(const std::string& rsid, uint32_t ssrc) {
eladalona52722f2017-06-26 11:23:54 -070090 // Record the new SSRC association for all of the sinks that were associated
91 // with the RSID.
92 auto it_range = rsid_sinks_.equal_range(rsid);
93 for (auto it = it_range.first; it != it_range.second; ++it) {
94 RtcpPacketSinkInterface* sink = it->second;
95 // Watch out for pre-existing SSRC-based associations.
96 if (!MultimapAssociationExists(ssrc_sinks_, ssrc, sink)) {
97 AddSink(ssrc, sink);
98 }
99 }
100
101 // RSIDs are uniquely associated with SSRCs; no need to keep in memory
102 // the RSID-to-sink association of resolved RSIDs.
103 rsid_sinks_.erase(it_range.first, it_range.second);
104}
105
106} // namespace webrtc