rtp_rtcp_demuxer_helper.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef CALL_RTP_RTCP_DEMUXER_HELPER_H_
  11. #define CALL_RTP_RTCP_DEMUXER_HELPER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include "absl/types/optional.h"
  15. #include "api/array_view.h"
  16. namespace webrtc {
  17. // TODO(eladalon): Remove this in the next CL.
  18. template <typename Container>
  19. bool MultimapAssociationExists(const Container& multimap,
  20. const typename Container::key_type& key,
  21. const typename Container::mapped_type& val) {
  22. auto it_range = multimap.equal_range(key);
  23. using Reference = typename Container::const_reference;
  24. return std::any_of(it_range.first, it_range.second,
  25. [val](Reference elem) { return elem.second == val; });
  26. }
  27. template <typename Container, typename Value>
  28. size_t RemoveFromMultimapByValue(Container* multimap, const Value& value) {
  29. size_t count = 0;
  30. for (auto it = multimap->begin(); it != multimap->end();) {
  31. if (it->second == value) {
  32. it = multimap->erase(it);
  33. ++count;
  34. } else {
  35. ++it;
  36. }
  37. }
  38. return count;
  39. }
  40. template <typename Map, typename Value>
  41. size_t RemoveFromMapByValue(Map* map, const Value& value) {
  42. size_t count = 0;
  43. for (auto it = map->begin(); it != map->end();) {
  44. if (it->second == value) {
  45. it = map->erase(it);
  46. ++count;
  47. } else {
  48. ++it;
  49. }
  50. }
  51. return count;
  52. }
  53. template <typename Container, typename Key>
  54. bool ContainerHasKey(const Container& c, const Key& k) {
  55. return std::find(c.cbegin(), c.cend(), k) != c.cend();
  56. }
  57. // TODO(eladalon): Remove this in the next CL.
  58. template <typename Container>
  59. bool MultimapHasValue(const Container& c,
  60. const typename Container::mapped_type& v) {
  61. auto predicate = [v](const typename Container::value_type& it) {
  62. return it.second == v;
  63. };
  64. return std::any_of(c.cbegin(), c.cend(), predicate);
  65. }
  66. template <typename Map>
  67. bool MapHasValue(const Map& map, const typename Map::mapped_type& value) {
  68. auto predicate = [value](const typename Map::value_type& it) {
  69. return it.second == value;
  70. };
  71. return std::any_of(map.cbegin(), map.cend(), predicate);
  72. }
  73. template <typename Container>
  74. bool MultimapHasKey(const Container& c,
  75. const typename Container::key_type& key) {
  76. auto it_range = c.equal_range(key);
  77. return it_range.first != it_range.second;
  78. }
  79. absl::optional<uint32_t> ParseRtcpPacketSenderSsrc(
  80. rtc::ArrayView<const uint8_t> packet);
  81. } // namespace webrtc
  82. #endif // CALL_RTP_RTCP_DEMUXER_HELPER_H_