blob: 6134d561436e7646b32f94bd6022876c6b108634 [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#ifndef CALL_RTP_RTCP_DEMUXER_HELPER_H_
12#define CALL_RTP_RTCP_DEMUXER_HELPER_H_
eladalona52722f2017-06-26 11:23:54 -070013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
eladalona52722f2017-06-26 11:23:54 -070016
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020017#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/array_view.h"
eladalona52722f2017-06-26 11:23:54 -070019
20namespace webrtc {
21
eladalon5daecca2017-08-04 06:34:54 -070022// TODO(eladalon): Remove this in the next CL.
eladalona52722f2017-06-26 11:23:54 -070023template <typename Container>
24bool MultimapAssociationExists(const Container& multimap,
25 const typename Container::key_type& key,
26 const typename Container::mapped_type& val) {
27 auto it_range = multimap.equal_range(key);
28 using Reference = typename Container::const_reference;
29 return std::any_of(it_range.first, it_range.second,
30 [val](Reference elem) { return elem.second == val; });
31}
32
33template <typename Container, typename Value>
34size_t RemoveFromMultimapByValue(Container* multimap, const Value& value) {
35 size_t count = 0;
36 for (auto it = multimap->begin(); it != multimap->end();) {
37 if (it->second == value) {
38 it = multimap->erase(it);
39 ++count;
40 } else {
41 ++it;
42 }
43 }
44 return count;
45}
46
eladalon5daecca2017-08-04 06:34:54 -070047template <typename Map, typename Value>
48size_t RemoveFromMapByValue(Map* map, const Value& value) {
49 size_t count = 0;
50 for (auto it = map->begin(); it != map->end();) {
51 if (it->second == value) {
52 it = map->erase(it);
53 ++count;
54 } else {
55 ++it;
56 }
57 }
58 return count;
59}
60
eladalona52722f2017-06-26 11:23:54 -070061template <typename Container, typename Key>
62bool ContainerHasKey(const Container& c, const Key& k) {
63 return std::find(c.cbegin(), c.cend(), k) != c.cend();
64}
65
eladalon5daecca2017-08-04 06:34:54 -070066// TODO(eladalon): Remove this in the next CL.
eladalona52722f2017-06-26 11:23:54 -070067template <typename Container>
68bool MultimapHasValue(const Container& c,
69 const typename Container::mapped_type& v) {
70 auto predicate = [v](const typename Container::value_type& it) {
71 return it.second == v;
72 };
73 return std::any_of(c.cbegin(), c.cend(), predicate);
74}
75
eladalon5daecca2017-08-04 06:34:54 -070076template <typename Map>
77bool MapHasValue(const Map& map, const typename Map::mapped_type& value) {
78 auto predicate = [value](const typename Map::value_type& it) {
79 return it.second == value;
80 };
81 return std::any_of(map.cbegin(), map.cend(), predicate);
82}
83
Steve Anton53c7ba62017-08-18 10:05:47 -070084template <typename Container>
85bool MultimapHasKey(const Container& c,
86 const typename Container::key_type& key) {
87 auto it_range = c.equal_range(key);
88 return it_range.first != it_range.second;
89}
90
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020091absl::optional<uint32_t> ParseRtcpPacketSenderSsrc(
eladalona52722f2017-06-26 11:23:54 -070092 rtc::ArrayView<const uint8_t> packet);
93
94} // namespace webrtc
95
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020096#endif // CALL_RTP_RTCP_DEMUXER_HELPER_H_