blob: caa81f6b99e99f8400298dbc70558676ddcb2606 [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>
17#include <set>
18#include <queue>
philipelc707ab72016-04-01 02:01:54 -070019
20#include "webrtc/base/criticalsection.h"
terelius52d4e6b2016-04-26 09:31:59 -070021#include "webrtc/base/scoped_ptr.h"
philipelc707ab72016-04-01 02:01:54 -070022#include "webrtc/base/thread_annotations.h"
23#include "webrtc/modules/video_coding/packet.h"
philipelf4139332016-04-20 10:26:34 +020024#include "webrtc/modules/video_coding/sequence_number_util.h"
philipelc707ab72016-04-01 02:01:54 -070025
26namespace webrtc {
27namespace video_coding {
28
29class FrameObject;
30class RtpFrameObject;
31
32class OnCompleteFrameCallback {
33 public:
34 virtual ~OnCompleteFrameCallback() {}
35 virtual void OnCompleteFrame(std::unique_ptr<FrameObject> frame) = 0;
36};
37
38class PacketBuffer {
39 public:
40 // Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
41 PacketBuffer(size_t start_buffer_size,
42 size_t max_buffer_size,
43 OnCompleteFrameCallback* frame_callback);
44
45 bool InsertPacket(const VCMPacket& packet);
46 void ClearTo(uint16_t seq_num);
47 void Flush();
48
49 private:
philipelf4139332016-04-20 10:26:34 +020050 static const uint16_t kPicIdLength = 1 << 7;
51 static const uint8_t kMaxTemporalLayer = 5;
52 static const int kMaxStashedFrames = 10;
53 static const int kMaxLayerInfo = 10;
54 static const int kMaxNotYetReceivedFrames = 20;
55
philipelc707ab72016-04-01 02:01:54 -070056 friend RtpFrameObject;
57 // Since we want the packet buffer to be as packet type agnostic
58 // as possible we extract only the information needed in order
philipelf4139332016-04-20 10:26:34 +020059 // to determine whether a sequence of packets is continuous or not.
philipelc707ab72016-04-01 02:01:54 -070060 struct ContinuityInfo {
philipelf4139332016-04-20 10:26:34 +020061 // The sequence number of the packet.
philipelc707ab72016-04-01 02:01:54 -070062 uint16_t seq_num = 0;
philipelf4139332016-04-20 10:26:34 +020063
64 // If this is the first packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070065 bool frame_begin = false;
philipelf4139332016-04-20 10:26:34 +020066
67 // If this is the last packet of the frame.
philipelc707ab72016-04-01 02:01:54 -070068 bool frame_end = false;
philipelf4139332016-04-20 10:26:34 +020069
70 // If this slot is currently used.
philipelc707ab72016-04-01 02:01:54 -070071 bool used = false;
philipelf4139332016-04-20 10:26:34 +020072
73 // If all its previous packets have been inserted into the packet buffer.
philipelc707ab72016-04-01 02:01:54 -070074 bool continuous = false;
philipelf4139332016-04-20 10:26:34 +020075
76 // If this packet has been used to create a frame already.
77 bool frame_created = false;
philipelc707ab72016-04-01 02:01:54 -070078 };
79
philipelf4139332016-04-20 10:26:34 +020080 // Expand the buffer.
philipelc707ab72016-04-01 02:01:54 -070081 bool ExpandBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020082
83 // Test if all previous packets has arrived for the given sequence number.
philipelc707ab72016-04-01 02:01:54 -070084 bool IsContinuous(uint16_t seq_num) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelf4139332016-04-20 10:26:34 +020085
86 // Test if all packets of a frame has arrived, and if so, creates a frame.
87 // May create multiple frames per invocation.
88 void FindFrames(uint16_t seq_num) EXCLUSIVE_LOCKS_REQUIRED(crit_);
89
90 // Copy the bitstream for |frame| to |destination|.
philipelc707ab72016-04-01 02:01:54 -070091 bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination);
philipelf4139332016-04-20 10:26:34 +020092
93 // Mark all slots used by |frame| as not used.
philipelc707ab72016-04-01 02:01:54 -070094 void ReturnFrame(RtpFrameObject* frame);
95
philipelf4139332016-04-20 10:26:34 +020096 // Find the references for this frame.
97 void ManageFrame(std::unique_ptr<RtpFrameObject> frame)
98 EXCLUSIVE_LOCKS_REQUIRED(crit_);
99
100 // Retry finding references for all frames that previously didn't have
101 // all information needed.
102 void RetryStashedFrames() EXCLUSIVE_LOCKS_REQUIRED(crit_);
103
104 // Find references for generic frames.
105 void ManageFrameGeneric(std::unique_ptr<RtpFrameObject> frame)
106 EXCLUSIVE_LOCKS_REQUIRED(crit_);
107
108 // Find references for Vp8 frames
109 void ManageFrameVp8(std::unique_ptr<RtpFrameObject> frame)
110 EXCLUSIVE_LOCKS_REQUIRED(crit_);
111
112 // Updates all necessary state used to determine frame references
113 // for Vp8 and then calls the |frame_callback| callback with the
114 // completed frame.
115 void CompletedFrameVp8(std::unique_ptr<RtpFrameObject> frame)
116 EXCLUSIVE_LOCKS_REQUIRED(crit_);
117
118 // All picture ids are unwrapped to 16 bits.
119 uint16_t UnwrapPictureId(uint16_t picture_id)
120 EXCLUSIVE_LOCKS_REQUIRED(crit_);
121
philipelc707ab72016-04-01 02:01:54 -0700122 rtc::CriticalSection crit_;
123
124 // Buffer size_ and max_size_ must always be a power of two.
125 size_t size_ GUARDED_BY(crit_);
126 const size_t max_size_;
127
philipelf4139332016-04-20 10:26:34 +0200128 // The fist sequence number currently in the buffer.
philipelc707ab72016-04-01 02:01:54 -0700129 uint16_t first_seq_num_ GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200130
131 // The last sequence number currently in the buffer.
132 uint16_t last_seq_num_ GUARDED_BY(crit_);
133
134 // If the packet buffer has received its first packet.
135 bool first_packet_received_ GUARDED_BY(crit_);
136
137 // Buffer that holds the inserted packets.
philipelc707ab72016-04-01 02:01:54 -0700138 std::vector<VCMPacket> data_buffer_ GUARDED_BY(crit_);
philipelf4139332016-04-20 10:26:34 +0200139
140 // Buffer that holds the information about which slot that is currently in use
141 // and information needed to determine the continuity between packets.
philipelc707ab72016-04-01 02:01:54 -0700142 std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_);
143
philipelf4139332016-04-20 10:26:34 +0200144 // The callback that is called when a frame has been created and all its
145 // references has been found.
philipelc707ab72016-04-01 02:01:54 -0700146 OnCompleteFrameCallback* const frame_callback_;
philipelf4139332016-04-20 10:26:34 +0200147
148 // Holds the last sequence number of the last frame that has been created
149 // given the last sequence number of a given keyframe.
150 std::map<uint16_t, uint16_t, DescendingSeqNumComp<uint16_t>>
151 last_seq_num_gop_ GUARDED_BY(crit_);
152
153 // Save the last picture id in order to detect when there is a gap in frames
154 // that have not yet been fully received.
155 int last_picture_id_ GUARDED_BY(crit_);
156
157 // The last unwrapped picture id. Used to unwrap the picture id from a length
158 // of |kPicIdLength| to 16 bits.
159 int last_unwrap_ GUARDED_BY(crit_);
160
161 // Frames earlier than the last received frame that have not yet been
162 // fully received.
163 std::set<uint8_t, DescendingSeqNumComp<uint8_t, kPicIdLength>>
164 not_yet_received_frames_ GUARDED_BY(crit_);
165
166 // Frames that have been fully received but didn't have all the information
167 // needed to determine their references.
168 std::queue<std::unique_ptr<RtpFrameObject>> stashed_frames_ GUARDED_BY(crit_);
169
170 // Holds the information about the last completed frame for a given temporal
171 // layer given a Tl0 picture index.
172 std::map<uint8_t,
173 std::array<int16_t, kMaxTemporalLayer>,
174 DescendingSeqNumComp<uint8_t>> layer_info_ GUARDED_BY(crit_);
philipelc707ab72016-04-01 02:01:54 -0700175};
176
177} // namespace video_coding
178} // namespace webrtc
179
180#endif // WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_