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