blob: 814d27957b664c521f44995e403d48e5d8ff9ae0 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
stefan@webrtc.org94355e02012-02-06 14:06:392 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:253 *
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.orga4407322013-07-16 12:32:0511#include "webrtc/modules/video_coding/main/source/session_info.h"
niklase@google.com470e71d2011-07-07 08:21:2512
pbos@webrtc.orga4407322013-07-16 12:32:0513#include "webrtc/modules/video_coding/main/source/packet.h"
mikhal@webrtc.orge185e9f2011-09-23 22:02:4014
niklase@google.com470e71d2011-07-07 08:21:2515namespace webrtc {
16
agalusza@google.comd818dcb2013-07-29 21:48:1117// Used in determining whether a frame is decodable.
18enum {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.
22static const float kLowPacketPercentageThreshold = 0.2f;
23static const float kHighPacketPercentageThreshold = 0.8f;
24
stefan@webrtc.org076fa6e2011-12-13 07:54:5625VCMSessionInfo::VCMSessionInfo()
26 : session_nack_(false),
27 complete_(false),
28 decodable_(false),
29 frame_type_(kVideoFrameDelta),
stefan@webrtc.org076fa6e2011-12-13 07:54:5630 packets_(),
31 empty_seq_num_low_(-1),
32 empty_seq_num_high_(-1),
mikhal@webrtc.orgf31a47a2013-08-26 17:10:1133 first_packet_seq_num_(-1),
34 last_packet_seq_num_(-1) {
niklase@google.com470e71d2011-07-07 08:21:2535}
36
stefan@webrtc.orgb07aa402012-01-10 11:45:0537void VCMSessionInfo::UpdateDataPointers(const uint8_t* old_base_ptr,
38 const uint8_t* new_base_ptr) {
stefan@webrtc.org076fa6e2011-12-13 07:54:5639 for (PacketIterator it = packets_.begin(); it != packets_.end(); ++it)
stefan@webrtc.orgb07aa402012-01-10 11:45:0540 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.com470e71d2011-07-07 08:21:2544}
45
stefan@webrtc.org076fa6e2011-12-13 07:54:5646int VCMSessionInfo::LowSequenceNumber() const {
47 if (packets_.empty())
48 return empty_seq_num_low_;
49 return packets_.front().seqNum;
stefan@webrtc.orgc3d89102011-09-08 06:50:2850}
51
stefan@webrtc.org076fa6e2011-12-13 07:54:5652int VCMSessionInfo::HighSequenceNumber() const {
53 if (packets_.empty())
54 return empty_seq_num_high_;
stefan@webrtc.org7bc465b2013-04-11 17:48:0255 if (empty_seq_num_high_ == -1)
56 return packets_.back().seqNum;
57 return LatestSequenceNumber(packets_.back().seqNum, empty_seq_num_high_);
niklase@google.com470e71d2011-07-07 08:21:2558}
59
stefan@webrtc.orgffd28f92011-10-19 15:55:3960int VCMSessionInfo::PictureId() const {
stefan@webrtc.org076fa6e2011-12-13 07:54:5661 if (packets_.empty() ||
wu@webrtc.org822fbd82013-08-15 23:38:5462 packets_.front().codecSpecificHeader.codec != kRtpVideoVp8)
stefan@webrtc.org076fa6e2011-12-13 07:54:5663 return kNoPictureId;
64 return packets_.front().codecSpecificHeader.codecHeader.VP8.pictureId;
stefan@webrtc.orgffd28f92011-10-19 15:55:3965}
66
mikhal@webrtc.orgf5ee1dc2011-12-08 19:04:4767int VCMSessionInfo::TemporalId() const {
stefan@webrtc.org076fa6e2011-12-13 07:54:5668 if (packets_.empty() ||
wu@webrtc.org822fbd82013-08-15 23:38:5469 packets_.front().codecSpecificHeader.codec != kRtpVideoVp8)
mikhal@webrtc.orgf5ee1dc2011-12-08 19:04:4770 return kNoTemporalIdx;
stefan@webrtc.org076fa6e2011-12-13 07:54:5671 return packets_.front().codecSpecificHeader.codecHeader.VP8.temporalIdx;
mikhal@webrtc.orgf5ee1dc2011-12-08 19:04:4772}
73
henrik.lundin@webrtc.orgeda86dc2011-12-13 14:11:0674bool VCMSessionInfo::LayerSync() const {
75 if (packets_.empty() ||
wu@webrtc.org822fbd82013-08-15 23:38:5476 packets_.front().codecSpecificHeader.codec != kRtpVideoVp8)
henrik.lundin@webrtc.orgeda86dc2011-12-13 14:11:0677 return false;
78 return packets_.front().codecSpecificHeader.codecHeader.VP8.layerSync;
79}
80
mikhal@webrtc.orgf5ee1dc2011-12-08 19:04:4781int VCMSessionInfo::Tl0PicId() const {
stefan@webrtc.org076fa6e2011-12-13 07:54:5682 if (packets_.empty() ||
wu@webrtc.org822fbd82013-08-15 23:38:5483 packets_.front().codecSpecificHeader.codec != kRtpVideoVp8)
mikhal@webrtc.orgf5ee1dc2011-12-08 19:04:4784 return kNoTl0PicIdx;
stefan@webrtc.org076fa6e2011-12-13 07:54:5685 return packets_.front().codecSpecificHeader.codecHeader.VP8.tl0PicIdx;
mikhal@webrtc.orgf5ee1dc2011-12-08 19:04:4786}
87
mikhal@webrtc.orgea714402011-12-12 02:29:3488bool VCMSessionInfo::NonReference() const {
stefan@webrtc.org076fa6e2011-12-13 07:54:5689 if (packets_.empty() ||
wu@webrtc.org822fbd82013-08-15 23:38:5490 packets_.front().codecSpecificHeader.codec != kRtpVideoVp8)
mikhal@webrtc.orgea714402011-12-12 02:29:3491 return false;
stefan@webrtc.org076fa6e2011-12-13 07:54:5692 return packets_.front().codecSpecificHeader.codecHeader.VP8.nonReference;
mikhal@webrtc.orgea714402011-12-12 02:29:3493}
94
stefan@webrtc.org076fa6e2011-12-13 07:54:5695void VCMSessionInfo::Reset() {
96 session_nack_ = false;
97 complete_ = false;
98 decodable_ = false;
99 frame_type_ = kVideoFrameDelta;
stefan@webrtc.org076fa6e2011-12-13 07:54:56100 packets_.clear();
101 empty_seq_num_low_ = -1;
102 empty_seq_num_high_ = -1;
mikhal@webrtc.orgf31a47a2013-08-26 17:10:11103 first_packet_seq_num_ = -1;
104 last_packet_seq_num_ = -1;
niklase@google.com470e71d2011-07-07 08:21:25105}
106
stefan@webrtc.org076fa6e2011-12-13 07:54:56107int 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.com470e71d2011-07-07 08:21:25112}
113
agalusza@google.comd818dcb2013-07-29 21:48:11114int VCMSessionInfo::NumPackets() const {
115 return packets_.size();
116}
117
stefan@webrtc.org076fa6e2011-12-13 07:54:56118int 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.org56210572012-01-17 12:45:47124 packet_size += (packet.insertStartCode ? kH264StartCodeLengthBytes : 0);
stefan@webrtc.org076fa6e2011-12-13 07:54:56125
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.com470e71d2011-07-07 08:21:25150}
151
stefan@webrtc.org076fa6e2011-12-13 07:54:56152void VCMSessionInfo::ShiftSubsequentPackets(PacketIterator it,
153 int steps_to_shift) {
154 ++it;
155 if (it == packets_.end())
156 return;
pbos@webrtc.org7b859cc2013-04-02 15:54:38157 uint8_t* first_packet_ptr = const_cast<uint8_t*>((*it).dataPtr);
stefan@webrtc.org076fa6e2011-12-13 07:54:56158 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.com470e71d2011-07-07 08:21:25166}
167
mikhal@webrtc.org6b9a7f82011-11-22 22:48:20168void VCMSessionInfo::UpdateCompleteSession() {
mikhal@webrtc.orgf31a47a2013-08-26 17:10:11169 if (HaveFirstPacket() && HaveLastPacket()) {
mikhal@webrtc.org6b9a7f82011-11-22 22:48:20170 // Do we have all the packets in this session?
stefan@webrtc.org076fa6e2011-12-13 07:54:56171 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.org6b9a7f82011-11-22 22:48:20178 break;
179 }
stefan@webrtc.org076fa6e2011-12-13 07:54:56180 prev_it = it;
niklase@google.com470e71d2011-07-07 08:21:25181 }
stefan@webrtc.org076fa6e2011-12-13 07:54:56182 complete_ = complete_session;
mikhal@webrtc.org6b9a7f82011-11-22 22:48:20183 }
niklase@google.com470e71d2011-07-07 08:21:25184}
185
agalusza@google.comd818dcb2013-07-29 21:48:11186void VCMSessionInfo::UpdateDecodableSession(const FrameData& frame_data) {
mikhal@webrtc.org6b9a7f82011-11-22 22:48:20187 // Irrelevant if session is already complete or decodable
stefan@webrtc.org076fa6e2011-12-13 07:54:56188 if (complete_ || decodable_)
mikhal@webrtc.org6b9a7f82011-11-22 22:48:20189 return;
agalusza@google.comd818dcb2013-07-29 21:48:11190
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.org6b9a7f82011-11-22 22:48:20203}
204
stefan@webrtc.org076fa6e2011-12-13 07:54:56205bool VCMSessionInfo::complete() const {
206 return complete_;
mikhal@webrtc.org6b9a7f82011-11-22 22:48:20207}
208
stefan@webrtc.org076fa6e2011-12-13 07:54:56209bool VCMSessionInfo::decodable() const {
210 return decodable_;
niklase@google.com470e71d2011-07-07 08:21:25211}
212
stefan@webrtc.org076fa6e2011-12-13 07:54:56213// 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.
216VCMSessionInfo::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.com470e71d2011-07-07 08:21:25234}
235
stefan@webrtc.org076fa6e2011-12-13 07:54:56236int 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.com470e71d2011-07-07 08:21:25241
stefan@webrtc.org076fa6e2011-12-13 07:54:56242 // 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.org076fa6e2011-12-13 07:54:56248 }
249 if (bytes_to_delete > 0)
250 ShiftSubsequentPackets(end, -bytes_to_delete);
251 return bytes_to_delete;
niklase@google.com470e71d2011-07-07 08:21:25252}
253
stefan@webrtc.org076fa6e2011-12-13 07:54:56254int VCMSessionInfo::BuildVP8FragmentationHeader(
255 uint8_t* frame_buffer,
256 int frame_buffer_length,
257 RTPFragmentationHeader* fragmentation) {
stefan@webrtc.orgc3d89102011-09-08 06:50:28258 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.org7b859cc2013-04-02 15:54:38263 kMaxVP8Partitions * sizeof(uint32_t));
stefan@webrtc.org076fa6e2011-12-13 07:54:56264 if (packets_.empty())
stefan@webrtc.orgc3d89102011-09-08 06:50:28265 return new_length;
mikhal@webrtc.org2b810bf2013-09-03 19:09:49266 PacketIterator it = FindNextPartitionBeginning(packets_.begin());
stefan@webrtc.org076fa6e2011-12-13 07:54:56267 while (it != packets_.end()) {
stefan@webrtc.orgc3d89102011-09-08 06:50:28268 const int partition_id =
stefan@webrtc.org076fa6e2011-12-13 07:54:56269 (*it).codecSpecificHeader.codecHeader.VP8.partitionId;
270 PacketIterator partition_end = FindPartitionEnd(it);
stefan@webrtc.orgc3d89102011-09-08 06:50:28271 fragmentation->fragmentationOffset[partition_id] =
stefan@webrtc.org076fa6e2011-12-13 07:54:56272 (*it).dataPtr - frame_buffer;
stefan@webrtc.orgc3d89102011-09-08 06:50:28273 assert(fragmentation->fragmentationOffset[partition_id] <
pbos@webrtc.org7b859cc2013-04-02 15:54:38274 static_cast<uint32_t>(frame_buffer_length));
stefan@webrtc.orgc3d89102011-09-08 06:50:28275 fragmentation->fragmentationLength[partition_id] =
stefan@webrtc.org076fa6e2011-12-13 07:54:56276 (*partition_end).dataPtr + (*partition_end).sizeBytes - (*it).dataPtr;
stefan@webrtc.orgc3d89102011-09-08 06:50:28277 assert(fragmentation->fragmentationLength[partition_id] <=
pbos@webrtc.org7b859cc2013-04-02 15:54:38278 static_cast<uint32_t>(frame_buffer_length));
stefan@webrtc.orgc3d89102011-09-08 06:50:28279 new_length += fragmentation->fragmentationLength[partition_id];
stefan@webrtc.org076fa6e2011-12-13 07:54:56280 ++partition_end;
mikhal@webrtc.org2b810bf2013-09-03 19:09:49281 it = FindNextPartitionBeginning(partition_end);
stefan@webrtc.orgc3d89102011-09-08 06:50:28282 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.org076fa6e2011-12-13 07:54:56289 for (int i = 1; i < fragmentation->fragmentationVectorSize; ++i) {
stefan@webrtc.orgc3d89102011-09-08 06:50:28290 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.org076fa6e2011-12-13 07:54:56302VCMSessionInfo::PacketIterator VCMSessionInfo::FindNextPartitionBeginning(
mikhal@webrtc.org2b810bf2013-09-03 19:09:49303 PacketIterator it) const {
stefan@webrtc.org076fa6e2011-12-13 07:54:56304 while (it != packets_.end()) {
305 if ((*it).codecSpecificHeader.codecHeader.VP8.beginningOfPartition) {
306 return it;
stefan@webrtc.org4c059d82011-10-13 07:35:37307 }
stefan@webrtc.org076fa6e2011-12-13 07:54:56308 ++it;
stefan@webrtc.orgc3d89102011-09-08 06:50:28309 }
stefan@webrtc.org076fa6e2011-12-13 07:54:56310 return it;
stefan@webrtc.orgc3d89102011-09-08 06:50:28311}
312
stefan@webrtc.org076fa6e2011-12-13 07:54:56313VCMSessionInfo::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.orgc3d89102011-09-08 06:50:28327 // Missing packet, the previous packet was the last in sequence.
stefan@webrtc.org076fa6e2011-12-13 07:54:56328 return prev_it;
stefan@webrtc.orgc3d89102011-09-08 06:50:28329 }
stefan@webrtc.org076fa6e2011-12-13 07:54:56330 prev_it = it;
331 ++it;
stefan@webrtc.orgc3d89102011-09-08 06:50:28332 }
stefan@webrtc.org076fa6e2011-12-13 07:54:56333 return prev_it;
stefan@webrtc.orgc3d89102011-09-08 06:50:28334}
335
stefan@webrtc.org076fa6e2011-12-13 07:54:56336bool 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.org7b859cc2013-04-02 15:54:38341 (static_cast<uint16_t>((*prev_packet_it).seqNum + 1) ==
stefan@webrtc.org076fa6e2011-12-13 07:54:56342 (*packet_it).seqNum));
stefan@webrtc.orgc3d89102011-09-08 06:50:28343}
344
stefan@webrtc.org076fa6e2011-12-13 07:54:56345int VCMSessionInfo::MakeDecodable() {
346 int return_length = 0;
stefan@webrtc.org4ce0ba02012-02-28 12:09:09347 if (packets_.empty()) {
348 return 0;
349 }
stefan@webrtc.org076fa6e2011-12-13 07:54:56350 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.com470e71d2011-07-07 08:21:25368 }
stefan@webrtc.org076fa6e2011-12-13 07:54:56369 prev_it = it;
370 }
371 return return_length;
niklase@google.com470e71d2011-07-07 08:21:25372}
373
mikhal@webrtc.orgdbf6a812013-08-21 20:40:47374void VCMSessionInfo::SetNotDecodableIfIncomplete() {
agalusza@google.comd177c102013-08-08 01:12:33375 // 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.com470e71d2011-07-07 08:21:25380bool
stefan@webrtc.org885cd132013-04-16 09:38:26381VCMSessionInfo::HaveFirstPacket() const {
mikhal@webrtc.orgf31a47a2013-08-26 17:10:11382 return !packets_.empty() && (first_packet_seq_num_ != -1);
stefan@webrtc.org885cd132013-04-16 09:38:26383}
384
385bool
stefan@webrtc.org076fa6e2011-12-13 07:54:56386VCMSessionInfo::HaveLastPacket() const {
mikhal@webrtc.orgf31a47a2013-08-26 17:10:11387 return !packets_.empty() && (last_packet_seq_num_ != -1);
niklase@google.com470e71d2011-07-07 08:21:25388}
389
niklase@google.com470e71d2011-07-07 08:21:25390bool
stefan@webrtc.org076fa6e2011-12-13 07:54:56391VCMSessionInfo::session_nack() const {
392 return session_nack_;
niklase@google.com470e71d2011-07-07 08:21:25393}
394
stefan@webrtc.org076fa6e2011-12-13 07:54:56395int VCMSessionInfo::InsertPacket(const VCMPacket& packet,
396 uint8_t* frame_buffer,
agalusza@google.coma7e360e2013-08-01 03:15:08397 VCMDecodeErrorMode decode_error_mode,
agalusza@google.comd818dcb2013-07-29 21:48:11398 const FrameData& frame_data) {
stefan@webrtc.org076fa6e2011-12-13 07:54:56399 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.com470e71d2011-07-07 08:21:25403 return 0;
stefan@webrtc.org076fa6e2011-12-13 07:54:56404 }
405
mikhal@webrtc.orgf31a47a2013-08-26 17:10:11406 if (packets_.size() == kMaxPacketsInSession) {
stefan@webrtc.org076fa6e2011-12-13 07:54:56407 return -1;
mikhal@webrtc.orgf31a47a2013-08-26 17:10:11408 }
stefan@webrtc.org076fa6e2011-12-13 07:54:56409
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.org7bc465b2013-04-11 17:48:02414 if (LatestSequenceNumber(packet.seqNum, (*rit).seqNum) == packet.seqNum)
stefan@webrtc.org076fa6e2011-12-13 07:54:56415 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.orgf31a47a2013-08-26 17:10:11422 // 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.org076fa6e2011-12-13 07:54:56448 // 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.coma7e360e2013-08-01 03:15:08453 if (decode_error_mode == kWithErrors)
454 decodable_ = true;
455 else if (decode_error_mode == kSelectiveErrors)
agalusza@google.comd818dcb2013-07-29 21:48:11456 UpdateDecodableSession(frame_data);
stefan@webrtc.org076fa6e2011-12-13 07:54:56457 return returnLength;
niklase@google.com470e71d2011-07-07 08:21:25458}
459
stefan@webrtc.org076fa6e2011-12-13 07:54:56460void 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.org7bc465b2013-04-11 17:48:02465 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.org076fa6e2011-12-13 07:54:56471 empty_seq_num_low_ = seq_num;
472}
473
stefan@webrtc.org076fa6e2011-12-13 07:54:56474} // namespace webrtc