blob: 6ebb9c4c9be30255cf04f1bd00e1cb2f82a23674 [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 Chapovalovaa3f5da2019-11-14 14:57:33 +010027#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
28#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Yves Gerey3e707812018-11-28 16:47:49 +010029#include "modules/rtp_rtcp/source/rtp_video_header.h"
Danil Chapovalovd06588a2020-01-10 17:26:53 +010030#include "modules/rtp_rtcp/source/video_rtp_depacketizer_av1.h"
Yves Gerey3e707812018-11-28 16:47:49 +010031#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(
Danil Chapovalov97ffbef2020-01-24 16:04:35 +010082 std::unique_ptr<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 Chapovalov97ffbef2020-01-24 16:04:35 +0100102 if (buffer_[index].used()) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200103 // 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.
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100109 while (ExpandBufferSize() && buffer_[seq_num % buffer_.size()].used()) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200110 }
111 index = seq_num % buffer_.size();
112
113 // Packet buffer is still full since we were unable to expand the buffer.
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100114 if (buffer_[index].used()) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200115 // 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;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100134 new_entry.packet = std::move(packet);
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100135
136 UpdateMissingPackets(seq_num);
137
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200138 result.frames = FindFrames(seq_num);
139 return result;
philipelc707ab72016-04-01 02:01:54 -0700140}
141
142void PacketBuffer::ClearTo(uint16_t seq_num) {
143 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700144 // We have already cleared past this sequence number, no need to do anything.
145 if (is_cleared_to_first_seq_num_ &&
146 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
147 return;
148 }
philipelaee3e0e2016-11-01 11:45:34 +0100149
150 // If the packet buffer was cleared between a frame was created and returned.
151 if (!first_packet_received_)
152 return;
153
philipelc5fb4682017-08-02 04:28:57 -0700154 // Avoid iterating over the buffer more than once by capping the number of
155 // iterations to the |size_| of the buffer.
156 ++seq_num;
157 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200158 size_t iterations = std::min(diff, buffer_.size());
philipelc5fb4682017-08-02 04:28:57 -0700159 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100160 StoredPacket& stored = buffer_[first_seq_num_ % buffer_.size()];
161 if (stored.used() && AheadOf<uint16_t>(seq_num, stored.seq_num())) {
162 stored.packet = nullptr;
philipelc5fb4682017-08-02 04:28:57 -0700163 }
philipelaee3e0e2016-11-01 11:45:34 +0100164 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700165 }
philipel2c9f9f22017-06-13 02:47:28 -0700166
philipelc5fb4682017-08-02 04:28:57 -0700167 // If |diff| is larger than |iterations| it means that we don't increment
168 // |first_seq_num_| until we reach |seq_num|, so we set it here.
169 first_seq_num_ = seq_num;
170
171 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100172 auto clear_to_it = missing_packets_.upper_bound(seq_num);
173 if (clear_to_it != missing_packets_.begin()) {
174 --clear_to_it;
175 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
176 }
philipelc707ab72016-04-01 02:01:54 -0700177}
178
Johannes Krona3705562019-08-26 16:37:11 +0200179void PacketBuffer::ClearInterval(uint16_t start_seq_num,
180 uint16_t stop_seq_num) {
181 size_t iterations = ForwardDiff<uint16_t>(start_seq_num, stop_seq_num + 1);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200182 RTC_DCHECK_LE(iterations, buffer_.size());
Johannes Krona3705562019-08-26 16:37:11 +0200183 uint16_t seq_num = start_seq_num;
184 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200185 size_t index = seq_num % buffer_.size();
186 RTC_DCHECK_EQ(buffer_[index].seq_num(), seq_num);
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100187 buffer_[index].packet = nullptr;
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 Chapovalov97ffbef2020-01-24 16:04:35 +0100196 entry.packet = nullptr;
philipelaee3e0e2016-11-01 11:45:34 +0100197 }
198
199 first_packet_received_ = false;
200 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700201 last_received_packet_ms_.reset();
202 last_received_keyframe_packet_ms_.reset();
203 newest_inserted_seq_num_.reset();
204 missing_packets_.clear();
205}
206
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200207PacketBuffer::InsertResult PacketBuffer::InsertPadding(uint16_t seq_num) {
208 PacketBuffer::InsertResult result;
209 rtc::CritScope lock(&crit_);
210 UpdateMissingPackets(seq_num);
211 result.frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
212 return result;
philipel3184f8e2017-05-18 08:08:53 -0700213}
214
Danil Chapovalov0040b662018-06-18 10:48:16 +0200215absl::optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700216 rtc::CritScope lock(&crit_);
217 return last_received_packet_ms_;
218}
219
Danil Chapovalov0040b662018-06-18 10:48:16 +0200220absl::optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700221 rtc::CritScope lock(&crit_);
222 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100223}
224
philipelc707ab72016-04-01 02:01:54 -0700225bool PacketBuffer::ExpandBufferSize() {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200226 if (buffer_.size() == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100227 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
Johannes Kronbd3f3052019-08-01 15:45:54 +0200228 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700229 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100230 }
philipelc707ab72016-04-01 02:01:54 -0700231
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200232 size_t new_size = std::min(max_size_, 2 * buffer_.size());
233 std::vector<StoredPacket> new_buffer(new_size);
234 for (StoredPacket& entry : buffer_) {
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100235 if (entry.used()) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100236 new_buffer[entry.seq_num() % new_size] = std::move(entry);
philipelc707ab72016-04-01 02:01:54 -0700237 }
238 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200239 buffer_ = std::move(new_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100240 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700241 return true;
242}
243
philipelaee3e0e2016-11-01 11:45:34 +0100244bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200245 size_t index = seq_num % buffer_.size();
246 int prev_index = index > 0 ? index - 1 : buffer_.size() - 1;
247 const StoredPacket& entry = buffer_[index];
248 const StoredPacket& prev_entry = buffer_[prev_index];
philipelf4139332016-04-20 10:26:34 +0200249
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100250 if (!entry.used())
philipelc707ab72016-04-01 02:01:54 -0700251 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200252 if (entry.seq_num() != seq_num)
philipel2c9f9f22017-06-13 02:47:28 -0700253 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200254 if (entry.frame_begin())
philipelc707ab72016-04-01 02:01:54 -0700255 return true;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100256 if (!prev_entry.used())
philipelc707ab72016-04-01 02:01:54 -0700257 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200258 if (prev_entry.seq_num() != static_cast<uint16_t>(entry.seq_num() - 1))
philipelea142f82017-01-11 02:01:56 -0800259 return false;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100260 if (prev_entry.packet->timestamp != entry.packet->timestamp)
philipelf4139332016-04-20 10:26:34 +0200261 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200262 if (prev_entry.continuous)
philipelc707ab72016-04-01 02:01:54 -0700263 return true;
264
265 return false;
266}
267
philipelfd5a20f2016-11-15 00:57:57 -0800268std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
269 uint16_t seq_num) {
270 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200271 for (size_t i = 0; i < buffer_.size() && PotentialNewFrame(seq_num); ++i) {
272 size_t index = seq_num % buffer_.size();
273 buffer_[index].continuous = true;
philipelc707ab72016-04-01 02:01:54 -0700274
philipelf4139332016-04-20 10:26:34 +0200275 // If all packets of the frame is continuous, find the first packet of the
276 // frame and create an RtpFrameObject.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200277 if (buffer_[index].frame_end()) {
philipelc707ab72016-04-01 02:01:54 -0700278 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200279
philipel5ceaaae2016-05-24 10:20:47 +0200280 // Find the start index by searching backward until the packet with
281 // the |frame_begin| flag is set.
282 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700283 size_t tested_packets = 0;
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100284 int64_t frame_timestamp = buffer_[start_index].packet->timestamp;
philipel53910712017-05-18 02:24:40 -0700285
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100286 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100287 bool is_h264 = buffer_[start_index].packet->codec() == kVideoCodecH264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100288 bool has_h264_sps = false;
289 bool has_h264_pps = false;
290 bool has_h264_idr = false;
291 bool is_h264_keyframe = false;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700292 int idr_width = -1;
293 int idr_height = -1;
philipel227f8b92017-08-04 06:39:31 -0700294 while (true) {
295 ++tested_packets;
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100296
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200297 if (!is_h264 && buffer_[start_index].frame_begin())
philipel5ceaaae2016-05-24 10:20:47 +0200298 break;
299
Shyam Sadhwani2b84dad2019-10-02 17:22:33 -0700300 if (is_h264) {
philipel7d745e52018-08-02 14:03:53 +0200301 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100302 &buffer_[start_index].packet->video_header.video_type_header);
philipel7d745e52018-08-02 14:03:53 +0200303 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200304 return found_frames;
305
philipel7d745e52018-08-02 14:03:53 +0200306 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
307 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100308 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200309 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100310 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200311 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100312 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700313 }
314 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100315 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
316 has_h264_pps) ||
317 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
318 is_h264_keyframe = true;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700319 // Store the resolution of key frame which is the packet with
320 // smallest index and valid resolution; typically its IDR or SPS
321 // packet; there may be packet preceeding this packet, IDR's
322 // resolution will be applied to them.
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100323 if (buffer_[start_index].packet->width() > 0 &&
324 buffer_[start_index].packet->height() > 0) {
325 idr_width = buffer_[start_index].packet->width();
326 idr_height = buffer_[start_index].packet->height();
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700327 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100328 }
philipel2c9f9f22017-06-13 02:47:28 -0700329 }
330
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200331 if (tested_packets == buffer_.size())
philipel227f8b92017-08-04 06:39:31 -0700332 break;
333
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200334 start_index = start_index > 0 ? start_index - 1 : buffer_.size() - 1;
philipel8c619242017-02-02 08:51:29 -0800335
336 // In the case of H264 we don't have a frame_begin bit (yes,
337 // |frame_begin| might be set to true but that is a lie). So instead
338 // we traverese backwards as long as we have a previous packet and
339 // the timestamp of that packet is the same as this one. This may cause
340 // the PacketBuffer to hand out incomplete frames.
341 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700342 if (is_h264 &&
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100343 (!buffer_[start_index].used() ||
344 buffer_[start_index].packet->timestamp != frame_timestamp)) {
philipel8c619242017-02-02 08:51:29 -0800345 break;
346 }
347
348 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700349 }
350
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100351 if (is_h264) {
352 // Warn if this is an unsafe frame.
353 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200354 RTC_LOG(LS_WARNING)
355 << "Received H.264-IDR frame "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100356 "(SPS: "
357 << has_h264_sps << ", PPS: " << has_h264_pps << "). Treating as "
Jonas Olssonfc501102018-06-15 14:24:10 +0200358 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
359 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
360 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700361 }
362
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100363 // Now that we have decided whether to treat this frame as a key frame
364 // or delta frame in the frame buffer, we update the field that
365 // determines if the RtpFrameObject is a key frame or delta frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200366 const size_t first_packet_index = start_seq_num % buffer_.size();
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100367 if (is_h264_keyframe) {
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100368 buffer_[first_packet_index].packet->video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100369 VideoFrameType::kVideoFrameKey;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700370 if (idr_width > 0 && idr_height > 0) {
371 // IDR frame was finalized and we have the correct resolution for
372 // IDR; update first packet to have same resolution as IDR.
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100373 buffer_[first_packet_index].packet->video_header.width = idr_width;
374 buffer_[first_packet_index].packet->video_header.height =
375 idr_height;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700376 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100377 } else {
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100378 buffer_[first_packet_index].packet->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 Chapovalov97ffbef2020-01-24 16:04:35 +0100385 buffer_[start_index].used()
386 ? buffer_[start_index]
387 .packet->video_header.frame_marking.temporal_id
388 : kNoTemporalIdx;
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) {
Danil Chapovalovd06588a2020-01-10 17:26:53 +0100444 bitstream = VideoRtpDepacketizerAv1::AssembleFrame(payloads);
Danil Chapovalov0682ca92019-11-28 16:50:02 +0100445 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()];
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100483 RTC_DCHECK(entry.used());
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100484 RTC_DCHECK_EQ(seq_num, entry.seq_num());
Danil Chapovalov97ffbef2020-01-24 16:04:35 +0100485 return *entry.packet;
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