1 /*
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 #include "modules/video_coding/packet_buffer.h"
12
13 #include <string.h>
14
15 #include <algorithm>
16 #include <cstdint>
17 #include <limits>
18 #include <utility>
19 #include <vector>
20
21 #include "absl/types/variant.h"
22 #include "api/array_view.h"
23 #include "api/rtp_packet_info.h"
24 #include "api/video/video_frame_type.h"
25 #include "common_video/h264/h264_common.h"
26 #include "modules/rtp_rtcp/source/rtp_header_extensions.h"
27 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
28 #include "modules/rtp_rtcp/source/rtp_video_header.h"
29 #include "modules/video_coding/codecs/h264/include/h264_globals.h"
30 #include "rtc_base/checks.h"
31 #include "rtc_base/logging.h"
32 #include "rtc_base/numerics/mod_ops.h"
33
34 namespace webrtc {
35 namespace video_coding {
36
Packet(const RtpPacketReceived & rtp_packet,const RTPVideoHeader & video_header)37 PacketBuffer::Packet::Packet(const RtpPacketReceived& rtp_packet,
38 const RTPVideoHeader& video_header)
39 : marker_bit(rtp_packet.Marker()),
40 payload_type(rtp_packet.PayloadType()),
41 seq_num(rtp_packet.SequenceNumber()),
42 timestamp(rtp_packet.Timestamp()),
43 times_nacked(-1),
44 video_header(video_header) {}
45
PacketBuffer(size_t start_buffer_size,size_t max_buffer_size)46 PacketBuffer::PacketBuffer(size_t start_buffer_size, size_t max_buffer_size)
47 : max_size_(max_buffer_size),
48 first_seq_num_(0),
49 first_packet_received_(false),
50 is_cleared_to_first_seq_num_(false),
51 buffer_(start_buffer_size),
52 sps_pps_idr_is_h264_keyframe_(false) {
53 RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
54 // Buffer size must always be a power of 2.
55 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
56 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
57 }
58
~PacketBuffer()59 PacketBuffer::~PacketBuffer() {
60 Clear();
61 }
62
InsertPacket(std::unique_ptr<PacketBuffer::Packet> packet)63 PacketBuffer::InsertResult PacketBuffer::InsertPacket(
64 std::unique_ptr<PacketBuffer::Packet> packet) {
65 PacketBuffer::InsertResult result;
66
67 uint16_t seq_num = packet->seq_num;
68 size_t index = seq_num % buffer_.size();
69
70 if (!first_packet_received_) {
71 first_seq_num_ = seq_num;
72 first_packet_received_ = true;
73 } else if (AheadOf(first_seq_num_, seq_num)) {
74 // If we have explicitly cleared past this packet then it's old,
75 // don't insert it, just silently ignore it.
76 if (is_cleared_to_first_seq_num_) {
77 return result;
78 }
79
80 first_seq_num_ = seq_num;
81 }
82
83 if (buffer_[index] != nullptr) {
84 // Duplicate packet, just delete the payload.
85 if (buffer_[index]->seq_num == packet->seq_num) {
86 return result;
87 }
88
89 // The packet buffer is full, try to expand the buffer.
90 while (ExpandBufferSize() && buffer_[seq_num % buffer_.size()] != nullptr) {
91 }
92 index = seq_num % buffer_.size();
93
94 // Packet buffer is still full since we were unable to expand the buffer.
95 if (buffer_[index] != nullptr) {
96 // Clear the buffer, delete payload, and return false to signal that a
97 // new keyframe is needed.
98 RTC_LOG(LS_WARNING) << "Clear PacketBuffer and request key frame.";
99 ClearInternal();
100 result.buffer_cleared = true;
101 return result;
102 }
103 }
104
105 packet->continuous = false;
106 buffer_[index] = std::move(packet);
107
108 UpdateMissingPackets(seq_num);
109
110 received_padding_.erase(
111 received_padding_.begin(),
112 received_padding_.lower_bound(seq_num - (buffer_.size() / 4)));
113
114 result.packets = FindFrames(seq_num);
115 return result;
116 }
117
ClearTo(uint16_t seq_num)118 void PacketBuffer::ClearTo(uint16_t seq_num) {
119 // We have already cleared past this sequence number, no need to do anything.
120 if (is_cleared_to_first_seq_num_ &&
121 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
122 return;
123 }
124
125 // If the packet buffer was cleared between a frame was created and returned.
126 if (!first_packet_received_)
127 return;
128
129 // Avoid iterating over the buffer more than once by capping the number of
130 // iterations to the `size_` of the buffer.
131 ++seq_num;
132 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
133 size_t iterations = std::min(diff, buffer_.size());
134 for (size_t i = 0; i < iterations; ++i) {
135 auto& stored = buffer_[first_seq_num_ % buffer_.size()];
136 if (stored != nullptr && AheadOf<uint16_t>(seq_num, stored->seq_num)) {
137 stored = nullptr;
138 }
139 ++first_seq_num_;
140 }
141
142 // If `diff` is larger than `iterations` it means that we don't increment
143 // `first_seq_num_` until we reach `seq_num`, so we set it here.
144 first_seq_num_ = seq_num;
145
146 is_cleared_to_first_seq_num_ = true;
147 missing_packets_.erase(missing_packets_.begin(),
148 missing_packets_.lower_bound(seq_num));
149
150 received_padding_.erase(received_padding_.begin(),
151 received_padding_.lower_bound(seq_num));
152 }
153
Clear()154 void PacketBuffer::Clear() {
155 ClearInternal();
156 }
157
InsertPadding(uint16_t seq_num)158 PacketBuffer::InsertResult PacketBuffer::InsertPadding(uint16_t seq_num) {
159 PacketBuffer::InsertResult result;
160 UpdateMissingPackets(seq_num);
161 received_padding_.insert(seq_num);
162 result.packets = FindFrames(static_cast<uint16_t>(seq_num + 1));
163 return result;
164 }
165
ForceSpsPpsIdrIsH264Keyframe()166 void PacketBuffer::ForceSpsPpsIdrIsH264Keyframe() {
167 sps_pps_idr_is_h264_keyframe_ = true;
168 }
169
ResetSpsPpsIdrIsH264Keyframe()170 void PacketBuffer::ResetSpsPpsIdrIsH264Keyframe() {
171 sps_pps_idr_is_h264_keyframe_ = false;
172 }
173
ClearInternal()174 void PacketBuffer::ClearInternal() {
175 for (auto& entry : buffer_) {
176 entry = nullptr;
177 }
178
179 first_packet_received_ = false;
180 is_cleared_to_first_seq_num_ = false;
181 newest_inserted_seq_num_.reset();
182 missing_packets_.clear();
183 received_padding_.clear();
184 }
185
ExpandBufferSize()186 bool PacketBuffer::ExpandBufferSize() {
187 if (buffer_.size() == max_size_) {
188 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
189 << "), failed to increase size.";
190 return false;
191 }
192
193 size_t new_size = std::min(max_size_, 2 * buffer_.size());
194 std::vector<std::unique_ptr<Packet>> new_buffer(new_size);
195 for (std::unique_ptr<Packet>& entry : buffer_) {
196 if (entry != nullptr) {
197 new_buffer[entry->seq_num % new_size] = std::move(entry);
198 }
199 }
200 buffer_ = std::move(new_buffer);
201 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
202 return true;
203 }
204
PotentialNewFrame(uint16_t seq_num) const205 bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
206 size_t index = seq_num % buffer_.size();
207 int prev_index = index > 0 ? index - 1 : buffer_.size() - 1;
208 const auto& entry = buffer_[index];
209 const auto& prev_entry = buffer_[prev_index];
210
211 if (entry == nullptr)
212 return false;
213 if (entry->seq_num != seq_num)
214 return false;
215 if (entry->is_first_packet_in_frame())
216 return true;
217 if (prev_entry == nullptr)
218 return false;
219 if (prev_entry->seq_num != static_cast<uint16_t>(entry->seq_num - 1))
220 return false;
221 if (prev_entry->timestamp != entry->timestamp)
222 return false;
223 if (prev_entry->continuous)
224 return true;
225
226 return false;
227 }
228
FindFrames(uint16_t seq_num)229 std::vector<std::unique_ptr<PacketBuffer::Packet>> PacketBuffer::FindFrames(
230 uint16_t seq_num) {
231 std::vector<std::unique_ptr<PacketBuffer::Packet>> found_frames;
232 auto start = seq_num;
233
234 for (size_t i = 0; i < buffer_.size(); ++i) {
235 if (received_padding_.find(seq_num) != received_padding_.end()) {
236 seq_num += 1;
237 continue;
238 }
239
240 if (!PotentialNewFrame(seq_num)) {
241 break;
242 }
243
244 size_t index = seq_num % buffer_.size();
245 buffer_[index]->continuous = true;
246
247 // If all packets of the frame is continuous, find the first packet of the
248 // frame and add all packets of the frame to the returned packets.
249 if (buffer_[index]->is_last_packet_in_frame()) {
250 uint16_t start_seq_num = seq_num;
251
252 // Find the start index by searching backward until the packet with
253 // the `frame_begin` flag is set.
254 int start_index = index;
255 size_t tested_packets = 0;
256 int64_t frame_timestamp = buffer_[start_index]->timestamp;
257
258 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
259 bool is_h264 = buffer_[start_index]->codec() == kVideoCodecH264;
260 bool has_h264_sps = false;
261 bool has_h264_pps = false;
262 bool has_h264_idr = false;
263 bool is_h264_keyframe = false;
264 int idr_width = -1;
265 int idr_height = -1;
266 bool full_frame_found = false;
267 while (true) {
268 ++tested_packets;
269
270 if (!is_h264) {
271 if (buffer_[start_index] == nullptr ||
272 buffer_[start_index]->is_first_packet_in_frame()) {
273 full_frame_found = buffer_[start_index] != nullptr;
274 break;
275 }
276 }
277
278 if (is_h264) {
279 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
280 &buffer_[start_index]->video_header.video_type_header);
281 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
282 return found_frames;
283
284 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
285 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
286 has_h264_sps = true;
287 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
288 has_h264_pps = true;
289 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
290 has_h264_idr = true;
291 }
292 }
293 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
294 has_h264_pps) ||
295 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
296 is_h264_keyframe = true;
297 // Store the resolution of key frame which is the packet with
298 // smallest index and valid resolution; typically its IDR or SPS
299 // packet; there may be packet preceeding this packet, IDR's
300 // resolution will be applied to them.
301 if (buffer_[start_index]->width() > 0 &&
302 buffer_[start_index]->height() > 0) {
303 idr_width = buffer_[start_index]->width();
304 idr_height = buffer_[start_index]->height();
305 }
306 }
307 }
308
309 if (tested_packets == buffer_.size())
310 break;
311
312 start_index = start_index > 0 ? start_index - 1 : buffer_.size() - 1;
313
314 // In the case of H264 we don't have a frame_begin bit (yes,
315 // `frame_begin` might be set to true but that is a lie). So instead
316 // we traverese backwards as long as we have a previous packet and
317 // the timestamp of that packet is the same as this one. This may cause
318 // the PacketBuffer to hand out incomplete frames.
319 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
320 if (is_h264 && (buffer_[start_index] == nullptr ||
321 buffer_[start_index]->timestamp != frame_timestamp)) {
322 break;
323 }
324
325 --start_seq_num;
326 }
327
328 if (is_h264) {
329 // Warn if this is an unsafe frame.
330 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
331 RTC_LOG(LS_WARNING)
332 << "Received H.264-IDR frame "
333 "(SPS: "
334 << has_h264_sps << ", PPS: " << has_h264_pps << "). Treating as "
335 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
336 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
337 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
338 }
339
340 // Now that we have decided whether to treat this frame as a key frame
341 // or delta frame in the frame buffer, we update the field that
342 // determines if the RtpFrameObject is a key frame or delta frame.
343 const size_t first_packet_index = start_seq_num % buffer_.size();
344 if (is_h264_keyframe) {
345 buffer_[first_packet_index]->video_header.frame_type =
346 VideoFrameType::kVideoFrameKey;
347 if (idr_width > 0 && idr_height > 0) {
348 // IDR frame was finalized and we have the correct resolution for
349 // IDR; update first packet to have same resolution as IDR.
350 buffer_[first_packet_index]->video_header.width = idr_width;
351 buffer_[first_packet_index]->video_header.height = idr_height;
352 }
353 } else {
354 buffer_[first_packet_index]->video_header.frame_type =
355 VideoFrameType::kVideoFrameDelta;
356 }
357
358 // If this is not a keyframe, make sure there are no gaps in the packet
359 // sequence numbers up until this point.
360 if (!is_h264_keyframe && missing_packets_.upper_bound(start_seq_num) !=
361 missing_packets_.begin()) {
362 return found_frames;
363 }
364 }
365
366 if (is_h264 || full_frame_found) {
367 const uint16_t end_seq_num = seq_num + 1;
368 // Use uint16_t type to handle sequence number wrap around case.
369 uint16_t num_packets = end_seq_num - start_seq_num;
370 found_frames.reserve(found_frames.size() + num_packets);
371 for (uint16_t i = start_seq_num; i != end_seq_num; ++i) {
372 std::unique_ptr<Packet>& packet = buffer_[i % buffer_.size()];
373 RTC_DCHECK(packet);
374 RTC_DCHECK_EQ(i, packet->seq_num);
375 // Ensure frame boundary flags are properly set.
376 packet->video_header.is_first_packet_in_frame = (i == start_seq_num);
377 packet->video_header.is_last_packet_in_frame = (i == seq_num);
378 found_frames.push_back(std::move(packet));
379 }
380
381 missing_packets_.erase(missing_packets_.begin(),
382 missing_packets_.upper_bound(seq_num));
383 received_padding_.erase(received_padding_.lower_bound(start),
384 received_padding_.upper_bound(seq_num));
385 }
386 }
387 ++seq_num;
388 }
389 return found_frames;
390 }
391
UpdateMissingPackets(uint16_t seq_num)392 void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
393 if (!newest_inserted_seq_num_)
394 newest_inserted_seq_num_ = seq_num;
395
396 const int kMaxPaddingAge = 1000;
397 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
398 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
399 auto erase_to = missing_packets_.lower_bound(old_seq_num);
400 missing_packets_.erase(missing_packets_.begin(), erase_to);
401
402 // Guard against inserting a large amount of missing packets if there is a
403 // jump in the sequence number.
404 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
405 *newest_inserted_seq_num_ = old_seq_num;
406
407 ++*newest_inserted_seq_num_;
408 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
409 missing_packets_.insert(*newest_inserted_seq_num_);
410 ++*newest_inserted_seq_num_;
411 }
412 } else {
413 missing_packets_.erase(seq_num);
414 }
415 }
416
417 } // namespace video_coding
418 } // namespace webrtc
419