niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 1 | /* |
stefan@webrtc.org | 94355e0 | 2012-02-06 14:06:39 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 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 | |
pbos@webrtc.org | a440732 | 2013-07-16 12:32:05 | [diff] [blame] | 11 | #include "webrtc/modules/video_coding/main/source/session_info.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 12 | |
pbos@webrtc.org | a440732 | 2013-07-16 12:32:05 | [diff] [blame] | 13 | #include "webrtc/modules/video_coding/main/source/packet.h" |
mikhal@webrtc.org | e185e9f | 2011-09-23 22:02:40 | [diff] [blame] | 14 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 15 | namespace webrtc { |
| 16 | |
agalusza@google.com | d818dcb | 2013-07-29 21:48:11 | [diff] [blame] | 17 | // Used in determining whether a frame is decodable. |
| 18 | enum {kRttThreshold = 100}; // Not decodable if Rtt is lower than this. |
| 19 | |
| 20 | // Do not decode frames if the number of packets is between these two |
| 21 | // thresholds. |
| 22 | static const float kLowPacketPercentageThreshold = 0.2f; |
| 23 | static const float kHighPacketPercentageThreshold = 0.8f; |
| 24 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 25 | VCMSessionInfo::VCMSessionInfo() |
| 26 | : session_nack_(false), |
| 27 | complete_(false), |
| 28 | decodable_(false), |
| 29 | frame_type_(kVideoFrameDelta), |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 30 | packets_(), |
| 31 | empty_seq_num_low_(-1), |
| 32 | empty_seq_num_high_(-1), |
mikhal@webrtc.org | f31a47a | 2013-08-26 17:10:11 | [diff] [blame] | 33 | first_packet_seq_num_(-1), |
| 34 | last_packet_seq_num_(-1) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 35 | } |
| 36 | |
stefan@webrtc.org | b07aa40 | 2012-01-10 11:45:05 | [diff] [blame] | 37 | void VCMSessionInfo::UpdateDataPointers(const uint8_t* old_base_ptr, |
| 38 | const uint8_t* new_base_ptr) { |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 39 | for (PacketIterator it = packets_.begin(); it != packets_.end(); ++it) |
stefan@webrtc.org | b07aa40 | 2012-01-10 11:45:05 | [diff] [blame] | 40 | if ((*it).dataPtr != NULL) { |
| 41 | assert(old_base_ptr != NULL && new_base_ptr != NULL); |
| 42 | (*it).dataPtr = new_base_ptr + ((*it).dataPtr - old_base_ptr); |
| 43 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 44 | } |
| 45 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 46 | int VCMSessionInfo::LowSequenceNumber() const { |
| 47 | if (packets_.empty()) |
| 48 | return empty_seq_num_low_; |
| 49 | return packets_.front().seqNum; |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 50 | } |
| 51 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 52 | int VCMSessionInfo::HighSequenceNumber() const { |
| 53 | if (packets_.empty()) |
| 54 | return empty_seq_num_high_; |
stefan@webrtc.org | 7bc465b | 2013-04-11 17:48:02 | [diff] [blame] | 55 | if (empty_seq_num_high_ == -1) |
| 56 | return packets_.back().seqNum; |
| 57 | return LatestSequenceNumber(packets_.back().seqNum, empty_seq_num_high_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 58 | } |
| 59 | |
stefan@webrtc.org | ffd28f9 | 2011-10-19 15:55:39 | [diff] [blame] | 60 | int VCMSessionInfo::PictureId() const { |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 61 | if (packets_.empty() || |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 62 | packets_.front().codecSpecificHeader.codec != kRtpVideoVp8) |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 63 | return kNoPictureId; |
| 64 | return packets_.front().codecSpecificHeader.codecHeader.VP8.pictureId; |
stefan@webrtc.org | ffd28f9 | 2011-10-19 15:55:39 | [diff] [blame] | 65 | } |
| 66 | |
mikhal@webrtc.org | f5ee1dc | 2011-12-08 19:04:47 | [diff] [blame] | 67 | int VCMSessionInfo::TemporalId() const { |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 68 | if (packets_.empty() || |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 69 | packets_.front().codecSpecificHeader.codec != kRtpVideoVp8) |
mikhal@webrtc.org | f5ee1dc | 2011-12-08 19:04:47 | [diff] [blame] | 70 | return kNoTemporalIdx; |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 71 | return packets_.front().codecSpecificHeader.codecHeader.VP8.temporalIdx; |
mikhal@webrtc.org | f5ee1dc | 2011-12-08 19:04:47 | [diff] [blame] | 72 | } |
| 73 | |
henrik.lundin@webrtc.org | eda86dc | 2011-12-13 14:11:06 | [diff] [blame] | 74 | bool VCMSessionInfo::LayerSync() const { |
| 75 | if (packets_.empty() || |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 76 | packets_.front().codecSpecificHeader.codec != kRtpVideoVp8) |
henrik.lundin@webrtc.org | eda86dc | 2011-12-13 14:11:06 | [diff] [blame] | 77 | return false; |
| 78 | return packets_.front().codecSpecificHeader.codecHeader.VP8.layerSync; |
| 79 | } |
| 80 | |
mikhal@webrtc.org | f5ee1dc | 2011-12-08 19:04:47 | [diff] [blame] | 81 | int VCMSessionInfo::Tl0PicId() const { |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 82 | if (packets_.empty() || |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 83 | packets_.front().codecSpecificHeader.codec != kRtpVideoVp8) |
mikhal@webrtc.org | f5ee1dc | 2011-12-08 19:04:47 | [diff] [blame] | 84 | return kNoTl0PicIdx; |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 85 | return packets_.front().codecSpecificHeader.codecHeader.VP8.tl0PicIdx; |
mikhal@webrtc.org | f5ee1dc | 2011-12-08 19:04:47 | [diff] [blame] | 86 | } |
| 87 | |
mikhal@webrtc.org | ea71440 | 2011-12-12 02:29:34 | [diff] [blame] | 88 | bool VCMSessionInfo::NonReference() const { |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 89 | if (packets_.empty() || |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 90 | packets_.front().codecSpecificHeader.codec != kRtpVideoVp8) |
mikhal@webrtc.org | ea71440 | 2011-12-12 02:29:34 | [diff] [blame] | 91 | return false; |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 92 | return packets_.front().codecSpecificHeader.codecHeader.VP8.nonReference; |
mikhal@webrtc.org | ea71440 | 2011-12-12 02:29:34 | [diff] [blame] | 93 | } |
| 94 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 95 | void VCMSessionInfo::Reset() { |
| 96 | session_nack_ = false; |
| 97 | complete_ = false; |
| 98 | decodable_ = false; |
| 99 | frame_type_ = kVideoFrameDelta; |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 100 | packets_.clear(); |
| 101 | empty_seq_num_low_ = -1; |
| 102 | empty_seq_num_high_ = -1; |
mikhal@webrtc.org | f31a47a | 2013-08-26 17:10:11 | [diff] [blame] | 103 | first_packet_seq_num_ = -1; |
| 104 | last_packet_seq_num_ = -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 105 | } |
| 106 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 107 | int VCMSessionInfo::SessionLength() const { |
| 108 | int length = 0; |
| 109 | for (PacketIteratorConst it = packets_.begin(); it != packets_.end(); ++it) |
| 110 | length += (*it).sizeBytes; |
| 111 | return length; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 112 | } |
| 113 | |
agalusza@google.com | d818dcb | 2013-07-29 21:48:11 | [diff] [blame] | 114 | int VCMSessionInfo::NumPackets() const { |
| 115 | return packets_.size(); |
| 116 | } |
| 117 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 118 | int VCMSessionInfo::InsertBuffer(uint8_t* frame_buffer, |
| 119 | PacketIterator packet_it) { |
| 120 | VCMPacket& packet = *packet_it; |
| 121 | PacketIterator it; |
| 122 | |
| 123 | int packet_size = packet.sizeBytes; |
pwestin@webrtc.org | 5621057 | 2012-01-17 12:45:47 | [diff] [blame] | 124 | packet_size += (packet.insertStartCode ? kH264StartCodeLengthBytes : 0); |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 125 | |
| 126 | // Calculate the offset into the frame buffer for this packet. |
| 127 | int offset = 0; |
| 128 | for (it = packets_.begin(); it != packet_it; ++it) |
| 129 | offset += (*it).sizeBytes; |
| 130 | |
| 131 | // Set the data pointer to pointing to the start of this packet in the |
| 132 | // frame buffer. |
| 133 | const uint8_t* data = packet.dataPtr; |
| 134 | packet.dataPtr = frame_buffer + offset; |
| 135 | packet.sizeBytes = packet_size; |
| 136 | |
| 137 | ShiftSubsequentPackets(packet_it, packet_size); |
| 138 | |
| 139 | const unsigned char startCode[] = {0, 0, 0, 1}; |
| 140 | if (packet.insertStartCode) { |
| 141 | memcpy(const_cast<uint8_t*>(packet.dataPtr), startCode, |
| 142 | kH264StartCodeLengthBytes); |
| 143 | } |
| 144 | memcpy(const_cast<uint8_t*>(packet.dataPtr |
| 145 | + (packet.insertStartCode ? kH264StartCodeLengthBytes : 0)), |
| 146 | data, |
| 147 | packet.sizeBytes); |
| 148 | |
| 149 | return packet_size; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 150 | } |
| 151 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 152 | void VCMSessionInfo::ShiftSubsequentPackets(PacketIterator it, |
| 153 | int steps_to_shift) { |
| 154 | ++it; |
| 155 | if (it == packets_.end()) |
| 156 | return; |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 | [diff] [blame] | 157 | uint8_t* first_packet_ptr = const_cast<uint8_t*>((*it).dataPtr); |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 158 | int shift_length = 0; |
| 159 | // Calculate the total move length and move the data pointers in advance. |
| 160 | for (; it != packets_.end(); ++it) { |
| 161 | shift_length += (*it).sizeBytes; |
| 162 | if ((*it).dataPtr != NULL) |
| 163 | (*it).dataPtr += steps_to_shift; |
| 164 | } |
| 165 | memmove(first_packet_ptr + steps_to_shift, first_packet_ptr, shift_length); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 166 | } |
| 167 | |
mikhal@webrtc.org | 6b9a7f8 | 2011-11-22 22:48:20 | [diff] [blame] | 168 | void VCMSessionInfo::UpdateCompleteSession() { |
mikhal@webrtc.org | f31a47a | 2013-08-26 17:10:11 | [diff] [blame] | 169 | if (HaveFirstPacket() && HaveLastPacket()) { |
mikhal@webrtc.org | 6b9a7f8 | 2011-11-22 22:48:20 | [diff] [blame] | 170 | // Do we have all the packets in this session? |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 171 | bool complete_session = true; |
| 172 | PacketIterator it = packets_.begin(); |
| 173 | PacketIterator prev_it = it; |
| 174 | ++it; |
| 175 | for (; it != packets_.end(); ++it) { |
| 176 | if (!InSequence(it, prev_it)) { |
| 177 | complete_session = false; |
mikhal@webrtc.org | 6b9a7f8 | 2011-11-22 22:48:20 | [diff] [blame] | 178 | break; |
| 179 | } |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 180 | prev_it = it; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 181 | } |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 182 | complete_ = complete_session; |
mikhal@webrtc.org | 6b9a7f8 | 2011-11-22 22:48:20 | [diff] [blame] | 183 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 184 | } |
| 185 | |
agalusza@google.com | d818dcb | 2013-07-29 21:48:11 | [diff] [blame] | 186 | void VCMSessionInfo::UpdateDecodableSession(const FrameData& frame_data) { |
mikhal@webrtc.org | 6b9a7f8 | 2011-11-22 22:48:20 | [diff] [blame] | 187 | // Irrelevant if session is already complete or decodable |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 188 | if (complete_ || decodable_) |
mikhal@webrtc.org | 6b9a7f8 | 2011-11-22 22:48:20 | [diff] [blame] | 189 | return; |
agalusza@google.com | d818dcb | 2013-07-29 21:48:11 | [diff] [blame] | 190 | |
| 191 | // TODO(agalusza): Account for bursty loss. |
| 192 | // TODO(agalusza): Refine these values to better approximate optimal ones. |
| 193 | if (frame_data.rtt_ms < kRttThreshold |
| 194 | || frame_type_ == kVideoFrameKey |
| 195 | || !HaveFirstPacket() |
| 196 | || (NumPackets() <= kHighPacketPercentageThreshold |
| 197 | * frame_data.rolling_average_packets_per_frame |
| 198 | && NumPackets() > kLowPacketPercentageThreshold |
| 199 | * frame_data.rolling_average_packets_per_frame)) |
| 200 | return; |
| 201 | |
| 202 | decodable_ = true; |
mikhal@webrtc.org | 6b9a7f8 | 2011-11-22 22:48:20 | [diff] [blame] | 203 | } |
| 204 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 205 | bool VCMSessionInfo::complete() const { |
| 206 | return complete_; |
mikhal@webrtc.org | 6b9a7f8 | 2011-11-22 22:48:20 | [diff] [blame] | 207 | } |
| 208 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 209 | bool VCMSessionInfo::decodable() const { |
| 210 | return decodable_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 211 | } |
| 212 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 213 | // Find the end of the NAL unit which the packet pointed to by |packet_it| |
| 214 | // belongs to. Returns an iterator to the last packet of the frame if the end |
| 215 | // of the NAL unit wasn't found. |
| 216 | VCMSessionInfo::PacketIterator VCMSessionInfo::FindNaluEnd( |
| 217 | PacketIterator packet_it) const { |
| 218 | if ((*packet_it).completeNALU == kNaluEnd || |
| 219 | (*packet_it).completeNALU == kNaluComplete) { |
| 220 | return packet_it; |
| 221 | } |
| 222 | // Find the end of the NAL unit. |
| 223 | for (; packet_it != packets_.end(); ++packet_it) { |
| 224 | if (((*packet_it).completeNALU == kNaluComplete && |
| 225 | (*packet_it).sizeBytes > 0) || |
| 226 | // Found next NALU. |
| 227 | (*packet_it).completeNALU == kNaluStart) |
| 228 | return --packet_it; |
| 229 | if ((*packet_it).completeNALU == kNaluEnd) |
| 230 | return packet_it; |
| 231 | } |
| 232 | // The end wasn't found. |
| 233 | return --packet_it; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 234 | } |
| 235 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 236 | int VCMSessionInfo::DeletePacketData(PacketIterator start, |
| 237 | PacketIterator end) { |
| 238 | int bytes_to_delete = 0; // The number of bytes to delete. |
| 239 | PacketIterator packet_after_end = end; |
| 240 | ++packet_after_end; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 241 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 242 | // Get the number of bytes to delete. |
| 243 | // Clear the size of these packets. |
| 244 | for (PacketIterator it = start; it != packet_after_end; ++it) { |
| 245 | bytes_to_delete += (*it).sizeBytes; |
| 246 | (*it).sizeBytes = 0; |
| 247 | (*it).dataPtr = NULL; |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 248 | } |
| 249 | if (bytes_to_delete > 0) |
| 250 | ShiftSubsequentPackets(end, -bytes_to_delete); |
| 251 | return bytes_to_delete; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 252 | } |
| 253 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 254 | int VCMSessionInfo::BuildVP8FragmentationHeader( |
| 255 | uint8_t* frame_buffer, |
| 256 | int frame_buffer_length, |
| 257 | RTPFragmentationHeader* fragmentation) { |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 258 | int new_length = 0; |
| 259 | // Allocate space for max number of partitions |
| 260 | fragmentation->VerifyAndAllocateFragmentationHeader(kMaxVP8Partitions); |
| 261 | fragmentation->fragmentationVectorSize = 0; |
| 262 | memset(fragmentation->fragmentationLength, 0, |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 | [diff] [blame] | 263 | kMaxVP8Partitions * sizeof(uint32_t)); |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 264 | if (packets_.empty()) |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 265 | return new_length; |
mikhal@webrtc.org | 2b810bf | 2013-09-03 19:09:49 | [diff] [blame^] | 266 | PacketIterator it = FindNextPartitionBeginning(packets_.begin()); |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 267 | while (it != packets_.end()) { |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 268 | const int partition_id = |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 269 | (*it).codecSpecificHeader.codecHeader.VP8.partitionId; |
| 270 | PacketIterator partition_end = FindPartitionEnd(it); |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 271 | fragmentation->fragmentationOffset[partition_id] = |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 272 | (*it).dataPtr - frame_buffer; |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 273 | assert(fragmentation->fragmentationOffset[partition_id] < |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 | [diff] [blame] | 274 | static_cast<uint32_t>(frame_buffer_length)); |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 275 | fragmentation->fragmentationLength[partition_id] = |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 276 | (*partition_end).dataPtr + (*partition_end).sizeBytes - (*it).dataPtr; |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 277 | assert(fragmentation->fragmentationLength[partition_id] <= |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 | [diff] [blame] | 278 | static_cast<uint32_t>(frame_buffer_length)); |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 279 | new_length += fragmentation->fragmentationLength[partition_id]; |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 280 | ++partition_end; |
mikhal@webrtc.org | 2b810bf | 2013-09-03 19:09:49 | [diff] [blame^] | 281 | it = FindNextPartitionBeginning(partition_end); |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 282 | if (partition_id + 1 > fragmentation->fragmentationVectorSize) |
| 283 | fragmentation->fragmentationVectorSize = partition_id + 1; |
| 284 | } |
| 285 | // Set all empty fragments to start where the previous fragment ends, |
| 286 | // and have zero length. |
| 287 | if (fragmentation->fragmentationLength[0] == 0) |
| 288 | fragmentation->fragmentationOffset[0] = 0; |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 289 | for (int i = 1; i < fragmentation->fragmentationVectorSize; ++i) { |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 290 | if (fragmentation->fragmentationLength[i] == 0) |
| 291 | fragmentation->fragmentationOffset[i] = |
| 292 | fragmentation->fragmentationOffset[i - 1] + |
| 293 | fragmentation->fragmentationLength[i - 1]; |
| 294 | assert(i == 0 || |
| 295 | fragmentation->fragmentationOffset[i] >= |
| 296 | fragmentation->fragmentationOffset[i - 1]); |
| 297 | } |
| 298 | assert(new_length <= frame_buffer_length); |
| 299 | return new_length; |
| 300 | } |
| 301 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 302 | VCMSessionInfo::PacketIterator VCMSessionInfo::FindNextPartitionBeginning( |
mikhal@webrtc.org | 2b810bf | 2013-09-03 19:09:49 | [diff] [blame^] | 303 | PacketIterator it) const { |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 304 | while (it != packets_.end()) { |
| 305 | if ((*it).codecSpecificHeader.codecHeader.VP8.beginningOfPartition) { |
| 306 | return it; |
stefan@webrtc.org | 4c059d8 | 2011-10-13 07:35:37 | [diff] [blame] | 307 | } |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 308 | ++it; |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 309 | } |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 310 | return it; |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 311 | } |
| 312 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 313 | VCMSessionInfo::PacketIterator VCMSessionInfo::FindPartitionEnd( |
| 314 | PacketIterator it) const { |
| 315 | assert((*it).codec == kVideoCodecVP8); |
| 316 | PacketIterator prev_it = it; |
| 317 | const int partition_id = |
| 318 | (*it).codecSpecificHeader.codecHeader.VP8.partitionId; |
| 319 | while (it != packets_.end()) { |
| 320 | bool beginning = |
| 321 | (*it).codecSpecificHeader.codecHeader.VP8.beginningOfPartition; |
| 322 | int current_partition_id = |
| 323 | (*it).codecSpecificHeader.codecHeader.VP8.partitionId; |
| 324 | bool packet_loss_found = (!beginning && !InSequence(it, prev_it)); |
| 325 | if (packet_loss_found || |
| 326 | (beginning && current_partition_id != partition_id)) { |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 327 | // Missing packet, the previous packet was the last in sequence. |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 328 | return prev_it; |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 329 | } |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 330 | prev_it = it; |
| 331 | ++it; |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 332 | } |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 333 | return prev_it; |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 334 | } |
| 335 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 336 | bool VCMSessionInfo::InSequence(const PacketIterator& packet_it, |
| 337 | const PacketIterator& prev_packet_it) { |
| 338 | // If the two iterators are pointing to the same packet they are considered |
| 339 | // to be in sequence. |
| 340 | return (packet_it == prev_packet_it || |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 | [diff] [blame] | 341 | (static_cast<uint16_t>((*prev_packet_it).seqNum + 1) == |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 342 | (*packet_it).seqNum)); |
stefan@webrtc.org | c3d8910 | 2011-09-08 06:50:28 | [diff] [blame] | 343 | } |
| 344 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 345 | int VCMSessionInfo::MakeDecodable() { |
| 346 | int return_length = 0; |
stefan@webrtc.org | 4ce0ba0 | 2012-02-28 12:09:09 | [diff] [blame] | 347 | if (packets_.empty()) { |
| 348 | return 0; |
| 349 | } |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 350 | PacketIterator it = packets_.begin(); |
| 351 | // Make sure we remove the first NAL unit if it's not decodable. |
| 352 | if ((*it).completeNALU == kNaluIncomplete || |
| 353 | (*it).completeNALU == kNaluEnd) { |
| 354 | PacketIterator nalu_end = FindNaluEnd(it); |
| 355 | return_length += DeletePacketData(it, nalu_end); |
| 356 | it = nalu_end; |
| 357 | } |
| 358 | PacketIterator prev_it = it; |
| 359 | // Take care of the rest of the NAL units. |
| 360 | for (; it != packets_.end(); ++it) { |
| 361 | bool start_of_nalu = ((*it).completeNALU == kNaluStart || |
| 362 | (*it).completeNALU == kNaluComplete); |
| 363 | if (!start_of_nalu && !InSequence(it, prev_it)) { |
| 364 | // Found a sequence number gap due to packet loss. |
| 365 | PacketIterator nalu_end = FindNaluEnd(it); |
| 366 | return_length += DeletePacketData(it, nalu_end); |
| 367 | it = nalu_end; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 368 | } |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 369 | prev_it = it; |
| 370 | } |
| 371 | return return_length; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 372 | } |
| 373 | |
mikhal@webrtc.org | dbf6a81 | 2013-08-21 20:40:47 | [diff] [blame] | 374 | void VCMSessionInfo::SetNotDecodableIfIncomplete() { |
agalusza@google.com | d177c10 | 2013-08-08 01:12:33 | [diff] [blame] | 375 | // We don't need to check for completeness first because the two are |
| 376 | // orthogonal. If complete_ is true, decodable_ is irrelevant. |
| 377 | decodable_ = false; |
| 378 | } |
| 379 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 380 | bool |
stefan@webrtc.org | 885cd13 | 2013-04-16 09:38:26 | [diff] [blame] | 381 | VCMSessionInfo::HaveFirstPacket() const { |
mikhal@webrtc.org | f31a47a | 2013-08-26 17:10:11 | [diff] [blame] | 382 | return !packets_.empty() && (first_packet_seq_num_ != -1); |
stefan@webrtc.org | 885cd13 | 2013-04-16 09:38:26 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | bool |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 386 | VCMSessionInfo::HaveLastPacket() const { |
mikhal@webrtc.org | f31a47a | 2013-08-26 17:10:11 | [diff] [blame] | 387 | return !packets_.empty() && (last_packet_seq_num_ != -1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 388 | } |
| 389 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 390 | bool |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 391 | VCMSessionInfo::session_nack() const { |
| 392 | return session_nack_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 393 | } |
| 394 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 395 | int VCMSessionInfo::InsertPacket(const VCMPacket& packet, |
| 396 | uint8_t* frame_buffer, |
agalusza@google.com | a7e360e | 2013-08-01 03:15:08 | [diff] [blame] | 397 | VCMDecodeErrorMode decode_error_mode, |
agalusza@google.com | d818dcb | 2013-07-29 21:48:11 | [diff] [blame] | 398 | const FrameData& frame_data) { |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 399 | if (packet.frameType == kFrameEmpty) { |
| 400 | // Update sequence number of an empty packet. |
| 401 | // Only media packets are inserted into the packet list. |
| 402 | InformOfEmptyPacket(packet.seqNum); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 403 | return 0; |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 404 | } |
| 405 | |
mikhal@webrtc.org | f31a47a | 2013-08-26 17:10:11 | [diff] [blame] | 406 | if (packets_.size() == kMaxPacketsInSession) { |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 407 | return -1; |
mikhal@webrtc.org | f31a47a | 2013-08-26 17:10:11 | [diff] [blame] | 408 | } |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 409 | |
| 410 | // Find the position of this packet in the packet list in sequence number |
| 411 | // order and insert it. Loop over the list in reverse order. |
| 412 | ReversePacketIterator rit = packets_.rbegin(); |
| 413 | for (; rit != packets_.rend(); ++rit) |
stefan@webrtc.org | 7bc465b | 2013-04-11 17:48:02 | [diff] [blame] | 414 | if (LatestSequenceNumber(packet.seqNum, (*rit).seqNum) == packet.seqNum) |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 415 | break; |
| 416 | |
| 417 | // Check for duplicate packets. |
| 418 | if (rit != packets_.rend() && |
| 419 | (*rit).seqNum == packet.seqNum && (*rit).sizeBytes > 0) |
| 420 | return -2; |
| 421 | |
mikhal@webrtc.org | f31a47a | 2013-08-26 17:10:11 | [diff] [blame] | 422 | // Only insert media packets between first and last packets (when available). |
| 423 | // Placing check here, as to properly account for duplicate packets. |
| 424 | // Check if this is first packet (only valid for some codecs) |
| 425 | // Should only be set for one packet per session. |
| 426 | if (packet.isFirstPacket && first_packet_seq_num_ == -1) { |
| 427 | // The first packet in a frame signals the frame type. |
| 428 | frame_type_ = packet.frameType; |
| 429 | // Store the sequence number for the first packet. |
| 430 | first_packet_seq_num_ = static_cast<int>(packet.seqNum); |
| 431 | } else if (first_packet_seq_num_ != -1 && |
| 432 | !IsNewerSequenceNumber(packet.seqNum, first_packet_seq_num_)) { |
| 433 | return -3; |
| 434 | } else if (frame_type_ == kFrameEmpty && packet.frameType != kFrameEmpty) { |
| 435 | // Update the frame type with the type of the first media packet. |
| 436 | // TODO(mikhal): Can this trigger? |
| 437 | frame_type_ = packet.frameType; |
| 438 | } |
| 439 | |
| 440 | // Track the marker bit, should only be set for one packet per session. |
| 441 | if (packet.markerBit && last_packet_seq_num_ == -1) { |
| 442 | last_packet_seq_num_ = static_cast<int>(packet.seqNum); |
| 443 | } else if (last_packet_seq_num_ != -1 && |
| 444 | IsNewerSequenceNumber(packet.seqNum, last_packet_seq_num_)) { |
| 445 | return -3; |
| 446 | } |
| 447 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 448 | // The insert operation invalidates the iterator |rit|. |
| 449 | PacketIterator packet_list_it = packets_.insert(rit.base(), packet); |
| 450 | |
| 451 | int returnLength = InsertBuffer(frame_buffer, packet_list_it); |
| 452 | UpdateCompleteSession(); |
agalusza@google.com | a7e360e | 2013-08-01 03:15:08 | [diff] [blame] | 453 | if (decode_error_mode == kWithErrors) |
| 454 | decodable_ = true; |
| 455 | else if (decode_error_mode == kSelectiveErrors) |
agalusza@google.com | d818dcb | 2013-07-29 21:48:11 | [diff] [blame] | 456 | UpdateDecodableSession(frame_data); |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 457 | return returnLength; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 458 | } |
| 459 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 460 | void VCMSessionInfo::InformOfEmptyPacket(uint16_t seq_num) { |
| 461 | // Empty packets may be FEC or filler packets. They are sequential and |
| 462 | // follow the data packets, therefore, we should only keep track of the high |
| 463 | // and low sequence numbers and may assume that the packets in between are |
| 464 | // empty packets belonging to the same frame (timestamp). |
stefan@webrtc.org | 7bc465b | 2013-04-11 17:48:02 | [diff] [blame] | 465 | if (empty_seq_num_high_ == -1) |
| 466 | empty_seq_num_high_ = seq_num; |
| 467 | else |
| 468 | empty_seq_num_high_ = LatestSequenceNumber(seq_num, empty_seq_num_high_); |
| 469 | if (empty_seq_num_low_ == -1 || IsNewerSequenceNumber(empty_seq_num_low_, |
| 470 | seq_num)) |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 471 | empty_seq_num_low_ = seq_num; |
| 472 | } |
| 473 | |
stefan@webrtc.org | 076fa6e | 2011-12-13 07:54:56 | [diff] [blame] | 474 | } // namespace webrtc |