1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h> // memset
#include <limits>
#include <set>
#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
#include "webrtc/system_wrappers/interface/logging.h"
namespace webrtc {
enum { kMinPacketRequestBytes = 50 };
RTPPacketHistory::RTPPacketHistory(Clock* clock)
: clock_(clock),
critsect_(CriticalSectionWrapper::CreateCriticalSection()),
store_(false),
prev_index_(0),
max_packet_length_(0) {
}
RTPPacketHistory::~RTPPacketHistory() {
{
CriticalSectionScoped cs(critsect_);
Free();
}
delete critsect_;
}
void RTPPacketHistory::SetStorePacketsStatus(bool enable,
uint16_t number_to_store) {
CriticalSectionScoped cs(critsect_);
if (enable) {
if (store_) {
LOG(LS_WARNING) << "Purging packet history in order to re-set status.";
Free();
}
Allocate(number_to_store);
} else {
Free();
}
}
void RTPPacketHistory::Allocate(uint16_t number_to_store) {
assert(number_to_store > 0);
assert(!store_);
store_ = true;
stored_packets_.resize(number_to_store);
stored_seq_nums_.resize(number_to_store);
stored_lengths_.resize(number_to_store);
stored_times_.resize(number_to_store);
stored_send_times_.resize(number_to_store);
stored_types_.resize(number_to_store);
}
void RTPPacketHistory::Free() {
if (!store_) {
return;
}
std::vector<std::vector<uint8_t> >::iterator it;
for (it = stored_packets_.begin(); it != stored_packets_.end(); ++it) {
it->clear();
}
stored_packets_.clear();
stored_seq_nums_.clear();
stored_lengths_.clear();
stored_times_.clear();
stored_send_times_.clear();
stored_types_.clear();
store_ = false;
prev_index_ = 0;
max_packet_length_ = 0;
}
bool RTPPacketHistory::StorePackets() const {
CriticalSectionScoped cs(critsect_);
return store_;
}
// private, lock should already be taken
void RTPPacketHistory::VerifyAndAllocatePacketLength(size_t packet_length) {
assert(packet_length > 0);
if (!store_) {
return;
}
if (packet_length <= max_packet_length_) {
return;
}
std::vector<std::vector<uint8_t> >::iterator it;
for (it = stored_packets_.begin(); it != stored_packets_.end(); ++it) {
it->resize(packet_length);
}
max_packet_length_ = packet_length;
}
int32_t RTPPacketHistory::PutRTPPacket(const uint8_t* packet,
size_t packet_length,
size_t max_packet_length,
int64_t capture_time_ms,
StorageType type) {
if (type == kDontStore) {
return 0;
}
CriticalSectionScoped cs(critsect_);
if (!store_) {
return 0;
}
assert(packet);
assert(packet_length > 3);
VerifyAndAllocatePacketLength(max_packet_length);
if (packet_length > max_packet_length_) {
LOG(LS_WARNING) << "Failed to store RTP packet with length: "
<< packet_length;
return -1;
}
const uint16_t seq_num = (packet[2] << 8) + packet[3];
// Store packet
std::vector<std::vector<uint8_t> >::iterator it =
stored_packets_.begin() + prev_index_;
std::copy(packet, packet + packet_length, it->begin());
stored_seq_nums_[prev_index_] = seq_num;
stored_lengths_[prev_index_] = packet_length;
stored_times_[prev_index_] = (capture_time_ms > 0) ? capture_time_ms :
clock_->TimeInMilliseconds();
stored_send_times_[prev_index_] = 0; // Packet not sent.
stored_types_[prev_index_] = type;
++prev_index_;
if (prev_index_ >= stored_seq_nums_.size()) {
prev_index_ = 0;
}
return 0;
}
bool RTPPacketHistory::HasRTPPacket(uint16_t sequence_number) const {
CriticalSectionScoped cs(critsect_);
if (!store_) {
return false;
}
int32_t index = 0;
bool found = FindSeqNum(sequence_number, &index);
if (!found) {
return false;
}
size_t length = stored_lengths_.at(index);
if (length == 0 || length > max_packet_length_) {
// Invalid length.
return false;
}
return true;
}
bool RTPPacketHistory::GetPacketAndSetSendTime(uint16_t sequence_number,
uint32_t min_elapsed_time_ms,
bool retransmit,
uint8_t* packet,
size_t* packet_length,
int64_t* stored_time_ms) {
assert(*packet_length >= max_packet_length_);
CriticalSectionScoped cs(critsect_);
if (!store_) {
return false;
}
int32_t index = 0;
bool found = FindSeqNum(sequence_number, &index);
if (!found) {
LOG(LS_WARNING) << "No match for getting seqNum " << sequence_number;
return false;
}
size_t length = stored_lengths_.at(index);
assert(length <= max_packet_length_);
if (length == 0) {
LOG(LS_WARNING) << "No match for getting seqNum " << sequence_number
<< ", len " << length;
return false;
}
// Verify elapsed time since last retrieve.
int64_t now = clock_->TimeInMilliseconds();
if (min_elapsed_time_ms > 0 &&
((now - stored_send_times_.at(index)) < min_elapsed_time_ms)) {
return false;
}
if (retransmit && stored_types_.at(index) == kDontRetransmit) {
// No bytes copied since this packet shouldn't be retransmitted or is
// of zero size.
return false;
}
stored_send_times_[index] = clock_->TimeInMilliseconds();
GetPacket(index, packet, packet_length, stored_time_ms);
return true;
}
void RTPPacketHistory::GetPacket(int index,
uint8_t* packet,
size_t* packet_length,
int64_t* stored_time_ms) const {
// Get packet.
size_t length = stored_lengths_.at(index);
std::vector<std::vector<uint8_t> >::const_iterator it_found_packet =
stored_packets_.begin() + index;
std::copy(it_found_packet->begin(), it_found_packet->begin() + length,
packet);
*packet_length = length;
*stored_time_ms = stored_times_.at(index);
}
bool RTPPacketHistory::GetBestFittingPacket(uint8_t* packet,
size_t* packet_length,
int64_t* stored_time_ms) {
CriticalSectionScoped cs(critsect_);
if (!store_)
return false;
int index = FindBestFittingPacket(*packet_length);
if (index < 0)
return false;
GetPacket(index, packet, packet_length, stored_time_ms);
return true;
}
// private, lock should already be taken
bool RTPPacketHistory::FindSeqNum(uint16_t sequence_number,
int32_t* index) const {
uint16_t temp_sequence_number = 0;
if (prev_index_ > 0) {
*index = prev_index_ - 1;
temp_sequence_number = stored_seq_nums_[*index];
} else {
*index = stored_seq_nums_.size() - 1;
temp_sequence_number = stored_seq_nums_[*index]; // wrap
}
int32_t idx = (prev_index_ - 1) - (temp_sequence_number - sequence_number);
if (idx >= 0 && idx < static_cast<int>(stored_seq_nums_.size())) {
*index = idx;
temp_sequence_number = stored_seq_nums_[*index];
}
if (temp_sequence_number != sequence_number) {
// We did not found a match, search all.
for (uint16_t m = 0; m < stored_seq_nums_.size(); m++) {
if (stored_seq_nums_[m] == sequence_number) {
*index = m;
temp_sequence_number = stored_seq_nums_[*index];
break;
}
}
}
if (temp_sequence_number == sequence_number) {
// We found a match.
return true;
}
return false;
}
int RTPPacketHistory::FindBestFittingPacket(size_t size) const {
if (size < kMinPacketRequestBytes || stored_lengths_.empty())
return -1;
size_t min_diff = std::numeric_limits<size_t>::max();
int best_index = -1; // Returned unchanged if we don't find anything.
for (size_t i = 0; i < stored_lengths_.size(); ++i) {
if (stored_lengths_[i] == 0)
continue;
size_t diff = (stored_lengths_[i] > size) ?
(stored_lengths_[i] - size) : (size - stored_lengths_[i]);
if (diff < min_diff) {
min_diff = diff;
best_index = static_cast<int>(i);
}
}
return best_index;
}
} // namespace webrtc
|