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: |
40 static const uint16_t kPicIdLength = 1 << 7; | |
41 | |
35 // Both |start_buffer_size| and |max_buffer_size| must be a power of 2. | 42 // Both |start_buffer_size| and |max_buffer_size| must be a power of 2. |
36 PacketBuffer(size_t start_buffer_size, | 43 PacketBuffer(size_t start_buffer_size, |
37 size_t max_buffer_size, | 44 size_t max_buffer_size, |
38 OnCompleteFrameCallback* frame_callback); | 45 OnCompleteFrameCallback* frame_callback); |
39 | 46 |
40 bool InsertPacket(const VCMPacket& packet); | 47 bool InsertPacket(const VCMPacket& packet); |
41 void ClearTo(uint16_t seq_num); | 48 void ClearTo(uint16_t seq_num); |
42 void Flush(); | 49 void Flush(); |
43 | 50 |
44 private: | 51 private: |
52 static const uint8_t kMaxTemporalLayer = 5; | |
pbos-webrtc
2016/04/01 14:24:45
I believe this can give linker errors since kMaxTe
philipel
2016/04/05 12:40:50
Acknowledged.
| |
53 | |
45 friend RtpFrameObject; | 54 friend RtpFrameObject; |
46 // Since we want the packet buffer to be as packet type agnostic | 55 // Since we want the packet buffer to be as packet type agnostic |
47 // as possible we extract only the information needed in order | 56 // as possible we extract only the information needed in order |
48 // to determin whether a sequence of packets is continuous or not. | 57 // to determin whether a sequence of packets is continuous or not. |
pbos-webrtc
2016/04/01 14:24:44
determine
philipel
2016/04/05 12:40:50
Done.
| |
49 struct ContinuityInfo { | 58 struct ContinuityInfo { |
59 // The sequence number of the packet/ | |
pbos-webrtc
2016/04/01 14:24:44
Replace / with .
philipel
2016/04/05 12:40:50
Done.
| |
50 uint16_t seq_num = 0; | 60 uint16_t seq_num = 0; |
61 | |
62 // If this is the first packet of the frame. | |
pbos-webrtc
2016/04/01 14:24:45
rename first_packet_of_frame maybe?
philipel
2016/04/05 12:40:50
Seems a bit to long, and I think |frame_begin| acc
| |
51 bool frame_begin = false; | 63 bool frame_begin = false; |
64 | |
65 // If this is the last packet of the frame. | |
pbos-webrtc
2016/04/01 14:24:45
last_packet_of_frame
philipel
2016/04/05 12:40:49
Ditto
| |
52 bool frame_end = false; | 66 bool frame_end = false; |
67 | |
68 // If this slot is currently used. | |
53 bool used = false; | 69 bool used = false; |
70 | |
71 // If all its previous packets have been inserted into the packet buffer. | |
54 bool continuous = false; | 72 bool continuous = false; |
73 | |
74 // If this packet has been used to create a frame already. | |
75 bool frame_created = false; | |
pbos-webrtc
2016/04/01 14:24:45
should this be in_use?
| |
55 }; | 76 }; |
56 | 77 |
78 // Expand the buffer. | |
57 bool ExpandBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_); | 79 bool ExpandBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
80 | |
81 // Test if all previous packets has arrived for the given sequence number. | |
58 bool IsContinuous(uint16_t seq_num) const EXCLUSIVE_LOCKS_REQUIRED(crit_); | 82 bool IsContinuous(uint16_t seq_num) const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
59 void FindCompleteFrames(uint16_t seq_num) EXCLUSIVE_LOCKS_REQUIRED(crit_); | 83 |
84 // Test if all packets of a frame has arrived, and if so, creates a frame. | |
85 // May create multiple frames per invocation. | |
86 void FindFrames(uint16_t seq_num) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
87 | |
88 // Copy the bitstream for |frame| to |destination|. | |
60 bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination); | 89 bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination); |
90 | |
91 // Mark all slots used by |frame| as not used. | |
pbos-webrtc
2016/04/01 14:24:44
s/used/previously used/
| |
61 void ReturnFrame(RtpFrameObject* frame); | 92 void ReturnFrame(RtpFrameObject* frame); |
62 | 93 |
94 // Find the references for this frame. | |
95 void FindReferences(std::unique_ptr<RtpFrameObject> frame) | |
96 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
97 | |
98 // Retry to find the references for all frames that previously didn't have | |
pbos-webrtc
2016/04/01 14:24:45
Retry finding references
philipel
2016/04/05 12:40:50
Done.
| |
99 // all information needed. | |
100 void RetryStashedFrames() EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
101 | |
102 // Find references for generic frames. | |
103 void FindReferencesGeneric(std::unique_ptr<RtpFrameObject> frame) | |
pbos-webrtc
2016/04/01 14:24:45
All of these FindReferences sound like they'd be c
philipel
2016/04/05 12:40:49
Renamed them to ManageFrameXXX.
| |
104 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
105 | |
106 // Find references for Vp8 frames | |
107 void FindReferencesVp8(std::unique_ptr<RtpFrameObject> frame) | |
108 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
109 | |
110 // Updates all necessary state used to determin frame references | |
pbos-webrtc
2016/04/01 14:24:45
determine
philipel
2016/04/05 12:40:50
Done.
| |
111 // for Vp8 and then calls the |frame_callback| callback with the | |
112 // completed frame. | |
113 void CompletedFrameVp8(std::unique_ptr<RtpFrameObject> frame) | |
114 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
115 | |
116 // All picture ids are unwrapped to 16 bits. | |
117 uint16_t UnwrapPictureId(uint16_t picture_id) | |
118 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
119 | |
63 rtc::CriticalSection crit_; | 120 rtc::CriticalSection crit_; |
64 | 121 |
65 // Buffer size_ and max_size_ must always be a power of two. | 122 // Buffer size_ and max_size_ must always be a power of two. |
66 size_t size_ GUARDED_BY(crit_); | 123 size_t size_ GUARDED_BY(crit_); |
67 const size_t max_size_; | 124 const size_t max_size_; |
68 | 125 |
126 // The fist sequence number currently in the buffer. | |
127 uint16_t first_seq_num_ GUARDED_BY(crit_); | |
128 | |
129 // The last sequence number currently in the buffer. | |
69 uint16_t last_seq_num_ GUARDED_BY(crit_); | 130 uint16_t last_seq_num_ GUARDED_BY(crit_); |
70 uint16_t first_seq_num_ GUARDED_BY(crit_); | 131 |
71 bool initialized_ GUARDED_BY(crit_); | 132 // Buffer that holds the inserted packets. |
72 std::vector<VCMPacket> data_buffer_ GUARDED_BY(crit_); | 133 std::vector<VCMPacket> data_buffer_ GUARDED_BY(crit_); |
134 | |
135 // Buffer that holds the information about which slot that is currently in use | |
136 // and information needed to determin the continuity between packets. | |
73 std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_); | 137 std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_); |
74 | 138 |
139 // The callback that is called when a frame has been created and all its | |
140 // references has been found. | |
75 OnCompleteFrameCallback* const frame_callback_; | 141 OnCompleteFrameCallback* const frame_callback_; |
142 | |
143 // Holds the last sequence number of the last frame that has been created | |
144 // given the last sequence number of a given keyframe. | |
145 std::map<uint16_t, uint16_t, DescendingSeqNumComp<uint16_t>> | |
146 last_seq_num_for_kf_ GUARDED_BY(crit_); | |
147 | |
148 // Save the last picture id in order to detect when there is a gap in frames | |
149 // that has not yet been fully received. | |
150 int last_picture_id_ GUARDED_BY(crit_); | |
151 | |
152 // The last unwrapped picture id. Used to unwrap the picture id from a length | |
153 // of |kPicIdLength| to 16 bits. | |
154 int last_unwrap_ GUARDED_BY(crit_); | |
155 | |
156 // Frames earlier than the last received frame that has not yet been | |
157 // fully received. | |
158 std::set<uint8_t, DescendingSeqNumComp<uint8_t>> | |
159 not_yet_received_frames_ GUARDED_BY(crit_); | |
160 | |
161 // Frames that has been fully received but didn't have all the information | |
162 // needed to determin its references. | |
163 std::queue<std::unique_ptr<RtpFrameObject>> stashed_frames_ GUARDED_BY(crit_); | |
164 | |
165 // Holds the information about the last completed frame for a given temporal | |
166 // layer given a Tl0 picture index. | |
167 std::map<uint8_t, | |
168 std::array<int16_t, kMaxTemporalLayer>, | |
pbos-webrtc
2016/04/01 14:24:45
pref vector if this is not high-performance code (
philipel
2016/04/05 12:40:50
I think map is the correct datastructure to use he
| |
169 DescendingSeqNumComp<uint8_t>> layer_info_ GUARDED_BY(crit_); | |
170 | |
171 bool initialized_ GUARDED_BY(crit_); | |
pbos-webrtc
2016/04/01 14:24:45
Pref putting this closer to the sequence numbers w
philipel
2016/04/05 12:40:49
Done.
| |
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 |