blob: e6469b150a760b9ba0954913fe79ddca296aff22 [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>
philipelc707ab72016-04-01 02:01:54 -070014#include <algorithm>
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <cstdint>
philipel17deeb42016-08-11 15:09:26 +020016#include <utility>
philipelc707ab72016-04-01 02:01:54 -070017
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "absl/types/variant.h"
19#include "api/video/encoded_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "common_video/h264/h264_common.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "modules/rtp_rtcp/source/rtp_video_header.h"
22#include "modules/video_coding/codecs/h264/include/h264_globals.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/video_coding/frame_object.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/atomic_ops.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
philipel17deeb42016-08-11 15:09:26 +020034rtc::scoped_refptr<PacketBuffer> PacketBuffer::Create(
35 Clock* clock,
36 size_t start_buffer_size,
37 size_t max_buffer_size,
Elad Alonb4643ad2019-02-22 11:19:50 +010038 OnAssembledFrameCallback* assembled_frame_callback) {
philipel17deeb42016-08-11 15:09:26 +020039 return rtc::scoped_refptr<PacketBuffer>(new PacketBuffer(
Elad Alonb4643ad2019-02-22 11:19:50 +010040 clock, start_buffer_size, max_buffer_size, assembled_frame_callback));
philipel17deeb42016-08-11 15:09:26 +020041}
42
philipelb4d31082016-07-11 08:46:29 -070043PacketBuffer::PacketBuffer(Clock* clock,
44 size_t start_buffer_size,
philipelc707ab72016-04-01 02:01:54 -070045 size_t max_buffer_size,
Elad Alonb4643ad2019-02-22 11:19:50 +010046 OnAssembledFrameCallback* assembled_frame_callback)
philipelb4d31082016-07-11 08:46:29 -070047 : clock_(clock),
48 size_(start_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070049 max_size_(max_buffer_size),
philipelc707ab72016-04-01 02:01:54 -070050 first_seq_num_(0),
philipelf4139332016-04-20 10:26:34 +020051 first_packet_received_(false),
philipelaee3e0e2016-11-01 11:45:34 +010052 is_cleared_to_first_seq_num_(false),
philipelc707ab72016-04-01 02:01:54 -070053 data_buffer_(start_buffer_size),
54 sequence_buffer_(start_buffer_size),
Elad Alonb4643ad2019-02-22 11:19:50 +010055 assembled_frame_callback_(assembled_frame_callback),
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010056 unique_frames_seen_(0),
Rasmus Brandt88f080a2017-11-02 14:28:06 +010057 sps_pps_idr_is_h264_keyframe_(
58 field_trial::IsEnabled("WebRTC-SpsPpsIdrIsH264Keyframe")) {
philipelc707ab72016-04-01 02:01:54 -070059 RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
60 // Buffer size must always be a power of 2.
61 RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
62 RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
63}
64
philipelaee3e0e2016-11-01 11:45:34 +010065PacketBuffer::~PacketBuffer() {
66 Clear();
67}
philipel17deeb42016-08-11 15:09:26 +020068
philipel759e0b72016-11-30 01:32:05 -080069bool PacketBuffer::InsertPacket(VCMPacket* packet) {
philipelfd5a20f2016-11-15 00:57:57 -080070 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
71 {
72 rtc::CritScope lock(&crit_);
philipel3184f8e2017-05-18 08:08:53 -070073
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010074 OnTimestampReceived(packet->timestamp);
75
philipel759e0b72016-11-30 01:32:05 -080076 uint16_t seq_num = packet->seqNum;
philipelfd5a20f2016-11-15 00:57:57 -080077 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -070078
philipelfd5a20f2016-11-15 00:57:57 -080079 if (!first_packet_received_) {
80 first_seq_num_ = seq_num;
philipelfd5a20f2016-11-15 00:57:57 -080081 first_packet_received_ = true;
82 } else if (AheadOf(first_seq_num_, seq_num)) {
83 // If we have explicitly cleared past this packet then it's old,
84 // don't insert it.
philipel759e0b72016-11-30 01:32:05 -080085 if (is_cleared_to_first_seq_num_) {
86 delete[] packet->dataPtr;
87 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -080088 return false;
philipel759e0b72016-11-30 01:32:05 -080089 }
philipelaee3e0e2016-11-01 11:45:34 +010090
philipelfd5a20f2016-11-15 00:57:57 -080091 first_seq_num_ = seq_num;
philipelc707ab72016-04-01 02:01:54 -070092 }
philipelc707ab72016-04-01 02:01:54 -070093
philipelfd5a20f2016-11-15 00:57:57 -080094 if (sequence_buffer_[index].used) {
philipel759e0b72016-11-30 01:32:05 -080095 // Duplicate packet, just delete the payload.
96 if (data_buffer_[index].seqNum == packet->seqNum) {
97 delete[] packet->dataPtr;
98 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -080099 return true;
philipel759e0b72016-11-30 01:32:05 -0800100 }
philipelfd5a20f2016-11-15 00:57:57 -0800101
102 // The packet buffer is full, try to expand the buffer.
103 while (ExpandBufferSize() && sequence_buffer_[seq_num % size_].used) {
104 }
105 index = seq_num % size_;
106
107 // Packet buffer is still full.
philipel759e0b72016-11-30 01:32:05 -0800108 if (sequence_buffer_[index].used) {
109 delete[] packet->dataPtr;
110 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800111 return false;
philipel759e0b72016-11-30 01:32:05 -0800112 }
philipelfd5a20f2016-11-15 00:57:57 -0800113 }
114
Niels Möllerd5e02f02019-02-20 13:12:21 +0100115 sequence_buffer_[index].frame_begin = packet->is_first_packet_in_frame();
116 sequence_buffer_[index].frame_end = packet->is_last_packet_in_frame();
philipel759e0b72016-11-30 01:32:05 -0800117 sequence_buffer_[index].seq_num = packet->seqNum;
philipelfd5a20f2016-11-15 00:57:57 -0800118 sequence_buffer_[index].continuous = false;
119 sequence_buffer_[index].frame_created = false;
120 sequence_buffer_[index].used = true;
philipel759e0b72016-11-30 01:32:05 -0800121 data_buffer_[index] = *packet;
122 packet->dataPtr = nullptr;
philipelfd5a20f2016-11-15 00:57:57 -0800123
philipel2c9f9f22017-06-13 02:47:28 -0700124 UpdateMissingPackets(packet->seqNum);
125
philipel3184f8e2017-05-18 08:08:53 -0700126 int64_t now_ms = clock_->TimeInMilliseconds();
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100127 last_received_packet_ms_ = now_ms;
Niels Möllerabbc50e2019-04-24 09:41:16 +0200128 if (packet->video_header.frame_type == VideoFrameType::kVideoFrameKey)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100129 last_received_keyframe_packet_ms_ = now_ms;
philipel3184f8e2017-05-18 08:08:53 -0700130
philipelfd5a20f2016-11-15 00:57:57 -0800131 found_frames = FindFrames(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700132 }
133
philipelfd5a20f2016-11-15 00:57:57 -0800134 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
Elad Alonb4643ad2019-02-22 11:19:50 +0100135 assembled_frame_callback_->OnAssembledFrame(std::move(frame));
philipelc707ab72016-04-01 02:01:54 -0700136
philipelc707ab72016-04-01 02:01:54 -0700137 return true;
138}
139
140void PacketBuffer::ClearTo(uint16_t seq_num) {
141 rtc::CritScope lock(&crit_);
philipelc5fb4682017-08-02 04:28:57 -0700142 // We have already cleared past this sequence number, no need to do anything.
143 if (is_cleared_to_first_seq_num_ &&
144 AheadOf<uint16_t>(first_seq_num_, seq_num)) {
145 return;
146 }
philipelaee3e0e2016-11-01 11:45:34 +0100147
148 // If the packet buffer was cleared between a frame was created and returned.
149 if (!first_packet_received_)
150 return;
151
philipelc5fb4682017-08-02 04:28:57 -0700152 // Avoid iterating over the buffer more than once by capping the number of
153 // iterations to the |size_| of the buffer.
154 ++seq_num;
155 size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num);
156 size_t iterations = std::min(diff, size_);
157 for (size_t i = 0; i < iterations; ++i) {
philipelaee3e0e2016-11-01 11:45:34 +0100158 size_t index = first_seq_num_ % size_;
philipelc5fb4682017-08-02 04:28:57 -0700159 RTC_DCHECK_EQ(data_buffer_[index].seqNum, sequence_buffer_[index].seq_num);
160 if (AheadOf<uint16_t>(seq_num, sequence_buffer_[index].seq_num)) {
161 delete[] data_buffer_[index].dataPtr;
162 data_buffer_[index].dataPtr = nullptr;
163 sequence_buffer_[index].used = false;
164 }
philipelaee3e0e2016-11-01 11:45:34 +0100165 ++first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700166 }
philipel2c9f9f22017-06-13 02:47:28 -0700167
philipelc5fb4682017-08-02 04:28:57 -0700168 // If |diff| is larger than |iterations| it means that we don't increment
169 // |first_seq_num_| until we reach |seq_num|, so we set it here.
170 first_seq_num_ = seq_num;
171
172 is_cleared_to_first_seq_num_ = true;
philipelbc5a4082017-12-06 10:41:08 +0100173 auto clear_to_it = missing_packets_.upper_bound(seq_num);
174 if (clear_to_it != missing_packets_.begin()) {
175 --clear_to_it;
176 missing_packets_.erase(missing_packets_.begin(), clear_to_it);
177 }
philipelc707ab72016-04-01 02:01:54 -0700178}
179
philipelaee3e0e2016-11-01 11:45:34 +0100180void PacketBuffer::Clear() {
181 rtc::CritScope lock(&crit_);
182 for (size_t i = 0; i < size_; ++i) {
183 delete[] data_buffer_[i].dataPtr;
184 data_buffer_[i].dataPtr = nullptr;
185 sequence_buffer_[i].used = false;
186 }
187
188 first_packet_received_ = false;
189 is_cleared_to_first_seq_num_ = false;
philipel2c9f9f22017-06-13 02:47:28 -0700190 last_received_packet_ms_.reset();
191 last_received_keyframe_packet_ms_.reset();
192 newest_inserted_seq_num_.reset();
193 missing_packets_.clear();
194}
195
196void PacketBuffer::PaddingReceived(uint16_t seq_num) {
197 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
198 {
199 rtc::CritScope lock(&crit_);
200 UpdateMissingPackets(seq_num);
201 found_frames = FindFrames(static_cast<uint16_t>(seq_num + 1));
202 }
203
204 for (std::unique_ptr<RtpFrameObject>& frame : found_frames)
Elad Alonb4643ad2019-02-22 11:19:50 +0100205 assembled_frame_callback_->OnAssembledFrame(std::move(frame));
philipel3184f8e2017-05-18 08:08:53 -0700206}
207
Danil Chapovalov0040b662018-06-18 10:48:16 +0200208absl::optional<int64_t> PacketBuffer::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700209 rtc::CritScope lock(&crit_);
210 return last_received_packet_ms_;
211}
212
Danil Chapovalov0040b662018-06-18 10:48:16 +0200213absl::optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700214 rtc::CritScope lock(&crit_);
215 return last_received_keyframe_packet_ms_;
philipelaee3e0e2016-11-01 11:45:34 +0100216}
217
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100218int PacketBuffer::GetUniqueFramesSeen() const {
219 rtc::CritScope lock(&crit_);
220 return unique_frames_seen_;
221}
222
philipelc707ab72016-04-01 02:01:54 -0700223bool PacketBuffer::ExpandBufferSize() {
philipelaee3e0e2016-11-01 11:45:34 +0100224 if (size_ == max_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100225 RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_
226 << "), failed to increase size. Clearing PacketBuffer.";
philipelc703dc22017-03-23 06:50:37 -0700227 Clear();
philipelc707ab72016-04-01 02:01:54 -0700228 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100229 }
philipelc707ab72016-04-01 02:01:54 -0700230
231 size_t new_size = std::min(max_size_, 2 * size_);
232 std::vector<VCMPacket> new_data_buffer(new_size);
233 std::vector<ContinuityInfo> new_sequence_buffer(new_size);
234 for (size_t i = 0; i < size_; ++i) {
235 if (sequence_buffer_[i].used) {
philipelf4139332016-04-20 10:26:34 +0200236 size_t index = sequence_buffer_[i].seq_num % new_size;
philipelc707ab72016-04-01 02:01:54 -0700237 new_sequence_buffer[index] = sequence_buffer_[i];
238 new_data_buffer[index] = data_buffer_[i];
239 }
240 }
241 size_ = new_size;
242 sequence_buffer_ = std::move(new_sequence_buffer);
243 data_buffer_ = std::move(new_data_buffer);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100244 RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size;
philipelc707ab72016-04-01 02:01:54 -0700245 return true;
246}
247
philipelaee3e0e2016-11-01 11:45:34 +0100248bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const {
philipelf4139332016-04-20 10:26:34 +0200249 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700250 int prev_index = index > 0 ? index - 1 : size_ - 1;
philipelf4139332016-04-20 10:26:34 +0200251
philipelc707ab72016-04-01 02:01:54 -0700252 if (!sequence_buffer_[index].used)
253 return false;
philipel2c9f9f22017-06-13 02:47:28 -0700254 if (sequence_buffer_[index].seq_num != seq_num)
255 return false;
philipelf4139332016-04-20 10:26:34 +0200256 if (sequence_buffer_[index].frame_created)
257 return false;
philipel20dce342016-11-28 16:14:57 +0100258 if (sequence_buffer_[index].frame_begin)
philipelc707ab72016-04-01 02:01:54 -0700259 return true;
260 if (!sequence_buffer_[prev_index].used)
261 return false;
philipelea142f82017-01-11 02:01:56 -0800262 if (sequence_buffer_[prev_index].frame_created)
263 return false;
philipelf4139332016-04-20 10:26:34 +0200264 if (sequence_buffer_[prev_index].seq_num !=
philipel2c2f34c2017-01-03 05:55:34 -0800265 static_cast<uint16_t>(sequence_buffer_[index].seq_num - 1)) {
philipelf4139332016-04-20 10:26:34 +0200266 return false;
philipelaee3e0e2016-11-01 11:45:34 +0100267 }
philipel8b6995b2019-01-09 12:39:18 +0100268 if (data_buffer_[prev_index].timestamp != data_buffer_[index].timestamp)
269 return false;
philipelc707ab72016-04-01 02:01:54 -0700270 if (sequence_buffer_[prev_index].continuous)
271 return true;
272
273 return false;
274}
275
philipelfd5a20f2016-11-15 00:57:57 -0800276std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames(
277 uint16_t seq_num) {
278 std::vector<std::unique_ptr<RtpFrameObject>> found_frames;
philipel53910712017-05-18 02:24:40 -0700279 for (size_t i = 0; i < size_ && PotentialNewFrame(seq_num); ++i) {
philipelaee3e0e2016-11-01 11:45:34 +0100280 size_t index = seq_num % size_;
philipelc707ab72016-04-01 02:01:54 -0700281 sequence_buffer_[index].continuous = true;
282
philipelf4139332016-04-20 10:26:34 +0200283 // If all packets of the frame is continuous, find the first packet of the
284 // frame and create an RtpFrameObject.
philipelc707ab72016-04-01 02:01:54 -0700285 if (sequence_buffer_[index].frame_end) {
philipel5ceaaae2016-05-24 10:20:47 +0200286 size_t frame_size = 0;
287 int max_nack_count = -1;
philipelc707ab72016-04-01 02:01:54 -0700288 uint16_t start_seq_num = seq_num;
Chen Xingf00bf422019-06-20 10:05:55 +0200289 int64_t min_recv_time = data_buffer_[index].packet_info.receive_time_ms();
290 int64_t max_recv_time = data_buffer_[index].packet_info.receive_time_ms();
291 RtpPacketInfos::vector_type packet_infos;
philipelf4139332016-04-20 10:26:34 +0200292
philipel5ceaaae2016-05-24 10:20:47 +0200293 // Find the start index by searching backward until the packet with
294 // the |frame_begin| flag is set.
295 int start_index = index;
philipel227f8b92017-08-04 06:39:31 -0700296 size_t tested_packets = 0;
philipel8c619242017-02-02 08:51:29 -0800297 int64_t frame_timestamp = data_buffer_[start_index].timestamp;
philipel53910712017-05-18 02:24:40 -0700298
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100299 // Identify H.264 keyframes by means of SPS, PPS, and IDR.
Niels Möllerd5e02f02019-02-20 13:12:21 +0100300 bool is_h264 = data_buffer_[start_index].codec() == kVideoCodecH264;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100301 bool has_h264_sps = false;
302 bool has_h264_pps = false;
303 bool has_h264_idr = false;
304 bool is_h264_keyframe = false;
305
philipel227f8b92017-08-04 06:39:31 -0700306 while (true) {
307 ++tested_packets;
philipel5ceaaae2016-05-24 10:20:47 +0200308 frame_size += data_buffer_[start_index].sizeBytes;
philipelfd5a20f2016-11-15 00:57:57 -0800309 max_nack_count =
310 std::max(max_nack_count, data_buffer_[start_index].timesNacked);
philipelf4139332016-04-20 10:26:34 +0200311 sequence_buffer_[start_index].frame_created = true;
philipel5ceaaae2016-05-24 10:20:47 +0200312
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100313 min_recv_time =
Chen Xingf00bf422019-06-20 10:05:55 +0200314 std::min(min_recv_time,
315 data_buffer_[start_index].packet_info.receive_time_ms());
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100316 max_recv_time =
Chen Xingf00bf422019-06-20 10:05:55 +0200317 std::max(max_recv_time,
318 data_buffer_[start_index].packet_info.receive_time_ms());
319
320 // Should use |push_front()| since the loop traverses backwards. But
321 // it's too inefficient to do so on a vector so we'll instead fix the
322 // order afterwards.
323 packet_infos.push_back(data_buffer_[start_index].packet_info);
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100324
philipel8c619242017-02-02 08:51:29 -0800325 if (!is_h264 && sequence_buffer_[start_index].frame_begin)
philipel5ceaaae2016-05-24 10:20:47 +0200326 break;
327
philipel2c9f9f22017-06-13 02:47:28 -0700328 if (is_h264 && !is_h264_keyframe) {
philipel7d745e52018-08-02 14:03:53 +0200329 const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
330 &data_buffer_[start_index].video_header.video_type_header);
331 if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
philipel09133af2018-05-17 14:11:09 +0200332 return found_frames;
333
philipel7d745e52018-08-02 14:03:53 +0200334 for (size_t j = 0; j < h264_header->nalus_length; ++j) {
335 if (h264_header->nalus[j].type == H264::NaluType::kSps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100336 has_h264_sps = true;
philipel7d745e52018-08-02 14:03:53 +0200337 } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100338 has_h264_pps = true;
philipel7d745e52018-08-02 14:03:53 +0200339 } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100340 has_h264_idr = true;
philipel2c9f9f22017-06-13 02:47:28 -0700341 }
342 }
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100343 if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
344 has_h264_pps) ||
345 (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
346 is_h264_keyframe = true;
347 }
philipel2c9f9f22017-06-13 02:47:28 -0700348 }
349
philipel227f8b92017-08-04 06:39:31 -0700350 if (tested_packets == size_)
351 break;
352
philipelf4139332016-04-20 10:26:34 +0200353 start_index = start_index > 0 ? start_index - 1 : size_ - 1;
philipel8c619242017-02-02 08:51:29 -0800354
355 // In the case of H264 we don't have a frame_begin bit (yes,
356 // |frame_begin| might be set to true but that is a lie). So instead
357 // we traverese backwards as long as we have a previous packet and
358 // the timestamp of that packet is the same as this one. This may cause
359 // the PacketBuffer to hand out incomplete frames.
360 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
philipel53910712017-05-18 02:24:40 -0700361 if (is_h264 &&
philipel8c619242017-02-02 08:51:29 -0800362 (!sequence_buffer_[start_index].used ||
363 data_buffer_[start_index].timestamp != frame_timestamp)) {
364 break;
365 }
366
367 --start_seq_num;
philipelc707ab72016-04-01 02:01:54 -0700368 }
369
Chen Xingf00bf422019-06-20 10:05:55 +0200370 // Fix the order since the packet-finding loop traverses backwards.
371 std::reverse(packet_infos.begin(), packet_infos.end());
372
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100373 if (is_h264) {
374 // Warn if this is an unsafe frame.
375 if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
Jonas Olssonfc501102018-06-15 14:24:10 +0200376 RTC_LOG(LS_WARNING)
377 << "Received H.264-IDR frame "
378 << "(SPS: " << has_h264_sps << ", PPS: " << has_h264_pps
379 << "). Treating as "
380 << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
381 << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
382 << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
philipel2c9f9f22017-06-13 02:47:28 -0700383 }
384
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100385 // Now that we have decided whether to treat this frame as a key frame
386 // or delta frame in the frame buffer, we update the field that
387 // determines if the RtpFrameObject is a key frame or delta frame.
388 const size_t first_packet_index = start_seq_num % size_;
389 RTC_CHECK_LT(first_packet_index, size_);
390 if (is_h264_keyframe) {
Niels Möllerabbc50e2019-04-24 09:41:16 +0200391 data_buffer_[first_packet_index].video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100392 VideoFrameType::kVideoFrameKey;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100393 } else {
Niels Möllerabbc50e2019-04-24 09:41:16 +0200394 data_buffer_[first_packet_index].video_header.frame_type =
Niels Möller8f7ce222019-03-21 15:43:58 +0100395 VideoFrameType::kVideoFrameDelta;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100396 }
397
Johnny Leebc7f41b2019-05-01 14:41:32 -0400398 // With IPPP, if this is not a keyframe, make sure there are no gaps
399 // in the packet sequence numbers up until this point.
400 const uint8_t h264tid =
401 data_buffer_[start_index].video_header.frame_marking.temporal_id;
402 if (h264tid == kNoTemporalIdx && !is_h264_keyframe
403 && missing_packets_.upper_bound(start_seq_num)
404 != missing_packets_.begin()) {
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100405 uint16_t stop_index = (index + 1) % size_;
406 while (start_index != stop_index) {
407 sequence_buffer_[start_index].frame_created = false;
408 start_index = (start_index + 1) % size_;
409 }
410
411 return found_frames;
412 }
philipel2c9f9f22017-06-13 02:47:28 -0700413 }
414
415 missing_packets_.erase(missing_packets_.begin(),
416 missing_packets_.upper_bound(seq_num));
417
philipelfd5a20f2016-11-15 00:57:57 -0800418 found_frames.emplace_back(
philipelb4d31082016-07-11 08:46:29 -0700419 new RtpFrameObject(this, start_seq_num, seq_num, frame_size,
Chen Xingf00bf422019-06-20 10:05:55 +0200420 max_nack_count, min_recv_time, max_recv_time,
421 RtpPacketInfos(std::move(packet_infos))));
philipelc707ab72016-04-01 02:01:54 -0700422 }
philipelc707ab72016-04-01 02:01:54 -0700423 ++seq_num;
424 }
philipelfd5a20f2016-11-15 00:57:57 -0800425 return found_frames;
philipelc707ab72016-04-01 02:01:54 -0700426}
427
428void PacketBuffer::ReturnFrame(RtpFrameObject* frame) {
429 rtc::CritScope lock(&crit_);
philipelf4139332016-04-20 10:26:34 +0200430 size_t index = frame->first_seq_num() % size_;
431 size_t end = (frame->last_seq_num() + 1) % size_;
432 uint16_t seq_num = frame->first_seq_num();
Johannes Kron957c62e2018-10-01 14:53:01 +0200433 uint32_t timestamp = frame->Timestamp();
philipelc707ab72016-04-01 02:01:54 -0700434 while (index != end) {
Johannes Kron957c62e2018-10-01 14:53:01 +0200435 // Check both seq_num and timestamp to handle the case when seq_num wraps
436 // around too quickly for high packet rates.
437 if (sequence_buffer_[index].seq_num == seq_num &&
438 data_buffer_[index].timestamp == timestamp) {
philipel1f39ba12016-09-21 11:27:47 +0200439 delete[] data_buffer_[index].dataPtr;
440 data_buffer_[index].dataPtr = nullptr;
philipelc707ab72016-04-01 02:01:54 -0700441 sequence_buffer_[index].used = false;
philipel1f39ba12016-09-21 11:27:47 +0200442 }
philipelf4139332016-04-20 10:26:34 +0200443
philipelc707ab72016-04-01 02:01:54 -0700444 index = (index + 1) % size_;
445 ++seq_num;
446 }
philipelc707ab72016-04-01 02:01:54 -0700447}
448
449bool PacketBuffer::GetBitstream(const RtpFrameObject& frame,
450 uint8_t* destination) {
451 rtc::CritScope lock(&crit_);
452
philipelf4139332016-04-20 10:26:34 +0200453 size_t index = frame.first_seq_num() % size_;
454 size_t end = (frame.last_seq_num() + 1) % size_;
455 uint16_t seq_num = frame.first_seq_num();
Johannes Kron957c62e2018-10-01 14:53:01 +0200456 uint32_t timestamp = frame.Timestamp();
philipel227f8b92017-08-04 06:39:31 -0700457 uint8_t* destination_end = destination + frame.size();
458
459 do {
Johannes Kron957c62e2018-10-01 14:53:01 +0200460 // Check both seq_num and timestamp to handle the case when seq_num wraps
461 // around too quickly for high packet rates.
philipelc707ab72016-04-01 02:01:54 -0700462 if (!sequence_buffer_[index].used ||
Johannes Kron957c62e2018-10-01 14:53:01 +0200463 sequence_buffer_[index].seq_num != seq_num ||
464 data_buffer_[index].timestamp != timestamp) {
philipelc707ab72016-04-01 02:01:54 -0700465 return false;
466 }
467
philipel227f8b92017-08-04 06:39:31 -0700468 RTC_DCHECK_EQ(data_buffer_[index].seqNum, sequence_buffer_[index].seq_num);
philipelc18f1d72017-08-02 04:18:02 -0700469 size_t length = data_buffer_[index].sizeBytes;
philipel227f8b92017-08-04 06:39:31 -0700470 if (destination + length > destination_end) {
philipel0fa82a62018-03-19 15:34:53 +0100471 RTC_LOG(LS_WARNING) << "Frame (" << frame.id.picture_id << ":"
472 << static_cast<int>(frame.id.spatial_layer) << ")"
Mirko Bonadei675513b2017-11-09 11:09:25 +0100473 << " bitstream buffer is not large enough.";
philipel227f8b92017-08-04 06:39:31 -0700474 return false;
475 }
476
477 const uint8_t* source = data_buffer_[index].dataPtr;
philipelc707ab72016-04-01 02:01:54 -0700478 memcpy(destination, source, length);
479 destination += length;
480 index = (index + 1) % size_;
481 ++seq_num;
philipel227f8b92017-08-04 06:39:31 -0700482 } while (index != end);
483
philipelc707ab72016-04-01 02:01:54 -0700484 return true;
485}
486
philipel02447bc2016-05-13 06:01:03 -0700487VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) {
philipel02447bc2016-05-13 06:01:03 -0700488 size_t index = seq_num % size_;
489 if (!sequence_buffer_[index].used ||
490 seq_num != sequence_buffer_[index].seq_num) {
491 return nullptr;
philipelf4139332016-04-20 10:26:34 +0200492 }
philipel02447bc2016-05-13 06:01:03 -0700493 return &data_buffer_[index];
philipelf4139332016-04-20 10:26:34 +0200494}
495
philipel17deeb42016-08-11 15:09:26 +0200496int PacketBuffer::AddRef() const {
497 return rtc::AtomicOps::Increment(&ref_count_);
498}
499
500int PacketBuffer::Release() const {
501 int count = rtc::AtomicOps::Decrement(&ref_count_);
502 if (!count) {
503 delete this;
504 }
505 return count;
506}
507
philipel2c9f9f22017-06-13 02:47:28 -0700508void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) {
509 if (!newest_inserted_seq_num_)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100510 newest_inserted_seq_num_ = seq_num;
philipel2c9f9f22017-06-13 02:47:28 -0700511
512 const int kMaxPaddingAge = 1000;
513 if (AheadOf(seq_num, *newest_inserted_seq_num_)) {
514 uint16_t old_seq_num = seq_num - kMaxPaddingAge;
515 auto erase_to = missing_packets_.lower_bound(old_seq_num);
516 missing_packets_.erase(missing_packets_.begin(), erase_to);
517
518 // Guard against inserting a large amount of missing packets if there is a
519 // jump in the sequence number.
520 if (AheadOf(old_seq_num, *newest_inserted_seq_num_))
521 *newest_inserted_seq_num_ = old_seq_num;
522
523 ++*newest_inserted_seq_num_;
524 while (AheadOf(seq_num, *newest_inserted_seq_num_)) {
525 missing_packets_.insert(*newest_inserted_seq_num_);
526 ++*newest_inserted_seq_num_;
527 }
528 } else {
529 missing_packets_.erase(seq_num);
530 }
531}
532
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100533void PacketBuffer::OnTimestampReceived(uint32_t rtp_timestamp) {
534 const size_t kMaxTimestampsHistory = 1000;
535 if (rtp_timestamps_history_set_.insert(rtp_timestamp).second) {
536 rtp_timestamps_history_queue_.push(rtp_timestamp);
537 ++unique_frames_seen_;
538 if (rtp_timestamps_history_set_.size() > kMaxTimestampsHistory) {
539 uint32_t discarded_timestamp = rtp_timestamps_history_queue_.front();
540 rtp_timestamps_history_set_.erase(discarded_timestamp);
541 rtp_timestamps_history_queue_.pop();
542 }
543 }
544}
545
philipelc707ab72016-04-01 02:01:54 -0700546} // namespace video_coding
547} // namespace webrtc