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