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