blob: 3e8b11f4fdc443dd70c05da061d0df142929ff91 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "common_video/h264/h264_common.h"
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010026#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
27#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "modules/rtp_rtcp/source/rtp_video_header.h"
29#include "modules/video_coding/codecs/h264/include/h264_globals.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/video_coding/frame_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/checks.h"
32#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010033#include "rtc_base/numerics/mod_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "system_wrappers/include/clock.h"
Rasmus Brandt88f080a2017-11-02 14:28:06 +010035#include "system_wrappers/include/field_trial.h"
philipelc707ab72016-04-01 02:01:54 -070036
37namespace webrtc {
38namespace video_coding {
39
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010040PacketBuffer::Packet::Packet(const RtpPacketReceived& rtp_packet,
41 const RTPVideoHeader& video_header,
42 int64_t ntp_time_ms,
43 int64_t receive_time_ms)
44 : marker_bit(rtp_packet.Marker()),
45 payload_type(rtp_packet.PayloadType()),
46 seq_num(rtp_packet.SequenceNumber()),
47 timestamp(rtp_packet.Timestamp()),
48 ntp_time_ms(ntp_time_ms),
49 times_nacked(-1),
50 video_header(video_header),
51 packet_info(rtp_packet.Ssrc(),
52 rtp_packet.Csrcs(),
53 rtp_packet.Timestamp(),
54 /*audio_level=*/absl::nullopt,
55 rtp_packet.GetExtension<AbsoluteCaptureTimeExtension>(),
56 receive_time_ms) {}
57
philipelb4d31082016-07-11 08:46:29 -070058PacketBuffer::PacketBuffer(Clock* clock,
59 size_t start_buffer_size,
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020060 size_t max_buffer_size)
philipelb4d31082016-07-11 08:46:29 -070061 : clock_(clock),
philipelc707ab72016-04-01 02:01:54 -070062 max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070063 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020064 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010065 is_cleared_to_first_seq_num_(false),
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020066 buffer_(start_buffer_size),
Rasmus Brandt88f080a2017-11-02 14:28:06 +010067 sps_pps_idr_is_h264_keyframe_(
68 field_trial::IsEnabled("WebRTC-SpsPpsIdrIsH264Keyframe")) {
philipelc707ab72016-04-01 02:01:54 -070069 RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
70 // Buffer size must always be a power of 2.
71 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
72 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
73}
74
philipelaee3e0e2016-11-01 11:45:34 +010075PacketBuffer::~PacketBuffer() {
76 Clear();
77}
philipel17deeb42016-08-11 15:09:26 +020078
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010079PacketBuffer::InsertResult PacketBuffer::InsertPacket(
80 PacketBuffer::Packet* packet) {
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020081 PacketBuffer::InsertResult result;
82 rtc::CritScope lock(&crit_);
philipel3184f8e2017-05-18 08:08:53 -070083
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010084 uint16_t seq_num = packet->seq_num;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020085 size_t index = seq_num % buffer_.size();
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010086
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +020087 if (!first_packet_received_) {
88 first_seq_num_ = seq_num;
89 first_packet_received_ = true;
90 } else if (AheadOf(first_seq_num_, seq_num)) {
91 // If we have explicitly cleared past this packet then it's old,
92 // don't insert it, just silently ignore it.
93 if (is_cleared_to_first_seq_num_) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +010094 delete[] packet->data;
95 packet->data = nullptr;
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) {
105 delete[] packet->data;
106 packet->data = nullptr;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200107 return result;
108 }
philipelc707ab72016-04-01 02:01:54 -0700109
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200110 // The packet buffer is full, try to expand the buffer.
111 while (ExpandBufferSize() && buffer_[seq_num % buffer_.size()].used) {
112 }
113 index = seq_num % buffer_.size();
114
115 // Packet buffer is still full since we were unable to expand the buffer.
116 if (buffer_[index].used) {
117 // Clear the buffer, delete payload, and return false to signal that a
118 // new keyframe is needed.
119 RTC_LOG(LS_WARNING) << "Clear PacketBuffer and request key frame.";
120 Clear();
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100121 delete[] packet->data;
122 packet->data = nullptr;
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200123 result.buffer_cleared = true;
124 return result;
125 }
126 }
127
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200128 int64_t now_ms = clock_->TimeInMilliseconds();
129 last_received_packet_ms_ = now_ms;
130 if (packet->video_header.frame_type == VideoFrameType::kVideoFrameKey)
131 last_received_keyframe_packet_ms_ = now_ms;
132
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100133 StoredPacket& new_entry = buffer_[index];
134 new_entry.continuous = false;
135 new_entry.used = true;
136 new_entry.data = std::move(*packet);
137 packet->data = nullptr;
138
139 UpdateMissingPackets(seq_num);
140
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200141 result.frames = FindFrames(seq_num);
142 return result;
philipelc707ab72016-04-01 02:01:54 -0700143}
144
145void PacketBuffer::ClearTo(uint16_t seq_num) {
146 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700147 // We have already cleared past this sequence number, no need to do anything.
148 if (is_cleared_to_first_seq_num_ &&
149 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
150 return;
151 }
philipelaee3e0e2016-11-01 11:45:34 +0100152
153 // If the packet buffer was cleared between a frame was created and returned.
154 if (!first_packet_received_)
155 return;
156
philipelc5fb4682017-08-02 04:28:57 -0700157 // Avoid iterating over the buffer more than once by capping the number of
158 // iterations to the |size_| of the buffer.
159 ++seq_num;
160 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200161 size_t iterations = std::min(diff, buffer_.size());
philipelc5fb4682017-08-02 04:28:57 -0700162 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200163 size_t index = first_seq_num_ % buffer_.size();
164 if (AheadOf<uint16_t>(seq_num, buffer_[index].seq_num())) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100165 delete[] buffer_[index].data.data;
166 buffer_[index].data.data = nullptr;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200167 buffer_[index].used = false;
philipelc5fb4682017-08-02 04:28:57 -0700168 }
philipelaee3e0e2016-11-01 11:45:34 +0100169 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700170 }
philipel2c9f9f22017-06-13 02:47:28 -0700171
philipelc5fb4682017-08-02 04:28:57 -0700172 // If |diff| is larger than |iterations| it means that we don't increment
173 // |first_seq_num_| until we reach |seq_num|, so we set it here.
174 first_seq_num_ = seq_num;
175
176 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100177 auto clear_to_it = missing_packets_.upper_bound(seq_num);
178 if (clear_to_it != missing_packets_.begin()) {
179 --clear_to_it;
180 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
181 }
philipelc707ab72016-04-01 02:01:54 -0700182}
183
Johannes Krona3705562019-08-26 16:37:11 +0200184void PacketBuffer::ClearInterval(uint16_t start_seq_num,
185 uint16_t stop_seq_num) {
186 size_t iterations = ForwardDiff<uint16_t>(start_seq_num, stop_seq_num + 1);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200187 RTC_DCHECK_LE(iterations, buffer_.size());
Johannes Krona3705562019-08-26 16:37:11 +0200188 uint16_t seq_num = start_seq_num;
189 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200190 size_t index = seq_num % buffer_.size();
191 RTC_DCHECK_EQ(buffer_[index].seq_num(), seq_num);
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100192 delete[] buffer_[index].data.data;
193 buffer_[index].data.data = nullptr;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200194 buffer_[index].used = false;
Johannes Krona3705562019-08-26 16:37:11 +0200195
196 ++seq_num;
197 }
198}
199
philipelaee3e0e2016-11-01 11:45:34 +0100200void PacketBuffer::Clear() {
201 rtc::CritScope lock(&crit_);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200202 for (StoredPacket& entry : buffer_) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100203 delete[] entry.data.data;
204 entry.data.data = nullptr;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200205 entry.used = false;
philipelaee3e0e2016-11-01 11:45:34 +0100206 }
207
208 first_packet_received_ = false;
209 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700210 last_received_packet_ms_.reset();
211 last_received_keyframe_packet_ms_.reset();
212 newest_inserted_seq_num_.reset();
213 missing_packets_.clear();
214}
215
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200216PacketBuffer::InsertResult PacketBuffer::InsertPadding(uint16_t seq_num) {
217 PacketBuffer::InsertResult result;
218 rtc::CritScope lock(&crit_);
219 UpdateMissingPackets(seq_num);
220 result.frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
221 return result;
philipel3184f8e2017-05-18 08:08:53 -0700222}
223
Danil Chapovalov0040b662018-06-18 10:48:16 +0200224absl::optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700225 rtc::CritScope lock(&crit_);
226 return last_received_packet_ms_;
227}
228
Danil Chapovalov0040b662018-06-18 10:48:16 +0200229absl::optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700230 rtc::CritScope lock(&crit_);
231 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100232}
233
philipelc707ab72016-04-01 02:01:54 -0700234bool PacketBuffer::ExpandBufferSize() {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200235 if (buffer_.size() == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100236 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
Johannes Kronbd3f3052019-08-01 15:45:54 +0200237 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700238 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100239 }
philipelc707ab72016-04-01 02:01:54 -0700240
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200241 size_t new_size = std::min(max_size_, 2 * buffer_.size());
242 std::vector<StoredPacket> new_buffer(new_size);
243 for (StoredPacket& entry : buffer_) {
244 if (entry.used) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100245 new_buffer[entry.seq_num() % new_size] = std::move(entry);
philipelc707ab72016-04-01 02:01:54 -0700246 }
247 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200248 buffer_ = std::move(new_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100249 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700250 return true;
251}
252
philipelaee3e0e2016-11-01 11:45:34 +0100253bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200254 size_t index = seq_num % buffer_.size();
255 int prev_index = index > 0 ? index - 1 : buffer_.size() - 1;
256 const StoredPacket& entry = buffer_[index];
257 const StoredPacket& prev_entry = buffer_[prev_index];
philipelf4139332016-04-20 10:26:34 +0200258
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200259 if (!entry.used)
philipelc707ab72016-04-01 02:01:54 -0700260 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200261 if (entry.seq_num() != seq_num)
philipel2c9f9f22017-06-13 02:47:28 -0700262 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200263 if (entry.frame_begin())
philipelc707ab72016-04-01 02:01:54 -0700264 return true;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200265 if (!prev_entry.used)
philipelc707ab72016-04-01 02:01:54 -0700266 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200267 if (prev_entry.seq_num() != static_cast<uint16_t>(entry.seq_num() - 1))
philipelea142f82017-01-11 02:01:56 -0800268 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200269 if (prev_entry.data.timestamp != entry.data.timestamp)
philipelf4139332016-04-20 10:26:34 +0200270 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200271 if (prev_entry.continuous)
philipelc707ab72016-04-01 02:01:54 -0700272 return true;
273
274 return false;
275}
276
philipelfd5a20f2016-11-15 00:57:57 -0800277std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
278 uint16_t seq_num) {
279 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200280 for (size_t i = 0; i < buffer_.size() && PotentialNewFrame(seq_num); ++i) {
281 size_t index = seq_num % buffer_.size();
282 buffer_[index].continuous = true;
philipelc707ab72016-04-01 02:01:54 -0700283
philipelf4139332016-04-20 10:26:34 +0200284 // If all packets of the frame is continuous, find the first packet of the
285 // frame and create an RtpFrameObject.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200286 if (buffer_[index].frame_end()) {
philipelc707ab72016-04-01 02:01:54 -0700287 uint16_t start_seq_num = seq_num;
philipelf4139332016-04-20 10:26:34 +0200288
philipel5ceaaae2016-05-24 10:20:47 +0200289 // Find the start index by searching backward until the packet with
290 // the |frame_begin| flag is set.
291 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700292 size_t tested_packets = 0;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200293 int64_t frame_timestamp = buffer_[start_index].data.timestamp;
philipel53910712017-05-18 02:24:40 -0700294
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100295 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200296 bool is_h264 = buffer_[start_index].data.codec() == kVideoCodecH264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100297 bool has_h264_sps = false;
298 bool has_h264_pps = false;
299 bool has_h264_idr = false;
300 bool is_h264_keyframe = false;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700301 int idr_width = -1;
302 int idr_height = -1;
philipel227f8b92017-08-04 06:39:31 -0700303 while (true) {
304 ++tested_packets;
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100305
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200306 if (!is_h264 && buffer_[start_index].frame_begin())
philipel5ceaaae2016-05-24 10:20:47 +0200307 break;
308
Shyam Sadhwani2b84dad2019-10-02 17:22:33 -0700309 if (is_h264) {
philipel7d745e52018-08-02 14:03:53 +0200310 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200311 &buffer_[start_index].data.video_header.video_type_header);
philipel7d745e52018-08-02 14:03:53 +0200312 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200313 return found_frames;
314
philipel7d745e52018-08-02 14:03:53 +0200315 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
316 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100317 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200318 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100319 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200320 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100321 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700322 }
323 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100324 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
325 has_h264_pps) ||
326 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
327 is_h264_keyframe = true;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700328 // Store the resolution of key frame which is the packet with
329 // smallest index and valid resolution; typically its IDR or SPS
330 // packet; there may be packet preceeding this packet, IDR's
331 // resolution will be applied to them.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200332 if (buffer_[start_index].data.width() > 0 &&
333 buffer_[start_index].data.height() > 0) {
334 idr_width = buffer_[start_index].data.width();
335 idr_height = buffer_[start_index].data.height();
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700336 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100337 }
philipel2c9f9f22017-06-13 02:47:28 -0700338 }
339
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200340 if (tested_packets == buffer_.size())
philipel227f8b92017-08-04 06:39:31 -0700341 break;
342
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200343 start_index = start_index > 0 ? start_index - 1 : buffer_.size() - 1;
philipel8c619242017-02-02 08:51:29 -0800344
345 // In the case of H264 we don't have a frame_begin bit (yes,
346 // |frame_begin| might be set to true but that is a lie). So instead
347 // we traverese backwards as long as we have a previous packet and
348 // the timestamp of that packet is the same as this one. This may cause
349 // the PacketBuffer to hand out incomplete frames.
350 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700351 if (is_h264 &&
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200352 (!buffer_[start_index].used ||
353 buffer_[start_index].data.timestamp != frame_timestamp)) {
philipel8c619242017-02-02 08:51:29 -0800354 break;
355 }
356
357 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700358 }
359
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100360 if (is_h264) {
361 // Warn if this is an unsafe frame.
362 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200363 RTC_LOG(LS_WARNING)
364 << "Received H.264-IDR frame "
365 << "(SPS: " << has_h264_sps << ", PPS: " << has_h264_pps
366 << "). Treating as "
367 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
368 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
369 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700370 }
371
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100372 // Now that we have decided whether to treat this frame as a key frame
373 // or delta frame in the frame buffer, we update the field that
374 // determines if the RtpFrameObject is a key frame or delta frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200375 const size_t first_packet_index = start_seq_num % buffer_.size();
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100376 if (is_h264_keyframe) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200377 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100378 VideoFrameType::kVideoFrameKey;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700379 if (idr_width > 0 && idr_height > 0) {
380 // IDR frame was finalized and we have the correct resolution for
381 // IDR; update first packet to have same resolution as IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200382 buffer_[first_packet_index].data.video_header.width = idr_width;
383 buffer_[first_packet_index].data.video_header.height = idr_height;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700384 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100385 } else {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200386 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100387 VideoFrameType::kVideoFrameDelta;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100388 }
389
Johnny Leebc7f41b2019-05-01 14:41:32 -0400390 // With IPPP, if this is not a keyframe, make sure there are no gaps
391 // in the packet sequence numbers up until this point.
392 const uint8_t h264tid =
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200393 buffer_[start_index].data.video_header.frame_marking.temporal_id;
Jonas Olssona4d87372019-07-05 19:08:33 +0200394 if (h264tid == kNoTemporalIdx && !is_h264_keyframe &&
395 missing_packets_.upper_bound(start_seq_num) !=
396 missing_packets_.begin()) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100397 return found_frames;
398 }
philipel2c9f9f22017-06-13 02:47:28 -0700399 }
400
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100401 found_frames.push_back(AssembleFrame(start_seq_num, seq_num));
402
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 Chapovalovaa3f5da2019-11-14 14:57:33 +0100435 frame_size += packet.size_bytes;
436 payloads.emplace_back(packet.data, packet.size_bytes);
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100437 packet_infos.push_back(packet.packet_info);
438 }
philipel227f8b92017-08-04 06:39:31 -0700439
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100440 auto bitstream = EncodedImageBuffer::Create(frame_size);
philipel227f8b92017-08-04 06:39:31 -0700441
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100442 uint8_t* write_at = bitstream->data();
443 for (rtc::ArrayView<const uint8_t> payload : payloads) {
444 memcpy(write_at, payload.data(), payload.size());
445 write_at += payload.size();
446 }
447 RTC_DCHECK_EQ(write_at - bitstream->data(), bitstream->size());
448
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100449 const Packet& first_packet = GetPacket(first_seq_num);
450 const Packet& last_packet = GetPacket(last_seq_num);
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100451 return std::make_unique<RtpFrameObject>(
452 first_seq_num, //
453 last_seq_num, //
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100454 last_packet.marker_bit, //
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100455 max_nack_count, //
456 min_recv_time, //
457 max_recv_time, //
458 first_packet.timestamp, //
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100459 first_packet.ntp_time_ms, //
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100460 last_packet.video_header.video_timing, //
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100461 first_packet.payload_type, //
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100462 first_packet.codec(), //
463 last_packet.video_header.rotation, //
464 last_packet.video_header.content_type, //
465 first_packet.video_header, //
466 last_packet.video_header.color_space, //
467 first_packet.generic_descriptor, //
468 RtpPacketInfos(std::move(packet_infos)), //
469 std::move(bitstream));
philipelc707ab72016-04-01 02:01:54 -0700470}
471
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100472const PacketBuffer::Packet& PacketBuffer::GetPacket(uint16_t seq_num) const {
Danil Chapovalov3527a4f2019-11-08 17:30:29 +0100473 const StoredPacket& entry = buffer_[seq_num % buffer_.size()];
474 RTC_DCHECK(entry.used);
475 RTC_DCHECK_EQ(seq_num, entry.seq_num());
476 return entry.data;
philipelf4139332016-04-20 10:26:34 +0200477}
478
philipel2c9f9f22017-06-13 02:47:28 -0700479void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
480 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100481 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700482
483 const int kMaxPaddingAge = 1000;
484 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
485 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
486 auto erase_to = missing_packets_.lower_bound(old_seq_num);
487 missing_packets_.erase(missing_packets_.begin(), erase_to);
488
489 // Guard against inserting a large amount of missing packets if there is a
490 // jump in the sequence number.
491 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
492 *newest_inserted_seq_num_ = old_seq_num;
493
494 ++*newest_inserted_seq_num_;
495 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
496 missing_packets_.insert(*newest_inserted_seq_num_);
497 ++*newest_inserted_seq_num_;
498 }
499 } else {
500 missing_packets_.erase(seq_num);
501 }
502}
503
philipelc707ab72016-04-01 02:01:54 -0700504} // namespace video_coding
505} // namespace webrtc