eladalon | a52722f | 2017-06-26 11:23:54 -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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef CALL_RTP_RTCP_DEMUXER_HELPER_H_ |
| 12 | #define CALL_RTP_RTCP_DEMUXER_HELPER_H_ |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 13 | |
| 14 | #include <algorithm> |
| 15 | #include <map> |
| 16 | #include <utility> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/array_view.h" |
| 19 | #include "api/optional.h" |
| 20 | #include "rtc_base/basictypes.h" |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
eladalon | 5daecca | 2017-08-04 06:34:54 -0700 | [diff] [blame] | 24 | // TODO(eladalon): Remove this in the next CL. |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 25 | template <typename Container> |
| 26 | bool 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 | |
| 35 | template <typename Container, typename Value> |
| 36 | size_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 | |
eladalon | 5daecca | 2017-08-04 06:34:54 -0700 | [diff] [blame] | 49 | template <typename Map, typename Value> |
| 50 | size_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 | |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 63 | template <typename Container, typename Key> |
| 64 | bool ContainerHasKey(const Container& c, const Key& k) { |
| 65 | return std::find(c.cbegin(), c.cend(), k) != c.cend(); |
| 66 | } |
| 67 | |
eladalon | 5daecca | 2017-08-04 06:34:54 -0700 | [diff] [blame] | 68 | // TODO(eladalon): Remove this in the next CL. |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 69 | template <typename Container> |
| 70 | bool 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 | |
eladalon | 5daecca | 2017-08-04 06:34:54 -0700 | [diff] [blame] | 78 | template <typename Map> |
| 79 | bool 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 Anton | 53c7ba6 | 2017-08-18 10:05:47 -0700 | [diff] [blame] | 86 | template <typename Container> |
| 87 | bool 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 | |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 93 | rtc::Optional<uint32_t> ParseRtcpPacketSenderSsrc( |
| 94 | rtc::ArrayView<const uint8_t> packet); |
| 95 | |
| 96 | } // namespace webrtc |
| 97 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 98 | #endif // CALL_RTP_RTCP_DEMUXER_HELPER_H_ |