blob: 0a4767d84a0d9730e53c63b192fe0815c8b9d74d [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
11#ifndef WEBRTC_CALL_RTP_RTCP_DEMUXER_HELPER_H_
12#define WEBRTC_CALL_RTP_RTCP_DEMUXER_HELPER_H_
13
14#include <algorithm>
15#include <map>
16#include <utility>
17
kwiberg529662a2017-09-04 05:43:17 -070018#include "webrtc/api/array_view.h"
kwiberg84f6a3f2017-09-05 08:43:13 -070019#include "webrtc/api/optional.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020020#include "webrtc/rtc_base/basictypes.h"
eladalona52722f2017-06-26 11:23:54 -070021
22namespace webrtc {
23
eladalon5daecca2017-08-04 06:34:54 -070024// TODO(eladalon): Remove this in the next CL.
eladalona52722f2017-06-26 11:23:54 -070025template <typename Container>
26bool MultimapAssociationExists(const Container& multimap,
27 const typename Container::key_type& key,
28 const typename Container::mapped_type& val) {
29 auto it_range = multimap.equal_range(key);
30 using Reference = typename Container::const_reference;
31 return std::any_of(it_range.first, it_range.second,
32 [val](Reference elem) { return elem.second == val; });
33}
34
35template <typename Container, typename Value>
36size_t RemoveFromMultimapByValue(Container* multimap, const Value& value) {
37 size_t count = 0;
38 for (auto it = multimap->begin(); it != multimap->end();) {
39 if (it->second == value) {
40 it = multimap->erase(it);
41 ++count;
42 } else {
43 ++it;
44 }
45 }
46 return count;
47}
48
eladalon5daecca2017-08-04 06:34:54 -070049template <typename Map, typename Value>
50size_t RemoveFromMapByValue(Map* map, const Value& value) {
51 size_t count = 0;
52 for (auto it = map->begin(); it != map->end();) {
53 if (it->second == value) {
54 it = map->erase(it);
55 ++count;
56 } else {
57 ++it;
58 }
59 }
60 return count;
61}
62
eladalona52722f2017-06-26 11:23:54 -070063template <typename Container, typename Key>
64bool ContainerHasKey(const Container& c, const Key& k) {
65 return std::find(c.cbegin(), c.cend(), k) != c.cend();
66}
67
eladalon5daecca2017-08-04 06:34:54 -070068// TODO(eladalon): Remove this in the next CL.
eladalona52722f2017-06-26 11:23:54 -070069template <typename Container>
70bool MultimapHasValue(const Container& c,
71 const typename Container::mapped_type& v) {
72 auto predicate = [v](const typename Container::value_type& it) {
73 return it.second == v;
74 };
75 return std::any_of(c.cbegin(), c.cend(), predicate);
76}
77
eladalon5daecca2017-08-04 06:34:54 -070078template <typename Map>
79bool MapHasValue(const Map& map, const typename Map::mapped_type& value) {
80 auto predicate = [value](const typename Map::value_type& it) {
81 return it.second == value;
82 };
83 return std::any_of(map.cbegin(), map.cend(), predicate);
84}
85
Steve Anton53c7ba62017-08-18 10:05:47 -070086template <typename Container>
87bool MultimapHasKey(const Container& c,
88 const typename Container::key_type& key) {
89 auto it_range = c.equal_range(key);
90 return it_range.first != it_range.second;
91}
92
eladalona52722f2017-06-26 11:23:54 -070093rtc::Optional<uint32_t> ParseRtcpPacketSenderSsrc(
94 rtc::ArrayView<const uint8_t> packet);
95
96} // namespace webrtc
97
98#endif // WEBRTC_CALL_RTP_RTCP_DEMUXER_HELPER_H_