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 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 16 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/array_view.h" |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
eladalon | 5daecca | 2017-08-04 06:34:54 -0700 | [diff] [blame] | 22 | // TODO(eladalon): Remove this in the next CL. |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 23 | template <typename Container> |
| 24 | bool 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 | |
| 33 | template <typename Container, typename Value> |
| 34 | size_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 | |
eladalon | 5daecca | 2017-08-04 06:34:54 -0700 | [diff] [blame] | 47 | template <typename Map, typename Value> |
| 48 | size_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 | |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 61 | template <typename Container, typename Key> |
| 62 | bool ContainerHasKey(const Container& c, const Key& k) { |
| 63 | return std::find(c.cbegin(), c.cend(), k) != c.cend(); |
| 64 | } |
| 65 | |
eladalon | 5daecca | 2017-08-04 06:34:54 -0700 | [diff] [blame] | 66 | // TODO(eladalon): Remove this in the next CL. |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 67 | template <typename Container> |
| 68 | bool 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 | |
eladalon | 5daecca | 2017-08-04 06:34:54 -0700 | [diff] [blame] | 76 | template <typename Map> |
| 77 | bool 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 Anton | 53c7ba6 | 2017-08-18 10:05:47 -0700 | [diff] [blame] | 84 | template <typename Container> |
| 85 | bool 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 Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 91 | absl::optional<uint32_t> ParseRtcpPacketSenderSsrc( |
eladalon | a52722f | 2017-06-26 11:23:54 -0700 | [diff] [blame] | 92 | rtc::ArrayView<const uint8_t> packet); |
| 93 | |
| 94 | } // namespace webrtc |
| 95 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 96 | #endif // CALL_RTP_RTCP_DEMUXER_HELPER_H_ |