blob: adc18ddce0f3e25d87b41b4f20a5117332fe2d81 [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"
philipelc707ab72016-04-01 02:01:54 -070033
34namespace webrtc {
35namespace video_coding {
36
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010037PacketBuffer::Packet::Packet(const RtpPacketReceived& rtp_packet,
philipel9599b3c2021-05-11 11:30:52 +020038 const RTPVideoHeader& video_header)
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010039 : marker_bit(rtp_packet.Marker()),
40 payload_type(rtp_packet.PayloadType()),
41 seq_num(rtp_packet.SequenceNumber()),
42 timestamp(rtp_packet.Timestamp()),
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010043 times_nacked(-1),
philipel9599b3c2021-05-11 11:30:52 +020044 video_header(video_header) {}
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010045
philipelce423ce2021-04-12 13:42:03 +020046PacketBuffer::PacketBuffer(size_t start_buffer_size, size_t max_buffer_size)
47 : max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070048 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020049 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010050 is_cleared_to_first_seq_num_(false),
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020051 buffer_(start_buffer_size),
Eldar Rello2127aaa2020-08-07 12:08:14 +030052 sps_pps_idr_is_h264_keyframe_(false) {
philipelc707ab72016-04-01 02:01:54 -070053 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
philipelaee3e0e2016-11-01 11:45:34 +010059PacketBuffer::~PacketBuffer() {
60 Clear();
61}
philipel17deeb42016-08-11 15:09:26 +020062
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010063PacketBuffer::InsertResult PacketBuffer::InsertPacket(
Danil Chapovalov97ffbef2020-01-24 16:04:35 +010064 std::unique_ptr<PacketBuffer::Packet> packet) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020065 PacketBuffer::InsertResult result;
philipel3184f8e2017-05-18 08:08:53 -070066
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010067 uint16_t seq_num = packet->seq_num;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020068 size_t index = seq_num % buffer_.size();
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010069
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020070 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_) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020077 return result;
philipelc707ab72016-04-01 02:01:54 -070078 }
philipelc707ab72016-04-01 02:01:54 -070079
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020080 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070081 }
82
Danil Chapovalovf4306eb2020-03-20 12:30:31 +010083 if (buffer_[index] != nullptr) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020084 // Duplicate packet, just delete the payload.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +010085 if (buffer_[index]->seq_num == packet->seq_num) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020086 return result;
87 }
philipelc707ab72016-04-01 02:01:54 -070088
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020089 // The packet buffer is full, try to expand the buffer.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +010090 while (ExpandBufferSize() && buffer_[seq_num % buffer_.size()] != nullptr) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020091 }
92 index = seq_num % buffer_.size();
93
94 // Packet buffer is still full since we were unable to expand the buffer.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +010095 if (buffer_[index] != nullptr) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020096 // 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.";
Markus Handell3eac1112020-05-27 17:08:41 +020099 ClearInternal();
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200100 result.buffer_cleared = true;
101 return result;
102 }
103 }
104
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100105 packet->continuous = false;
106 buffer_[index] = std::move(packet);
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100107
108 UpdateMissingPackets(seq_num);
109
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100110 result.packets = FindFrames(seq_num);
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200111 return result;
philipelc707ab72016-04-01 02:01:54 -0700112}
113
114void PacketBuffer::ClearTo(uint16_t seq_num) {
philipelc5fb4682017-08-02 04:28:57 -0700115 // We have already cleared past this sequence number, no need to do anything.
116 if (is_cleared_to_first_seq_num_ &&
117 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
118 return;
119 }
philipelaee3e0e2016-11-01 11:45:34 +0100120
121 // If the packet buffer was cleared between a frame was created and returned.
122 if (!first_packet_received_)
123 return;
124
philipelc5fb4682017-08-02 04:28:57 -0700125 // Avoid iterating over the buffer more than once by capping the number of
Artem Titovdcd7fc72021-08-09 13:02:57 +0200126 // iterations to the `size_` of the buffer.
philipelc5fb4682017-08-02 04:28:57 -0700127 ++seq_num;
128 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200129 size_t iterations = std::min(diff, buffer_.size());
philipelc5fb4682017-08-02 04:28:57 -0700130 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100131 auto& stored = buffer_[first_seq_num_ % buffer_.size()];
132 if (stored != nullptr && AheadOf<uint16_t>(seq_num, stored->seq_num)) {
133 stored = nullptr;
philipelc5fb4682017-08-02 04:28:57 -0700134 }
philipelaee3e0e2016-11-01 11:45:34 +0100135 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700136 }
philipel2c9f9f22017-06-13 02:47:28 -0700137
Artem Titovdcd7fc72021-08-09 13:02:57 +0200138 // If `diff` is larger than `iterations` it means that we don't increment
139 // `first_seq_num_` until we reach `seq_num`, so we set it here.
philipelc5fb4682017-08-02 04:28:57 -0700140 first_seq_num_ = seq_num;
141
142 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100143 auto clear_to_it = missing_packets_.upper_bound(seq_num);
144 if (clear_to_it != missing_packets_.begin()) {
145 --clear_to_it;
146 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
147 }
philipelc707ab72016-04-01 02:01:54 -0700148}
149
philipelaee3e0e2016-11-01 11:45:34 +0100150void PacketBuffer::Clear() {
Markus Handell3eac1112020-05-27 17:08:41 +0200151 ClearInternal();
philipel2c9f9f22017-06-13 02:47:28 -0700152}
153
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200154PacketBuffer::InsertResult PacketBuffer::InsertPadding(uint16_t seq_num) {
155 PacketBuffer::InsertResult result;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200156 UpdateMissingPackets(seq_num);
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100157 result.packets = FindFrames(static_cast<uint16_t>(seq_num + 1));
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200158 return result;
philipel3184f8e2017-05-18 08:08:53 -0700159}
160
Eldar Rello2127aaa2020-08-07 12:08:14 +0300161void PacketBuffer::ForceSpsPpsIdrIsH264Keyframe() {
162 sps_pps_idr_is_h264_keyframe_ = true;
163}
philipelce423ce2021-04-12 13:42:03 +0200164
Markus Handell3eac1112020-05-27 17:08:41 +0200165void PacketBuffer::ClearInternal() {
166 for (auto& entry : buffer_) {
167 entry = nullptr;
168 }
169
170 first_packet_received_ = false;
171 is_cleared_to_first_seq_num_ = false;
Markus Handell3eac1112020-05-27 17:08:41 +0200172 newest_inserted_seq_num_.reset();
173 missing_packets_.clear();
174}
175
philipelc707ab72016-04-01 02:01:54 -0700176bool PacketBuffer::ExpandBufferSize() {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200177 if (buffer_.size() == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100178 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
Johannes Kronbd3f3052019-08-01 15:45:54 +0200179 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700180 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100181 }
philipelc707ab72016-04-01 02:01:54 -0700182
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200183 size_t new_size = std::min(max_size_, 2 * buffer_.size());
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100184 std::vector<std::unique_ptr<Packet>> new_buffer(new_size);
185 for (std::unique_ptr<Packet>& entry : buffer_) {
186 if (entry != nullptr) {
187 new_buffer[entry->seq_num % new_size] = std::move(entry);
philipelc707ab72016-04-01 02:01:54 -0700188 }
189 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200190 buffer_ = std::move(new_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100191 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700192 return true;
193}
194
philipelaee3e0e2016-11-01 11:45:34 +0100195bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200196 size_t index = seq_num % buffer_.size();
197 int prev_index = index > 0 ? index - 1 : buffer_.size() - 1;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100198 const auto& entry = buffer_[index];
199 const auto& prev_entry = buffer_[prev_index];
philipelf4139332016-04-20 10:26:34 +0200200
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100201 if (entry == nullptr)
philipelc707ab72016-04-01 02:01:54 -0700202 return false;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100203 if (entry->seq_num != seq_num)
philipel2c9f9f22017-06-13 02:47:28 -0700204 return false;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100205 if (entry->is_first_packet_in_frame())
philipelc707ab72016-04-01 02:01:54 -0700206 return true;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100207 if (prev_entry == nullptr)
philipelc707ab72016-04-01 02:01:54 -0700208 return false;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100209 if (prev_entry->seq_num != static_cast<uint16_t>(entry->seq_num - 1))
philipelea142f82017-01-11 02:01:56 -0800210 return false;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100211 if (prev_entry->timestamp != entry->timestamp)
philipelf4139332016-04-20 10:26:34 +0200212 return false;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100213 if (prev_entry->continuous)
philipelc707ab72016-04-01 02:01:54 -0700214 return true;
215
216 return false;
217}
218
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100219std::vector<std::unique_ptr<PacketBuffer::Packet>> PacketBuffer::FindFrames(
philipelfd5a20f2016-11-15 00:57:57 -0800220 uint16_t seq_num) {
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100221 std::vector<std::unique_ptr<PacketBuffer::Packet>> found_frames;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200222 for (size_t i = 0; i < buffer_.size() && PotentialNewFrame(seq_num); ++i) {
223 size_t index = seq_num % buffer_.size();
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100224 buffer_[index]->continuous = true;
philipelc707ab72016-04-01 02:01:54 -0700225
philipelf4139332016-04-20 10:26:34 +0200226 // If all packets of the frame is continuous, find the first packet of the
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100227 // frame and add all packets of the frame to the returned packets.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100228 if (buffer_[index]->is_last_packet_in_frame()) {
philipelc707ab72016-04-01 02:01:54 -0700229 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200230
philipel5ceaaae2016-05-24 10:20:47 +0200231 // Find the start index by searching backward until the packet with
Artem Titovdcd7fc72021-08-09 13:02:57 +0200232 // the `frame_begin` flag is set.
philipel5ceaaae2016-05-24 10:20:47 +0200233 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700234 size_t tested_packets = 0;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100235 int64_t frame_timestamp = buffer_[start_index]->timestamp;
philipel53910712017-05-18 02:24:40 -0700236
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100237 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100238 bool is_h264 = buffer_[start_index]->codec() == kVideoCodecH264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100239 bool has_h264_sps = false;
240 bool has_h264_pps = false;
241 bool has_h264_idr = false;
242 bool is_h264_keyframe = false;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700243 int idr_width = -1;
244 int idr_height = -1;
philipel90623e12022-04-28 14:31:55 +0200245 bool full_frame_found = false;
philipel227f8b92017-08-04 06:39:31 -0700246 while (true) {
247 ++tested_packets;
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100248
philipel90623e12022-04-28 14:31:55 +0200249 if (!is_h264) {
250 if (buffer_[start_index] == nullptr ||
251 buffer_[start_index]->is_first_packet_in_frame()) {
252 full_frame_found = buffer_[start_index] != nullptr;
253 break;
254 }
255 }
philipel5ceaaae2016-05-24 10:20:47 +0200256
Shyam Sadhwani2b84dad2019-10-02 17:22:33 -0700257 if (is_h264) {
philipel7d745e52018-08-02 14:03:53 +0200258 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100259 &buffer_[start_index]->video_header.video_type_header);
philipel7d745e52018-08-02 14:03:53 +0200260 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200261 return found_frames;
262
philipel7d745e52018-08-02 14:03:53 +0200263 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
264 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100265 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200266 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100267 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200268 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100269 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700270 }
271 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100272 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
273 has_h264_pps) ||
274 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
275 is_h264_keyframe = true;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700276 // Store the resolution of key frame which is the packet with
277 // smallest index and valid resolution; typically its IDR or SPS
278 // packet; there may be packet preceeding this packet, IDR's
279 // resolution will be applied to them.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100280 if (buffer_[start_index]->width() > 0 &&
281 buffer_[start_index]->height() > 0) {
282 idr_width = buffer_[start_index]->width();
283 idr_height = buffer_[start_index]->height();
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700284 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100285 }
philipel2c9f9f22017-06-13 02:47:28 -0700286 }
287
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200288 if (tested_packets == buffer_.size())
philipel227f8b92017-08-04 06:39:31 -0700289 break;
290
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200291 start_index = start_index > 0 ? start_index - 1 : buffer_.size() - 1;
philipel8c619242017-02-02 08:51:29 -0800292
293 // In the case of H264 we don't have a frame_begin bit (yes,
Artem Titovdcd7fc72021-08-09 13:02:57 +0200294 // `frame_begin` might be set to true but that is a lie). So instead
philipel8c619242017-02-02 08:51:29 -0800295 // we traverese backwards as long as we have a previous packet and
296 // the timestamp of that packet is the same as this one. This may cause
297 // the PacketBuffer to hand out incomplete frames.
298 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100299 if (is_h264 && (buffer_[start_index] == nullptr ||
300 buffer_[start_index]->timestamp != frame_timestamp)) {
philipel8c619242017-02-02 08:51:29 -0800301 break;
302 }
303
304 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700305 }
306
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100307 if (is_h264) {
308 // Warn if this is an unsafe frame.
309 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200310 RTC_LOG(LS_WARNING)
311 << "Received H.264-IDR frame "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100312 "(SPS: "
313 << has_h264_sps << ", PPS: " << has_h264_pps << "). Treating as "
Jonas Olssonfc501102018-06-15 14:24:10 +0200314 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
315 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
316 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700317 }
318
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100319 // Now that we have decided whether to treat this frame as a key frame
320 // or delta frame in the frame buffer, we update the field that
321 // determines if the RtpFrameObject is a key frame or delta frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200322 const size_t first_packet_index = start_seq_num % buffer_.size();
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100323 if (is_h264_keyframe) {
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100324 buffer_[first_packet_index]->video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100325 VideoFrameType::kVideoFrameKey;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700326 if (idr_width > 0 && idr_height > 0) {
327 // IDR frame was finalized and we have the correct resolution for
328 // IDR; update first packet to have same resolution as IDR.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100329 buffer_[first_packet_index]->video_header.width = idr_width;
330 buffer_[first_packet_index]->video_header.height = idr_height;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700331 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100332 } else {
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100333 buffer_[first_packet_index]->video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100334 VideoFrameType::kVideoFrameDelta;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100335 }
336
philipel94659782020-06-15 12:26:39 +0200337 // If this is not a keyframe, make sure there are no gaps in the packet
338 // sequence numbers up until this point.
339 if (!is_h264_keyframe && missing_packets_.upper_bound(start_seq_num) !=
340 missing_packets_.begin()) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100341 return found_frames;
342 }
philipel2c9f9f22017-06-13 02:47:28 -0700343 }
344
philipel90623e12022-04-28 14:31:55 +0200345 if (is_h264 || full_frame_found) {
346 const uint16_t end_seq_num = seq_num + 1;
347 // Use uint16_t type to handle sequence number wrap around case.
348 uint16_t num_packets = end_seq_num - start_seq_num;
349 found_frames.reserve(found_frames.size() + num_packets);
350 for (uint16_t i = start_seq_num; i != end_seq_num; ++i) {
351 std::unique_ptr<Packet>& packet = buffer_[i % buffer_.size()];
352 RTC_DCHECK(packet);
353 RTC_DCHECK_EQ(i, packet->seq_num);
354 // Ensure frame boundary flags are properly set.
355 packet->video_header.is_first_packet_in_frame = (i == start_seq_num);
356 packet->video_header.is_last_packet_in_frame = (i == seq_num);
357 found_frames.push_back(std::move(packet));
358 }
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100359
philipel90623e12022-04-28 14:31:55 +0200360 missing_packets_.erase(missing_packets_.begin(),
361 missing_packets_.upper_bound(seq_num));
362 }
philipelc707ab72016-04-01 02:01:54 -0700363 }
philipelc707ab72016-04-01 02:01:54 -0700364 ++seq_num;
365 }
philipelfd5a20f2016-11-15 00:57:57 -0800366 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700367}
368
philipel2c9f9f22017-06-13 02:47:28 -0700369void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
370 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100371 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700372
373 const int kMaxPaddingAge = 1000;
374 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
375 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
376 auto erase_to = missing_packets_.lower_bound(old_seq_num);
377 missing_packets_.erase(missing_packets_.begin(), erase_to);
378
379 // Guard against inserting a large amount of missing packets if there is a
380 // jump in the sequence number.
381 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
382 *newest_inserted_seq_num_ = old_seq_num;
383
384 ++*newest_inserted_seq_num_;
385 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
386 missing_packets_.insert(*newest_inserted_seq_num_);
387 ++*newest_inserted_seq_num_;
388 }
389 } else {
390 missing_packets_.erase(seq_num);
391 }
392}
393
philipelc707ab72016-04-01 02:01:54 -0700394} // namespace video_coding
395} // namespace webrtc