nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 1 | /* |
| 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" |
nisse | d76b7b2 | 2017-06-01 04:02:35 -0700 | [diff] [blame] | 13 | #include "webrtc/call/rtp_packet_sink_interface.h" |
nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 14 | #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" |
| 15 | |
| 16 | namespace webrtc { |
| 17 | |
eladalon | 760a076 | 2017-05-31 09:12:25 -0700 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | template <typename Key, typename Value> |
| 21 | bool 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 | |
nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 32 | RtpDemuxer::RtpDemuxer() {} |
| 33 | |
| 34 | RtpDemuxer::~RtpDemuxer() { |
| 35 | RTC_DCHECK(sinks_.empty()); |
| 36 | } |
| 37 | |
| 38 | void RtpDemuxer::AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink) { |
| 39 | RTC_DCHECK(sink); |
eladalon | 760a076 | 2017-05-31 09:12:25 -0700 | [diff] [blame] | 40 | RTC_DCHECK(!MultimapAssociationExists(sinks_, ssrc, sink)); |
nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 41 | sinks_.emplace(ssrc, sink); |
| 42 | } |
| 43 | |
| 44 | size_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 | |
| 58 | bool 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 |