blob: d2a2bcfb47fb11f18bc2aa60cdf0399137aae1f9 [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"
philipelc707ab72016-04-01 02:01:54 -070034
35namespace webrtc {
36namespace video_coding {
37
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010038PacketBuffer::Packet::Packet(const RtpPacketReceived& rtp_packet,
39 const RTPVideoHeader& video_header,
40 int64_t ntp_time_ms,
41 int64_t receive_time_ms)
42 : marker_bit(rtp_packet.Marker()),
43 payload_type(rtp_packet.PayloadType()),
44 seq_num(rtp_packet.SequenceNumber()),
45 timestamp(rtp_packet.Timestamp()),
46 ntp_time_ms(ntp_time_ms),
47 times_nacked(-1),
48 video_header(video_header),
49 packet_info(rtp_packet.Ssrc(),
50 rtp_packet.Csrcs(),
51 rtp_packet.Timestamp(),
52 /*audio_level=*/absl::nullopt,
53 rtp_packet.GetExtension<AbsoluteCaptureTimeExtension>(),
54 receive_time_ms) {}
55
philipelb4d31082016-07-11 08:46:29 -070056PacketBuffer::PacketBuffer(Clock* clock,
57 size_t start_buffer_size,
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020058 size_t max_buffer_size)
philipelb4d31082016-07-11 08:46:29 -070059 : clock_(clock),
philipelc707ab72016-04-01 02:01:54 -070060 max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070061 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020062 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010063 is_cleared_to_first_seq_num_(false),
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020064 buffer_(start_buffer_size),
Eldar Rello2127aaa2020-08-07 12:08:14 +030065 sps_pps_idr_is_h264_keyframe_(false) {
philipelc707ab72016-04-01 02:01:54 -070066 RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
67 // Buffer size must always be a power of 2.
68 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
69 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
70}
71
philipelaee3e0e2016-11-01 11:45:34 +010072PacketBuffer::~PacketBuffer() {
73 Clear();
74}
philipel17deeb42016-08-11 15:09:26 +020075
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010076PacketBuffer::InsertResult PacketBuffer::InsertPacket(
Danil Chapovalov97ffbef2020-01-24 16:04:35 +010077 std::unique_ptr<PacketBuffer::Packet> packet) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020078 PacketBuffer::InsertResult result;
Markus Handellf70fbc82020-06-04 00:41:20 +020079 MutexLock lock(&mutex_);
philipel3184f8e2017-05-18 08:08:53 -070080
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010081 uint16_t seq_num = packet->seq_num;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020082 size_t index = seq_num % buffer_.size();
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010083
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020084 if (!first_packet_received_) {
85 first_seq_num_ = seq_num;
86 first_packet_received_ = true;
87 } else if (AheadOf(first_seq_num_, seq_num)) {
88 // If we have explicitly cleared past this packet then it's old,
89 // don't insert it, just silently ignore it.
90 if (is_cleared_to_first_seq_num_) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020091 return result;
philipelc707ab72016-04-01 02:01:54 -070092 }
philipelc707ab72016-04-01 02:01:54 -070093
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020094 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070095 }
96
Danil Chapovalovf4306eb2020-03-20 12:30:31 +010097 if (buffer_[index] != nullptr) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020098 // Duplicate packet, just delete the payload.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +010099 if (buffer_[index]->seq_num == packet->seq_num) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200100 return result;
101 }
philipelc707ab72016-04-01 02:01:54 -0700102
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200103 // The packet buffer is full, try to expand the buffer.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100104 while (ExpandBufferSize() && buffer_[seq_num % buffer_.size()] != nullptr) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200105 }
106 index = seq_num % buffer_.size();
107
108 // Packet buffer is still full since we were unable to expand the buffer.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100109 if (buffer_[index] != nullptr) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200110 // Clear the buffer, delete payload, and return false to signal that a
111 // new keyframe is needed.
112 RTC_LOG(LS_WARNING) << "Clear PacketBuffer and request key frame.";
Markus Handell3eac1112020-05-27 17:08:41 +0200113 ClearInternal();
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200114 result.buffer_cleared = true;
115 return result;
116 }
117 }
118
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200119 int64_t now_ms = clock_->TimeInMilliseconds();
120 last_received_packet_ms_ = now_ms;
Danil Chapovalovc9e532a2019-12-10 17:03:00 +0100121 if (packet->video_header.frame_type == VideoFrameType::kVideoFrameKey ||
122 last_received_keyframe_rtp_timestamp_ == packet->timestamp) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200123 last_received_keyframe_packet_ms_ = now_ms;
Danil Chapovalovc9e532a2019-12-10 17:03:00 +0100124 last_received_keyframe_rtp_timestamp_ = packet->timestamp;
125 }
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200126
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100127 packet->continuous = false;
128 buffer_[index] = std::move(packet);
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100129
130 UpdateMissingPackets(seq_num);
131
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100132 result.packets = FindFrames(seq_num);
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200133 return result;
philipelc707ab72016-04-01 02:01:54 -0700134}
135
136void PacketBuffer::ClearTo(uint16_t seq_num) {
Markus Handellf70fbc82020-06-04 00:41:20 +0200137 MutexLock lock(&mutex_);
philipelc5fb4682017-08-02 04:28:57 -0700138 // We have already cleared past this sequence number, no need to do anything.
139 if (is_cleared_to_first_seq_num_ &&
140 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
141 return;
142 }
philipelaee3e0e2016-11-01 11:45:34 +0100143
144 // If the packet buffer was cleared between a frame was created and returned.
145 if (!first_packet_received_)
146 return;
147
philipelc5fb4682017-08-02 04:28:57 -0700148 // Avoid iterating over the buffer more than once by capping the number of
149 // iterations to the |size_| of the buffer.
150 ++seq_num;
151 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200152 size_t iterations = std::min(diff, buffer_.size());
philipelc5fb4682017-08-02 04:28:57 -0700153 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100154 auto& stored = buffer_[first_seq_num_ % buffer_.size()];
155 if (stored != nullptr && AheadOf<uint16_t>(seq_num, stored->seq_num)) {
156 stored = nullptr;
philipelc5fb4682017-08-02 04:28:57 -0700157 }
philipelaee3e0e2016-11-01 11:45:34 +0100158 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700159 }
philipel2c9f9f22017-06-13 02:47:28 -0700160
philipelc5fb4682017-08-02 04:28:57 -0700161 // If |diff| is larger than |iterations| it means that we don't increment
162 // |first_seq_num_| until we reach |seq_num|, so we set it here.
163 first_seq_num_ = seq_num;
164
165 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100166 auto clear_to_it = missing_packets_.upper_bound(seq_num);
167 if (clear_to_it != missing_packets_.begin()) {
168 --clear_to_it;
169 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
170 }
philipelc707ab72016-04-01 02:01:54 -0700171}
172
philipelaee3e0e2016-11-01 11:45:34 +0100173void PacketBuffer::Clear() {
Markus Handellf70fbc82020-06-04 00:41:20 +0200174 MutexLock lock(&mutex_);
Markus Handell3eac1112020-05-27 17:08:41 +0200175 ClearInternal();
philipel2c9f9f22017-06-13 02:47:28 -0700176}
177
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200178PacketBuffer::InsertResult PacketBuffer::InsertPadding(uint16_t seq_num) {
179 PacketBuffer::InsertResult result;
Markus Handellf70fbc82020-06-04 00:41:20 +0200180 MutexLock lock(&mutex_);
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200181 UpdateMissingPackets(seq_num);
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100182 result.packets = FindFrames(static_cast<uint16_t>(seq_num + 1));
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200183 return result;
philipel3184f8e2017-05-18 08:08:53 -0700184}
185
Danil Chapovalov0040b662018-06-18 10:48:16 +0200186absl::optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
Markus Handellf70fbc82020-06-04 00:41:20 +0200187 MutexLock lock(&mutex_);
philipel3184f8e2017-05-18 08:08:53 -0700188 return last_received_packet_ms_;
189}
190
Danil Chapovalov0040b662018-06-18 10:48:16 +0200191absl::optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
Markus Handellf70fbc82020-06-04 00:41:20 +0200192 MutexLock lock(&mutex_);
philipel3184f8e2017-05-18 08:08:53 -0700193 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100194}
Eldar Rello2127aaa2020-08-07 12:08:14 +0300195void PacketBuffer::ForceSpsPpsIdrIsH264Keyframe() {
196 sps_pps_idr_is_h264_keyframe_ = true;
197}
Markus Handell3eac1112020-05-27 17:08:41 +0200198void 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
philipelc707ab72016-04-01 02:01:54 -0700211bool PacketBuffer::ExpandBufferSize() {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200212 if (buffer_.size() == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100213 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
Johannes Kronbd3f3052019-08-01 15:45:54 +0200214 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700215 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100216 }
philipelc707ab72016-04-01 02:01:54 -0700217
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200218 size_t new_size = std::min(max_size_, 2 * buffer_.size());
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100219 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);
philipelc707ab72016-04-01 02:01:54 -0700223 }
224 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200225 buffer_ = std::move(new_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100226 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700227 return true;
228}
229
philipelaee3e0e2016-11-01 11:45:34 +0100230bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200231 size_t index = seq_num % buffer_.size();
232 int prev_index = index > 0 ? index - 1 : buffer_.size() - 1;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100233 const auto& entry = buffer_[index];
234 const auto& prev_entry = buffer_[prev_index];
philipelf4139332016-04-20 10:26:34 +0200235
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100236 if (entry == nullptr)
philipelc707ab72016-04-01 02:01:54 -0700237 return false;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100238 if (entry->seq_num != seq_num)
philipel2c9f9f22017-06-13 02:47:28 -0700239 return false;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100240 if (entry->is_first_packet_in_frame())
philipelc707ab72016-04-01 02:01:54 -0700241 return true;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100242 if (prev_entry == nullptr)
philipelc707ab72016-04-01 02:01:54 -0700243 return false;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100244 if (prev_entry->seq_num != static_cast<uint16_t>(entry->seq_num - 1))
philipelea142f82017-01-11 02:01:56 -0800245 return false;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100246 if (prev_entry->timestamp != entry->timestamp)
philipelf4139332016-04-20 10:26:34 +0200247 return false;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100248 if (prev_entry->continuous)
philipelc707ab72016-04-01 02:01:54 -0700249 return true;
250
251 return false;
252}
253
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100254std::vector<std::unique_ptr<PacketBuffer::Packet>> PacketBuffer::FindFrames(
philipelfd5a20f2016-11-15 00:57:57 -0800255 uint16_t seq_num) {
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100256 std::vector<std::unique_ptr<PacketBuffer::Packet>> found_frames;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200257 for (size_t i = 0; i < buffer_.size() && PotentialNewFrame(seq_num); ++i) {
258 size_t index = seq_num % buffer_.size();
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100259 buffer_[index]->continuous = true;
philipelc707ab72016-04-01 02:01:54 -0700260
philipelf4139332016-04-20 10:26:34 +0200261 // If all packets of the frame is continuous, find the first packet of the
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100262 // frame and add all packets of the frame to the returned packets.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100263 if (buffer_[index]->is_last_packet_in_frame()) {
philipelc707ab72016-04-01 02:01:54 -0700264 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200265
philipel5ceaaae2016-05-24 10:20:47 +0200266 // Find the start index by searching backward until the packet with
267 // the |frame_begin| flag is set.
268 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700269 size_t tested_packets = 0;
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100270 int64_t frame_timestamp = buffer_[start_index]->timestamp;
philipel53910712017-05-18 02:24:40 -0700271
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100272 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100273 bool is_h264 = buffer_[start_index]->codec() == kVideoCodecH264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100274 bool has_h264_sps = false;
275 bool has_h264_pps = false;
276 bool has_h264_idr = false;
277 bool is_h264_keyframe = false;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700278 int idr_width = -1;
279 int idr_height = -1;
philipel227f8b92017-08-04 06:39:31 -0700280 while (true) {
281 ++tested_packets;
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100282
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100283 if (!is_h264 && buffer_[start_index]->is_first_packet_in_frame())
philipel5ceaaae2016-05-24 10:20:47 +0200284 break;
285
Shyam Sadhwani2b84dad2019-10-02 17:22:33 -0700286 if (is_h264) {
philipel7d745e52018-08-02 14:03:53 +0200287 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100288 &buffer_[start_index]->video_header.video_type_header);
philipel7d745e52018-08-02 14:03:53 +0200289 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200290 return found_frames;
291
philipel7d745e52018-08-02 14:03:53 +0200292 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
293 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100294 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200295 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100296 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200297 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100298 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700299 }
300 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100301 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;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700305 // 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.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100309 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();
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700313 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100314 }
philipel2c9f9f22017-06-13 02:47:28 -0700315 }
316
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200317 if (tested_packets == buffer_.size())
philipel227f8b92017-08-04 06:39:31 -0700318 break;
319
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200320 start_index = start_index > 0 ? start_index - 1 : buffer_.size() - 1;
philipel8c619242017-02-02 08:51:29 -0800321
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
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100328 if (is_h264 && (buffer_[start_index] == nullptr ||
329 buffer_[start_index]->timestamp != frame_timestamp)) {
philipel8c619242017-02-02 08:51:29 -0800330 break;
331 }
332
333 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700334 }
335
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100336 if (is_h264) {
337 // Warn if this is an unsafe frame.
338 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200339 RTC_LOG(LS_WARNING)
340 << "Received H.264-IDR frame "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100341 "(SPS: "
342 << has_h264_sps << ", PPS: " << has_h264_pps << "). Treating as "
Jonas Olssonfc501102018-06-15 14:24:10 +0200343 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
344 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
345 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700346 }
347
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100348 // 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.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200351 const size_t first_packet_index = start_seq_num % buffer_.size();
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100352 if (is_h264_keyframe) {
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100353 buffer_[first_packet_index]->video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100354 VideoFrameType::kVideoFrameKey;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700355 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.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100358 buffer_[first_packet_index]->video_header.width = idr_width;
359 buffer_[first_packet_index]->video_header.height = idr_height;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700360 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100361 } else {
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100362 buffer_[first_packet_index]->video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100363 VideoFrameType::kVideoFrameDelta;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100364 }
365
philipel94659782020-06-15 12:26:39 +0200366 // 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()) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100370 return found_frames;
371 }
philipel2c9f9f22017-06-13 02:47:28 -0700372 }
373
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100374 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) {
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100379 std::unique_ptr<Packet>& packet = buffer_[i % buffer_.size()];
380 RTC_DCHECK(packet);
381 RTC_DCHECK_EQ(i, packet->seq_num);
Danil Chapovalov810b4ca2020-03-19 13:56:11 +0100382 // Ensure frame boundary flags are properly set.
Danil Chapovalovf4306eb2020-03-20 12:30:31 +0100383 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));
Danil Chapovalov0682ca92019-11-28 16:50:02 +0100386 }
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100387
philipel2c9f9f22017-06-13 02:47:28 -0700388 missing_packets_.erase(missing_packets_.begin(),
389 missing_packets_.upper_bound(seq_num));
philipelc707ab72016-04-01 02:01:54 -0700390 }
philipelc707ab72016-04-01 02:01:54 -0700391 ++seq_num;
392 }
philipelfd5a20f2016-11-15 00:57:57 -0800393 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700394}
395
philipel2c9f9f22017-06-13 02:47:28 -0700396void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
397 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100398 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700399
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
philipelc707ab72016-04-01 02:01:54 -0700421} // namespace video_coding
422} // namespace webrtc