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 | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 15 | #include <map> |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 16 | #include <memory> |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 17 | #include <queue> |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 18 | #include <set> |
| 19 | #include <utility> |
| 20 | #include <vector> |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 21 | |
| 22 | #include "webrtc/base/criticalsection.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 23 | #include "webrtc/base/thread_annotations.h" |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 24 | #include "webrtc/modules/include/module_common_types.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 25 | #include "webrtc/modules/video_coding/packet.h" |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 26 | #include "webrtc/modules/video_coding/sequence_number_util.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | namespace video_coding { |
| 30 | |
| 31 | class FrameObject; |
| 32 | class RtpFrameObject; |
| 33 | |
| 34 | class OnCompleteFrameCallback { |
| 35 | public: |
| 36 | virtual ~OnCompleteFrameCallback() {} |
| 37 | virtual void OnCompleteFrame(std::unique_ptr<FrameObject> frame) = 0; |
| 38 | }; |
| 39 | |
| 40 | class PacketBuffer { |
| 41 | public: |
| 42 | // Both |start_buffer_size| and |max_buffer_size| must be a power of 2. |
| 43 | PacketBuffer(size_t start_buffer_size, |
| 44 | size_t max_buffer_size, |
| 45 | OnCompleteFrameCallback* frame_callback); |
| 46 | |
| 47 | bool InsertPacket(const VCMPacket& packet); |
| 48 | void ClearTo(uint16_t seq_num); |
| 49 | void Flush(); |
| 50 | |
| 51 | private: |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 52 | static const uint16_t kPicIdLength = 1 << 7; |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 53 | static const uint8_t kMaxTemporalLayers = 5; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 54 | static const int kMaxStashedFrames = 10; |
| 55 | static const int kMaxLayerInfo = 10; |
| 56 | static const int kMaxNotYetReceivedFrames = 20; |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 57 | static const int kMaxGofSaved = 15; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 58 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 59 | friend RtpFrameObject; |
| 60 | // Since we want the packet buffer to be as packet type agnostic |
| 61 | // as possible we extract only the information needed in order |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 62 | // to determine whether a sequence of packets is continuous or not. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 63 | struct ContinuityInfo { |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 64 | // The sequence number of the packet. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 65 | uint16_t seq_num = 0; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 66 | |
| 67 | // If this is the first packet of the frame. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 68 | bool frame_begin = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 69 | |
| 70 | // If this is the last packet of the frame. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 71 | bool frame_end = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 72 | |
| 73 | // If this slot is currently used. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 74 | bool used = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 75 | |
| 76 | // If all its previous packets have been inserted into the packet buffer. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 77 | bool continuous = false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 78 | |
| 79 | // If this packet has been used to create a frame already. |
| 80 | bool frame_created = false; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 83 | // Expand the buffer. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 84 | bool ExpandBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 85 | |
| 86 | // Test if all previous packets has arrived for the given sequence number. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 87 | bool IsContinuous(uint16_t seq_num) const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 88 | |
| 89 | // Test if all packets of a frame has arrived, and if so, creates a frame. |
| 90 | // May create multiple frames per invocation. |
| 91 | void FindFrames(uint16_t seq_num) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 92 | |
| 93 | // Copy the bitstream for |frame| to |destination|. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 94 | bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 95 | |
| 96 | // Mark all slots used by |frame| as not used. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 97 | void ReturnFrame(RtpFrameObject* frame); |
| 98 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 99 | // Find the references for this frame. |
| 100 | void ManageFrame(std::unique_ptr<RtpFrameObject> frame) |
| 101 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 102 | |
| 103 | // Retry finding references for all frames that previously didn't have |
| 104 | // all information needed. |
| 105 | void RetryStashedFrames() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 106 | |
| 107 | // Find references for generic frames. |
| 108 | void ManageFrameGeneric(std::unique_ptr<RtpFrameObject> frame) |
| 109 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 110 | |
| 111 | // Find references for Vp8 frames |
| 112 | void ManageFrameVp8(std::unique_ptr<RtpFrameObject> frame) |
| 113 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 114 | |
| 115 | // Updates all necessary state used to determine frame references |
| 116 | // for Vp8 and then calls the |frame_callback| callback with the |
| 117 | // completed frame. |
| 118 | void CompletedFrameVp8(std::unique_ptr<RtpFrameObject> frame) |
| 119 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 120 | |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 121 | // Find references for Vp9 frames |
| 122 | void ManageFrameVp9(std::unique_ptr<RtpFrameObject> frame) |
| 123 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 124 | |
| 125 | // Unwrap the picture id and the frame references and then call the |
| 126 | // |frame_callback| callback with the completed frame. |
| 127 | void CompletedFrameVp9(std::unique_ptr<RtpFrameObject> frame) |
| 128 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 129 | |
| 130 | // Check if we are missing a frame necessary to determine the references |
| 131 | // for this frame. |
| 132 | bool MissingRequiredFrameVp9(uint16_t picture_id, const GofInfoVP9& gof) |
| 133 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 134 | |
| 135 | // Updates which frames that have been received. If there is a gap, |
| 136 | // missing frames will be added to |missing_frames_for_layer_| or |
| 137 | // if this is an already missing frame then it will be removed. |
| 138 | void FrameReceivedVp9(uint16_t picture_id, const GofInfoVP9& gof) |
| 139 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 140 | |
| 141 | // Check if there is a frame with the up-switch flag set in the interval |
| 142 | // (|pid_ref|, |picture_id|) with temporal layer smaller than |temporal_idx|. |
| 143 | bool UpSwitchInIntervalVp9(uint16_t picture_id, |
| 144 | uint8_t temporal_idx, |
| 145 | uint16_t pid_ref) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 146 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 147 | // All picture ids are unwrapped to 16 bits. |
| 148 | uint16_t UnwrapPictureId(uint16_t picture_id) |
| 149 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 150 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 151 | rtc::CriticalSection crit_; |
| 152 | |
| 153 | // Buffer size_ and max_size_ must always be a power of two. |
| 154 | size_t size_ GUARDED_BY(crit_); |
| 155 | const size_t max_size_; |
| 156 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 157 | // The fist sequence number currently in the buffer. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 158 | uint16_t first_seq_num_ GUARDED_BY(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 159 | |
| 160 | // The last sequence number currently in the buffer. |
| 161 | uint16_t last_seq_num_ GUARDED_BY(crit_); |
| 162 | |
| 163 | // If the packet buffer has received its first packet. |
| 164 | bool first_packet_received_ GUARDED_BY(crit_); |
| 165 | |
| 166 | // Buffer that holds the inserted packets. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 167 | std::vector<VCMPacket> data_buffer_ GUARDED_BY(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 168 | |
| 169 | // Buffer that holds the information about which slot that is currently in use |
| 170 | // and information needed to determine the continuity between packets. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 171 | std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_); |
| 172 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 173 | // The callback that is called when a frame has been created and all its |
| 174 | // references has been found. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 175 | OnCompleteFrameCallback* const frame_callback_; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 176 | |
| 177 | // Holds the last sequence number of the last frame that has been created |
| 178 | // given the last sequence number of a given keyframe. |
| 179 | std::map<uint16_t, uint16_t, DescendingSeqNumComp<uint16_t>> |
| 180 | last_seq_num_gop_ GUARDED_BY(crit_); |
| 181 | |
| 182 | // Save the last picture id in order to detect when there is a gap in frames |
| 183 | // that have not yet been fully received. |
| 184 | int last_picture_id_ GUARDED_BY(crit_); |
| 185 | |
| 186 | // The last unwrapped picture id. Used to unwrap the picture id from a length |
| 187 | // of |kPicIdLength| to 16 bits. |
| 188 | int last_unwrap_ GUARDED_BY(crit_); |
| 189 | |
| 190 | // Frames earlier than the last received frame that have not yet been |
| 191 | // fully received. |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 192 | std::set<uint16_t, DescendingSeqNumComp<uint16_t, kPicIdLength>> |
| 193 | not_yet_received_frames_ GUARDED_BY(crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 194 | |
| 195 | // Frames that have been fully received but didn't have all the information |
| 196 | // needed to determine their references. |
| 197 | std::queue<std::unique_ptr<RtpFrameObject>> stashed_frames_ GUARDED_BY(crit_); |
| 198 | |
| 199 | // Holds the information about the last completed frame for a given temporal |
| 200 | // layer given a Tl0 picture index. |
| 201 | std::map<uint8_t, |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 202 | std::array<int16_t, kMaxTemporalLayers>, |
| 203 | DescendingSeqNumComp<uint8_t>> |
| 204 | layer_info_ GUARDED_BY(crit_); |
| 205 | |
| 206 | // Where the current scalability structure is in the |
| 207 | // |scalability_structures_| array. |
| 208 | uint8_t current_ss_idx_; |
| 209 | |
| 210 | // Holds received scalability structures. |
| 211 | std::array<GofInfoVP9, kMaxGofSaved> scalability_structures_ |
| 212 | GUARDED_BY(crit_); |
| 213 | |
| 214 | // Holds the picture id and the Gof information for a given TL0 picture index. |
| 215 | std::map<uint8_t, |
| 216 | std::pair<uint16_t, GofInfoVP9*>, |
| 217 | DescendingSeqNumComp<uint8_t>> |
| 218 | gof_info_ GUARDED_BY(crit_); |
| 219 | |
| 220 | // Keep track of which picture id and which temporal layer that had the |
| 221 | // up switch flag set. |
| 222 | std::map<uint16_t, uint8_t> up_switch_ GUARDED_BY(crit_); |
| 223 | |
| 224 | // For every temporal layer, keep a set of which frames that are missing. |
| 225 | std::array<std::set<uint16_t, DescendingSeqNumComp<uint16_t, kPicIdLength>>, |
| 226 | kMaxTemporalLayers> |
| 227 | missing_frames_for_layer_ GUARDED_BY(crit_); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | } // namespace video_coding |
| 231 | } // namespace webrtc |
| 232 | |
| 233 | #endif // WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_ |