rtp_packet_infos.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2019 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 API_RTP_PACKET_INFOS_H_
  11. #define API_RTP_PACKET_INFOS_H_
  12. #include <cstdint>
  13. #include <utility>
  14. #include <vector>
  15. #include "api/ref_counted_base.h"
  16. #include "api/rtp_packet_info.h"
  17. #include "api/scoped_refptr.h"
  18. #include "rtc_base/system/rtc_export.h"
  19. namespace webrtc {
  20. // Semi-immutable structure to hold information about packets used to assemble
  21. // an audio or video frame. Uses internal reference counting to make it very
  22. // cheap to copy.
  23. //
  24. // We should ideally just use |std::vector<RtpPacketInfo>| and have it
  25. // |std::move()|-ed as the per-packet information is transferred from one object
  26. // to another. But moving the info, instead of copying it, is not easily done
  27. // for the current video code.
  28. class RTC_EXPORT RtpPacketInfos {
  29. public:
  30. using vector_type = std::vector<RtpPacketInfo>;
  31. using value_type = vector_type::value_type;
  32. using size_type = vector_type::size_type;
  33. using difference_type = vector_type::difference_type;
  34. using const_reference = vector_type::const_reference;
  35. using const_pointer = vector_type::const_pointer;
  36. using const_iterator = vector_type::const_iterator;
  37. using const_reverse_iterator = vector_type::const_reverse_iterator;
  38. using reference = const_reference;
  39. using pointer = const_pointer;
  40. using iterator = const_iterator;
  41. using reverse_iterator = const_reverse_iterator;
  42. RtpPacketInfos() {}
  43. explicit RtpPacketInfos(const vector_type& entries)
  44. : data_(Data::Create(entries)) {}
  45. explicit RtpPacketInfos(vector_type&& entries)
  46. : data_(Data::Create(std::move(entries))) {}
  47. RtpPacketInfos(const RtpPacketInfos& other) = default;
  48. RtpPacketInfos(RtpPacketInfos&& other) = default;
  49. RtpPacketInfos& operator=(const RtpPacketInfos& other) = default;
  50. RtpPacketInfos& operator=(RtpPacketInfos&& other) = default;
  51. const_reference operator[](size_type pos) const { return entries()[pos]; }
  52. const_reference at(size_type pos) const { return entries().at(pos); }
  53. const_reference front() const { return entries().front(); }
  54. const_reference back() const { return entries().back(); }
  55. const_iterator begin() const { return entries().begin(); }
  56. const_iterator end() const { return entries().end(); }
  57. const_reverse_iterator rbegin() const { return entries().rbegin(); }
  58. const_reverse_iterator rend() const { return entries().rend(); }
  59. const_iterator cbegin() const { return entries().cbegin(); }
  60. const_iterator cend() const { return entries().cend(); }
  61. const_reverse_iterator crbegin() const { return entries().crbegin(); }
  62. const_reverse_iterator crend() const { return entries().crend(); }
  63. bool empty() const { return entries().empty(); }
  64. size_type size() const { return entries().size(); }
  65. private:
  66. class Data : public rtc::RefCountedBase {
  67. public:
  68. static rtc::scoped_refptr<Data> Create(const vector_type& entries) {
  69. // Performance optimization for the empty case.
  70. if (entries.empty()) {
  71. return nullptr;
  72. }
  73. return new Data(entries);
  74. }
  75. static rtc::scoped_refptr<Data> Create(vector_type&& entries) {
  76. // Performance optimization for the empty case.
  77. if (entries.empty()) {
  78. return nullptr;
  79. }
  80. return new Data(std::move(entries));
  81. }
  82. const vector_type& entries() const { return entries_; }
  83. private:
  84. explicit Data(const vector_type& entries) : entries_(entries) {}
  85. explicit Data(vector_type&& entries) : entries_(std::move(entries)) {}
  86. ~Data() override {}
  87. const vector_type entries_;
  88. };
  89. static const vector_type& empty_entries() {
  90. static const vector_type& value = *new vector_type();
  91. return value;
  92. }
  93. const vector_type& entries() const {
  94. if (data_ != nullptr) {
  95. return data_->entries();
  96. } else {
  97. return empty_entries();
  98. }
  99. }
  100. rtc::scoped_refptr<Data> data_;
  101. };
  102. } // namespace webrtc
  103. #endif // API_RTP_PACKET_INFOS_H_