blob: 08da1aee616d37c32aec2489451d28bd07be7ca4 [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
18#include "api/ref_counted_base.h"
19#include "api/rtp_packet_info.h"
20#include "api/scoped_refptr.h"
21
22namespace webrtc {
23
24// Semi-immutable structure to hold information about packets used to assemble
25// an audio or video frame. Uses internal reference counting to make it very
26// cheap to copy.
27//
28// We should ideally just use |std::vector<RtpPacketInfo>| and have it
29// |std::move()|-ed as the per-packet information is transferred from one object
30// to another. But moving the info, instead of copying it, is not easily done
31// for the current video code.
32class RtpPacketInfos {
33 public:
34 using vector_type = std::vector<RtpPacketInfo>;
35
36 using value_type = vector_type::value_type;
37 using size_type = vector_type::size_type;
38 using difference_type = vector_type::difference_type;
39 using const_reference = vector_type::const_reference;
40 using const_pointer = vector_type::const_pointer;
41 using const_iterator = vector_type::const_iterator;
42 using const_reverse_iterator = vector_type::const_reverse_iterator;
43
44 using reference = const_reference;
45 using pointer = const_pointer;
46 using iterator = const_iterator;
47 using reverse_iterator = const_reverse_iterator;
48
49 RtpPacketInfos() {}
Minyue Lic759f832019-08-09 13:20:03 +020050 explicit RtpPacketInfos(const vector_type& entries)
51 : data_(Data::Create(entries)) {}
52
53 explicit RtpPacketInfos(vector_type&& entries)
54 : data_(Data::Create(std::move(entries))) {}
Chen Xingd2a66862019-06-03 14:53:42 +020055
56 RtpPacketInfos(const RtpPacketInfos& other) = default;
57 RtpPacketInfos(RtpPacketInfos&& other) = default;
58 RtpPacketInfos& operator=(const RtpPacketInfos& other) = default;
59 RtpPacketInfos& operator=(RtpPacketInfos&& other) = default;
60
61 const_reference operator[](size_type pos) const { return entries()[pos]; }
62
63 const_reference at(size_type pos) const { return entries().at(pos); }
64 const_reference front() const { return entries().front(); }
65 const_reference back() const { return entries().back(); }
66
67 const_iterator begin() const { return entries().begin(); }
68 const_iterator end() const { return entries().end(); }
69 const_reverse_iterator rbegin() const { return entries().rbegin(); }
70 const_reverse_iterator rend() const { return entries().rend(); }
71
72 const_iterator cbegin() const { return entries().cbegin(); }
73 const_iterator cend() const { return entries().cend(); }
74 const_reverse_iterator crbegin() const { return entries().crbegin(); }
75 const_reverse_iterator crend() const { return entries().crend(); }
76
77 bool empty() const { return entries().empty(); }
78 size_type size() const { return entries().size(); }
79
80 private:
81 class Data : public rtc::RefCountedBase {
82 public:
Minyue Lic759f832019-08-09 13:20:03 +020083 static rtc::scoped_refptr<Data> Create(const vector_type& entries) {
Chen Xing1796a822019-07-24 10:58:52 +020084 // Performance optimization for the empty case.
85 if (entries.empty()) {
86 return nullptr;
87 }
88
Chen Xingd2a66862019-06-03 14:53:42 +020089 return new Data(entries);
90 }
91
Minyue Lic759f832019-08-09 13:20:03 +020092 static rtc::scoped_refptr<Data> Create(vector_type&& entries) {
93 // Performance optimization for the empty case.
94 if (entries.empty()) {
95 return nullptr;
96 }
97
98 return new Data(std::move(entries));
99 }
100
Chen Xingd2a66862019-06-03 14:53:42 +0200101 const vector_type& entries() const { return entries_; }
102
103 private:
Minyue Lic759f832019-08-09 13:20:03 +0200104 explicit Data(const vector_type& entries) : entries_(entries) {}
105 explicit Data(vector_type&& entries) : entries_(std::move(entries)) {}
Chen Xingd2a66862019-06-03 14:53:42 +0200106 ~Data() override {}
107
108 const vector_type entries_;
109 };
110
111 static const vector_type& empty_entries() {
112 static const vector_type& value = *new vector_type();
113 return value;
114 }
115
116 const vector_type& entries() const {
117 if (data_ != nullptr) {
118 return data_->entries();
119 } else {
120 return empty_entries();
121 }
122 }
123
124 rtc::scoped_refptr<Data> data_;
125};
126
127} // namespace webrtc
128
129#endif // API_RTP_PACKET_INFOS_H_