blob: 0fbd0429b507ec0ecd4f1ad436e57d6aff621611 [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"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "api/video/encoded_frame.h"
Danil Chapovalov0682ca92019-11-28 16:50:02 +010025#include "api/video/video_frame_type.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "common_video/h264/h264_common.h"
Danil Chapovalov0682ca92019-11-28 16:50:02 +010027#include "modules/rtp_rtcp/source/rtp_depacketizer_av1.h"
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010028#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
29#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Yves Gerey3e707812018-11-28 16:47:49 +010030#include "modules/rtp_rtcp/source/rtp_video_header.h"
31#include "modules/video_coding/codecs/h264/include/h264_globals.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/video_coding/frame_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/checks.h"
34#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010035#include "rtc_base/numerics/mod_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "system_wrappers/include/clock.h"
Rasmus Brandt88f080a2017-11-02 14:28:06 +010037#include "system_wrappers/include/field_trial.h"
philipelc707ab72016-04-01 02:01:54 -070038
39namespace webrtc {
40namespace video_coding {
41
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010042PacketBuffer::Packet::Packet(const RtpPacketReceived& rtp_packet,
43 const RTPVideoHeader& video_header,
44 int64_t ntp_time_ms,
45 int64_t receive_time_ms)
46 : marker_bit(rtp_packet.Marker()),
47 payload_type(rtp_packet.PayloadType()),
48 seq_num(rtp_packet.SequenceNumber()),
49 timestamp(rtp_packet.Timestamp()),
50 ntp_time_ms(ntp_time_ms),
51 times_nacked(-1),
52 video_header(video_header),
53 packet_info(rtp_packet.Ssrc(),
54 rtp_packet.Csrcs(),
55 rtp_packet.Timestamp(),
56 /*audio_level=*/absl::nullopt,
57 rtp_packet.GetExtension<AbsoluteCaptureTimeExtension>(),
58 receive_time_ms) {}
59
philipelb4d31082016-07-11 08:46:29 -070060PacketBuffer::PacketBuffer(Clock* clock,
61 size_t start_buffer_size,
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020062 size_t max_buffer_size)
philipelb4d31082016-07-11 08:46:29 -070063 : clock_(clock),
philipelc707ab72016-04-01 02:01:54 -070064 max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070065 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020066 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010067 is_cleared_to_first_seq_num_(false),
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020068 buffer_(start_buffer_size),
Rasmus Brandt88f080a2017-11-02 14:28:06 +010069 sps_pps_idr_is_h264_keyframe_(
70 field_trial::IsEnabled("WebRTC-SpsPpsIdrIsH264Keyframe")) {
philipelc707ab72016-04-01 02:01:54 -070071 RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
72 // Buffer size must always be a power of 2.
73 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
74 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
75}
76
philipelaee3e0e2016-11-01 11:45:34 +010077PacketBuffer::~PacketBuffer() {
78 Clear();
79}
philipel17deeb42016-08-11 15:09:26 +020080
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010081PacketBuffer::InsertResult PacketBuffer::InsertPacket(
82 PacketBuffer::Packet* packet) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020083 PacketBuffer::InsertResult result;
84 rtc::CritScope lock(&crit_);
philipel3184f8e2017-05-18 08:08:53 -070085
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010086 uint16_t seq_num = packet->seq_num;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020087 size_t index = seq_num % buffer_.size();
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010088
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020089 if (!first_packet_received_) {
90 first_seq_num_ = seq_num;
91 first_packet_received_ = true;
92 } else if (AheadOf(first_seq_num_, seq_num)) {
93 // If we have explicitly cleared past this packet then it's old,
94 // don't insert it, just silently ignore it.
95 if (is_cleared_to_first_seq_num_) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020096 return result;
philipelc707ab72016-04-01 02:01:54 -070097 }
philipelc707ab72016-04-01 02:01:54 -070098
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020099 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -0700100 }
101
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200102 if (buffer_[index].used) {
103 // Duplicate packet, just delete the payload.
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100104 if (buffer_[index].seq_num() == packet->seq_num) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200105 return result;
106 }
philipelc707ab72016-04-01 02:01:54 -0700107
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200108 // The packet buffer is full, try to expand the buffer.
109 while (ExpandBufferSize() && buffer_[seq_num % buffer_.size()].used) {
110 }
111 index = seq_num % buffer_.size();
112
113 // Packet buffer is still full since we were unable to expand the buffer.
114 if (buffer_[index].used) {
115 // Clear the buffer, delete payload, and return false to signal that a
116 // new keyframe is needed.
117 RTC_LOG(LS_WARNING) << "Clear PacketBuffer and request key frame.";
118 Clear();
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200119 result.buffer_cleared = true;
120 return result;
121 }
122 }
123
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200124 int64_t now_ms = clock_->TimeInMilliseconds();
125 last_received_packet_ms_ = now_ms;
Danil Chapovalovc9e532a2019-12-10 17:03:00 +0100126 if (packet->video_header.frame_type == VideoFrameType::kVideoFrameKey ||
127 last_received_keyframe_rtp_timestamp_ == packet->timestamp) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200128 last_received_keyframe_packet_ms_ = now_ms;
Danil Chapovalovc9e532a2019-12-10 17:03:00 +0100129 last_received_keyframe_rtp_timestamp_ = packet->timestamp;
130 }
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200131
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100132 StoredPacket& new_entry = buffer_[index];
133 new_entry.continuous = false;
134 new_entry.used = true;
135 new_entry.data = std::move(*packet);
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100136
137 UpdateMissingPackets(seq_num);
138
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200139 result.frames = FindFrames(seq_num);
140 return result;
philipelc707ab72016-04-01 02:01:54 -0700141}
142
143void PacketBuffer::ClearTo(uint16_t seq_num) {
144 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700145 // We have already cleared past this sequence number, no need to do anything.
146 if (is_cleared_to_first_seq_num_ &&
147 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
148 return;
149 }
philipelaee3e0e2016-11-01 11:45:34 +0100150
151 // If the packet buffer was cleared between a frame was created and returned.
152 if (!first_packet_received_)
153 return;
154
philipelc5fb4682017-08-02 04:28:57 -0700155 // Avoid iterating over the buffer more than once by capping the number of
156 // iterations to the |size_| of the buffer.
157 ++seq_num;
158 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200159 size_t iterations = std::min(diff, buffer_.size());
philipelc5fb4682017-08-02 04:28:57 -0700160 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200161 size_t index = first_seq_num_ % buffer_.size();
162 if (AheadOf<uint16_t>(seq_num, buffer_[index].seq_num())) {
Danil Chapovalove3c48842019-12-02 15:54:27 +0100163 buffer_[index].data.video_payload = {};
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200164 buffer_[index].used = false;
philipelc5fb4682017-08-02 04:28:57 -0700165 }
philipelaee3e0e2016-11-01 11:45:34 +0100166 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700167 }
philipel2c9f9f22017-06-13 02:47:28 -0700168
philipelc5fb4682017-08-02 04:28:57 -0700169 // If |diff| is larger than |iterations| it means that we don't increment
170 // |first_seq_num_| until we reach |seq_num|, so we set it here.
171 first_seq_num_ = seq_num;
172
173 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100174 auto clear_to_it = missing_packets_.upper_bound(seq_num);
175 if (clear_to_it != missing_packets_.begin()) {
176 --clear_to_it;
177 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
178 }
philipelc707ab72016-04-01 02:01:54 -0700179}
180
Johannes Krona3705562019-08-26 16:37:11 +0200181void PacketBuffer::ClearInterval(uint16_t start_seq_num,
182 uint16_t stop_seq_num) {
183 size_t iterations = ForwardDiff<uint16_t>(start_seq_num, stop_seq_num + 1);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200184 RTC_DCHECK_LE(iterations, buffer_.size());
Johannes Krona3705562019-08-26 16:37:11 +0200185 uint16_t seq_num = start_seq_num;
186 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200187 size_t index = seq_num % buffer_.size();
188 RTC_DCHECK_EQ(buffer_[index].seq_num(), seq_num);
Danil Chapovalove3c48842019-12-02 15:54:27 +0100189 buffer_[index].data.video_payload = {};
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200190 buffer_[index].used = false;
Johannes Krona3705562019-08-26 16:37:11 +0200191
192 ++seq_num;
193 }
194}
195
philipelaee3e0e2016-11-01 11:45:34 +0100196void PacketBuffer::Clear() {
197 rtc::CritScope lock(&crit_);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200198 for (StoredPacket& entry : buffer_) {
Danil Chapovalove3c48842019-12-02 15:54:27 +0100199 entry.data.video_payload = {};
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200200 entry.used = false;
philipelaee3e0e2016-11-01 11:45:34 +0100201 }
202
203 first_packet_received_ = false;
204 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700205 last_received_packet_ms_.reset();
206 last_received_keyframe_packet_ms_.reset();
207 newest_inserted_seq_num_.reset();
208 missing_packets_.clear();
209}
210
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200211PacketBuffer::InsertResult PacketBuffer::InsertPadding(uint16_t seq_num) {
212 PacketBuffer::InsertResult result;
213 rtc::CritScope lock(&crit_);
214 UpdateMissingPackets(seq_num);
215 result.frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
216 return result;
philipel3184f8e2017-05-18 08:08:53 -0700217}
218
Danil Chapovalov0040b662018-06-18 10:48:16 +0200219absl::optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700220 rtc::CritScope lock(&crit_);
221 return last_received_packet_ms_;
222}
223
Danil Chapovalov0040b662018-06-18 10:48:16 +0200224absl::optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700225 rtc::CritScope lock(&crit_);
226 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100227}
228
philipelc707ab72016-04-01 02:01:54 -0700229bool PacketBuffer::ExpandBufferSize() {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200230 if (buffer_.size() == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100231 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
Johannes Kronbd3f3052019-08-01 15:45:54 +0200232 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700233 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100234 }
philipelc707ab72016-04-01 02:01:54 -0700235
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200236 size_t new_size = std::min(max_size_, 2 * buffer_.size());
237 std::vector<StoredPacket> new_buffer(new_size);
238 for (StoredPacket& entry : buffer_) {
239 if (entry.used) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100240 new_buffer[entry.seq_num() % new_size] = std::move(entry);
philipelc707ab72016-04-01 02:01:54 -0700241 }
242 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200243 buffer_ = std::move(new_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100244 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700245 return true;
246}
247
philipelaee3e0e2016-11-01 11:45:34 +0100248bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200249 size_t index = seq_num % buffer_.size();
250 int prev_index = index > 0 ? index - 1 : buffer_.size() - 1;
251 const StoredPacket& entry = buffer_[index];
252 const StoredPacket& prev_entry = buffer_[prev_index];
philipelf4139332016-04-20 10:26:34 +0200253
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200254 if (!entry.used)
philipelc707ab72016-04-01 02:01:54 -0700255 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200256 if (entry.seq_num() != seq_num)
philipel2c9f9f22017-06-13 02:47:28 -0700257 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200258 if (entry.frame_begin())
philipelc707ab72016-04-01 02:01:54 -0700259 return true;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200260 if (!prev_entry.used)
philipelc707ab72016-04-01 02:01:54 -0700261 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200262 if (prev_entry.seq_num() != static_cast<uint16_t>(entry.seq_num() - 1))
philipelea142f82017-01-11 02:01:56 -0800263 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200264 if (prev_entry.data.timestamp != entry.data.timestamp)
philipelf4139332016-04-20 10:26:34 +0200265 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200266 if (prev_entry.continuous)
philipelc707ab72016-04-01 02:01:54 -0700267 return true;
268
269 return false;
270}
271
philipelfd5a20f2016-11-15 00:57:57 -0800272std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
273 uint16_t seq_num) {
274 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200275 for (size_t i = 0; i < buffer_.size() && PotentialNewFrame(seq_num); ++i) {
276 size_t index = seq_num % buffer_.size();
277 buffer_[index].continuous = true;
philipelc707ab72016-04-01 02:01:54 -0700278
philipelf4139332016-04-20 10:26:34 +0200279 // If all packets of the frame is continuous, find the first packet of the
280 // frame and create an RtpFrameObject.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200281 if (buffer_[index].frame_end()) {
philipelc707ab72016-04-01 02:01:54 -0700282 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200283
philipel5ceaaae2016-05-24 10:20:47 +0200284 // Find the start index by searching backward until the packet with
285 // the |frame_begin| flag is set.
286 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700287 size_t tested_packets = 0;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200288 int64_t frame_timestamp = buffer_[start_index].data.timestamp;
philipel53910712017-05-18 02:24:40 -0700289
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100290 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200291 bool is_h264 = buffer_[start_index].data.codec() == kVideoCodecH264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100292 bool has_h264_sps = false;
293 bool has_h264_pps = false;
294 bool has_h264_idr = false;
295 bool is_h264_keyframe = false;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700296 int idr_width = -1;
297 int idr_height = -1;
philipel227f8b92017-08-04 06:39:31 -0700298 while (true) {
299 ++tested_packets;
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100300
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200301 if (!is_h264 && buffer_[start_index].frame_begin())
philipel5ceaaae2016-05-24 10:20:47 +0200302 break;
303
Shyam Sadhwani2b84dad2019-10-02 17:22:33 -0700304 if (is_h264) {
philipel7d745e52018-08-02 14:03:53 +0200305 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200306 &buffer_[start_index].data.video_header.video_type_header);
philipel7d745e52018-08-02 14:03:53 +0200307 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200308 return found_frames;
309
philipel7d745e52018-08-02 14:03:53 +0200310 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
311 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100312 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200313 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100314 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200315 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100316 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700317 }
318 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100319 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
320 has_h264_pps) ||
321 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
322 is_h264_keyframe = true;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700323 // Store the resolution of key frame which is the packet with
324 // smallest index and valid resolution; typically its IDR or SPS
325 // packet; there may be packet preceeding this packet, IDR's
326 // resolution will be applied to them.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200327 if (buffer_[start_index].data.width() > 0 &&
328 buffer_[start_index].data.height() > 0) {
329 idr_width = buffer_[start_index].data.width();
330 idr_height = buffer_[start_index].data.height();
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700331 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100332 }
philipel2c9f9f22017-06-13 02:47:28 -0700333 }
334
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200335 if (tested_packets == buffer_.size())
philipel227f8b92017-08-04 06:39:31 -0700336 break;
337
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200338 start_index = start_index > 0 ? start_index - 1 : buffer_.size() - 1;
philipel8c619242017-02-02 08:51:29 -0800339
340 // In the case of H264 we don't have a frame_begin bit (yes,
341 // |frame_begin| might be set to true but that is a lie). So instead
342 // we traverese backwards as long as we have a previous packet and
343 // the timestamp of that packet is the same as this one. This may cause
344 // the PacketBuffer to hand out incomplete frames.
345 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700346 if (is_h264 &&
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200347 (!buffer_[start_index].used ||
348 buffer_[start_index].data.timestamp != frame_timestamp)) {
philipel8c619242017-02-02 08:51:29 -0800349 break;
350 }
351
352 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700353 }
354
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100355 if (is_h264) {
356 // Warn if this is an unsafe frame.
357 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200358 RTC_LOG(LS_WARNING)
359 << "Received H.264-IDR frame "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100360 "(SPS: "
361 << has_h264_sps << ", PPS: " << has_h264_pps << "). Treating as "
Jonas Olssonfc501102018-06-15 14:24:10 +0200362 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
363 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
364 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700365 }
366
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100367 // Now that we have decided whether to treat this frame as a key frame
368 // or delta frame in the frame buffer, we update the field that
369 // determines if the RtpFrameObject is a key frame or delta frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200370 const size_t first_packet_index = start_seq_num % buffer_.size();
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100371 if (is_h264_keyframe) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200372 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100373 VideoFrameType::kVideoFrameKey;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700374 if (idr_width > 0 && idr_height > 0) {
375 // IDR frame was finalized and we have the correct resolution for
376 // IDR; update first packet to have same resolution as IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200377 buffer_[first_packet_index].data.video_header.width = idr_width;
378 buffer_[first_packet_index].data.video_header.height = idr_height;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700379 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100380 } else {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200381 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100382 VideoFrameType::kVideoFrameDelta;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100383 }
384
Johnny Leebc7f41b2019-05-01 14:41:32 -0400385 // With IPPP, if this is not a keyframe, make sure there are no gaps
386 // in the packet sequence numbers up until this point.
387 const uint8_t h264tid =
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200388 buffer_[start_index].data.video_header.frame_marking.temporal_id;
Jonas Olssona4d87372019-07-05 19:08:33 +0200389 if (h264tid == kNoTemporalIdx && !is_h264_keyframe &&
390 missing_packets_.upper_bound(start_seq_num) !=
391 missing_packets_.begin()) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100392 return found_frames;
393 }
philipel2c9f9f22017-06-13 02:47:28 -0700394 }
395
Danil Chapovalov0682ca92019-11-28 16:50:02 +0100396 if (auto frame = AssembleFrame(start_seq_num, seq_num)) {
397 found_frames.push_back(std::move(frame));
398 } else {
399 RTC_LOG(LS_ERROR) << "Failed to assemble frame from packets "
400 << start_seq_num << "-" << seq_num;
401 }
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100402
philipel2c9f9f22017-06-13 02:47:28 -0700403 missing_packets_.erase(missing_packets_.begin(),
404 missing_packets_.upper_bound(seq_num));
Johannes Krona3705562019-08-26 16:37:11 +0200405 ClearInterval(start_seq_num, seq_num);
philipelc707ab72016-04-01 02:01:54 -0700406 }
philipelc707ab72016-04-01 02:01:54 -0700407 ++seq_num;
408 }
philipelfd5a20f2016-11-15 00:57:57 -0800409 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700410}
411
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100412std::unique_ptr<RtpFrameObject> PacketBuffer::AssembleFrame(
philipelb5e47852019-09-20 11:30:12 +0200413 uint16_t first_seq_num,
414 uint16_t last_seq_num) {
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100415 const uint16_t end_seq_num = last_seq_num + 1;
416 const uint16_t num_packets = end_seq_num - first_seq_num;
417 int max_nack_count = -1;
418 int64_t min_recv_time = std::numeric_limits<int64_t>::max();
419 int64_t max_recv_time = std::numeric_limits<int64_t>::min();
420 size_t frame_size = 0;
philipelc707ab72016-04-01 02:01:54 -0700421
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100422 std::vector<rtc::ArrayView<const uint8_t>> payloads;
423 RtpPacketInfos::vector_type packet_infos;
424 payloads.reserve(num_packets);
425 packet_infos.reserve(num_packets);
philipel227f8b92017-08-04 06:39:31 -0700426
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100427 for (uint16_t seq_num = first_seq_num; seq_num != end_seq_num; ++seq_num) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100428 const Packet& packet = GetPacket(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700429
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100430 max_nack_count = std::max(max_nack_count, packet.times_nacked);
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100431 min_recv_time =
432 std::min(min_recv_time, packet.packet_info.receive_time_ms());
433 max_recv_time =
434 std::max(max_recv_time, packet.packet_info.receive_time_ms());
Danil Chapovalove3c48842019-12-02 15:54:27 +0100435 frame_size += packet.video_payload.size();
436 payloads.emplace_back(packet.video_payload);
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100437 packet_infos.push_back(packet.packet_info);
438 }
philipel227f8b92017-08-04 06:39:31 -0700439
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100440 const Packet& first_packet = GetPacket(first_seq_num);
Danil Chapovalov0682ca92019-11-28 16:50:02 +0100441 rtc::scoped_refptr<EncodedImageBuffer> bitstream;
442 // TODO(danilchap): Hide codec-specific code paths behind an interface.
443 if (first_packet.codec() == VideoCodecType::kVideoCodecAV1) {
444 bitstream = RtpDepacketizerAv1::AssembleFrame(payloads);
445 if (!bitstream) {
446 // Failed to assemble a frame. Discard and continue.
447 return nullptr;
448 }
449 } else {
450 bitstream = EncodedImageBuffer::Create(frame_size);
451
452 uint8_t* write_at = bitstream->data();
453 for (rtc::ArrayView<const uint8_t> payload : payloads) {
454 memcpy(write_at, payload.data(), payload.size());
455 write_at += payload.size();
456 }
457 RTC_DCHECK_EQ(write_at - bitstream->data(), bitstream->size());
458 }
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100459 const Packet& last_packet = GetPacket(last_seq_num);
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100460 return std::make_unique<RtpFrameObject>(
461 first_seq_num, //
462 last_seq_num, //
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100463 last_packet.marker_bit, //
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100464 max_nack_count, //
465 min_recv_time, //
466 max_recv_time, //
467 first_packet.timestamp, //
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100468 first_packet.ntp_time_ms, //
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100469 last_packet.video_header.video_timing, //
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100470 first_packet.payload_type, //
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100471 first_packet.codec(), //
472 last_packet.video_header.rotation, //
473 last_packet.video_header.content_type, //
474 first_packet.video_header, //
475 last_packet.video_header.color_space, //
476 first_packet.generic_descriptor, //
477 RtpPacketInfos(std::move(packet_infos)), //
478 std::move(bitstream));
philipelc707ab72016-04-01 02:01:54 -0700479}
480
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100481const PacketBuffer::Packet& PacketBuffer::GetPacket(uint16_t seq_num) const {
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100482 const StoredPacket& entry = buffer_[seq_num % buffer_.size()];
483 RTC_DCHECK(entry.used);
484 RTC_DCHECK_EQ(seq_num, entry.seq_num());
485 return entry.data;
philipelf4139332016-04-20 10:26:34 +0200486}
487
philipel2c9f9f22017-06-13 02:47:28 -0700488void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
489 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100490 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700491
492 const int kMaxPaddingAge = 1000;
493 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
494 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
495 auto erase_to = missing_packets_.lower_bound(old_seq_num);
496 missing_packets_.erase(missing_packets_.begin(), erase_to);
497
498 // Guard against inserting a large amount of missing packets if there is a
499 // jump in the sequence number.
500 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
501 *newest_inserted_seq_num_ = old_seq_num;
502
503 ++*newest_inserted_seq_num_;
504 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
505 missing_packets_.insert(*newest_inserted_seq_num_);
506 ++*newest_inserted_seq_num_;
507 }
508 } else {
509 missing_packets_.erase(seq_num);
510 }
511}
512
philipelc707ab72016-04-01 02:01:54 -0700513} // namespace video_coding
514} // namespace webrtc