blob: 92f39ed2996d860929f3ca4b5ec559d600342d73 [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>
philipel17deeb42016-08-11 15:09:26 +020017#include <utility>
philipelc707ab72016-04-01 02:01:54 -070018
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "absl/types/variant.h"
20#include "api/video/encoded_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "common_video/h264/h264_common.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "modules/rtp_rtcp/source/rtp_video_header.h"
23#include "modules/video_coding/codecs/h264/include/h264_globals.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/video_coding/frame_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/checks.h"
26#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010027#include "rtc_base/numerics/mod_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "system_wrappers/include/clock.h"
Rasmus Brandt88f080a2017-11-02 14:28:06 +010029#include "system_wrappers/include/field_trial.h"
philipelc707ab72016-04-01 02:01:54 -070030
31namespace webrtc {
32namespace video_coding {
33
philipelb4d31082016-07-11 08:46:29 -070034PacketBuffer::PacketBuffer(Clock* clock,
35 size_t start_buffer_size,
philipelc707ab72016-04-01 02:01:54 -070036 size_t max_buffer_size,
Elad Alonb4643ad2019-02-22 11:19:50 +010037 OnAssembledFrameCallback* assembled_frame_callback)
philipelb4d31082016-07-11 08:46:29 -070038 : clock_(clock),
philipelc707ab72016-04-01 02:01:54 -070039 max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070040 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020041 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010042 is_cleared_to_first_seq_num_(false),
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020043 buffer_(start_buffer_size),
Elad Alonb4643ad2019-02-22 11:19:50 +010044 assembled_frame_callback_(assembled_frame_callback),
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010045 unique_frames_seen_(0),
Rasmus Brandt88f080a2017-11-02 14:28:06 +010046 sps_pps_idr_is_h264_keyframe_(
47 field_trial::IsEnabled("WebRTC-SpsPpsIdrIsH264Keyframe")) {
philipelc707ab72016-04-01 02:01:54 -070048 RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
49 // Buffer size must always be a power of 2.
50 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
51 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
52}
53
philipelaee3e0e2016-11-01 11:45:34 +010054PacketBuffer::~PacketBuffer() {
55 Clear();
56}
philipel17deeb42016-08-11 15:09:26 +020057
philipel759e0b72016-11-30 01:32:05 -080058bool PacketBuffer::InsertPacket(VCMPacket* packet) {
philipelfd5a20f2016-11-15 00:57:57 -080059 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
60 {
61 rtc::CritScope lock(&crit_);
philipel3184f8e2017-05-18 08:08:53 -070062
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010063 OnTimestampReceived(packet->timestamp);
64
philipel759e0b72016-11-30 01:32:05 -080065 uint16_t seq_num = packet->seqNum;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020066 size_t index = seq_num % buffer_.size();
philipelc707ab72016-04-01 02:01:54 -070067
philipelfd5a20f2016-11-15 00:57:57 -080068 if (!first_packet_received_) {
69 first_seq_num_ = seq_num;
philipelfd5a20f2016-11-15 00:57:57 -080070 first_packet_received_ = true;
71 } else if (AheadOf(first_seq_num_, seq_num)) {
72 // If we have explicitly cleared past this packet then it's old,
Johannes Kronbd3f3052019-08-01 15:45:54 +020073 // don't insert it, just silently ignore it.
philipel759e0b72016-11-30 01:32:05 -080074 if (is_cleared_to_first_seq_num_) {
75 delete[] packet->dataPtr;
76 packet->dataPtr = nullptr;
Johannes Kronbd3f3052019-08-01 15:45:54 +020077 return true;
philipel759e0b72016-11-30 01:32:05 -080078 }
philipelaee3e0e2016-11-01 11:45:34 +010079
philipelfd5a20f2016-11-15 00:57:57 -080080 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070081 }
philipelc707ab72016-04-01 02:01:54 -070082
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020083 if (buffer_[index].used) {
philipel759e0b72016-11-30 01:32:05 -080084 // Duplicate packet, just delete the payload.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020085 if (buffer_[index].seq_num() == packet->seqNum) {
philipel759e0b72016-11-30 01:32:05 -080086 delete[] packet->dataPtr;
87 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -080088 return true;
philipel759e0b72016-11-30 01:32:05 -080089 }
philipelfd5a20f2016-11-15 00:57:57 -080090
91 // The packet buffer is full, try to expand the buffer.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020092 while (ExpandBufferSize() && buffer_[seq_num % buffer_.size()].used) {
philipelfd5a20f2016-11-15 00:57:57 -080093 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020094 index = seq_num % buffer_.size();
philipelfd5a20f2016-11-15 00:57:57 -080095
Johannes Kronbd3f3052019-08-01 15:45:54 +020096 // Packet buffer is still full since we were unable to expand the buffer.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +020097 if (buffer_[index].used) {
Johannes Kronbd3f3052019-08-01 15:45:54 +020098 // Clear the buffer, delete payload, and return false to signal that a
99 // new keyframe is needed.
100 RTC_LOG(LS_WARNING) << "Clear PacketBuffer and request key frame.";
101 Clear();
philipel759e0b72016-11-30 01:32:05 -0800102 delete[] packet->dataPtr;
103 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800104 return false;
philipel759e0b72016-11-30 01:32:05 -0800105 }
philipelfd5a20f2016-11-15 00:57:57 -0800106 }
107
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200108 StoredPacket& new_entry = buffer_[index];
109 new_entry.continuous = false;
110 new_entry.used = true;
111 new_entry.data = *packet;
philipel759e0b72016-11-30 01:32:05 -0800112 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800113
philipel2c9f9f22017-06-13 02:47:28 -0700114 UpdateMissingPackets(packet->seqNum);
115
philipel3184f8e2017-05-18 08:08:53 -0700116 int64_t now_ms = clock_->TimeInMilliseconds();
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100117 last_received_packet_ms_ = now_ms;
Niels Möllerabbc50e2019-04-24 09:41:16 +0200118 if (packet->video_header.frame_type == VideoFrameType::kVideoFrameKey)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100119 last_received_keyframe_packet_ms_ = now_ms;
philipel3184f8e2017-05-18 08:08:53 -0700120
philipelfd5a20f2016-11-15 00:57:57 -0800121 found_frames = FindFrames(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700122 }
123
philipelfd5a20f2016-11-15 00:57:57 -0800124 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
Elad Alonb4643ad2019-02-22 11:19:50 +0100125 assembled_frame_callback_->OnAssembledFrame(std::move(frame));
philipelc707ab72016-04-01 02:01:54 -0700126
philipelc707ab72016-04-01 02:01:54 -0700127 return true;
128}
129
130void PacketBuffer::ClearTo(uint16_t seq_num) {
131 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700132 // We have already cleared past this sequence number, no need to do anything.
133 if (is_cleared_to_first_seq_num_ &&
134 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
135 return;
136 }
philipelaee3e0e2016-11-01 11:45:34 +0100137
138 // If the packet buffer was cleared between a frame was created and returned.
139 if (!first_packet_received_)
140 return;
141
philipelc5fb4682017-08-02 04:28:57 -0700142 // Avoid iterating over the buffer more than once by capping the number of
143 // iterations to the |size_| of the buffer.
144 ++seq_num;
145 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200146 size_t iterations = std::min(diff, buffer_.size());
philipelc5fb4682017-08-02 04:28:57 -0700147 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200148 size_t index = first_seq_num_ % buffer_.size();
149 if (AheadOf<uint16_t>(seq_num, buffer_[index].seq_num())) {
150 delete[] buffer_[index].data.dataPtr;
151 buffer_[index].data.dataPtr = nullptr;
152 buffer_[index].used = false;
philipelc5fb4682017-08-02 04:28:57 -0700153 }
philipelaee3e0e2016-11-01 11:45:34 +0100154 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700155 }
philipel2c9f9f22017-06-13 02:47:28 -0700156
philipelc5fb4682017-08-02 04:28:57 -0700157 // If |diff| is larger than |iterations| it means that we don't increment
158 // |first_seq_num_| until we reach |seq_num|, so we set it here.
159 first_seq_num_ = seq_num;
160
161 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100162 auto clear_to_it = missing_packets_.upper_bound(seq_num);
163 if (clear_to_it != missing_packets_.begin()) {
164 --clear_to_it;
165 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
166 }
philipelc707ab72016-04-01 02:01:54 -0700167}
168
Johannes Krona3705562019-08-26 16:37:11 +0200169void PacketBuffer::ClearInterval(uint16_t start_seq_num,
170 uint16_t stop_seq_num) {
171 size_t iterations = ForwardDiff<uint16_t>(start_seq_num, stop_seq_num + 1);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200172 RTC_DCHECK_LE(iterations, buffer_.size());
Johannes Krona3705562019-08-26 16:37:11 +0200173 uint16_t seq_num = start_seq_num;
174 for (size_t i = 0; i < iterations; ++i) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200175 size_t index = seq_num % buffer_.size();
176 RTC_DCHECK_EQ(buffer_[index].seq_num(), seq_num);
177 delete[] buffer_[index].data.dataPtr;
178 buffer_[index].data.dataPtr = nullptr;
179 buffer_[index].used = false;
Johannes Krona3705562019-08-26 16:37:11 +0200180
181 ++seq_num;
182 }
183}
184
philipelaee3e0e2016-11-01 11:45:34 +0100185void PacketBuffer::Clear() {
186 rtc::CritScope lock(&crit_);
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200187 for (StoredPacket& entry : buffer_) {
188 delete[] entry.data.dataPtr;
189 entry.data.dataPtr = nullptr;
190 entry.used = false;
philipelaee3e0e2016-11-01 11:45:34 +0100191 }
192
193 first_packet_received_ = false;
194 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700195 last_received_packet_ms_.reset();
196 last_received_keyframe_packet_ms_.reset();
197 newest_inserted_seq_num_.reset();
198 missing_packets_.clear();
199}
200
201void PacketBuffer::PaddingReceived(uint16_t seq_num) {
202 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
203 {
204 rtc::CritScope lock(&crit_);
205 UpdateMissingPackets(seq_num);
206 found_frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
207 }
208
209 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
Elad Alonb4643ad2019-02-22 11:19:50 +0100210 assembled_frame_callback_->OnAssembledFrame(std::move(frame));
philipel3184f8e2017-05-18 08:08:53 -0700211}
212
Danil Chapovalov0040b662018-06-18 10:48:16 +0200213absl::optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700214 rtc::CritScope lock(&crit_);
215 return last_received_packet_ms_;
216}
217
Danil Chapovalov0040b662018-06-18 10:48:16 +0200218absl::optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700219 rtc::CritScope lock(&crit_);
220 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100221}
222
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100223int PacketBuffer::GetUniqueFramesSeen() const {
224 rtc::CritScope lock(&crit_);
225 return unique_frames_seen_;
226}
227
philipelc707ab72016-04-01 02:01:54 -0700228bool PacketBuffer::ExpandBufferSize() {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200229 if (buffer_.size() == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100230 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
Johannes Kronbd3f3052019-08-01 15:45:54 +0200231 << "), failed to increase size.";
philipelc707ab72016-04-01 02:01:54 -0700232 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100233 }
philipelc707ab72016-04-01 02:01:54 -0700234
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200235 size_t new_size = std::min(max_size_, 2 * buffer_.size());
236 std::vector<StoredPacket> new_buffer(new_size);
237 for (StoredPacket& entry : buffer_) {
238 if (entry.used) {
239 new_buffer[entry.seq_num() % new_size] = entry;
philipelc707ab72016-04-01 02:01:54 -0700240 }
241 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200242 buffer_ = std::move(new_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100243 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700244 return true;
245}
246
philipelaee3e0e2016-11-01 11:45:34 +0100247bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200248 size_t index = seq_num % buffer_.size();
249 int prev_index = index > 0 ? index - 1 : buffer_.size() - 1;
250 const StoredPacket& entry = buffer_[index];
251 const StoredPacket& prev_entry = buffer_[prev_index];
philipelf4139332016-04-20 10:26:34 +0200252
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200253 if (!entry.used)
philipelc707ab72016-04-01 02:01:54 -0700254 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200255 if (entry.seq_num() != seq_num)
philipel2c9f9f22017-06-13 02:47:28 -0700256 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200257 if (entry.frame_begin())
philipelc707ab72016-04-01 02:01:54 -0700258 return true;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200259 if (!prev_entry.used)
philipelc707ab72016-04-01 02:01:54 -0700260 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200261 if (prev_entry.seq_num() != static_cast<uint16_t>(entry.seq_num() - 1))
philipelea142f82017-01-11 02:01:56 -0800262 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200263 if (prev_entry.data.timestamp != entry.data.timestamp)
philipelf4139332016-04-20 10:26:34 +0200264 return false;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200265 if (prev_entry.continuous)
philipelc707ab72016-04-01 02:01:54 -0700266 return true;
267
268 return false;
269}
270
philipelfd5a20f2016-11-15 00:57:57 -0800271std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
272 uint16_t seq_num) {
273 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200274 for (size_t i = 0; i < buffer_.size() && PotentialNewFrame(seq_num); ++i) {
275 size_t index = seq_num % buffer_.size();
276 buffer_[index].continuous = true;
philipelc707ab72016-04-01 02:01:54 -0700277
philipelf4139332016-04-20 10:26:34 +0200278 // If all packets of the frame is continuous, find the first packet of the
279 // frame and create an RtpFrameObject.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200280 if (buffer_[index].frame_end()) {
philipel5ceaaae2016-05-24 10:20:47 +0200281 size_t frame_size = 0;
282 int max_nack_count = -1;
philipelc707ab72016-04-01 02:01:54 -0700283 uint16_t start_seq_num = seq_num;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200284 int64_t min_recv_time = buffer_[index].data.packet_info.receive_time_ms();
285 int64_t max_recv_time = buffer_[index].data.packet_info.receive_time_ms();
Chen Xingf00bf422019-06-20 10:05:55 +0200286 RtpPacketInfos::vector_type packet_infos;
philipelf4139332016-04-20 10:26:34 +0200287
philipel5ceaaae2016-05-24 10:20:47 +0200288 // Find the start index by searching backward until the packet with
289 // the |frame_begin| flag is set.
290 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700291 size_t tested_packets = 0;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200292 int64_t frame_timestamp = buffer_[start_index].data.timestamp;
philipel53910712017-05-18 02:24:40 -0700293
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100294 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200295 bool is_h264 = buffer_[start_index].data.codec() == kVideoCodecH264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100296 bool has_h264_sps = false;
297 bool has_h264_pps = false;
298 bool has_h264_idr = false;
299 bool is_h264_keyframe = false;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700300 int idr_width = -1;
301 int idr_height = -1;
philipel227f8b92017-08-04 06:39:31 -0700302 while (true) {
303 ++tested_packets;
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200304 frame_size += buffer_[start_index].data.sizeBytes;
philipelfd5a20f2016-11-15 00:57:57 -0800305 max_nack_count =
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200306 std::max(max_nack_count, buffer_[start_index].data.timesNacked);
philipel5ceaaae2016-05-24 10:20:47 +0200307
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100308 min_recv_time =
Chen Xingf00bf422019-06-20 10:05:55 +0200309 std::min(min_recv_time,
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200310 buffer_[start_index].data.packet_info.receive_time_ms());
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100311 max_recv_time =
Chen Xingf00bf422019-06-20 10:05:55 +0200312 std::max(max_recv_time,
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200313 buffer_[start_index].data.packet_info.receive_time_ms());
Chen Xingf00bf422019-06-20 10:05:55 +0200314
315 // Should use |push_front()| since the loop traverses backwards. But
316 // it's too inefficient to do so on a vector so we'll instead fix the
317 // order afterwards.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200318 packet_infos.push_back(buffer_[start_index].data.packet_info);
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100319
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200320 if (!is_h264 && buffer_[start_index].frame_begin())
philipel5ceaaae2016-05-24 10:20:47 +0200321 break;
322
Shyam Sadhwani2b84dad2019-10-02 17:22:33 -0700323 if (is_h264) {
philipel7d745e52018-08-02 14:03:53 +0200324 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200325 &buffer_[start_index].data.video_header.video_type_header);
philipel7d745e52018-08-02 14:03:53 +0200326 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200327 return found_frames;
328
philipel7d745e52018-08-02 14:03:53 +0200329 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
330 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100331 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200332 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100333 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200334 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100335 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700336 }
337 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100338 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
339 has_h264_pps) ||
340 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
341 is_h264_keyframe = true;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700342 // Store the resolution of key frame which is the packet with
343 // smallest index and valid resolution; typically its IDR or SPS
344 // packet; there may be packet preceeding this packet, IDR's
345 // resolution will be applied to them.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200346 if (buffer_[start_index].data.width() > 0 &&
347 buffer_[start_index].data.height() > 0) {
348 idr_width = buffer_[start_index].data.width();
349 idr_height = buffer_[start_index].data.height();
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700350 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100351 }
philipel2c9f9f22017-06-13 02:47:28 -0700352 }
353
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200354 if (tested_packets == buffer_.size())
philipel227f8b92017-08-04 06:39:31 -0700355 break;
356
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200357 start_index = start_index > 0 ? start_index - 1 : buffer_.size() - 1;
philipel8c619242017-02-02 08:51:29 -0800358
359 // In the case of H264 we don't have a frame_begin bit (yes,
360 // |frame_begin| might be set to true but that is a lie). So instead
361 // we traverese backwards as long as we have a previous packet and
362 // the timestamp of that packet is the same as this one. This may cause
363 // the PacketBuffer to hand out incomplete frames.
364 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700365 if (is_h264 &&
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200366 (!buffer_[start_index].used ||
367 buffer_[start_index].data.timestamp != frame_timestamp)) {
philipel8c619242017-02-02 08:51:29 -0800368 break;
369 }
370
371 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700372 }
373
Chen Xingf00bf422019-06-20 10:05:55 +0200374 // Fix the order since the packet-finding loop traverses backwards.
375 std::reverse(packet_infos.begin(), packet_infos.end());
376
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100377 if (is_h264) {
378 // Warn if this is an unsafe frame.
379 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200380 RTC_LOG(LS_WARNING)
381 << "Received H.264-IDR frame "
382 << "(SPS: " << has_h264_sps << ", PPS: " << has_h264_pps
383 << "). Treating as "
384 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
385 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
386 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700387 }
388
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100389 // Now that we have decided whether to treat this frame as a key frame
390 // or delta frame in the frame buffer, we update the field that
391 // determines if the RtpFrameObject is a key frame or delta frame.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200392 const size_t first_packet_index = start_seq_num % buffer_.size();
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100393 if (is_h264_keyframe) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200394 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100395 VideoFrameType::kVideoFrameKey;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700396 if (idr_width > 0 && idr_height > 0) {
397 // IDR frame was finalized and we have the correct resolution for
398 // IDR; update first packet to have same resolution as IDR.
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200399 buffer_[first_packet_index].data.video_header.width = idr_width;
400 buffer_[first_packet_index].data.video_header.height = idr_height;
Shyam Sadhwani5b2df172019-10-16 09:13:38 -0700401 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100402 } else {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200403 buffer_[first_packet_index].data.video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100404 VideoFrameType::kVideoFrameDelta;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100405 }
406
Johnny Leebc7f41b2019-05-01 14:41:32 -0400407 // With IPPP, if this is not a keyframe, make sure there are no gaps
408 // in the packet sequence numbers up until this point.
409 const uint8_t h264tid =
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200410 buffer_[start_index].data.video_header.frame_marking.temporal_id;
Jonas Olssona4d87372019-07-05 19:08:33 +0200411 if (h264tid == kNoTemporalIdx && !is_h264_keyframe &&
412 missing_packets_.upper_bound(start_seq_num) !=
413 missing_packets_.begin()) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100414 return found_frames;
415 }
philipel2c9f9f22017-06-13 02:47:28 -0700416 }
417
418 missing_packets_.erase(missing_packets_.begin(),
419 missing_packets_.upper_bound(seq_num));
420
philipel85d5c192019-09-25 17:15:37 +0200421 const VCMPacket* first_packet = GetPacket(start_seq_num);
422 const VCMPacket* last_packet = GetPacket(seq_num);
philipel76161f72019-09-19 11:22:22 +0200423 auto frame = std::make_unique<RtpFrameObject>(
philipel85d5c192019-09-25 17:15:37 +0200424 start_seq_num, seq_num, last_packet->markerBit, max_nack_count,
425 min_recv_time, max_recv_time, first_packet->timestamp,
426 first_packet->ntp_time_ms_, last_packet->video_header.video_timing,
427 first_packet->payloadType, first_packet->codec(),
428 last_packet->video_header.rotation,
429 last_packet->video_header.content_type, first_packet->video_header,
430 last_packet->video_header.color_space,
431 first_packet->generic_descriptor,
432 RtpPacketInfos(std::move(packet_infos)),
philipelb5e47852019-09-20 11:30:12 +0200433 GetEncodedImageBuffer(frame_size, start_seq_num, seq_num));
434
philipel76161f72019-09-19 11:22:22 +0200435 found_frames.emplace_back(std::move(frame));
436
Johannes Krona3705562019-08-26 16:37:11 +0200437 ClearInterval(start_seq_num, seq_num);
philipelc707ab72016-04-01 02:01:54 -0700438 }
philipelc707ab72016-04-01 02:01:54 -0700439 ++seq_num;
440 }
philipelfd5a20f2016-11-15 00:57:57 -0800441 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700442}
443
philipelb5e47852019-09-20 11:30:12 +0200444rtc::scoped_refptr<EncodedImageBuffer> PacketBuffer::GetEncodedImageBuffer(
445 size_t frame_size,
446 uint16_t first_seq_num,
447 uint16_t last_seq_num) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200448 size_t index = first_seq_num % buffer_.size();
449 size_t end = (last_seq_num + 1) % buffer_.size();
philipelc707ab72016-04-01 02:01:54 -0700450
philipelb5e47852019-09-20 11:30:12 +0200451 auto buffer = EncodedImageBuffer::Create(frame_size);
452 size_t offset = 0;
philipel227f8b92017-08-04 06:39:31 -0700453
454 do {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200455 RTC_DCHECK(buffer_[index].used);
philipelc707ab72016-04-01 02:01:54 -0700456
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200457 size_t length = buffer_[index].data.sizeBytes;
philipelb5e47852019-09-20 11:30:12 +0200458 RTC_CHECK_LE(offset + length, buffer->size());
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200459 memcpy(buffer->data() + offset, buffer_[index].data.dataPtr, length);
philipelb5e47852019-09-20 11:30:12 +0200460 offset += length;
philipel227f8b92017-08-04 06:39:31 -0700461
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200462 index = (index + 1) % buffer_.size();
philipel227f8b92017-08-04 06:39:31 -0700463 } while (index != end);
464
philipelb5e47852019-09-20 11:30:12 +0200465 return buffer;
philipelc707ab72016-04-01 02:01:54 -0700466}
467
philipel02447bc2016-05-13 06:01:03 -0700468VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) {
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200469 StoredPacket& entry = buffer_[seq_num % buffer_.size()];
470 if (!entry.used || seq_num != entry.seq_num()) {
philipel02447bc2016-05-13 06:01:03 -0700471 return nullptr;
philipelf4139332016-04-20 10:26:34 +0200472 }
Danil Chapovalov4aae11d2019-10-18 11:17:03 +0200473 return &entry.data;
philipelf4139332016-04-20 10:26:34 +0200474}
475
philipel2c9f9f22017-06-13 02:47:28 -0700476void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
477 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100478 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700479
480 const int kMaxPaddingAge = 1000;
481 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
482 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
483 auto erase_to = missing_packets_.lower_bound(old_seq_num);
484 missing_packets_.erase(missing_packets_.begin(), erase_to);
485
486 // Guard against inserting a large amount of missing packets if there is a
487 // jump in the sequence number.
488 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
489 *newest_inserted_seq_num_ = old_seq_num;
490
491 ++*newest_inserted_seq_num_;
492 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
493 missing_packets_.insert(*newest_inserted_seq_num_);
494 ++*newest_inserted_seq_num_;
495 }
496 } else {
497 missing_packets_.erase(seq_num);
498 }
499}
500
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100501void PacketBuffer::OnTimestampReceived(uint32_t rtp_timestamp) {
502 const size_t kMaxTimestampsHistory = 1000;
503 if (rtp_timestamps_history_set_.insert(rtp_timestamp).second) {
504 rtp_timestamps_history_queue_.push(rtp_timestamp);
505 ++unique_frames_seen_;
506 if (rtp_timestamps_history_set_.size() > kMaxTimestampsHistory) {
507 uint32_t discarded_timestamp = rtp_timestamps_history_queue_.front();
508 rtp_timestamps_history_set_.erase(discarded_timestamp);
509 rtp_timestamps_history_queue_.pop();
510 }
511 }
512}
513
philipelc707ab72016-04-01 02:01:54 -0700514} // namespace video_coding
515} // namespace webrtc