blob: b6fc521d049f68a9b0eb803b3ccb2f1703de341b [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;
126 if (packet->video_header.frame_type == VideoFrameType::kVideoFrameKey)
127 last_received_keyframe_packet_ms_ = now_ms;
128
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100129 StoredPacket& new_entry = buffer_[index];
130 new_entry.continuous = false;
131 new_entry.used = true;
132 new_entry.data = std::move(*packet);
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100133
134 UpdateMissingPackets(seq_num);
135
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200136 result.frames = FindFrames(seq_num);
137 return result;
philipelc707ab72016-04-01 02:01:54 -0700138}
139
140void PacketBuffer::ClearTo(uint16_t seq_num) {
141 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700142 // We have already cleared past this sequence number, no need to do anything.
143 if (is_cleared_to_first_seq_num_ &&
144 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
145 return;
146 }
philipelaee3e0e2016-11-01 11:45:34 +0100147
148 // If the packet buffer was cleared between a frame was created and returned.
149 if (!first_packet_received_)
150 return;
151
philipelc5fb4682017-08-02 04:28:57 -0700152 // Avoid iterating over the buffer more than once by capping the number of
153 // iterations to the |size_| of the buffer.
154 ++seq_num;
155 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200156 size_t iterations = std::min(diff, buffer_.size());
philipelc5fb4682017-08-02 04:28:57 -0700157 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200158 size_t index = first_seq_num_ % buffer_.size();
159 if (AheadOf<uint16_t>(seq_num, buffer_[index].seq_num())) {
Danil Chapovalove3c48842019-12-02 15:54:27 +0100160 buffer_[index].data.video_payload = {};
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200161 buffer_[index].used = false;
philipelc5fb4682017-08-02 04:28:57 -0700162 }
philipelaee3e0e2016-11-01 11:45:34 +0100163 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700164 }
philipel2c9f9f22017-06-13 02:47:28 -0700165
philipelc5fb4682017-08-02 04:28:57 -0700166 // If |diff| is larger than |iterations| it means that we don't increment
167 // |first_seq_num_| until we reach |seq_num|, so we set it here.
168 first_seq_num_ = seq_num;
169
170 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100171 auto clear_to_it = missing_packets_.upper_bound(seq_num);
172 if (clear_to_it != missing_packets_.begin()) {
173 --clear_to_it;
174 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
175 }
philipelc707ab72016-04-01 02:01:54 -0700176}
177
Johannes Krona3705562019-08-26 16:37:11 +0200178void PacketBuffer::ClearInterval(uint16_t start_seq_num,
179 uint16_t stop_seq_num) {
180 size_t iterations = ForwardDiff<uint16_t>(start_seq_num, stop_seq_num + 1);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200181 RTC_DCHECK_LE(iterations, buffer_.size());
Johannes Krona3705562019-08-26 16:37:11 +0200182 uint16_t seq_num = start_seq_num;
183 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200184 size_t index = seq_num % buffer_.size();
185 RTC_DCHECK_EQ(buffer_[index].seq_num(), seq_num);
Danil Chapovalove3c48842019-12-02 15:54:27 +0100186 buffer_[index].data.video_payload = {};
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200187 buffer_[index].used = false;
Johannes Krona3705562019-08-26 16:37:11 +0200188
189 ++seq_num;
190 }
191}
192
philipelaee3e0e2016-11-01 11:45:34 +0100193void PacketBuffer::Clear() {
194 rtc::CritScope lock(&crit_);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200195 for (StoredPacket& entry : buffer_) {
Danil Chapovalove3c48842019-12-02 15:54:27 +0100196 entry.data.video_payload = {};
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200197 entry.used = false;
philipelaee3e0e2016-11-01 11:45:34 +0100198 }
199
200 first_packet_received_ = false;
201 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700202 last_received_packet_ms_.reset();
203 last_received_keyframe_packet_ms_.reset();
204 newest_inserted_seq_num_.reset();
205 missing_packets_.clear();
206}
207
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200208PacketBuffer::InsertResult PacketBuffer::InsertPadding(uint16_t seq_num) {
209 PacketBuffer::InsertResult result;
210 rtc::CritScope lock(&crit_);
211 UpdateMissingPackets(seq_num);
212 result.frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
213 return result;
philipel3184f8e2017-05-18 08:08:53 -0700214}
215
Danil Chapovalov0040b662018-06-18 10:48:16 +0200216absl::optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700217 rtc::CritScope lock(&crit_);
218 return last_received_packet_ms_;
219}
220
Danil Chapovalov0040b662018-06-18 10:48:16 +0200221absl::optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700222 rtc::CritScope lock(&crit_);
223 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100224}
225
philipelc707ab72016-04-01 02:01:54 -0700226bool PacketBuffer::ExpandBufferSize() {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200227 if (buffer_.size() == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100228 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
Johannes Kronbd3f3052019-08-01 15:45:54 +0200229 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700230 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100231 }
philipelc707ab72016-04-01 02:01:54 -0700232
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200233 size_t new_size = std::min(max_size_, 2 * buffer_.size());
234 std::vector<StoredPacket> new_buffer(new_size);
235 for (StoredPacket& entry : buffer_) {
236 if (entry.used) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100237 new_buffer[entry.seq_num() % new_size] = std::move(entry);
philipelc707ab72016-04-01 02:01:54 -0700238 }
239 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200240 buffer_ = std::move(new_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100241 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700242 return true;
243}
244
philipelaee3e0e2016-11-01 11:45:34 +0100245bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200246 size_t index = seq_num % buffer_.size();
247 int prev_index = index > 0 ? index - 1 : buffer_.size() - 1;
248 const StoredPacket& entry = buffer_[index];
249 const StoredPacket& prev_entry = buffer_[prev_index];
philipelf4139332016-04-20 10:26:34 +0200250
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200251 if (!entry.used)
philipelc707ab72016-04-01 02:01:54 -0700252 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200253 if (entry.seq_num() != seq_num)
philipel2c9f9f22017-06-13 02:47:28 -0700254 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200255 if (entry.frame_begin())
philipelc707ab72016-04-01 02:01:54 -0700256 return true;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200257 if (!prev_entry.used)
philipelc707ab72016-04-01 02:01:54 -0700258 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200259 if (prev_entry.seq_num() != static_cast<uint16_t>(entry.seq_num() - 1))
philipelea142f82017-01-11 02:01:56 -0800260 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200261 if (prev_entry.data.timestamp != entry.data.timestamp)
philipelf4139332016-04-20 10:26:34 +0200262 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200263 if (prev_entry.continuous)
philipelc707ab72016-04-01 02:01:54 -0700264 return true;
265
266 return false;
267}
268
philipelfd5a20f2016-11-15 00:57:57 -0800269std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
270 uint16_t seq_num) {
271 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200272 for (size_t i = 0; i < buffer_.size() && PotentialNewFrame(seq_num); ++i) {
273 size_t index = seq_num % buffer_.size();
274 buffer_[index].continuous = true;
philipelc707ab72016-04-01 02:01:54 -0700275
philipelf4139332016-04-20 10:26:34 +0200276 // If all packets of the frame is continuous, find the first packet of the
277 // frame and create an RtpFrameObject.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200278 if (buffer_[index].frame_end()) {
philipelc707ab72016-04-01 02:01:54 -0700279 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200280
philipel5ceaaae2016-05-24 10:20:47 +0200281 // Find the start index by searching backward until the packet with
282 // the |frame_begin| flag is set.
283 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700284 size_t tested_packets = 0;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200285 int64_t frame_timestamp = buffer_[start_index].data.timestamp;
philipel53910712017-05-18 02:24:40 -0700286
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100287 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200288 bool is_h264 = buffer_[start_index].data.codec() == kVideoCodecH264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100289 bool has_h264_sps = false;
290 bool has_h264_pps = false;
291 bool has_h264_idr = false;
292 bool is_h264_keyframe = false;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700293 int idr_width = -1;
294 int idr_height = -1;
philipel227f8b92017-08-04 06:39:31 -0700295 while (true) {
296 ++tested_packets;
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100297
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200298 if (!is_h264 && buffer_[start_index].frame_begin())
philipel5ceaaae2016-05-24 10:20:47 +0200299 break;
300
Shyam Sadhwani2b84dad2019-10-02 17:22:33 -0700301 if (is_h264) {
philipel7d745e52018-08-02 14:03:53 +0200302 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200303 &buffer_[start_index].data.video_header.video_type_header);
philipel7d745e52018-08-02 14:03:53 +0200304 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200305 return found_frames;
306
philipel7d745e52018-08-02 14:03:53 +0200307 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
308 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100309 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200310 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100311 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200312 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100313 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700314 }
315 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100316 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
317 has_h264_pps) ||
318 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
319 is_h264_keyframe = true;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700320 // Store the resolution of key frame which is the packet with
321 // smallest index and valid resolution; typically its IDR or SPS
322 // packet; there may be packet preceeding this packet, IDR's
323 // resolution will be applied to them.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200324 if (buffer_[start_index].data.width() > 0 &&
325 buffer_[start_index].data.height() > 0) {
326 idr_width = buffer_[start_index].data.width();
327 idr_height = buffer_[start_index].data.height();
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700328 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100329 }
philipel2c9f9f22017-06-13 02:47:28 -0700330 }
331
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200332 if (tested_packets == buffer_.size())
philipel227f8b92017-08-04 06:39:31 -0700333 break;
334
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200335 start_index = start_index > 0 ? start_index - 1 : buffer_.size() - 1;
philipel8c619242017-02-02 08:51:29 -0800336
337 // In the case of H264 we don't have a frame_begin bit (yes,
338 // |frame_begin| might be set to true but that is a lie). So instead
339 // we traverese backwards as long as we have a previous packet and
340 // the timestamp of that packet is the same as this one. This may cause
341 // the PacketBuffer to hand out incomplete frames.
342 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700343 if (is_h264 &&
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200344 (!buffer_[start_index].used ||
345 buffer_[start_index].data.timestamp != frame_timestamp)) {
philipel8c619242017-02-02 08:51:29 -0800346 break;
347 }
348
349 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700350 }
351
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100352 if (is_h264) {
353 // Warn if this is an unsafe frame.
354 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200355 RTC_LOG(LS_WARNING)
356 << "Received H.264-IDR frame "
357 << "(SPS: " << has_h264_sps << ", PPS: " << has_h264_pps
358 << "). Treating as "
359 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
360 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
361 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700362 }
363
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100364 // Now that we have decided whether to treat this frame as a key frame
365 // or delta frame in the frame buffer, we update the field that
366 // determines if the RtpFrameObject is a key frame or delta frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200367 const size_t first_packet_index = start_seq_num % buffer_.size();
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100368 if (is_h264_keyframe) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200369 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100370 VideoFrameType::kVideoFrameKey;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700371 if (idr_width > 0 && idr_height > 0) {
372 // IDR frame was finalized and we have the correct resolution for
373 // IDR; update first packet to have same resolution as IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200374 buffer_[first_packet_index].data.video_header.width = idr_width;
375 buffer_[first_packet_index].data.video_header.height = idr_height;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700376 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100377 } else {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200378 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100379 VideoFrameType::kVideoFrameDelta;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100380 }
381
Johnny Leebc7f41b2019-05-01 14:41:32 -0400382 // With IPPP, if this is not a keyframe, make sure there are no gaps
383 // in the packet sequence numbers up until this point.
384 const uint8_t h264tid =
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200385 buffer_[start_index].data.video_header.frame_marking.temporal_id;
Jonas Olssona4d87372019-07-05 19:08:33 +0200386 if (h264tid == kNoTemporalIdx && !is_h264_keyframe &&
387 missing_packets_.upper_bound(start_seq_num) !=
388 missing_packets_.begin()) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100389 return found_frames;
390 }
philipel2c9f9f22017-06-13 02:47:28 -0700391 }
392
Danil Chapovalov0682ca92019-11-28 16:50:02 +0100393 if (auto frame = AssembleFrame(start_seq_num, seq_num)) {
394 found_frames.push_back(std::move(frame));
395 } else {
396 RTC_LOG(LS_ERROR) << "Failed to assemble frame from packets "
397 << start_seq_num << "-" << seq_num;
398 }
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100399
philipel2c9f9f22017-06-13 02:47:28 -0700400 missing_packets_.erase(missing_packets_.begin(),
401 missing_packets_.upper_bound(seq_num));
Johannes Krona3705562019-08-26 16:37:11 +0200402 ClearInterval(start_seq_num, seq_num);
philipelc707ab72016-04-01 02:01:54 -0700403 }
philipelc707ab72016-04-01 02:01:54 -0700404 ++seq_num;
405 }
philipelfd5a20f2016-11-15 00:57:57 -0800406 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700407}
408
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100409std::unique_ptr<RtpFrameObject> PacketBuffer::AssembleFrame(
philipelb5e47852019-09-20 11:30:12 +0200410 uint16_t first_seq_num,
411 uint16_t last_seq_num) {
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100412 const uint16_t end_seq_num = last_seq_num + 1;
413 const uint16_t num_packets = end_seq_num - first_seq_num;
414 int max_nack_count = -1;
415 int64_t min_recv_time = std::numeric_limits<int64_t>::max();
416 int64_t max_recv_time = std::numeric_limits<int64_t>::min();
417 size_t frame_size = 0;
philipelc707ab72016-04-01 02:01:54 -0700418
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100419 std::vector<rtc::ArrayView<const uint8_t>> payloads;
420 RtpPacketInfos::vector_type packet_infos;
421 payloads.reserve(num_packets);
422 packet_infos.reserve(num_packets);
philipel227f8b92017-08-04 06:39:31 -0700423
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100424 for (uint16_t seq_num = first_seq_num; seq_num != end_seq_num; ++seq_num) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100425 const Packet& packet = GetPacket(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700426
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100427 max_nack_count = std::max(max_nack_count, packet.times_nacked);
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100428 min_recv_time =
429 std::min(min_recv_time, packet.packet_info.receive_time_ms());
430 max_recv_time =
431 std::max(max_recv_time, packet.packet_info.receive_time_ms());
Danil Chapovalove3c48842019-12-02 15:54:27 +0100432 frame_size += packet.video_payload.size();
433 payloads.emplace_back(packet.video_payload);
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100434 packet_infos.push_back(packet.packet_info);
435 }
philipel227f8b92017-08-04 06:39:31 -0700436
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100437 const Packet& first_packet = GetPacket(first_seq_num);
Danil Chapovalov0682ca92019-11-28 16:50:02 +0100438 rtc::scoped_refptr<EncodedImageBuffer> bitstream;
439 // TODO(danilchap): Hide codec-specific code paths behind an interface.
440 if (first_packet.codec() == VideoCodecType::kVideoCodecAV1) {
441 bitstream = RtpDepacketizerAv1::AssembleFrame(payloads);
442 if (!bitstream) {
443 // Failed to assemble a frame. Discard and continue.
444 return nullptr;
445 }
446 } else {
447 bitstream = EncodedImageBuffer::Create(frame_size);
448
449 uint8_t* write_at = bitstream->data();
450 for (rtc::ArrayView<const uint8_t> payload : payloads) {
451 memcpy(write_at, payload.data(), payload.size());
452 write_at += payload.size();
453 }
454 RTC_DCHECK_EQ(write_at - bitstream->data(), bitstream->size());
455 }
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100456 const Packet& last_packet = GetPacket(last_seq_num);
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100457 return std::make_unique<RtpFrameObject>(
458 first_seq_num, //
459 last_seq_num, //
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100460 last_packet.marker_bit, //
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100461 max_nack_count, //
462 min_recv_time, //
463 max_recv_time, //
464 first_packet.timestamp, //
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100465 first_packet.ntp_time_ms, //
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100466 last_packet.video_header.video_timing, //
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100467 first_packet.payload_type, //
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100468 first_packet.codec(), //
469 last_packet.video_header.rotation, //
470 last_packet.video_header.content_type, //
471 first_packet.video_header, //
472 last_packet.video_header.color_space, //
473 first_packet.generic_descriptor, //
474 RtpPacketInfos(std::move(packet_infos)), //
475 std::move(bitstream));
philipelc707ab72016-04-01 02:01:54 -0700476}
477
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100478const PacketBuffer::Packet& PacketBuffer::GetPacket(uint16_t seq_num) const {
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100479 const StoredPacket& entry = buffer_[seq_num % buffer_.size()];
480 RTC_DCHECK(entry.used);
481 RTC_DCHECK_EQ(seq_num, entry.seq_num());
482 return entry.data;
philipelf4139332016-04-20 10:26:34 +0200483}
484
philipel2c9f9f22017-06-13 02:47:28 -0700485void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
486 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100487 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700488
489 const int kMaxPaddingAge = 1000;
490 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
491 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
492 auto erase_to = missing_packets_.lower_bound(old_seq_num);
493 missing_packets_.erase(missing_packets_.begin(), erase_to);
494
495 // Guard against inserting a large amount of missing packets if there is a
496 // jump in the sequence number.
497 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
498 *newest_inserted_seq_num_ = old_seq_num;
499
500 ++*newest_inserted_seq_num_;
501 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
502 missing_packets_.insert(*newest_inserted_seq_num_);
503 ++*newest_inserted_seq_num_;
504 }
505 } else {
506 missing_packets_.erase(seq_num);
507 }
508}
509
philipelc707ab72016-04-01 02:01:54 -0700510} // namespace video_coding
511} // namespace webrtc