blob: 30dfc21e41aacf08b192995245ee0aeeb0ac941e [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 Chapovalovaa3f5da2019-11-14 14:57:33 +010096 delete[] packet->data;
97 packet->data = nullptr;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020098 return result;
philipelc707ab72016-04-01 02:01:54 -070099 }
philipelc707ab72016-04-01 02:01:54 -0700100
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200101 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -0700102 }
103
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200104 if (buffer_[index].used) {
105 // Duplicate packet, just delete the payload.
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100106 if (buffer_[index].seq_num() == packet->seq_num) {
107 delete[] packet->data;
108 packet->data = nullptr;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200109 return result;
110 }
philipelc707ab72016-04-01 02:01:54 -0700111
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200112 // The packet buffer is full, try to expand the buffer.
113 while (ExpandBufferSize() && buffer_[seq_num % buffer_.size()].used) {
114 }
115 index = seq_num % buffer_.size();
116
117 // Packet buffer is still full since we were unable to expand the buffer.
118 if (buffer_[index].used) {
119 // Clear the buffer, delete payload, and return false to signal that a
120 // new keyframe is needed.
121 RTC_LOG(LS_WARNING) << "Clear PacketBuffer and request key frame.";
122 Clear();
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100123 delete[] packet->data;
124 packet->data = nullptr;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200125 result.buffer_cleared = true;
126 return result;
127 }
128 }
129
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200130 int64_t now_ms = clock_->TimeInMilliseconds();
131 last_received_packet_ms_ = now_ms;
132 if (packet->video_header.frame_type == VideoFrameType::kVideoFrameKey)
133 last_received_keyframe_packet_ms_ = now_ms;
134
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100135 StoredPacket& new_entry = buffer_[index];
136 new_entry.continuous = false;
137 new_entry.used = true;
138 new_entry.data = std::move(*packet);
139 packet->data = nullptr;
140
141 UpdateMissingPackets(seq_num);
142
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200143 result.frames = FindFrames(seq_num);
144 return result;
philipelc707ab72016-04-01 02:01:54 -0700145}
146
147void PacketBuffer::ClearTo(uint16_t seq_num) {
148 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700149 // We have already cleared past this sequence number, no need to do anything.
150 if (is_cleared_to_first_seq_num_ &&
151 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
152 return;
153 }
philipelaee3e0e2016-11-01 11:45:34 +0100154
155 // If the packet buffer was cleared between a frame was created and returned.
156 if (!first_packet_received_)
157 return;
158
philipelc5fb4682017-08-02 04:28:57 -0700159 // Avoid iterating over the buffer more than once by capping the number of
160 // iterations to the |size_| of the buffer.
161 ++seq_num;
162 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200163 size_t iterations = std::min(diff, buffer_.size());
philipelc5fb4682017-08-02 04:28:57 -0700164 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200165 size_t index = first_seq_num_ % buffer_.size();
166 if (AheadOf<uint16_t>(seq_num, buffer_[index].seq_num())) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100167 delete[] buffer_[index].data.data;
168 buffer_[index].data.data = nullptr;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200169 buffer_[index].used = false;
philipelc5fb4682017-08-02 04:28:57 -0700170 }
philipelaee3e0e2016-11-01 11:45:34 +0100171 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700172 }
philipel2c9f9f22017-06-13 02:47:28 -0700173
philipelc5fb4682017-08-02 04:28:57 -0700174 // If |diff| is larger than |iterations| it means that we don't increment
175 // |first_seq_num_| until we reach |seq_num|, so we set it here.
176 first_seq_num_ = seq_num;
177
178 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100179 auto clear_to_it = missing_packets_.upper_bound(seq_num);
180 if (clear_to_it != missing_packets_.begin()) {
181 --clear_to_it;
182 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
183 }
philipelc707ab72016-04-01 02:01:54 -0700184}
185
Johannes Krona3705562019-08-26 16:37:11 +0200186void PacketBuffer::ClearInterval(uint16_t start_seq_num,
187 uint16_t stop_seq_num) {
188 size_t iterations = ForwardDiff<uint16_t>(start_seq_num, stop_seq_num + 1);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200189 RTC_DCHECK_LE(iterations, buffer_.size());
Johannes Krona3705562019-08-26 16:37:11 +0200190 uint16_t seq_num = start_seq_num;
191 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200192 size_t index = seq_num % buffer_.size();
193 RTC_DCHECK_EQ(buffer_[index].seq_num(), seq_num);
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100194 delete[] buffer_[index].data.data;
195 buffer_[index].data.data = nullptr;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200196 buffer_[index].used = false;
Johannes Krona3705562019-08-26 16:37:11 +0200197
198 ++seq_num;
199 }
200}
201
philipelaee3e0e2016-11-01 11:45:34 +0100202void PacketBuffer::Clear() {
203 rtc::CritScope lock(&crit_);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200204 for (StoredPacket& entry : buffer_) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100205 delete[] entry.data.data;
206 entry.data.data = nullptr;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200207 entry.used = false;
philipelaee3e0e2016-11-01 11:45:34 +0100208 }
209
210 first_packet_received_ = false;
211 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700212 last_received_packet_ms_.reset();
213 last_received_keyframe_packet_ms_.reset();
214 newest_inserted_seq_num_.reset();
215 missing_packets_.clear();
216}
217
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200218PacketBuffer::InsertResult PacketBuffer::InsertPadding(uint16_t seq_num) {
219 PacketBuffer::InsertResult result;
220 rtc::CritScope lock(&crit_);
221 UpdateMissingPackets(seq_num);
222 result.frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
223 return result;
philipel3184f8e2017-05-18 08:08:53 -0700224}
225
Danil Chapovalov0040b662018-06-18 10:48:16 +0200226absl::optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700227 rtc::CritScope lock(&crit_);
228 return last_received_packet_ms_;
229}
230
Danil Chapovalov0040b662018-06-18 10:48:16 +0200231absl::optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700232 rtc::CritScope lock(&crit_);
233 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100234}
235
philipelc707ab72016-04-01 02:01:54 -0700236bool PacketBuffer::ExpandBufferSize() {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200237 if (buffer_.size() == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100238 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
Johannes Kronbd3f3052019-08-01 15:45:54 +0200239 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700240 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100241 }
philipelc707ab72016-04-01 02:01:54 -0700242
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200243 size_t new_size = std::min(max_size_, 2 * buffer_.size());
244 std::vector<StoredPacket> new_buffer(new_size);
245 for (StoredPacket& entry : buffer_) {
246 if (entry.used) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100247 new_buffer[entry.seq_num() % new_size] = std::move(entry);
philipelc707ab72016-04-01 02:01:54 -0700248 }
249 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200250 buffer_ = std::move(new_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100251 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700252 return true;
253}
254
philipelaee3e0e2016-11-01 11:45:34 +0100255bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200256 size_t index = seq_num % buffer_.size();
257 int prev_index = index > 0 ? index - 1 : buffer_.size() - 1;
258 const StoredPacket& entry = buffer_[index];
259 const StoredPacket& prev_entry = buffer_[prev_index];
philipelf4139332016-04-20 10:26:34 +0200260
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200261 if (!entry.used)
philipelc707ab72016-04-01 02:01:54 -0700262 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200263 if (entry.seq_num() != seq_num)
philipel2c9f9f22017-06-13 02:47:28 -0700264 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200265 if (entry.frame_begin())
philipelc707ab72016-04-01 02:01:54 -0700266 return true;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200267 if (!prev_entry.used)
philipelc707ab72016-04-01 02:01:54 -0700268 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200269 if (prev_entry.seq_num() != static_cast<uint16_t>(entry.seq_num() - 1))
philipelea142f82017-01-11 02:01:56 -0800270 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200271 if (prev_entry.data.timestamp != entry.data.timestamp)
philipelf4139332016-04-20 10:26:34 +0200272 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200273 if (prev_entry.continuous)
philipelc707ab72016-04-01 02:01:54 -0700274 return true;
275
276 return false;
277}
278
philipelfd5a20f2016-11-15 00:57:57 -0800279std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
280 uint16_t seq_num) {
281 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200282 for (size_t i = 0; i < buffer_.size() && PotentialNewFrame(seq_num); ++i) {
283 size_t index = seq_num % buffer_.size();
284 buffer_[index].continuous = true;
philipelc707ab72016-04-01 02:01:54 -0700285
philipelf4139332016-04-20 10:26:34 +0200286 // If all packets of the frame is continuous, find the first packet of the
287 // frame and create an RtpFrameObject.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200288 if (buffer_[index].frame_end()) {
philipelc707ab72016-04-01 02:01:54 -0700289 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200290
philipel5ceaaae2016-05-24 10:20:47 +0200291 // Find the start index by searching backward until the packet with
292 // the |frame_begin| flag is set.
293 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700294 size_t tested_packets = 0;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200295 int64_t frame_timestamp = buffer_[start_index].data.timestamp;
philipel53910712017-05-18 02:24:40 -0700296
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100297 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200298 bool is_h264 = buffer_[start_index].data.codec() == kVideoCodecH264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100299 bool has_h264_sps = false;
300 bool has_h264_pps = false;
301 bool has_h264_idr = false;
302 bool is_h264_keyframe = false;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700303 int idr_width = -1;
304 int idr_height = -1;
philipel227f8b92017-08-04 06:39:31 -0700305 while (true) {
306 ++tested_packets;
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100307
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200308 if (!is_h264 && buffer_[start_index].frame_begin())
philipel5ceaaae2016-05-24 10:20:47 +0200309 break;
310
Shyam Sadhwani2b84dad2019-10-02 17:22:33 -0700311 if (is_h264) {
philipel7d745e52018-08-02 14:03:53 +0200312 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200313 &buffer_[start_index].data.video_header.video_type_header);
philipel7d745e52018-08-02 14:03:53 +0200314 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200315 return found_frames;
316
philipel7d745e52018-08-02 14:03:53 +0200317 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
318 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100319 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200320 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100321 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200322 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100323 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700324 }
325 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100326 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
327 has_h264_pps) ||
328 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
329 is_h264_keyframe = true;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700330 // Store the resolution of key frame which is the packet with
331 // smallest index and valid resolution; typically its IDR or SPS
332 // packet; there may be packet preceeding this packet, IDR's
333 // resolution will be applied to them.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200334 if (buffer_[start_index].data.width() > 0 &&
335 buffer_[start_index].data.height() > 0) {
336 idr_width = buffer_[start_index].data.width();
337 idr_height = buffer_[start_index].data.height();
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700338 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100339 }
philipel2c9f9f22017-06-13 02:47:28 -0700340 }
341
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200342 if (tested_packets == buffer_.size())
philipel227f8b92017-08-04 06:39:31 -0700343 break;
344
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200345 start_index = start_index > 0 ? start_index - 1 : buffer_.size() - 1;
philipel8c619242017-02-02 08:51:29 -0800346
347 // In the case of H264 we don't have a frame_begin bit (yes,
348 // |frame_begin| might be set to true but that is a lie). So instead
349 // we traverese backwards as long as we have a previous packet and
350 // the timestamp of that packet is the same as this one. This may cause
351 // the PacketBuffer to hand out incomplete frames.
352 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700353 if (is_h264 &&
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200354 (!buffer_[start_index].used ||
355 buffer_[start_index].data.timestamp != frame_timestamp)) {
philipel8c619242017-02-02 08:51:29 -0800356 break;
357 }
358
359 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700360 }
361
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100362 if (is_h264) {
363 // Warn if this is an unsafe frame.
364 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200365 RTC_LOG(LS_WARNING)
366 << "Received H.264-IDR frame "
367 << "(SPS: " << has_h264_sps << ", PPS: " << has_h264_pps
368 << "). Treating as "
369 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
370 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
371 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700372 }
373
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100374 // Now that we have decided whether to treat this frame as a key frame
375 // or delta frame in the frame buffer, we update the field that
376 // determines if the RtpFrameObject is a key frame or delta frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200377 const size_t first_packet_index = start_seq_num % buffer_.size();
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100378 if (is_h264_keyframe) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200379 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100380 VideoFrameType::kVideoFrameKey;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700381 if (idr_width > 0 && idr_height > 0) {
382 // IDR frame was finalized and we have the correct resolution for
383 // IDR; update first packet to have same resolution as IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200384 buffer_[first_packet_index].data.video_header.width = idr_width;
385 buffer_[first_packet_index].data.video_header.height = idr_height;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700386 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100387 } else {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200388 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100389 VideoFrameType::kVideoFrameDelta;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100390 }
391
Johnny Leebc7f41b2019-05-01 14:41:32 -0400392 // With IPPP, if this is not a keyframe, make sure there are no gaps
393 // in the packet sequence numbers up until this point.
394 const uint8_t h264tid =
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200395 buffer_[start_index].data.video_header.frame_marking.temporal_id;
Jonas Olssona4d87372019-07-05 19:08:33 +0200396 if (h264tid == kNoTemporalIdx && !is_h264_keyframe &&
397 missing_packets_.upper_bound(start_seq_num) !=
398 missing_packets_.begin()) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100399 return found_frames;
400 }
philipel2c9f9f22017-06-13 02:47:28 -0700401 }
402
Danil Chapovalov0682ca92019-11-28 16:50:02 +0100403 if (auto frame = AssembleFrame(start_seq_num, seq_num)) {
404 found_frames.push_back(std::move(frame));
405 } else {
406 RTC_LOG(LS_ERROR) << "Failed to assemble frame from packets "
407 << start_seq_num << "-" << seq_num;
408 }
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100409
philipel2c9f9f22017-06-13 02:47:28 -0700410 missing_packets_.erase(missing_packets_.begin(),
411 missing_packets_.upper_bound(seq_num));
Johannes Krona3705562019-08-26 16:37:11 +0200412 ClearInterval(start_seq_num, seq_num);
philipelc707ab72016-04-01 02:01:54 -0700413 }
philipelc707ab72016-04-01 02:01:54 -0700414 ++seq_num;
415 }
philipelfd5a20f2016-11-15 00:57:57 -0800416 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700417}
418
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100419std::unique_ptr<RtpFrameObject> PacketBuffer::AssembleFrame(
philipelb5e47852019-09-20 11:30:12 +0200420 uint16_t first_seq_num,
421 uint16_t last_seq_num) {
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100422 const uint16_t end_seq_num = last_seq_num + 1;
423 const uint16_t num_packets = end_seq_num - first_seq_num;
424 int max_nack_count = -1;
425 int64_t min_recv_time = std::numeric_limits<int64_t>::max();
426 int64_t max_recv_time = std::numeric_limits<int64_t>::min();
427 size_t frame_size = 0;
philipelc707ab72016-04-01 02:01:54 -0700428
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100429 std::vector<rtc::ArrayView<const uint8_t>> payloads;
430 RtpPacketInfos::vector_type packet_infos;
431 payloads.reserve(num_packets);
432 packet_infos.reserve(num_packets);
philipel227f8b92017-08-04 06:39:31 -0700433
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100434 for (uint16_t seq_num = first_seq_num; seq_num != end_seq_num; ++seq_num) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100435 const Packet& packet = GetPacket(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700436
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100437 max_nack_count = std::max(max_nack_count, packet.times_nacked);
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100438 min_recv_time =
439 std::min(min_recv_time, packet.packet_info.receive_time_ms());
440 max_recv_time =
441 std::max(max_recv_time, packet.packet_info.receive_time_ms());
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100442 frame_size += packet.size_bytes;
443 payloads.emplace_back(packet.data, packet.size_bytes);
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100444 packet_infos.push_back(packet.packet_info);
445 }
philipel227f8b92017-08-04 06:39:31 -0700446
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100447 const Packet& first_packet = GetPacket(first_seq_num);
Danil Chapovalov0682ca92019-11-28 16:50:02 +0100448 rtc::scoped_refptr<EncodedImageBuffer> bitstream;
449 // TODO(danilchap): Hide codec-specific code paths behind an interface.
450 if (first_packet.codec() == VideoCodecType::kVideoCodecAV1) {
451 bitstream = RtpDepacketizerAv1::AssembleFrame(payloads);
452 if (!bitstream) {
453 // Failed to assemble a frame. Discard and continue.
454 return nullptr;
455 }
456 } else {
457 bitstream = EncodedImageBuffer::Create(frame_size);
458
459 uint8_t* write_at = bitstream->data();
460 for (rtc::ArrayView<const uint8_t> payload : payloads) {
461 memcpy(write_at, payload.data(), payload.size());
462 write_at += payload.size();
463 }
464 RTC_DCHECK_EQ(write_at - bitstream->data(), bitstream->size());
465 }
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100466 const Packet& last_packet = GetPacket(last_seq_num);
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100467 return std::make_unique<RtpFrameObject>(
468 first_seq_num, //
469 last_seq_num, //
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100470 last_packet.marker_bit, //
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100471 max_nack_count, //
472 min_recv_time, //
473 max_recv_time, //
474 first_packet.timestamp, //
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100475 first_packet.ntp_time_ms, //
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100476 last_packet.video_header.video_timing, //
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100477 first_packet.payload_type, //
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100478 first_packet.codec(), //
479 last_packet.video_header.rotation, //
480 last_packet.video_header.content_type, //
481 first_packet.video_header, //
482 last_packet.video_header.color_space, //
483 first_packet.generic_descriptor, //
484 RtpPacketInfos(std::move(packet_infos)), //
485 std::move(bitstream));
philipelc707ab72016-04-01 02:01:54 -0700486}
487
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100488const PacketBuffer::Packet& PacketBuffer::GetPacket(uint16_t seq_num) const {
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100489 const StoredPacket& entry = buffer_[seq_num % buffer_.size()];
490 RTC_DCHECK(entry.used);
491 RTC_DCHECK_EQ(seq_num, entry.seq_num());
492 return entry.data;
philipelf4139332016-04-20 10:26:34 +0200493}
494
philipel2c9f9f22017-06-13 02:47:28 -0700495void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
496 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100497 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700498
499 const int kMaxPaddingAge = 1000;
500 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
501 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
502 auto erase_to = missing_packets_.lower_bound(old_seq_num);
503 missing_packets_.erase(missing_packets_.begin(), erase_to);
504
505 // Guard against inserting a large amount of missing packets if there is a
506 // jump in the sequence number.
507 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
508 *newest_inserted_seq_num_ = old_seq_num;
509
510 ++*newest_inserted_seq_num_;
511 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
512 missing_packets_.insert(*newest_inserted_seq_num_);
513 ++*newest_inserted_seq_num_;
514 }
515 } else {
516 missing_packets_.erase(seq_num);
517 }
518}
519
philipelc707ab72016-04-01 02:01:54 -0700520} // namespace video_coding
521} // namespace webrtc