blob: 98307842fd01824b7d6ec8dd0f3f9a56136e3e45 [file] [log] [blame]
philipelc707ab72016-04-01 02:01:54 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/packet_buffer.h"
philipelc707ab72016-04-01 02:01:54 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
philipelc707ab72016-04-01 02:01:54 -070015#include <algorithm>
Yves Gerey3e707812018-11-28 16:47:49 +010016#include <cstdint>
Danil Chapovalov3527a4f2019-11-08 17:30:29 +010017#include <limits>
philipel17deeb42016-08-11 15:09:26 +020018#include <utility>
Danil Chapovalov3527a4f2019-11-08 17:30:29 +010019#include <vector>
philipelc707ab72016-04-01 02:01:54 -070020
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "absl/types/variant.h"
Danil Chapovalov3527a4f2019-11-08 17:30:29 +010022#include "api/array_view.h"
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010023#include "api/rtp_packet_info.h"
Danil Chapovalov0682ca92019-11-28 16:50:02 +010024#include "api/video/video_frame_type.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "common_video/h264/h264_common.h"
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010026#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
27#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "modules/rtp_rtcp/source/rtp_video_header.h"
29#include "modules/video_coding/codecs/h264/include/h264_globals.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/checks.h"
31#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010032#include "rtc_base/numerics/mod_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "system_wrappers/include/clock.h"
Rasmus Brandt88f080a2017-11-02 14:28:06 +010034#include "system_wrappers/include/field_trial.h"
philipelc707ab72016-04-01 02:01:54 -070035
36namespace webrtc {
37namespace video_coding {
38
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010039PacketBuffer::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
philipelb4d31082016-07-11 08:46:29 -070057PacketBuffer::PacketBuffer(Clock* clock,
58 size_t start_buffer_size,
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020059 size_t max_buffer_size)
philipelb4d31082016-07-11 08:46:29 -070060 : clock_(clock),
philipelc707ab72016-04-01 02:01:54 -070061 max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070062 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020063 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010064 is_cleared_to_first_seq_num_(false),
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020065 buffer_(start_buffer_size),
Rasmus Brandt88f080a2017-11-02 14:28:06 +010066 sps_pps_idr_is_h264_keyframe_(
67 field_trial::IsEnabled("WebRTC-SpsPpsIdrIsH264Keyframe")) {
philipelc707ab72016-04-01 02:01:54 -070068 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
philipelaee3e0e2016-11-01 11:45:34 +010074PacketBuffer::~PacketBuffer() {
75 Clear();
76}
philipel17deeb42016-08-11 15:09:26 +020077
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010078PacketBuffer::InsertResult PacketBuffer::InsertPacket(
Danil Chapovalov97ffbef2020-01-24 16:04:35 +010079 std::unique_ptr<PacketBuffer::Packet> packet) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020080 PacketBuffer::InsertResult result;
81 rtc::CritScope lock(&crit_);
philipel3184f8e2017-05-18 08:08:53 -070082
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010083 uint16_t seq_num = packet->seq_num;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020084 size_t index = seq_num % buffer_.size();
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010085
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020086 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_) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020093 return result;
philipelc707ab72016-04-01 02:01:54 -070094 }
philipelc707ab72016-04-01 02:01:54 -070095
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020096 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070097 }
98
Danil Chapovalov97ffbef2020-01-24 16:04:35 +010099 if (buffer_[index].used()) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200100 // Duplicate packet, just delete the payload.
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100101 if (buffer_[index].seq_num() == packet->seq_num) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200102 return result;
103 }
philipelc707ab72016-04-01 02:01:54 -0700104
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200105 // The packet buffer is full, try to expand the buffer.
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100106 while (ExpandBufferSize() && buffer_[seq_num % buffer_.size()].used()) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200107 }
108 index = seq_num % buffer_.size();
109
110 // Packet buffer is still full since we were unable to expand the buffer.
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100111 if (buffer_[index].used()) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200112 // 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 Clear();
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200116 result.buffer_cleared = true;
117 return result;
118 }
119 }
120
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200121 int64_t now_ms = clock_->TimeInMilliseconds();
122 last_received_packet_ms_ = now_ms;
Danil Chapovalovc9e532a2019-12-10 17:03:00 +0100123 if (packet->video_header.frame_type == VideoFrameType::kVideoFrameKey ||
124 last_received_keyframe_rtp_timestamp_ == packet->timestamp) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200125 last_received_keyframe_packet_ms_ = now_ms;
Danil Chapovalovc9e532a2019-12-10 17:03:00 +0100126 last_received_keyframe_rtp_timestamp_ = packet->timestamp;
127 }
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200128
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100129 StoredPacket& new_entry = buffer_[index];
130 new_entry.continuous = false;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100131 new_entry.packet = std::move(packet);
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100132
133 UpdateMissingPackets(seq_num);
134
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100135 result.packets = FindFrames(seq_num);
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200136 return result;
philipelc707ab72016-04-01 02:01:54 -0700137}
138
139void PacketBuffer::ClearTo(uint16_t seq_num) {
140 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700141 // We have already cleared past this sequence number, no need to do anything.
142 if (is_cleared_to_first_seq_num_ &&
143 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
144 return;
145 }
philipelaee3e0e2016-11-01 11:45:34 +0100146
147 // If the packet buffer was cleared between a frame was created and returned.
148 if (!first_packet_received_)
149 return;
150
philipelc5fb4682017-08-02 04:28:57 -0700151 // Avoid iterating over the buffer more than once by capping the number of
152 // iterations to the |size_| of the buffer.
153 ++seq_num;
154 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200155 size_t iterations = std::min(diff, buffer_.size());
philipelc5fb4682017-08-02 04:28:57 -0700156 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100157 StoredPacket& stored = buffer_[first_seq_num_ % buffer_.size()];
158 if (stored.used() && AheadOf<uint16_t>(seq_num, stored.seq_num())) {
159 stored.packet = nullptr;
philipelc5fb4682017-08-02 04:28:57 -0700160 }
philipelaee3e0e2016-11-01 11:45:34 +0100161 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700162 }
philipel2c9f9f22017-06-13 02:47:28 -0700163
philipelc5fb4682017-08-02 04:28:57 -0700164 // If |diff| is larger than |iterations| it means that we don't increment
165 // |first_seq_num_| until we reach |seq_num|, so we set it here.
166 first_seq_num_ = seq_num;
167
168 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100169 auto clear_to_it = missing_packets_.upper_bound(seq_num);
170 if (clear_to_it != missing_packets_.begin()) {
171 --clear_to_it;
172 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
173 }
philipelc707ab72016-04-01 02:01:54 -0700174}
175
philipelaee3e0e2016-11-01 11:45:34 +0100176void PacketBuffer::Clear() {
177 rtc::CritScope lock(&crit_);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200178 for (StoredPacket& entry : buffer_) {
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100179 entry.packet = nullptr;
philipelaee3e0e2016-11-01 11:45:34 +0100180 }
181
182 first_packet_received_ = false;
183 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700184 last_received_packet_ms_.reset();
185 last_received_keyframe_packet_ms_.reset();
186 newest_inserted_seq_num_.reset();
187 missing_packets_.clear();
188}
189
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200190PacketBuffer::InsertResult PacketBuffer::InsertPadding(uint16_t seq_num) {
191 PacketBuffer::InsertResult result;
192 rtc::CritScope lock(&crit_);
193 UpdateMissingPackets(seq_num);
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100194 result.packets = FindFrames(static_cast<uint16_t>(seq_num + 1));
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200195 return result;
philipel3184f8e2017-05-18 08:08:53 -0700196}
197
Danil Chapovalov0040b662018-06-18 10:48:16 +0200198absl::optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700199 rtc::CritScope lock(&crit_);
200 return last_received_packet_ms_;
201}
202
Danil Chapovalov0040b662018-06-18 10:48:16 +0200203absl::optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700204 rtc::CritScope lock(&crit_);
205 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100206}
207
philipelc707ab72016-04-01 02:01:54 -0700208bool PacketBuffer::ExpandBufferSize() {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200209 if (buffer_.size() == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100210 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
Johannes Kronbd3f3052019-08-01 15:45:54 +0200211 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700212 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100213 }
philipelc707ab72016-04-01 02:01:54 -0700214
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200215 size_t new_size = std::min(max_size_, 2 * buffer_.size());
216 std::vector<StoredPacket> new_buffer(new_size);
217 for (StoredPacket& entry : buffer_) {
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100218 if (entry.used()) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100219 new_buffer[entry.seq_num() % new_size] = std::move(entry);
philipelc707ab72016-04-01 02:01:54 -0700220 }
221 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200222 buffer_ = std::move(new_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100223 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700224 return true;
225}
226
philipelaee3e0e2016-11-01 11:45:34 +0100227bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200228 size_t index = seq_num % buffer_.size();
229 int prev_index = index > 0 ? index - 1 : buffer_.size() - 1;
230 const StoredPacket& entry = buffer_[index];
231 const StoredPacket& prev_entry = buffer_[prev_index];
philipelf4139332016-04-20 10:26:34 +0200232
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100233 if (!entry.used())
philipelc707ab72016-04-01 02:01:54 -0700234 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200235 if (entry.seq_num() != seq_num)
philipel2c9f9f22017-06-13 02:47:28 -0700236 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200237 if (entry.frame_begin())
philipelc707ab72016-04-01 02:01:54 -0700238 return true;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100239 if (!prev_entry.used())
philipelc707ab72016-04-01 02:01:54 -0700240 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200241 if (prev_entry.seq_num() != static_cast<uint16_t>(entry.seq_num() - 1))
philipelea142f82017-01-11 02:01:56 -0800242 return false;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100243 if (prev_entry.packet->timestamp != entry.packet->timestamp)
philipelf4139332016-04-20 10:26:34 +0200244 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200245 if (prev_entry.continuous)
philipelc707ab72016-04-01 02:01:54 -0700246 return true;
247
248 return false;
249}
250
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100251std::vector<std::unique_ptr<PacketBuffer::Packet>> PacketBuffer::FindFrames(
philipelfd5a20f2016-11-15 00:57:57 -0800252 uint16_t seq_num) {
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100253 std::vector<std::unique_ptr<PacketBuffer::Packet>> found_frames;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200254 for (size_t i = 0; i < buffer_.size() && PotentialNewFrame(seq_num); ++i) {
255 size_t index = seq_num % buffer_.size();
256 buffer_[index].continuous = true;
philipelc707ab72016-04-01 02:01:54 -0700257
philipelf4139332016-04-20 10:26:34 +0200258 // If all packets of the frame is continuous, find the first packet of the
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100259 // frame and add all packets of the frame to the returned packets.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200260 if (buffer_[index].frame_end()) {
philipelc707ab72016-04-01 02:01:54 -0700261 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200262
philipel5ceaaae2016-05-24 10:20:47 +0200263 // Find the start index by searching backward until the packet with
264 // the |frame_begin| flag is set.
265 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700266 size_t tested_packets = 0;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100267 int64_t frame_timestamp = buffer_[start_index].packet->timestamp;
philipel53910712017-05-18 02:24:40 -0700268
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100269 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100270 bool is_h264 = buffer_[start_index].packet->codec() == kVideoCodecH264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100271 bool has_h264_sps = false;
272 bool has_h264_pps = false;
273 bool has_h264_idr = false;
274 bool is_h264_keyframe = false;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700275 int idr_width = -1;
276 int idr_height = -1;
philipel227f8b92017-08-04 06:39:31 -0700277 while (true) {
278 ++tested_packets;
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100279
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200280 if (!is_h264 && buffer_[start_index].frame_begin())
philipel5ceaaae2016-05-24 10:20:47 +0200281 break;
282
Shyam Sadhwani2b84dad2019-10-02 17:22:33 -0700283 if (is_h264) {
philipel7d745e52018-08-02 14:03:53 +0200284 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100285 &buffer_[start_index].packet->video_header.video_type_header);
philipel7d745e52018-08-02 14:03:53 +0200286 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200287 return found_frames;
288
philipel7d745e52018-08-02 14:03:53 +0200289 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
290 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100291 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200292 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100293 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200294 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100295 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700296 }
297 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100298 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
299 has_h264_pps) ||
300 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
301 is_h264_keyframe = true;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700302 // Store the resolution of key frame which is the packet with
303 // smallest index and valid resolution; typically its IDR or SPS
304 // packet; there may be packet preceeding this packet, IDR's
305 // resolution will be applied to them.
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100306 if (buffer_[start_index].packet->width() > 0 &&
307 buffer_[start_index].packet->height() > 0) {
308 idr_width = buffer_[start_index].packet->width();
309 idr_height = buffer_[start_index].packet->height();
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700310 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100311 }
philipel2c9f9f22017-06-13 02:47:28 -0700312 }
313
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200314 if (tested_packets == buffer_.size())
philipel227f8b92017-08-04 06:39:31 -0700315 break;
316
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200317 start_index = start_index > 0 ? start_index - 1 : buffer_.size() - 1;
philipel8c619242017-02-02 08:51:29 -0800318
319 // In the case of H264 we don't have a frame_begin bit (yes,
320 // |frame_begin| might be set to true but that is a lie). So instead
321 // we traverese backwards as long as we have a previous packet and
322 // the timestamp of that packet is the same as this one. This may cause
323 // the PacketBuffer to hand out incomplete frames.
324 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700325 if (is_h264 &&
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100326 (!buffer_[start_index].used() ||
327 buffer_[start_index].packet->timestamp != frame_timestamp)) {
philipel8c619242017-02-02 08:51:29 -0800328 break;
329 }
330
331 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700332 }
333
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100334 if (is_h264) {
335 // Warn if this is an unsafe frame.
336 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200337 RTC_LOG(LS_WARNING)
338 << "Received H.264-IDR frame "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100339 "(SPS: "
340 << has_h264_sps << ", PPS: " << has_h264_pps << "). Treating as "
Jonas Olssonfc501102018-06-15 14:24:10 +0200341 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
342 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
343 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700344 }
345
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100346 // Now that we have decided whether to treat this frame as a key frame
347 // or delta frame in the frame buffer, we update the field that
348 // determines if the RtpFrameObject is a key frame or delta frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200349 const size_t first_packet_index = start_seq_num % buffer_.size();
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100350 if (is_h264_keyframe) {
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100351 buffer_[first_packet_index].packet->video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100352 VideoFrameType::kVideoFrameKey;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700353 if (idr_width > 0 && idr_height > 0) {
354 // IDR frame was finalized and we have the correct resolution for
355 // IDR; update first packet to have same resolution as IDR.
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100356 buffer_[first_packet_index].packet->video_header.width = idr_width;
357 buffer_[first_packet_index].packet->video_header.height =
358 idr_height;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700359 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100360 } else {
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100361 buffer_[first_packet_index].packet->video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100362 VideoFrameType::kVideoFrameDelta;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100363 }
364
Johnny Leebc7f41b2019-05-01 14:41:32 -0400365 // With IPPP, if this is not a keyframe, make sure there are no gaps
366 // in the packet sequence numbers up until this point.
367 const uint8_t h264tid =
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100368 buffer_[start_index].used()
369 ? buffer_[start_index]
370 .packet->video_header.frame_marking.temporal_id
371 : kNoTemporalIdx;
Jonas Olssona4d87372019-07-05 19:08:33 +0200372 if (h264tid == kNoTemporalIdx && !is_h264_keyframe &&
373 missing_packets_.upper_bound(start_seq_num) !=
374 missing_packets_.begin()) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100375 return found_frames;
376 }
philipel2c9f9f22017-06-13 02:47:28 -0700377 }
378
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100379 const uint16_t end_seq_num = seq_num + 1;
380 // Use uint16_t type to handle sequence number wrap around case.
381 uint16_t num_packets = end_seq_num - start_seq_num;
382 found_frames.reserve(found_frames.size() + num_packets);
383 for (uint16_t i = start_seq_num; i != end_seq_num; ++i) {
384 StoredPacket& entry = buffer_[i % buffer_.size()];
385 RTC_DCHECK(entry.used());
386 RTC_DCHECK_EQ(i, entry.seq_num());
387 // Ensure frame boundary flags are properly set.
388 entry.packet->video_header.is_first_packet_in_frame =
389 (i == start_seq_num);
390 entry.packet->video_header.is_last_packet_in_frame = (i == seq_num);
391 found_frames.push_back(std::move(entry.packet));
Danil Chapovalov0682ca92019-11-28 16:50:02 +0100392 }
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100393
philipel2c9f9f22017-06-13 02:47:28 -0700394 missing_packets_.erase(missing_packets_.begin(),
395 missing_packets_.upper_bound(seq_num));
philipelc707ab72016-04-01 02:01:54 -0700396 }
philipelc707ab72016-04-01 02:01:54 -0700397 ++seq_num;
398 }
philipelfd5a20f2016-11-15 00:57:57 -0800399 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700400}
401
philipel2c9f9f22017-06-13 02:47:28 -0700402void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
403 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100404 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700405
406 const int kMaxPaddingAge = 1000;
407 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
408 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
409 auto erase_to = missing_packets_.lower_bound(old_seq_num);
410 missing_packets_.erase(missing_packets_.begin(), erase_to);
411
412 // Guard against inserting a large amount of missing packets if there is a
413 // jump in the sequence number.
414 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
415 *newest_inserted_seq_num_ = old_seq_num;
416
417 ++*newest_inserted_seq_num_;
418 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
419 missing_packets_.insert(*newest_inserted_seq_num_);
420 ++*newest_inserted_seq_num_;
421 }
422 } else {
423 missing_packets_.erase(seq_num);
424 }
425}
426
philipelc707ab72016-04-01 02:01:54 -0700427} // namespace video_coding
428} // namespace webrtc