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