blob: 75049b33ba83b189c7df30d6ad58335a266c5ead [file] [log] [blame]
philipelc707ab72016-04-01 02:01:54 -07001/*
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
philipelf4139332016-04-20 10:26:34 +020014#include <array>
philipelc707ab72016-04-01 02:01:54 -070015#include <vector>
philipelf4139332016-04-20 10:26:34 +020016#include <map>
kwiberg84be5112016-04-27 01:19:58 -070017#include <memory>
philipelf4139332016-04-20 10:26:34 +020018#include <set>
19#include <queue>
philipelc707ab72016-04-01 02:01:54 -070020
21#include "webrtc/base/criticalsection.h"
terelius52d4e6b2016-04-26 09:31:59 -070022#include "webrtc/base/scoped_ptr.h"
philipelc707ab72016-04-01 02:01:54 -070023#include "webrtc/base/thread_annotations.h"
24#include "webrtc/modules/video_coding/packet.h"
philipelf4139332016-04-20 10:26:34 +020025#include "webrtc/modules/video_coding/sequence_number_util.h"
philipelc707ab72016-04-01 02:01:54 -070026
27namespace webrtc {
28namespace video_coding {
29
30class FrameObject;
31class RtpFrameObject;
32
33class OnCompleteFrameCallback {
34 public:
35 virtual ~OnCompleteFrameCallback() {}
36 virtual void OnCompleteFrame(std::unique_ptr<FrameObject> frame) = 0;
37};
38
39class 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:
philipelf4139332016-04-20 10:26:34 +020051 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
philipelc707ab72016-04-01 02:01:54 -070057 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
philipelf4139332016-04-20 10:26:34 +020060 // to determine whether a sequence of packets is continuous or not.
philipelc707ab72016-04-01 02:01:54 -070061 struct ContinuityInfo {
philipelf4139332016-04-20 10:26:34 +020062 // The sequence number of the packet.
philipelc707ab72016-04-01 02:01:54 -070063 uint16_t seq_num = 0;
philipelf4139332016-04-20 10:26:34 +020064
65 // If this is the first packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070066 bool frame_begin = false;
philipelf4139332016-04-20 10:26:34 +020067
68 // If this is the last packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070069 bool frame_end = false;
philipelf4139332016-04-20 10:26:34 +020070
71 // If this slot is currently used.
philipelc707ab72016-04-01 02:01:54 -070072 bool used = false;
philipelf4139332016-04-20 10:26:34 +020073
74 // If all its previous packets have been inserted into the packet buffer.
philipelc707ab72016-04-01 02:01:54 -070075 bool continuous = false;
philipelf4139332016-04-20 10:26:34 +020076
77 // If this packet has been used to create a frame already.
78 bool frame_created = false;
philipelc707ab72016-04-01 02:01:54 -070079 };
80
philipelf4139332016-04-20 10:26:34 +020081 // Expand the buffer.
philipelc707ab72016-04-01 02:01:54 -070082 bool ExpandBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020083
84 // Test if all previous packets has arrived for the given sequence number.
philipelc707ab72016-04-01 02:01:54 -070085 bool IsContinuous(uint16_t seq_num) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020086
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|.
philipelc707ab72016-04-01 02:01:54 -070092 bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination);
philipelf4139332016-04-20 10:26:34 +020093
94 // Mark all slots used by |frame| as not used.
philipelc707ab72016-04-01 02:01:54 -070095 void ReturnFrame(RtpFrameObject* frame);
96
philipelf4139332016-04-20 10:26:34 +020097 // 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
philipelc707ab72016-04-01 02:01:54 -0700123 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
philipelf4139332016-04-20 10:26:34 +0200129 // The fist sequence number currently in the buffer.
philipelc707ab72016-04-01 02:01:54 -0700130 uint16_t first_seq_num_ GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200131
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.
philipelc707ab72016-04-01 02:01:54 -0700139 std::vector<VCMPacket> data_buffer_ GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200140
141 // Buffer that holds the information about which slot that is currently in use
142 // and information needed to determine the continuity between packets.
philipelc707ab72016-04-01 02:01:54 -0700143 std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_);
144
philipelf4139332016-04-20 10:26:34 +0200145 // The callback that is called when a frame has been created and all its
146 // references has been found.
philipelc707ab72016-04-01 02:01:54 -0700147 OnCompleteFrameCallback* const frame_callback_;
philipelf4139332016-04-20 10:26:34 +0200148
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_);
philipelc707ab72016-04-01 02:01:54 -0700176};
177
178} // namespace video_coding
179} // namespace webrtc
180
181#endif // WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_