blob: 4b66713538edad265d8fbb5a21aa7394dccd6063 [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
14#include <algorithm>
15#include <map>
16#include <utility>
17
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020018#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/array_view.h"
eladalona52722f2017-06-26 11:23:54 -070020
21namespace webrtc {
22
eladalon5daecca2017-08-04 06:34:54 -070023// TODO(eladalon): Remove this in the next CL.
eladalona52722f2017-06-26 11:23:54 -070024template <typename Container>
25bool MultimapAssociationExists(const Container& multimap,
26 const typename Container::key_type& key,
27 const typename Container::mapped_type& val) {
28 auto it_range = multimap.equal_range(key);
29 using Reference = typename Container::const_reference;
30 return std::any_of(it_range.first, it_range.second,
31 [val](Reference elem) { return elem.second == val; });
32}
33
34template <typename Container, typename Value>
35size_t RemoveFromMultimapByValue(Container* multimap, const Value& value) {
36 size_t count = 0;
37 for (auto it = multimap->begin(); it != multimap->end();) {
38 if (it->second == value) {
39 it = multimap->erase(it);
40 ++count;
41 } else {
42 ++it;
43 }
44 }
45 return count;
46}
47
eladalon5daecca2017-08-04 06:34:54 -070048template <typename Map, typename Value>
49size_t RemoveFromMapByValue(Map* map, const Value& value) {
50 size_t count = 0;
51 for (auto it = map->begin(); it != map->end();) {
52 if (it->second == value) {
53 it = map->erase(it);
54 ++count;
55 } else {
56 ++it;
57 }
58 }
59 return count;
60}
61
eladalona52722f2017-06-26 11:23:54 -070062template <typename Container, typename Key>
63bool ContainerHasKey(const Container& c, const Key& k) {
64 return std::find(c.cbegin(), c.cend(), k) != c.cend();
65}
66
eladalon5daecca2017-08-04 06:34:54 -070067// TODO(eladalon): Remove this in the next CL.
eladalona52722f2017-06-26 11:23:54 -070068template <typename Container>
69bool MultimapHasValue(const Container& c,
70 const typename Container::mapped_type& v) {
71 auto predicate = [v](const typename Container::value_type& it) {
72 return it.second == v;
73 };
74 return std::any_of(c.cbegin(), c.cend(), predicate);
75}
76
eladalon5daecca2017-08-04 06:34:54 -070077template <typename Map>
78bool MapHasValue(const Map& map, const typename Map::mapped_type& value) {
79 auto predicate = [value](const typename Map::value_type& it) {
80 return it.second == value;
81 };
82 return std::any_of(map.cbegin(), map.cend(), predicate);
83}
84
Steve Anton53c7ba62017-08-18 10:05:47 -070085template <typename Container>
86bool MultimapHasKey(const Container& c,
87 const typename Container::key_type& key) {
88 auto it_range = c.equal_range(key);
89 return it_range.first != it_range.second;
90}
91
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020092absl::optional<uint32_t> ParseRtcpPacketSenderSsrc(
eladalona52722f2017-06-26 11:23:54 -070093 rtc::ArrayView<const uint8_t> packet);
94
95} // namespace webrtc
96
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020097#endif // CALL_RTP_RTCP_DEMUXER_HELPER_H_