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