| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2012 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 | |
| 11 #include "webrtc/modules/video_coding/main/source/session_info.h" | |
| 12 | |
| 13 #include "webrtc/base/logging.h" | |
| 14 #include "webrtc/modules/video_coding/main/source/packet.h" | |
| 15 | |
| 16 namespace webrtc { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 uint16_t BufferToUWord16(const uint8_t* dataBuffer) { | |
| 21 return (dataBuffer[0] << 8) | dataBuffer[1]; | |
| 22 } | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 VCMSessionInfo::VCMSessionInfo() | |
| 27 : session_nack_(false), | |
| 28 complete_(false), | |
| 29 decodable_(false), | |
| 30 frame_type_(kVideoFrameDelta), | |
| 31 packets_(), | |
| 32 empty_seq_num_low_(-1), | |
| 33 empty_seq_num_high_(-1), | |
| 34 first_packet_seq_num_(-1), | |
| 35 last_packet_seq_num_(-1) { | |
| 36 } | |
| 37 | |
| 38 void VCMSessionInfo::UpdateDataPointers(const uint8_t* old_base_ptr, | |
| 39 const uint8_t* new_base_ptr) { | |
| 40 for (PacketIterator it = packets_.begin(); it != packets_.end(); ++it) | |
| 41 if ((*it).dataPtr != NULL) { | |
| 42 assert(old_base_ptr != NULL && new_base_ptr != NULL); | |
| 43 (*it).dataPtr = new_base_ptr + ((*it).dataPtr - old_base_ptr); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 int VCMSessionInfo::LowSequenceNumber() const { | |
| 48 if (packets_.empty()) | |
| 49 return empty_seq_num_low_; | |
| 50 return packets_.front().seqNum; | |
| 51 } | |
| 52 | |
| 53 int VCMSessionInfo::HighSequenceNumber() const { | |
| 54 if (packets_.empty()) | |
| 55 return empty_seq_num_high_; | |
| 56 if (empty_seq_num_high_ == -1) | |
| 57 return packets_.back().seqNum; | |
| 58 return LatestSequenceNumber(packets_.back().seqNum, empty_seq_num_high_); | |
| 59 } | |
| 60 | |
| 61 int VCMSessionInfo::PictureId() const { | |
| 62 if (packets_.empty()) | |
| 63 return kNoPictureId; | |
| 64 if (packets_.front().codecSpecificHeader.codec == kRtpVideoVp8) { | |
| 65 return packets_.front().codecSpecificHeader.codecHeader.VP8.pictureId; | |
| 66 } else if (packets_.front().codecSpecificHeader.codec == kRtpVideoVp9) { | |
| 67 return packets_.front().codecSpecificHeader.codecHeader.VP9.picture_id; | |
| 68 } else { | |
| 69 return kNoPictureId; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 int VCMSessionInfo::TemporalId() const { | |
| 74 if (packets_.empty()) | |
| 75 return kNoTemporalIdx; | |
| 76 if (packets_.front().codecSpecificHeader.codec == kRtpVideoVp8) { | |
| 77 return packets_.front().codecSpecificHeader.codecHeader.VP8.temporalIdx; | |
| 78 } else if (packets_.front().codecSpecificHeader.codec == kRtpVideoVp9) { | |
| 79 return packets_.front().codecSpecificHeader.codecHeader.VP9.temporal_idx; | |
| 80 } else { | |
| 81 return kNoTemporalIdx; | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 bool VCMSessionInfo::LayerSync() const { | |
| 86 if (packets_.empty()) | |
| 87 return false; | |
| 88 if (packets_.front().codecSpecificHeader.codec == kRtpVideoVp8) { | |
| 89 return packets_.front().codecSpecificHeader.codecHeader.VP8.layerSync; | |
| 90 } else if (packets_.front().codecSpecificHeader.codec == kRtpVideoVp9) { | |
| 91 return | |
| 92 packets_.front().codecSpecificHeader.codecHeader.VP9.temporal_up_switch; | |
| 93 } else { | |
| 94 return false; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 int VCMSessionInfo::Tl0PicId() const { | |
| 99 if (packets_.empty()) | |
| 100 return kNoTl0PicIdx; | |
| 101 if (packets_.front().codecSpecificHeader.codec == kRtpVideoVp8) { | |
| 102 return packets_.front().codecSpecificHeader.codecHeader.VP8.tl0PicIdx; | |
| 103 } else if (packets_.front().codecSpecificHeader.codec == kRtpVideoVp9) { | |
| 104 return packets_.front().codecSpecificHeader.codecHeader.VP9.tl0_pic_idx; | |
| 105 } else { | |
| 106 return kNoTl0PicIdx; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 bool VCMSessionInfo::NonReference() const { | |
| 111 if (packets_.empty() || | |
| 112 packets_.front().codecSpecificHeader.codec != kRtpVideoVp8) | |
| 113 return false; | |
| 114 return packets_.front().codecSpecificHeader.codecHeader.VP8.nonReference; | |
| 115 } | |
| 116 | |
| 117 void VCMSessionInfo::SetGofInfo(const GofInfoVP9& gof_info, size_t idx) { | |
| 118 if (packets_.empty() || | |
| 119 packets_.front().codecSpecificHeader.codec != kRtpVideoVp9 || | |
| 120 packets_.front().codecSpecificHeader.codecHeader.VP9.flexible_mode) { | |
| 121 return; | |
| 122 } | |
| 123 packets_.front().codecSpecificHeader.codecHeader.VP9.temporal_idx = | |
| 124 gof_info.temporal_idx[idx]; | |
| 125 packets_.front().codecSpecificHeader.codecHeader.VP9.temporal_up_switch = | |
| 126 gof_info.temporal_up_switch[idx]; | |
| 127 packets_.front().codecSpecificHeader.codecHeader.VP9.num_ref_pics = | |
| 128 gof_info.num_ref_pics[idx]; | |
| 129 for (uint8_t i = 0; i < gof_info.num_ref_pics[idx]; ++i) { | |
| 130 packets_.front().codecSpecificHeader.codecHeader.VP9.pid_diff[i] = | |
| 131 gof_info.pid_diff[idx][i]; | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 void VCMSessionInfo::Reset() { | |
| 136 session_nack_ = false; | |
| 137 complete_ = false; | |
| 138 decodable_ = false; | |
| 139 frame_type_ = kVideoFrameDelta; | |
| 140 packets_.clear(); | |
| 141 empty_seq_num_low_ = -1; | |
| 142 empty_seq_num_high_ = -1; | |
| 143 first_packet_seq_num_ = -1; | |
| 144 last_packet_seq_num_ = -1; | |
| 145 } | |
| 146 | |
| 147 size_t VCMSessionInfo::SessionLength() const { | |
| 148 size_t length = 0; | |
| 149 for (PacketIteratorConst it = packets_.begin(); it != packets_.end(); ++it) | |
| 150 length += (*it).sizeBytes; | |
| 151 return length; | |
| 152 } | |
| 153 | |
| 154 int VCMSessionInfo::NumPackets() const { | |
| 155 return packets_.size(); | |
| 156 } | |
| 157 | |
| 158 size_t VCMSessionInfo::InsertBuffer(uint8_t* frame_buffer, | |
| 159 PacketIterator packet_it) { | |
| 160 VCMPacket& packet = *packet_it; | |
| 161 PacketIterator it; | |
| 162 | |
| 163 // Calculate the offset into the frame buffer for this packet. | |
| 164 size_t offset = 0; | |
| 165 for (it = packets_.begin(); it != packet_it; ++it) | |
| 166 offset += (*it).sizeBytes; | |
| 167 | |
| 168 // Set the data pointer to pointing to the start of this packet in the | |
| 169 // frame buffer. | |
| 170 const uint8_t* packet_buffer = packet.dataPtr; | |
| 171 packet.dataPtr = frame_buffer + offset; | |
| 172 | |
| 173 // We handle H.264 STAP-A packets in a special way as we need to remove the | |
| 174 // two length bytes between each NAL unit, and potentially add start codes. | |
| 175 // TODO(pbos): Remove H264 parsing from this step and use a fragmentation | |
| 176 // header supplied by the H264 depacketizer. | |
| 177 const size_t kH264NALHeaderLengthInBytes = 1; | |
| 178 const size_t kLengthFieldLength = 2; | |
| 179 if (packet.codecSpecificHeader.codec == kRtpVideoH264 && | |
| 180 packet.codecSpecificHeader.codecHeader.H264.packetization_type == | |
| 181 kH264StapA) { | |
| 182 size_t required_length = 0; | |
| 183 const uint8_t* nalu_ptr = packet_buffer + kH264NALHeaderLengthInBytes; | |
| 184 while (nalu_ptr < packet_buffer + packet.sizeBytes) { | |
| 185 size_t length = BufferToUWord16(nalu_ptr); | |
| 186 required_length += | |
| 187 length + (packet.insertStartCode ? kH264StartCodeLengthBytes : 0); | |
| 188 nalu_ptr += kLengthFieldLength + length; | |
| 189 } | |
| 190 ShiftSubsequentPackets(packet_it, required_length); | |
| 191 nalu_ptr = packet_buffer + kH264NALHeaderLengthInBytes; | |
| 192 uint8_t* frame_buffer_ptr = frame_buffer + offset; | |
| 193 while (nalu_ptr < packet_buffer + packet.sizeBytes) { | |
| 194 size_t length = BufferToUWord16(nalu_ptr); | |
| 195 nalu_ptr += kLengthFieldLength; | |
| 196 frame_buffer_ptr += Insert(nalu_ptr, | |
| 197 length, | |
| 198 packet.insertStartCode, | |
| 199 const_cast<uint8_t*>(frame_buffer_ptr)); | |
| 200 nalu_ptr += length; | |
| 201 } | |
| 202 packet.sizeBytes = required_length; | |
| 203 return packet.sizeBytes; | |
| 204 } | |
| 205 ShiftSubsequentPackets( | |
| 206 packet_it, | |
| 207 packet.sizeBytes + | |
| 208 (packet.insertStartCode ? kH264StartCodeLengthBytes : 0)); | |
| 209 | |
| 210 packet.sizeBytes = Insert(packet_buffer, | |
| 211 packet.sizeBytes, | |
| 212 packet.insertStartCode, | |
| 213 const_cast<uint8_t*>(packet.dataPtr)); | |
| 214 return packet.sizeBytes; | |
| 215 } | |
| 216 | |
| 217 size_t VCMSessionInfo::Insert(const uint8_t* buffer, | |
| 218 size_t length, | |
| 219 bool insert_start_code, | |
| 220 uint8_t* frame_buffer) { | |
| 221 if (insert_start_code) { | |
| 222 const unsigned char startCode[] = {0, 0, 0, 1}; | |
| 223 memcpy(frame_buffer, startCode, kH264StartCodeLengthBytes); | |
| 224 } | |
| 225 memcpy(frame_buffer + (insert_start_code ? kH264StartCodeLengthBytes : 0), | |
| 226 buffer, | |
| 227 length); | |
| 228 length += (insert_start_code ? kH264StartCodeLengthBytes : 0); | |
| 229 | |
| 230 return length; | |
| 231 } | |
| 232 | |
| 233 void VCMSessionInfo::ShiftSubsequentPackets(PacketIterator it, | |
| 234 int steps_to_shift) { | |
| 235 ++it; | |
| 236 if (it == packets_.end()) | |
| 237 return; | |
| 238 uint8_t* first_packet_ptr = const_cast<uint8_t*>((*it).dataPtr); | |
| 239 int shift_length = 0; | |
| 240 // Calculate the total move length and move the data pointers in advance. | |
| 241 for (; it != packets_.end(); ++it) { | |
| 242 shift_length += (*it).sizeBytes; | |
| 243 if ((*it).dataPtr != NULL) | |
| 244 (*it).dataPtr += steps_to_shift; | |
| 245 } | |
| 246 memmove(first_packet_ptr + steps_to_shift, first_packet_ptr, shift_length); | |
| 247 } | |
| 248 | |
| 249 void VCMSessionInfo::UpdateCompleteSession() { | |
| 250 if (HaveFirstPacket() && HaveLastPacket()) { | |
| 251 // Do we have all the packets in this session? | |
| 252 bool complete_session = true; | |
| 253 PacketIterator it = packets_.begin(); | |
| 254 PacketIterator prev_it = it; | |
| 255 ++it; | |
| 256 for (; it != packets_.end(); ++it) { | |
| 257 if (!InSequence(it, prev_it)) { | |
| 258 complete_session = false; | |
| 259 break; | |
| 260 } | |
| 261 prev_it = it; | |
| 262 } | |
| 263 complete_ = complete_session; | |
| 264 } | |
| 265 } | |
| 266 | |
| 267 void VCMSessionInfo::UpdateDecodableSession(const FrameData& frame_data) { | |
| 268 // Irrelevant if session is already complete or decodable | |
| 269 if (complete_ || decodable_) | |
| 270 return; | |
| 271 // TODO(agalusza): Account for bursty loss. | |
| 272 // TODO(agalusza): Refine these values to better approximate optimal ones. | |
| 273 // Do not decode frames if the RTT is lower than this. | |
| 274 const int64_t kRttThreshold = 100; | |
| 275 // Do not decode frames if the number of packets is between these two | |
| 276 // thresholds. | |
| 277 const float kLowPacketPercentageThreshold = 0.2f; | |
| 278 const float kHighPacketPercentageThreshold = 0.8f; | |
| 279 if (frame_data.rtt_ms < kRttThreshold | |
| 280 || frame_type_ == kVideoFrameKey | |
| 281 || !HaveFirstPacket() | |
| 282 || (NumPackets() <= kHighPacketPercentageThreshold | |
| 283 * frame_data.rolling_average_packets_per_frame | |
| 284 && NumPackets() > kLowPacketPercentageThreshold | |
| 285 * frame_data.rolling_average_packets_per_frame)) | |
| 286 return; | |
| 287 | |
| 288 decodable_ = true; | |
| 289 } | |
| 290 | |
| 291 bool VCMSessionInfo::complete() const { | |
| 292 return complete_; | |
| 293 } | |
| 294 | |
| 295 bool VCMSessionInfo::decodable() const { | |
| 296 return decodable_; | |
| 297 } | |
| 298 | |
| 299 // Find the end of the NAL unit which the packet pointed to by |packet_it| | |
| 300 // belongs to. Returns an iterator to the last packet of the frame if the end | |
| 301 // of the NAL unit wasn't found. | |
| 302 VCMSessionInfo::PacketIterator VCMSessionInfo::FindNaluEnd( | |
| 303 PacketIterator packet_it) const { | |
| 304 if ((*packet_it).completeNALU == kNaluEnd || | |
| 305 (*packet_it).completeNALU == kNaluComplete) { | |
| 306 return packet_it; | |
| 307 } | |
| 308 // Find the end of the NAL unit. | |
| 309 for (; packet_it != packets_.end(); ++packet_it) { | |
| 310 if (((*packet_it).completeNALU == kNaluComplete && | |
| 311 (*packet_it).sizeBytes > 0) || | |
| 312 // Found next NALU. | |
| 313 (*packet_it).completeNALU == kNaluStart) | |
| 314 return --packet_it; | |
| 315 if ((*packet_it).completeNALU == kNaluEnd) | |
| 316 return packet_it; | |
| 317 } | |
| 318 // The end wasn't found. | |
| 319 return --packet_it; | |
| 320 } | |
| 321 | |
| 322 size_t VCMSessionInfo::DeletePacketData(PacketIterator start, | |
| 323 PacketIterator end) { | |
| 324 size_t bytes_to_delete = 0; // The number of bytes to delete. | |
| 325 PacketIterator packet_after_end = end; | |
| 326 ++packet_after_end; | |
| 327 | |
| 328 // Get the number of bytes to delete. | |
| 329 // Clear the size of these packets. | |
| 330 for (PacketIterator it = start; it != packet_after_end; ++it) { | |
| 331 bytes_to_delete += (*it).sizeBytes; | |
| 332 (*it).sizeBytes = 0; | |
| 333 (*it).dataPtr = NULL; | |
| 334 } | |
| 335 if (bytes_to_delete > 0) | |
| 336 ShiftSubsequentPackets(end, -static_cast<int>(bytes_to_delete)); | |
| 337 return bytes_to_delete; | |
| 338 } | |
| 339 | |
| 340 size_t VCMSessionInfo::BuildVP8FragmentationHeader( | |
| 341 uint8_t* frame_buffer, | |
| 342 size_t frame_buffer_length, | |
| 343 RTPFragmentationHeader* fragmentation) { | |
| 344 size_t new_length = 0; | |
| 345 // Allocate space for max number of partitions | |
| 346 fragmentation->VerifyAndAllocateFragmentationHeader(kMaxVP8Partitions); | |
| 347 fragmentation->fragmentationVectorSize = 0; | |
| 348 memset(fragmentation->fragmentationLength, 0, | |
| 349 kMaxVP8Partitions * sizeof(size_t)); | |
| 350 if (packets_.empty()) | |
| 351 return new_length; | |
| 352 PacketIterator it = FindNextPartitionBeginning(packets_.begin()); | |
| 353 while (it != packets_.end()) { | |
| 354 const int partition_id = | |
| 355 (*it).codecSpecificHeader.codecHeader.VP8.partitionId; | |
| 356 PacketIterator partition_end = FindPartitionEnd(it); | |
| 357 fragmentation->fragmentationOffset[partition_id] = | |
| 358 (*it).dataPtr - frame_buffer; | |
| 359 assert(fragmentation->fragmentationOffset[partition_id] < | |
| 360 frame_buffer_length); | |
| 361 fragmentation->fragmentationLength[partition_id] = | |
| 362 (*partition_end).dataPtr + (*partition_end).sizeBytes - (*it).dataPtr; | |
| 363 assert(fragmentation->fragmentationLength[partition_id] <= | |
| 364 frame_buffer_length); | |
| 365 new_length += fragmentation->fragmentationLength[partition_id]; | |
| 366 ++partition_end; | |
| 367 it = FindNextPartitionBeginning(partition_end); | |
| 368 if (partition_id + 1 > fragmentation->fragmentationVectorSize) | |
| 369 fragmentation->fragmentationVectorSize = partition_id + 1; | |
| 370 } | |
| 371 // Set all empty fragments to start where the previous fragment ends, | |
| 372 // and have zero length. | |
| 373 if (fragmentation->fragmentationLength[0] == 0) | |
| 374 fragmentation->fragmentationOffset[0] = 0; | |
| 375 for (int i = 1; i < fragmentation->fragmentationVectorSize; ++i) { | |
| 376 if (fragmentation->fragmentationLength[i] == 0) | |
| 377 fragmentation->fragmentationOffset[i] = | |
| 378 fragmentation->fragmentationOffset[i - 1] + | |
| 379 fragmentation->fragmentationLength[i - 1]; | |
| 380 assert(i == 0 || | |
| 381 fragmentation->fragmentationOffset[i] >= | |
| 382 fragmentation->fragmentationOffset[i - 1]); | |
| 383 } | |
| 384 assert(new_length <= frame_buffer_length); | |
| 385 return new_length; | |
| 386 } | |
| 387 | |
| 388 VCMSessionInfo::PacketIterator VCMSessionInfo::FindNextPartitionBeginning( | |
| 389 PacketIterator it) const { | |
| 390 while (it != packets_.end()) { | |
| 391 if ((*it).codecSpecificHeader.codecHeader.VP8.beginningOfPartition) { | |
| 392 return it; | |
| 393 } | |
| 394 ++it; | |
| 395 } | |
| 396 return it; | |
| 397 } | |
| 398 | |
| 399 VCMSessionInfo::PacketIterator VCMSessionInfo::FindPartitionEnd( | |
| 400 PacketIterator it) const { | |
| 401 assert((*it).codec == kVideoCodecVP8); | |
| 402 PacketIterator prev_it = it; | |
| 403 const int partition_id = | |
| 404 (*it).codecSpecificHeader.codecHeader.VP8.partitionId; | |
| 405 while (it != packets_.end()) { | |
| 406 bool beginning = | |
| 407 (*it).codecSpecificHeader.codecHeader.VP8.beginningOfPartition; | |
| 408 int current_partition_id = | |
| 409 (*it).codecSpecificHeader.codecHeader.VP8.partitionId; | |
| 410 bool packet_loss_found = (!beginning && !InSequence(it, prev_it)); | |
| 411 if (packet_loss_found || | |
| 412 (beginning && current_partition_id != partition_id)) { | |
| 413 // Missing packet, the previous packet was the last in sequence. | |
| 414 return prev_it; | |
| 415 } | |
| 416 prev_it = it; | |
| 417 ++it; | |
| 418 } | |
| 419 return prev_it; | |
| 420 } | |
| 421 | |
| 422 bool VCMSessionInfo::InSequence(const PacketIterator& packet_it, | |
| 423 const PacketIterator& prev_packet_it) { | |
| 424 // If the two iterators are pointing to the same packet they are considered | |
| 425 // to be in sequence. | |
| 426 return (packet_it == prev_packet_it || | |
| 427 (static_cast<uint16_t>((*prev_packet_it).seqNum + 1) == | |
| 428 (*packet_it).seqNum)); | |
| 429 } | |
| 430 | |
| 431 size_t VCMSessionInfo::MakeDecodable() { | |
| 432 size_t return_length = 0; | |
| 433 if (packets_.empty()) { | |
| 434 return 0; | |
| 435 } | |
| 436 PacketIterator it = packets_.begin(); | |
| 437 // Make sure we remove the first NAL unit if it's not decodable. | |
| 438 if ((*it).completeNALU == kNaluIncomplete || | |
| 439 (*it).completeNALU == kNaluEnd) { | |
| 440 PacketIterator nalu_end = FindNaluEnd(it); | |
| 441 return_length += DeletePacketData(it, nalu_end); | |
| 442 it = nalu_end; | |
| 443 } | |
| 444 PacketIterator prev_it = it; | |
| 445 // Take care of the rest of the NAL units. | |
| 446 for (; it != packets_.end(); ++it) { | |
| 447 bool start_of_nalu = ((*it).completeNALU == kNaluStart || | |
| 448 (*it).completeNALU == kNaluComplete); | |
| 449 if (!start_of_nalu && !InSequence(it, prev_it)) { | |
| 450 // Found a sequence number gap due to packet loss. | |
| 451 PacketIterator nalu_end = FindNaluEnd(it); | |
| 452 return_length += DeletePacketData(it, nalu_end); | |
| 453 it = nalu_end; | |
| 454 } | |
| 455 prev_it = it; | |
| 456 } | |
| 457 return return_length; | |
| 458 } | |
| 459 | |
| 460 void VCMSessionInfo::SetNotDecodableIfIncomplete() { | |
| 461 // We don't need to check for completeness first because the two are | |
| 462 // orthogonal. If complete_ is true, decodable_ is irrelevant. | |
| 463 decodable_ = false; | |
| 464 } | |
| 465 | |
| 466 bool | |
| 467 VCMSessionInfo::HaveFirstPacket() const { | |
| 468 return !packets_.empty() && (first_packet_seq_num_ != -1); | |
| 469 } | |
| 470 | |
| 471 bool | |
| 472 VCMSessionInfo::HaveLastPacket() const { | |
| 473 return !packets_.empty() && (last_packet_seq_num_ != -1); | |
| 474 } | |
| 475 | |
| 476 bool | |
| 477 VCMSessionInfo::session_nack() const { | |
| 478 return session_nack_; | |
| 479 } | |
| 480 | |
| 481 int VCMSessionInfo::InsertPacket(const VCMPacket& packet, | |
| 482 uint8_t* frame_buffer, | |
| 483 VCMDecodeErrorMode decode_error_mode, | |
| 484 const FrameData& frame_data) { | |
| 485 if (packet.frameType == kEmptyFrame) { | |
| 486 // Update sequence number of an empty packet. | |
| 487 // Only media packets are inserted into the packet list. | |
| 488 InformOfEmptyPacket(packet.seqNum); | |
| 489 return 0; | |
| 490 } | |
| 491 | |
| 492 if (packets_.size() == kMaxPacketsInSession) { | |
| 493 LOG(LS_ERROR) << "Max number of packets per frame has been reached."; | |
| 494 return -1; | |
| 495 } | |
| 496 | |
| 497 // Find the position of this packet in the packet list in sequence number | |
| 498 // order and insert it. Loop over the list in reverse order. | |
| 499 ReversePacketIterator rit = packets_.rbegin(); | |
| 500 for (; rit != packets_.rend(); ++rit) | |
| 501 if (LatestSequenceNumber(packet.seqNum, (*rit).seqNum) == packet.seqNum) | |
| 502 break; | |
| 503 | |
| 504 // Check for duplicate packets. | |
| 505 if (rit != packets_.rend() && | |
| 506 (*rit).seqNum == packet.seqNum && (*rit).sizeBytes > 0) | |
| 507 return -2; | |
| 508 | |
| 509 if (packet.codec == kVideoCodecH264) { | |
| 510 frame_type_ = packet.frameType; | |
| 511 if (packet.isFirstPacket && | |
| 512 (first_packet_seq_num_ == -1 || | |
| 513 IsNewerSequenceNumber(first_packet_seq_num_, packet.seqNum))) { | |
| 514 first_packet_seq_num_ = packet.seqNum; | |
| 515 } | |
| 516 if (packet.markerBit && | |
| 517 (last_packet_seq_num_ == -1 || | |
| 518 IsNewerSequenceNumber(packet.seqNum, last_packet_seq_num_))) { | |
| 519 last_packet_seq_num_ = packet.seqNum; | |
| 520 } | |
| 521 } else { | |
| 522 // Only insert media packets between first and last packets (when | |
| 523 // available). | |
| 524 // Placing check here, as to properly account for duplicate packets. | |
| 525 // Check if this is first packet (only valid for some codecs) | |
| 526 // Should only be set for one packet per session. | |
| 527 if (packet.isFirstPacket && first_packet_seq_num_ == -1) { | |
| 528 // The first packet in a frame signals the frame type. | |
| 529 frame_type_ = packet.frameType; | |
| 530 // Store the sequence number for the first packet. | |
| 531 first_packet_seq_num_ = static_cast<int>(packet.seqNum); | |
| 532 } else if (first_packet_seq_num_ != -1 && | |
| 533 IsNewerSequenceNumber(first_packet_seq_num_, packet.seqNum)) { | |
| 534 LOG(LS_WARNING) << "Received packet with a sequence number which is out " | |
| 535 "of frame boundaries"; | |
| 536 return -3; | |
| 537 } else if (frame_type_ == kEmptyFrame && packet.frameType != kEmptyFrame) { | |
| 538 // Update the frame type with the type of the first media packet. | |
| 539 // TODO(mikhal): Can this trigger? | |
| 540 frame_type_ = packet.frameType; | |
| 541 } | |
| 542 | |
| 543 // Track the marker bit, should only be set for one packet per session. | |
| 544 if (packet.markerBit && last_packet_seq_num_ == -1) { | |
| 545 last_packet_seq_num_ = static_cast<int>(packet.seqNum); | |
| 546 } else if (last_packet_seq_num_ != -1 && | |
| 547 IsNewerSequenceNumber(packet.seqNum, last_packet_seq_num_)) { | |
| 548 LOG(LS_WARNING) << "Received packet with a sequence number which is out " | |
| 549 "of frame boundaries"; | |
| 550 return -3; | |
| 551 } | |
| 552 } | |
| 553 | |
| 554 // The insert operation invalidates the iterator |rit|. | |
| 555 PacketIterator packet_list_it = packets_.insert(rit.base(), packet); | |
| 556 | |
| 557 size_t returnLength = InsertBuffer(frame_buffer, packet_list_it); | |
| 558 UpdateCompleteSession(); | |
| 559 if (decode_error_mode == kWithErrors) | |
| 560 decodable_ = true; | |
| 561 else if (decode_error_mode == kSelectiveErrors) | |
| 562 UpdateDecodableSession(frame_data); | |
| 563 return static_cast<int>(returnLength); | |
| 564 } | |
| 565 | |
| 566 void VCMSessionInfo::InformOfEmptyPacket(uint16_t seq_num) { | |
| 567 // Empty packets may be FEC or filler packets. They are sequential and | |
| 568 // follow the data packets, therefore, we should only keep track of the high | |
| 569 // and low sequence numbers and may assume that the packets in between are | |
| 570 // empty packets belonging to the same frame (timestamp). | |
| 571 if (empty_seq_num_high_ == -1) | |
| 572 empty_seq_num_high_ = seq_num; | |
| 573 else | |
| 574 empty_seq_num_high_ = LatestSequenceNumber(seq_num, empty_seq_num_high_); | |
| 575 if (empty_seq_num_low_ == -1 || IsNewerSequenceNumber(empty_seq_num_low_, | |
| 576 seq_num)) | |
| 577 empty_seq_num_low_ = seq_num; | |
| 578 } | |
| 579 | |
| 580 } // namespace webrtc | |
| OLD | NEW |