philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/packet_buffer.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <limits> |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 15 | #include <sstream> |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 16 | #include <utility> |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "common_video/h264/h264_common.h" |
| 19 | #include "modules/video_coding/frame_object.h" |
| 20 | #include "rtc_base/atomicops.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/logging.h" |
| 23 | #include "system_wrappers/include/clock.h" |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 24 | #include "system_wrappers/include/field_trial.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | namespace video_coding { |
| 28 | |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 29 | rtc::scoped_refptr<PacketBuffer> PacketBuffer::Create( |
| 30 | Clock* clock, |
| 31 | size_t start_buffer_size, |
| 32 | size_t max_buffer_size, |
| 33 | OnReceivedFrameCallback* received_frame_callback) { |
| 34 | return rtc::scoped_refptr<PacketBuffer>(new PacketBuffer( |
| 35 | clock, start_buffer_size, max_buffer_size, received_frame_callback)); |
| 36 | } |
| 37 | |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 38 | PacketBuffer::PacketBuffer(Clock* clock, |
| 39 | size_t start_buffer_size, |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 40 | size_t max_buffer_size, |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 41 | OnReceivedFrameCallback* received_frame_callback) |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 42 | : clock_(clock), |
| 43 | size_(start_buffer_size), |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 44 | max_size_(max_buffer_size), |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 45 | first_seq_num_(0), |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 46 | first_packet_received_(false), |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 47 | is_cleared_to_first_seq_num_(false), |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 48 | data_buffer_(start_buffer_size), |
| 49 | sequence_buffer_(start_buffer_size), |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 50 | received_frame_callback_(received_frame_callback), |
Ilya Nikolaevskiy | d397a0d | 2018-02-21 15:57:09 +0100 | [diff] [blame] | 51 | unique_frames_seen_(0), |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 52 | sps_pps_idr_is_h264_keyframe_( |
| 53 | field_trial::IsEnabled("WebRTC-SpsPpsIdrIsH264Keyframe")) { |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 54 | RTC_DCHECK_LE(start_buffer_size, max_buffer_size); |
| 55 | // Buffer size must always be a power of 2. |
| 56 | RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0); |
| 57 | RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0); |
| 58 | } |
| 59 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 60 | PacketBuffer::~PacketBuffer() { |
| 61 | Clear(); |
| 62 | } |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 63 | |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 64 | bool PacketBuffer::InsertPacket(VCMPacket* packet) { |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 65 | std::vector<std::unique_ptr<RtpFrameObject>> found_frames; |
| 66 | { |
| 67 | rtc::CritScope lock(&crit_); |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 68 | |
Ilya Nikolaevskiy | d397a0d | 2018-02-21 15:57:09 +0100 | [diff] [blame] | 69 | OnTimestampReceived(packet->timestamp); |
| 70 | |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 71 | uint16_t seq_num = packet->seqNum; |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 72 | size_t index = seq_num % size_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 73 | |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 74 | if (!first_packet_received_) { |
| 75 | first_seq_num_ = seq_num; |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 76 | first_packet_received_ = true; |
| 77 | } else if (AheadOf(first_seq_num_, seq_num)) { |
| 78 | // If we have explicitly cleared past this packet then it's old, |
| 79 | // don't insert it. |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 80 | if (is_cleared_to_first_seq_num_) { |
| 81 | delete[] packet->dataPtr; |
| 82 | packet->dataPtr = nullptr; |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 83 | return false; |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 84 | } |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 85 | |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 86 | first_seq_num_ = seq_num; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 87 | } |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 88 | |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 89 | if (sequence_buffer_[index].used) { |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 90 | // Duplicate packet, just delete the payload. |
| 91 | if (data_buffer_[index].seqNum == packet->seqNum) { |
| 92 | delete[] packet->dataPtr; |
| 93 | packet->dataPtr = nullptr; |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 94 | return true; |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 95 | } |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 96 | |
| 97 | // The packet buffer is full, try to expand the buffer. |
| 98 | while (ExpandBufferSize() && sequence_buffer_[seq_num % size_].used) { |
| 99 | } |
| 100 | index = seq_num % size_; |
| 101 | |
| 102 | // Packet buffer is still full. |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 103 | if (sequence_buffer_[index].used) { |
| 104 | delete[] packet->dataPtr; |
| 105 | packet->dataPtr = nullptr; |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 106 | return false; |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 107 | } |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 108 | } |
| 109 | |
johan | 0d1b2b6 | 2017-01-10 04:21:35 -0800 | [diff] [blame] | 110 | sequence_buffer_[index].frame_begin = packet->is_first_packet_in_frame; |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 111 | sequence_buffer_[index].frame_end = packet->markerBit; |
| 112 | sequence_buffer_[index].seq_num = packet->seqNum; |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 113 | sequence_buffer_[index].continuous = false; |
| 114 | sequence_buffer_[index].frame_created = false; |
| 115 | sequence_buffer_[index].used = true; |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 116 | data_buffer_[index] = *packet; |
| 117 | packet->dataPtr = nullptr; |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 118 | |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 119 | UpdateMissingPackets(packet->seqNum); |
| 120 | |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 121 | int64_t now_ms = clock_->TimeInMilliseconds(); |
Oskar Sundbom | 6bd3902 | 2017-11-16 10:54:49 +0100 | [diff] [blame] | 122 | last_received_packet_ms_ = now_ms; |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 123 | if (packet->frameType == kVideoFrameKey) |
Oskar Sundbom | 6bd3902 | 2017-11-16 10:54:49 +0100 | [diff] [blame] | 124 | last_received_keyframe_packet_ms_ = now_ms; |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 125 | |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 126 | found_frames = FindFrames(seq_num); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 127 | } |
| 128 | |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 129 | for (std::unique_ptr<RtpFrameObject>& frame : found_frames) |
| 130 | received_frame_callback_->OnReceivedFrame(std::move(frame)); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 131 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | |
| 135 | void PacketBuffer::ClearTo(uint16_t seq_num) { |
| 136 | rtc::CritScope lock(&crit_); |
philipel | c5fb468 | 2017-08-02 04:28:57 -0700 | [diff] [blame] | 137 | // We have already cleared past this sequence number, no need to do anything. |
| 138 | if (is_cleared_to_first_seq_num_ && |
| 139 | AheadOf<uint16_t>(first_seq_num_, seq_num)) { |
| 140 | return; |
| 141 | } |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 142 | |
| 143 | // If the packet buffer was cleared between a frame was created and returned. |
| 144 | if (!first_packet_received_) |
| 145 | return; |
| 146 | |
philipel | c5fb468 | 2017-08-02 04:28:57 -0700 | [diff] [blame] | 147 | // Avoid iterating over the buffer more than once by capping the number of |
| 148 | // iterations to the |size_| of the buffer. |
| 149 | ++seq_num; |
| 150 | size_t diff = ForwardDiff<uint16_t>(first_seq_num_, seq_num); |
| 151 | size_t iterations = std::min(diff, size_); |
| 152 | for (size_t i = 0; i < iterations; ++i) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 153 | size_t index = first_seq_num_ % size_; |
philipel | c5fb468 | 2017-08-02 04:28:57 -0700 | [diff] [blame] | 154 | RTC_DCHECK_EQ(data_buffer_[index].seqNum, sequence_buffer_[index].seq_num); |
| 155 | if (AheadOf<uint16_t>(seq_num, sequence_buffer_[index].seq_num)) { |
| 156 | delete[] data_buffer_[index].dataPtr; |
| 157 | data_buffer_[index].dataPtr = nullptr; |
| 158 | sequence_buffer_[index].used = false; |
| 159 | } |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 160 | ++first_seq_num_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 161 | } |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 162 | |
philipel | c5fb468 | 2017-08-02 04:28:57 -0700 | [diff] [blame] | 163 | // If |diff| is larger than |iterations| it means that we don't increment |
| 164 | // |first_seq_num_| until we reach |seq_num|, so we set it here. |
| 165 | first_seq_num_ = seq_num; |
| 166 | |
| 167 | is_cleared_to_first_seq_num_ = true; |
philipel | bc5a408 | 2017-12-06 10:41:08 +0100 | [diff] [blame] | 168 | auto clear_to_it = missing_packets_.upper_bound(seq_num); |
| 169 | if (clear_to_it != missing_packets_.begin()) { |
| 170 | --clear_to_it; |
| 171 | missing_packets_.erase(missing_packets_.begin(), clear_to_it); |
| 172 | } |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 173 | } |
| 174 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 175 | void PacketBuffer::Clear() { |
| 176 | rtc::CritScope lock(&crit_); |
| 177 | for (size_t i = 0; i < size_; ++i) { |
| 178 | delete[] data_buffer_[i].dataPtr; |
| 179 | data_buffer_[i].dataPtr = nullptr; |
| 180 | sequence_buffer_[i].used = false; |
| 181 | } |
| 182 | |
| 183 | first_packet_received_ = false; |
| 184 | is_cleared_to_first_seq_num_ = false; |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 185 | last_received_packet_ms_.reset(); |
| 186 | last_received_keyframe_packet_ms_.reset(); |
| 187 | newest_inserted_seq_num_.reset(); |
| 188 | missing_packets_.clear(); |
| 189 | } |
| 190 | |
| 191 | void PacketBuffer::PaddingReceived(uint16_t seq_num) { |
| 192 | std::vector<std::unique_ptr<RtpFrameObject>> found_frames; |
| 193 | { |
| 194 | rtc::CritScope lock(&crit_); |
| 195 | UpdateMissingPackets(seq_num); |
| 196 | found_frames = FindFrames(static_cast<uint16_t>(seq_num + 1)); |
| 197 | } |
| 198 | |
| 199 | for (std::unique_ptr<RtpFrameObject>& frame : found_frames) |
| 200 | received_frame_callback_->OnReceivedFrame(std::move(frame)); |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | rtc::Optional<int64_t> PacketBuffer::LastReceivedPacketMs() const { |
| 204 | rtc::CritScope lock(&crit_); |
| 205 | return last_received_packet_ms_; |
| 206 | } |
| 207 | |
| 208 | rtc::Optional<int64_t> PacketBuffer::LastReceivedKeyframePacketMs() const { |
| 209 | rtc::CritScope lock(&crit_); |
| 210 | return last_received_keyframe_packet_ms_; |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Ilya Nikolaevskiy | d397a0d | 2018-02-21 15:57:09 +0100 | [diff] [blame] | 213 | int PacketBuffer::GetUniqueFramesSeen() const { |
| 214 | rtc::CritScope lock(&crit_); |
| 215 | return unique_frames_seen_; |
| 216 | } |
| 217 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 218 | bool PacketBuffer::ExpandBufferSize() { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 219 | if (size_ == max_size_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 220 | RTC_LOG(LS_WARNING) << "PacketBuffer is already at max size (" << max_size_ |
| 221 | << "), failed to increase size. Clearing PacketBuffer."; |
philipel | c703dc2 | 2017-03-23 06:50:37 -0700 | [diff] [blame] | 222 | Clear(); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 223 | return false; |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 224 | } |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 225 | |
| 226 | size_t new_size = std::min(max_size_, 2 * size_); |
| 227 | std::vector<VCMPacket> new_data_buffer(new_size); |
| 228 | std::vector<ContinuityInfo> new_sequence_buffer(new_size); |
| 229 | for (size_t i = 0; i < size_; ++i) { |
| 230 | if (sequence_buffer_[i].used) { |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 231 | size_t index = sequence_buffer_[i].seq_num % new_size; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 232 | new_sequence_buffer[index] = sequence_buffer_[i]; |
| 233 | new_data_buffer[index] = data_buffer_[i]; |
| 234 | } |
| 235 | } |
| 236 | size_ = new_size; |
| 237 | sequence_buffer_ = std::move(new_sequence_buffer); |
| 238 | data_buffer_ = std::move(new_data_buffer); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 239 | RTC_LOG(LS_INFO) << "PacketBuffer size expanded to " << new_size; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 240 | return true; |
| 241 | } |
| 242 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 243 | bool PacketBuffer::PotentialNewFrame(uint16_t seq_num) const { |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 244 | size_t index = seq_num % size_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 245 | int prev_index = index > 0 ? index - 1 : size_ - 1; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 246 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 247 | if (!sequence_buffer_[index].used) |
| 248 | return false; |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 249 | if (sequence_buffer_[index].seq_num != seq_num) |
| 250 | return false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 251 | if (sequence_buffer_[index].frame_created) |
| 252 | return false; |
philipel | 20dce34 | 2016-11-28 16:14:57 +0100 | [diff] [blame] | 253 | if (sequence_buffer_[index].frame_begin) |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 254 | return true; |
| 255 | if (!sequence_buffer_[prev_index].used) |
| 256 | return false; |
philipel | ea142f8 | 2017-01-11 02:01:56 -0800 | [diff] [blame] | 257 | if (sequence_buffer_[prev_index].frame_created) |
| 258 | return false; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 259 | if (sequence_buffer_[prev_index].seq_num != |
philipel | 2c2f34c | 2017-01-03 05:55:34 -0800 | [diff] [blame] | 260 | static_cast<uint16_t>(sequence_buffer_[index].seq_num - 1)) { |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 261 | return false; |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 262 | } |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 263 | if (sequence_buffer_[prev_index].continuous) |
| 264 | return true; |
| 265 | |
| 266 | return false; |
| 267 | } |
| 268 | |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 269 | std::vector<std::unique_ptr<RtpFrameObject>> PacketBuffer::FindFrames( |
| 270 | uint16_t seq_num) { |
| 271 | std::vector<std::unique_ptr<RtpFrameObject>> found_frames; |
philipel | 5391071 | 2017-05-18 02:24:40 -0700 | [diff] [blame] | 272 | for (size_t i = 0; i < size_ && PotentialNewFrame(seq_num); ++i) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 273 | size_t index = seq_num % size_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 274 | sequence_buffer_[index].continuous = true; |
| 275 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 276 | // If all packets of the frame is continuous, find the first packet of the |
| 277 | // frame and create an RtpFrameObject. |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 278 | if (sequence_buffer_[index].frame_end) { |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 279 | size_t frame_size = 0; |
| 280 | int max_nack_count = -1; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 281 | uint16_t start_seq_num = seq_num; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 282 | |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 283 | // Find the start index by searching backward until the packet with |
| 284 | // the |frame_begin| flag is set. |
| 285 | int start_index = index; |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 286 | size_t tested_packets = 0; |
philipel | 8c61924 | 2017-02-02 08:51:29 -0800 | [diff] [blame] | 287 | int64_t frame_timestamp = data_buffer_[start_index].timestamp; |
philipel | 5391071 | 2017-05-18 02:24:40 -0700 | [diff] [blame] | 288 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 289 | // Identify H.264 keyframes by means of SPS, PPS, and IDR. |
| 290 | bool is_h264 = data_buffer_[start_index].codec == kVideoCodecH264; |
| 291 | bool has_h264_sps = false; |
| 292 | bool has_h264_pps = false; |
| 293 | bool has_h264_idr = false; |
| 294 | bool is_h264_keyframe = false; |
| 295 | |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 296 | while (true) { |
| 297 | ++tested_packets; |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 298 | frame_size += data_buffer_[start_index].sizeBytes; |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 299 | max_nack_count = |
| 300 | std::max(max_nack_count, data_buffer_[start_index].timesNacked); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 301 | sequence_buffer_[start_index].frame_created = true; |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 302 | |
philipel | 8c61924 | 2017-02-02 08:51:29 -0800 | [diff] [blame] | 303 | if (!is_h264 && sequence_buffer_[start_index].frame_begin) |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 304 | break; |
| 305 | |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 306 | if (is_h264 && !is_h264_keyframe) { |
| 307 | const RTPVideoHeaderH264& header = |
| 308 | data_buffer_[start_index].video_header.codecHeader.H264; |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 309 | for (size_t j = 0; j < header.nalus_length; ++j) { |
| 310 | if (header.nalus[j].type == H264::NaluType::kSps) { |
| 311 | has_h264_sps = true; |
| 312 | } else if (header.nalus[j].type == H264::NaluType::kPps) { |
| 313 | has_h264_pps = true; |
| 314 | } else if (header.nalus[j].type == H264::NaluType::kIdr) { |
| 315 | has_h264_idr = true; |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 318 | if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps && |
| 319 | has_h264_pps) || |
| 320 | (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) { |
| 321 | is_h264_keyframe = true; |
| 322 | } |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 323 | } |
| 324 | |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 325 | if (tested_packets == size_) |
| 326 | break; |
| 327 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 328 | start_index = start_index > 0 ? start_index - 1 : size_ - 1; |
philipel | 8c61924 | 2017-02-02 08:51:29 -0800 | [diff] [blame] | 329 | |
| 330 | // In the case of H264 we don't have a frame_begin bit (yes, |
| 331 | // |frame_begin| might be set to true but that is a lie). So instead |
| 332 | // we traverese backwards as long as we have a previous packet and |
| 333 | // the timestamp of that packet is the same as this one. This may cause |
| 334 | // the PacketBuffer to hand out incomplete frames. |
| 335 | // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106 |
philipel | 5391071 | 2017-05-18 02:24:40 -0700 | [diff] [blame] | 336 | if (is_h264 && |
philipel | 8c61924 | 2017-02-02 08:51:29 -0800 | [diff] [blame] | 337 | (!sequence_buffer_[start_index].used || |
| 338 | data_buffer_[start_index].timestamp != frame_timestamp)) { |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | --start_seq_num; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 345 | if (is_h264) { |
| 346 | // Warn if this is an unsafe frame. |
| 347 | if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) { |
| 348 | std::stringstream ss; |
| 349 | ss << "Received H.264-IDR frame " |
| 350 | << "(SPS: " << has_h264_sps << ", PPS: " << has_h264_pps << "). "; |
| 351 | if (sps_pps_idr_is_h264_keyframe_) { |
| 352 | ss << "Treating as delta frame since " |
| 353 | "WebRTC-SpsPpsIdrIsH264Keyframe is enabled."; |
| 354 | } else { |
| 355 | ss << "Treating as key frame since " |
| 356 | "WebRTC-SpsPpsIdrIsH264Keyframe is disabled."; |
| 357 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 358 | RTC_LOG(LS_WARNING) << ss.str(); |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 361 | // Now that we have decided whether to treat this frame as a key frame |
| 362 | // or delta frame in the frame buffer, we update the field that |
| 363 | // determines if the RtpFrameObject is a key frame or delta frame. |
| 364 | const size_t first_packet_index = start_seq_num % size_; |
| 365 | RTC_CHECK_LT(first_packet_index, size_); |
| 366 | if (is_h264_keyframe) { |
| 367 | data_buffer_[first_packet_index].frameType = kVideoFrameKey; |
| 368 | } else { |
| 369 | data_buffer_[first_packet_index].frameType = kVideoFrameDelta; |
| 370 | } |
| 371 | |
| 372 | // If this is not a keyframe, make sure there are no gaps in the |
| 373 | // packet sequence numbers up until this point. |
| 374 | if (!is_h264_keyframe && missing_packets_.upper_bound(start_seq_num) != |
| 375 | missing_packets_.begin()) { |
| 376 | uint16_t stop_index = (index + 1) % size_; |
| 377 | while (start_index != stop_index) { |
| 378 | sequence_buffer_[start_index].frame_created = false; |
| 379 | start_index = (start_index + 1) % size_; |
| 380 | } |
| 381 | |
| 382 | return found_frames; |
| 383 | } |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | missing_packets_.erase(missing_packets_.begin(), |
| 387 | missing_packets_.upper_bound(seq_num)); |
| 388 | |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 389 | found_frames.emplace_back( |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 390 | new RtpFrameObject(this, start_seq_num, seq_num, frame_size, |
| 391 | max_nack_count, clock_->TimeInMilliseconds())); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 392 | } |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 393 | ++seq_num; |
| 394 | } |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 395 | return found_frames; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void PacketBuffer::ReturnFrame(RtpFrameObject* frame) { |
| 399 | rtc::CritScope lock(&crit_); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 400 | size_t index = frame->first_seq_num() % size_; |
| 401 | size_t end = (frame->last_seq_num() + 1) % size_; |
| 402 | uint16_t seq_num = frame->first_seq_num(); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 403 | while (index != end) { |
philipel | 1f39ba1 | 2016-09-21 11:27:47 +0200 | [diff] [blame] | 404 | if (sequence_buffer_[index].seq_num == seq_num) { |
| 405 | delete[] data_buffer_[index].dataPtr; |
| 406 | data_buffer_[index].dataPtr = nullptr; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 407 | sequence_buffer_[index].used = false; |
philipel | 1f39ba1 | 2016-09-21 11:27:47 +0200 | [diff] [blame] | 408 | } |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 409 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 410 | index = (index + 1) % size_; |
| 411 | ++seq_num; |
| 412 | } |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | bool PacketBuffer::GetBitstream(const RtpFrameObject& frame, |
| 416 | uint8_t* destination) { |
| 417 | rtc::CritScope lock(&crit_); |
| 418 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 419 | size_t index = frame.first_seq_num() % size_; |
| 420 | size_t end = (frame.last_seq_num() + 1) % size_; |
| 421 | uint16_t seq_num = frame.first_seq_num(); |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 422 | uint8_t* destination_end = destination + frame.size(); |
| 423 | |
| 424 | do { |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 425 | if (!sequence_buffer_[index].used || |
| 426 | sequence_buffer_[index].seq_num != seq_num) { |
| 427 | return false; |
| 428 | } |
| 429 | |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 430 | RTC_DCHECK_EQ(data_buffer_[index].seqNum, sequence_buffer_[index].seq_num); |
philipel | c18f1d7 | 2017-08-02 04:18:02 -0700 | [diff] [blame] | 431 | size_t length = data_buffer_[index].sizeBytes; |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 432 | if (destination + length > destination_end) { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 433 | RTC_LOG(LS_WARNING) << "Frame (" << frame.id.picture_id << ":" |
| 434 | << static_cast<int>(frame.id.spatial_layer) << ")" |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 435 | << " bitstream buffer is not large enough."; |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 436 | return false; |
| 437 | } |
| 438 | |
| 439 | const uint8_t* source = data_buffer_[index].dataPtr; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 440 | memcpy(destination, source, length); |
| 441 | destination += length; |
| 442 | index = (index + 1) % size_; |
| 443 | ++seq_num; |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 444 | } while (index != end); |
| 445 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 446 | return true; |
| 447 | } |
| 448 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 449 | VCMPacket* PacketBuffer::GetPacket(uint16_t seq_num) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 450 | size_t index = seq_num % size_; |
| 451 | if (!sequence_buffer_[index].used || |
| 452 | seq_num != sequence_buffer_[index].seq_num) { |
| 453 | return nullptr; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 454 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 455 | return &data_buffer_[index]; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 456 | } |
| 457 | |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 458 | int PacketBuffer::AddRef() const { |
| 459 | return rtc::AtomicOps::Increment(&ref_count_); |
| 460 | } |
| 461 | |
| 462 | int PacketBuffer::Release() const { |
| 463 | int count = rtc::AtomicOps::Decrement(&ref_count_); |
| 464 | if (!count) { |
| 465 | delete this; |
| 466 | } |
| 467 | return count; |
| 468 | } |
| 469 | |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 470 | void PacketBuffer::UpdateMissingPackets(uint16_t seq_num) { |
| 471 | if (!newest_inserted_seq_num_) |
Oskar Sundbom | 6bd3902 | 2017-11-16 10:54:49 +0100 | [diff] [blame] | 472 | newest_inserted_seq_num_ = seq_num; |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 473 | |
| 474 | const int kMaxPaddingAge = 1000; |
| 475 | if (AheadOf(seq_num, *newest_inserted_seq_num_)) { |
| 476 | uint16_t old_seq_num = seq_num - kMaxPaddingAge; |
| 477 | auto erase_to = missing_packets_.lower_bound(old_seq_num); |
| 478 | missing_packets_.erase(missing_packets_.begin(), erase_to); |
| 479 | |
| 480 | // Guard against inserting a large amount of missing packets if there is a |
| 481 | // jump in the sequence number. |
| 482 | if (AheadOf(old_seq_num, *newest_inserted_seq_num_)) |
| 483 | *newest_inserted_seq_num_ = old_seq_num; |
| 484 | |
| 485 | ++*newest_inserted_seq_num_; |
| 486 | while (AheadOf(seq_num, *newest_inserted_seq_num_)) { |
| 487 | missing_packets_.insert(*newest_inserted_seq_num_); |
| 488 | ++*newest_inserted_seq_num_; |
| 489 | } |
| 490 | } else { |
| 491 | missing_packets_.erase(seq_num); |
| 492 | } |
| 493 | } |
| 494 | |
Ilya Nikolaevskiy | d397a0d | 2018-02-21 15:57:09 +0100 | [diff] [blame] | 495 | void PacketBuffer::OnTimestampReceived(uint32_t rtp_timestamp) { |
| 496 | const size_t kMaxTimestampsHistory = 1000; |
| 497 | if (rtp_timestamps_history_set_.insert(rtp_timestamp).second) { |
| 498 | rtp_timestamps_history_queue_.push(rtp_timestamp); |
| 499 | ++unique_frames_seen_; |
| 500 | if (rtp_timestamps_history_set_.size() > kMaxTimestampsHistory) { |
| 501 | uint32_t discarded_timestamp = rtp_timestamps_history_queue_.front(); |
| 502 | rtp_timestamps_history_set_.erase(discarded_timestamp); |
| 503 | rtp_timestamps_history_queue_.pop(); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 508 | } // namespace video_coding |
| 509 | } // namespace webrtc |