blob: 7445729fbb2faf854da88851215e50f555a4d3fc [file] [log] [blame]
Chen Xingd2a66862019-06-03 14:53:42 +02001/*
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
11#ifndef API_RTP_PACKET_INFOS_H_
12#define API_RTP_PACKET_INFOS_H_
13
14#include <cstdint>
Minyue Lic759f832019-08-09 13:20:03 +020015#include <utility>
Chen Xingd2a66862019-06-03 14:53:42 +020016#include <vector>
17
Niels Möller7c8c4db2022-06-13 10:36:38 +020018#include "api/make_ref_counted.h"
Chen Xingd2a66862019-06-03 14:53:42 +020019#include "api/ref_counted_base.h"
20#include "api/rtp_packet_info.h"
21#include "api/scoped_refptr.h"
Johannes Kron0809e7e2020-01-21 11:54:21 +010022#include "rtc_base/system/rtc_export.h"
Chen Xingd2a66862019-06-03 14:53:42 +020023
24namespace webrtc {
25
26// Semi-immutable structure to hold information about packets used to assemble
27// an audio or video frame. Uses internal reference counting to make it very
28// cheap to copy.
29//
Artem Titovcfea2182021-08-10 01:22:31 +020030// We should ideally just use `std::vector<RtpPacketInfo>` and have it
31// `std::move()`-ed as the per-packet information is transferred from one object
Chen Xingd2a66862019-06-03 14:53:42 +020032// to another. But moving the info, instead of copying it, is not easily done
33// for the current video code.
Johannes Kron0809e7e2020-01-21 11:54:21 +010034class RTC_EXPORT RtpPacketInfos {
Chen Xingd2a66862019-06-03 14:53:42 +020035 public:
36 using vector_type = std::vector<RtpPacketInfo>;
37
38 using value_type = vector_type::value_type;
39 using size_type = vector_type::size_type;
40 using difference_type = vector_type::difference_type;
41 using const_reference = vector_type::const_reference;
42 using const_pointer = vector_type::const_pointer;
43 using const_iterator = vector_type::const_iterator;
44 using const_reverse_iterator = vector_type::const_reverse_iterator;
45
46 using reference = const_reference;
47 using pointer = const_pointer;
48 using iterator = const_iterator;
49 using reverse_iterator = const_reverse_iterator;
50
51 RtpPacketInfos() {}
Minyue Lic759f832019-08-09 13:20:03 +020052 explicit RtpPacketInfos(const vector_type& entries)
53 : data_(Data::Create(entries)) {}
54
55 explicit RtpPacketInfos(vector_type&& entries)
56 : data_(Data::Create(std::move(entries))) {}
Chen Xingd2a66862019-06-03 14:53:42 +020057
58 RtpPacketInfos(const RtpPacketInfos& other) = default;
59 RtpPacketInfos(RtpPacketInfos&& other) = default;
60 RtpPacketInfos& operator=(const RtpPacketInfos& other) = default;
61 RtpPacketInfos& operator=(RtpPacketInfos&& other) = default;
62
63 const_reference operator[](size_type pos) const { return entries()[pos]; }
64
65 const_reference at(size_type pos) const { return entries().at(pos); }
66 const_reference front() const { return entries().front(); }
67 const_reference back() const { return entries().back(); }
68
69 const_iterator begin() const { return entries().begin(); }
70 const_iterator end() const { return entries().end(); }
71 const_reverse_iterator rbegin() const { return entries().rbegin(); }
72 const_reverse_iterator rend() const { return entries().rend(); }
73
74 const_iterator cbegin() const { return entries().cbegin(); }
75 const_iterator cend() const { return entries().cend(); }
76 const_reverse_iterator crbegin() const { return entries().crbegin(); }
77 const_reverse_iterator crend() const { return entries().crend(); }
78
79 bool empty() const { return entries().empty(); }
80 size_type size() const { return entries().size(); }
81
82 private:
Niels Möllerbb57de22022-01-11 15:40:22 +010083 class Data final : public rtc::RefCountedNonVirtual<Data> {
Chen Xingd2a66862019-06-03 14:53:42 +020084 public:
Minyue Lic759f832019-08-09 13:20:03 +020085 static rtc::scoped_refptr<Data> Create(const vector_type& entries) {
Chen Xing1796a822019-07-24 10:58:52 +020086 // Performance optimization for the empty case.
87 if (entries.empty()) {
88 return nullptr;
89 }
90
Niels Möllerbb57de22022-01-11 15:40:22 +010091 return rtc::make_ref_counted<Data>(entries);
Chen Xingd2a66862019-06-03 14:53:42 +020092 }
93
Minyue Lic759f832019-08-09 13:20:03 +020094 static rtc::scoped_refptr<Data> Create(vector_type&& entries) {
95 // Performance optimization for the empty case.
96 if (entries.empty()) {
97 return nullptr;
98 }
99
Niels Möllerbb57de22022-01-11 15:40:22 +0100100 return rtc::make_ref_counted<Data>(std::move(entries));
Minyue Lic759f832019-08-09 13:20:03 +0200101 }
102
Chen Xingd2a66862019-06-03 14:53:42 +0200103 const vector_type& entries() const { return entries_; }
104
Minyue Lic759f832019-08-09 13:20:03 +0200105 explicit Data(const vector_type& entries) : entries_(entries) {}
106 explicit Data(vector_type&& entries) : entries_(std::move(entries)) {}
Niels Möllerbb57de22022-01-11 15:40:22 +0100107 ~Data() = default;
Chen Xingd2a66862019-06-03 14:53:42 +0200108
Niels Möllerbb57de22022-01-11 15:40:22 +0100109 private:
Chen Xingd2a66862019-06-03 14:53:42 +0200110 const vector_type entries_;
111 };
112
113 static const vector_type& empty_entries() {
114 static const vector_type& value = *new vector_type();
115 return value;
116 }
117
118 const vector_type& entries() const {
119 if (data_ != nullptr) {
120 return data_->entries();
121 } else {
122 return empty_entries();
123 }
124 }
125
126 rtc::scoped_refptr<Data> data_;
127};
128
129} // namespace webrtc
130
131#endif // API_RTP_PACKET_INFOS_H_