blob: 916e612c17ca394573a451bb8b1a9081f5fed660 [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"
nissee4bcd6d2017-05-16 04:47:04 -070014#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
15
16namespace webrtc {
17
eladalon760a0762017-05-31 09:12:25 -070018namespace {
19
20template <typename Key, typename Value>
21bool MultimapAssociationExists(const std::multimap<Key, Value>& multimap,
22 Key key,
23 Value val) {
24 auto it_range = multimap.equal_range(key);
25 using Reference = typename std::multimap<Key, Value>::const_reference;
26 return std::any_of(it_range.first, it_range.second,
27 [val](Reference elem) { return elem.second == val; });
28}
29
30} // namespace
31
nissee4bcd6d2017-05-16 04:47:04 -070032RtpDemuxer::RtpDemuxer() {}
33
34RtpDemuxer::~RtpDemuxer() {
35 RTC_DCHECK(sinks_.empty());
36}
37
38void RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) {
39 RTC_DCHECK(sink);
eladalon760a0762017-05-31 09:12:25 -070040 RTC_DCHECK(!MultimapAssociationExists(sinks_, ssrc, sink));
nissee4bcd6d2017-05-16 04:47:04 -070041 sinks_.emplace(ssrc, sink);
42}
43
44size_t RtpDemuxer::RemoveSink(const RtpPacketSinkInterface* sink) {
45 RTC_DCHECK(sink);
46 size_t count = 0;
47 for (auto it = sinks_.begin(); it != sinks_.end(); ) {
48 if (it->second == sink) {
49 it = sinks_.erase(it);
50 ++count;
51 } else {
52 ++it;
53 }
54 }
55 return count;
56}
57
58bool RtpDemuxer::OnRtpPacket(const RtpPacketReceived& packet) {
59 bool found = false;
60 auto it_range = sinks_.equal_range(packet.Ssrc());
61 for (auto it = it_range.first; it != it_range.second; ++it) {
62 found = true;
63 it->second->OnRtpPacket(packet);
64 }
65 return found;
66}
67
68} // namespace webrtc