philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_ |
| 13 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 14 | #include <array> |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 15 | #include <vector> |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 16 | #include <map> |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 17 | #include <memory> |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 18 | #include <set> |
| 19 | #include <queue> |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 20 | |
| 21 | #include "webrtc/base/criticalsection.h" |
terelius | 52d4e6b | 2016-04-26 09:31:59 -0700 | [diff] [blame] | 22 | #include "webrtc/base/scoped_ptr.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 23 | #include "webrtc/base/thread_annotations.h" |
| 24 | #include "webrtc/modules/video_coding/packet.h" |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 25 | #include "webrtc/modules/video_coding/sequence_number_util.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace video_coding { |
| 29 | |
| 30 | class FrameObject; |
| 31 | class RtpFrameObject; |
| 32 | |
| 33 | class OnCompleteFrameCallback { |
| 34 | public: |
| 35 | virtual ~OnCompleteFrameCallback() {} |
| 36 | virtual void OnCompleteFrame(std::unique_ptr<FrameObject> frame) = 0; |
| 37 | }; |
| 38 | |
| 39 | class PacketBuffer { |
| 40 | public: |
| 41 | // Both |start_buffer_size| and |max_buffer_size| must be a power of 2. |
| 42 | PacketBuffer(size_t start_buffer_size, |
| 43 | size_t max_buffer_size, |
| 44 | OnCompleteFrameCallback* frame_callback); |
| 45 | |
| 46 | bool InsertPacket(const VCMPacket& packet); |
| 47 | void ClearTo(uint16_t seq_num); |
| 48 | void Flush(); |
| 49 | |
| 50 | private: |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 51 | static const uint16_t kPicIdLength = 1 << 7; |
| 52 | static const uint8_t kMaxTemporalLayer = 5; |
| 53 | static const int kMaxStashedFrames = 10; |
| 54 | static const int kMaxLayerInfo = 10; |
| 55 | static const int kMaxNotYetReceivedFrames = 20; |
| 56 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 57 | friend RtpFrameObject; |
| 58 | // Since we want the packet buffer to be as packet type agnostic |
| 59 | // as possible we extract only the information needed in order |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 60 | // to determine whether a sequence of packets is continuous or not. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 61 | struct ContinuityInfo { |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 62 | // The sequence number of the packet. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 63 | uint16_t seq_num = 0; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 64 | |
| 65 | // If this is the first packet of the frame. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 66 | bool frame_begin = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 67 | |
| 68 | // If this is the last packet of the frame. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 69 | bool frame_end = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 70 | |
| 71 | // If this slot is currently used. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 72 | bool used = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 73 | |
| 74 | // If all its previous packets have been inserted into the packet buffer. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 75 | bool continuous = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 76 | |
| 77 | // If this packet has been used to create a frame already. |
| 78 | bool frame_created = false; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 81 | // Expand the buffer. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 82 | bool ExpandBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 83 | |
| 84 | // Test if all previous packets has arrived for the given sequence number. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 85 | bool IsContinuous(uint16_t seq_num) const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 86 | |
| 87 | // Test if all packets of a frame has arrived, and if so, creates a frame. |
| 88 | // May create multiple frames per invocation. |
| 89 | void FindFrames(uint16_t seq_num) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 90 | |
| 91 | // Copy the bitstream for |frame| to |destination|. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 92 | bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 93 | |
| 94 | // Mark all slots used by |frame| as not used. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 95 | void ReturnFrame(RtpFrameObject* frame); |
| 96 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 97 | // Find the references for this frame. |
| 98 | void ManageFrame(std::unique_ptr<RtpFrameObject> frame) |
| 99 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 100 | |
| 101 | // Retry finding references for all frames that previously didn't have |
| 102 | // all information needed. |
| 103 | void RetryStashedFrames() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 104 | |
| 105 | // Find references for generic frames. |
| 106 | void ManageFrameGeneric(std::unique_ptr<RtpFrameObject> frame) |
| 107 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 108 | |
| 109 | // Find references for Vp8 frames |
| 110 | void ManageFrameVp8(std::unique_ptr<RtpFrameObject> frame) |
| 111 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 112 | |
| 113 | // Updates all necessary state used to determine frame references |
| 114 | // for Vp8 and then calls the |frame_callback| callback with the |
| 115 | // completed frame. |
| 116 | void CompletedFrameVp8(std::unique_ptr<RtpFrameObject> frame) |
| 117 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 118 | |
| 119 | // All picture ids are unwrapped to 16 bits. |
| 120 | uint16_t UnwrapPictureId(uint16_t picture_id) |
| 121 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 122 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 123 | rtc::CriticalSection crit_; |
| 124 | |
| 125 | // Buffer size_ and max_size_ must always be a power of two. |
| 126 | size_t size_ GUARDED_BY(crit_); |
| 127 | const size_t max_size_; |
| 128 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 129 | // The fist sequence number currently in the buffer. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 130 | uint16_t first_seq_num_ GUARDED_BY(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 131 | |
| 132 | // The last sequence number currently in the buffer. |
| 133 | uint16_t last_seq_num_ GUARDED_BY(crit_); |
| 134 | |
| 135 | // If the packet buffer has received its first packet. |
| 136 | bool first_packet_received_ GUARDED_BY(crit_); |
| 137 | |
| 138 | // Buffer that holds the inserted packets. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 139 | std::vector<VCMPacket> data_buffer_ GUARDED_BY(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 140 | |
| 141 | // Buffer that holds the information about which slot that is currently in use |
| 142 | // and information needed to determine the continuity between packets. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 143 | std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_); |
| 144 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 145 | // The callback that is called when a frame has been created and all its |
| 146 | // references has been found. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 147 | OnCompleteFrameCallback* const frame_callback_; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 148 | |
| 149 | // Holds the last sequence number of the last frame that has been created |
| 150 | // given the last sequence number of a given keyframe. |
| 151 | std::map<uint16_t, uint16_t, DescendingSeqNumComp<uint16_t>> |
| 152 | last_seq_num_gop_ GUARDED_BY(crit_); |
| 153 | |
| 154 | // Save the last picture id in order to detect when there is a gap in frames |
| 155 | // that have not yet been fully received. |
| 156 | int last_picture_id_ GUARDED_BY(crit_); |
| 157 | |
| 158 | // The last unwrapped picture id. Used to unwrap the picture id from a length |
| 159 | // of |kPicIdLength| to 16 bits. |
| 160 | int last_unwrap_ GUARDED_BY(crit_); |
| 161 | |
| 162 | // Frames earlier than the last received frame that have not yet been |
| 163 | // fully received. |
| 164 | std::set<uint8_t, DescendingSeqNumComp<uint8_t, kPicIdLength>> |
| 165 | not_yet_received_frames_ GUARDED_BY(crit_); |
| 166 | |
| 167 | // Frames that have been fully received but didn't have all the information |
| 168 | // needed to determine their references. |
| 169 | std::queue<std::unique_ptr<RtpFrameObject>> stashed_frames_ GUARDED_BY(crit_); |
| 170 | |
| 171 | // Holds the information about the last completed frame for a given temporal |
| 172 | // layer given a Tl0 picture index. |
| 173 | std::map<uint8_t, |
| 174 | std::array<int16_t, kMaxTemporalLayer>, |
| 175 | DescendingSeqNumComp<uint8_t>> layer_info_ GUARDED_BY(crit_); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | } // namespace video_coding |
| 179 | } // namespace webrtc |
| 180 | |
| 181 | #endif // WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_ |