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 static const int kMaxStashedFrames = 10; |
| 53 static const int kMaxLayerInfo = 10; |
| 54 static const int kMaxNotYetReceivedFrames = 20; |
| 55 |
45 friend RtpFrameObject; | 56 friend RtpFrameObject; |
46 // Since we want the packet buffer to be as packet type agnostic | 57 // Since we want the packet buffer to be as packet type agnostic |
47 // as possible we extract only the information needed in order | 58 // as possible we extract only the information needed in order |
48 // to determin whether a sequence of packets is continuous or not. | 59 // to determine whether a sequence of packets is continuous or not. |
49 struct ContinuityInfo { | 60 struct ContinuityInfo { |
| 61 // The sequence number of the packet. |
50 uint16_t seq_num = 0; | 62 uint16_t seq_num = 0; |
| 63 |
| 64 // If this is the first packet of the frame. |
51 bool frame_begin = false; | 65 bool frame_begin = false; |
| 66 |
| 67 // If this is the last packet of the frame. |
52 bool frame_end = false; | 68 bool frame_end = false; |
| 69 |
| 70 // If this slot is currently used. |
53 bool used = false; | 71 bool used = false; |
| 72 |
| 73 // If all its previous packets have been inserted into the packet buffer. |
54 bool continuous = false; | 74 bool continuous = false; |
| 75 |
| 76 // If this packet has been used to create a frame already. |
| 77 bool frame_created = false; |
55 }; | 78 }; |
56 | 79 |
| 80 // Expand the buffer. |
57 bool ExpandBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_); | 81 bool ExpandBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 82 |
| 83 // Test if all previous packets has arrived for the given sequence number. |
58 bool IsContinuous(uint16_t seq_num) const EXCLUSIVE_LOCKS_REQUIRED(crit_); | 84 bool IsContinuous(uint16_t seq_num) const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
59 void FindCompleteFrames(uint16_t seq_num) EXCLUSIVE_LOCKS_REQUIRED(crit_); | 85 |
| 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|. |
60 bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination); | 91 bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination); |
| 92 |
| 93 // Mark all slots used by |frame| as not used. |
61 void ReturnFrame(RtpFrameObject* frame); | 94 void ReturnFrame(RtpFrameObject* frame); |
62 | 95 |
| 96 // 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 |
63 rtc::CriticalSection crit_; | 122 rtc::CriticalSection crit_; |
64 | 123 |
65 // Buffer size_ and max_size_ must always be a power of two. | 124 // Buffer size_ and max_size_ must always be a power of two. |
66 size_t size_ GUARDED_BY(crit_); | 125 size_t size_ GUARDED_BY(crit_); |
67 const size_t max_size_; | 126 const size_t max_size_; |
68 | 127 |
| 128 // The fist sequence number currently in the buffer. |
| 129 uint16_t first_seq_num_ GUARDED_BY(crit_); |
| 130 |
| 131 // The last sequence number currently in the buffer. |
69 uint16_t last_seq_num_ GUARDED_BY(crit_); | 132 uint16_t last_seq_num_ GUARDED_BY(crit_); |
70 uint16_t first_seq_num_ GUARDED_BY(crit_); | 133 |
71 bool initialized_ GUARDED_BY(crit_); | 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. |
72 std::vector<VCMPacket> data_buffer_ GUARDED_BY(crit_); | 138 std::vector<VCMPacket> data_buffer_ GUARDED_BY(crit_); |
| 139 |
| 140 // Buffer that holds the information about which slot that is currently in use |
| 141 // and information needed to determine the continuity between packets. |
73 std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_); | 142 std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_); |
74 | 143 |
| 144 // The callback that is called when a frame has been created and all its |
| 145 // references has been found. |
75 OnCompleteFrameCallback* const frame_callback_; | 146 OnCompleteFrameCallback* const frame_callback_; |
| 147 |
| 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_); |
76 }; | 175 }; |
77 | 176 |
78 } // namespace video_coding | 177 } // namespace video_coding |
79 } // namespace webrtc | 178 } // namespace webrtc |
80 | 179 |
81 #endif // WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_ | 180 #endif // WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_ |
OLD | NEW |