Add support for multiple streams to RtpPlayer:
- Tests video_rtp_play.cc, video_rtp_play_mt.cc, decode_from_storage.cc rewritten - rtp_player.cc/.h rewritten; added interfaces for externally setting up sinks - Support for reading .rtp files pulled out into rtp_file_reader namespace - Added support for reading .pcap (libpcap/wireshark/tcpdump) files, see pcap_file_reader BUG= TEST=trybots Review URL: https://webrtc-codereview.appspot.com/1201009 git-svn-id: http://webrtc.googlecode.com/svn/trunk@3856 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
@ -10,435 +10,479 @@
|
||||
|
||||
#include "webrtc/modules/video_coding/main/test/rtp_player.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#include <Winsock2.h>
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
#include <cstdio>
|
||||
#include <map>
|
||||
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
|
||||
#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
|
||||
#include "webrtc/modules/video_coding/main/source/internal_defines.h"
|
||||
#include "webrtc/modules/video_coding/main/test/pcap_file_reader.h"
|
||||
#include "webrtc/modules/video_coding/main/test/rtp_file_reader.h"
|
||||
#include "webrtc/modules/video_coding/main/test/test_util.h"
|
||||
#include "webrtc/system_wrappers/interface/clock.h"
|
||||
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
|
||||
#include "webrtc/system_wrappers/interface/scoped_ptr.h"
|
||||
|
||||
using namespace webrtc;
|
||||
#if 1
|
||||
# define DEBUG_LOG1(text, arg)
|
||||
#else
|
||||
# define DEBUG_LOG1(text, arg) (printf(text "\n", arg))
|
||||
#endif
|
||||
|
||||
RawRtpPacket::RawRtpPacket(uint8_t* rtp_data, uint16_t rtp_length)
|
||||
: data(rtp_data),
|
||||
length(rtp_length),
|
||||
resend_time_ms(-1) {
|
||||
data = new uint8_t[length];
|
||||
memcpy(data, rtp_data, length);
|
||||
}
|
||||
namespace webrtc {
|
||||
namespace rtpplayer {
|
||||
|
||||
RawRtpPacket::~RawRtpPacket() {
|
||||
delete [] data;
|
||||
}
|
||||
enum {
|
||||
kMaxPacketBufferSize = 4096,
|
||||
kDefaultTransmissionTimeOffsetExtensionId = 2
|
||||
};
|
||||
|
||||
LostPackets::LostPackets()
|
||||
: crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
|
||||
loss_count_(0),
|
||||
debug_file_(NULL),
|
||||
packets_() {
|
||||
debug_file_ = fopen("PacketLossDebug.txt", "w");
|
||||
}
|
||||
class RawRtpPacket {
|
||||
public:
|
||||
RawRtpPacket(const uint8_t* data, uint32_t length, uint32_t ssrc,
|
||||
uint16_t seq_num)
|
||||
: data_(new uint8_t[length]),
|
||||
length_(length),
|
||||
resend_time_ms_(-1),
|
||||
ssrc_(ssrc),
|
||||
seq_num_(seq_num) {
|
||||
assert(data);
|
||||
memcpy(data_.get(), data, length_);
|
||||
}
|
||||
|
||||
LostPackets::~LostPackets() {
|
||||
if (debug_file_) {
|
||||
const uint8_t* data() const { return data_.get(); }
|
||||
uint32_t length() const { return length_; }
|
||||
int64_t resend_time_ms() const { return resend_time_ms_; }
|
||||
void set_resend_time_ms(int64_t timeMs) { resend_time_ms_ = timeMs; }
|
||||
uint32_t ssrc() const { return ssrc_; }
|
||||
uint16_t seq_num() const { return seq_num_; }
|
||||
|
||||
private:
|
||||
scoped_array<uint8_t> data_;
|
||||
uint32_t length_;
|
||||
int64_t resend_time_ms_;
|
||||
uint32_t ssrc_;
|
||||
uint16_t seq_num_;
|
||||
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(RawRtpPacket);
|
||||
};
|
||||
|
||||
class LostPackets {
|
||||
public:
|
||||
LostPackets(Clock* clock, uint32_t rtt_ms)
|
||||
: crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
|
||||
debug_file_(fopen("PacketLossDebug.txt", "w")),
|
||||
loss_count_(0),
|
||||
packets_(),
|
||||
clock_(clock),
|
||||
rtt_ms_(rtt_ms) {
|
||||
assert(clock);
|
||||
}
|
||||
|
||||
~LostPackets() {
|
||||
if (debug_file_) {
|
||||
fclose(debug_file_);
|
||||
debug_file_ = NULL;
|
||||
}
|
||||
while (!packets_.empty()) {
|
||||
delete packets_.back();
|
||||
packets_.pop_back();
|
||||
}
|
||||
}
|
||||
while (!packets_.empty()) {
|
||||
delete packets_.front();
|
||||
packets_.pop_front();
|
||||
}
|
||||
delete crit_sect_;
|
||||
}
|
||||
|
||||
void LostPackets::AddPacket(RawRtpPacket* packet) {
|
||||
CriticalSectionScoped cs(crit_sect_);
|
||||
packets_.push_back(packet);
|
||||
uint16_t seq_num = (packet->data[2] << 8) + packet->data[3];
|
||||
if (debug_file_ != NULL) {
|
||||
fprintf(debug_file_, "%u Lost packet: %u\n", loss_count_, seq_num);
|
||||
void AddPacket(RawRtpPacket* packet) {
|
||||
assert(packet);
|
||||
printf("Throw: %08x:%u\n", packet->ssrc(), packet->seq_num());
|
||||
CriticalSectionScoped cs(crit_sect_.get());
|
||||
if (debug_file_) {
|
||||
fprintf(debug_file_, "%u Lost packet: %u\n", loss_count_,
|
||||
packet->seq_num());
|
||||
}
|
||||
packets_.push_back(packet);
|
||||
loss_count_++;
|
||||
}
|
||||
++loss_count_;
|
||||
}
|
||||
|
||||
void LostPackets::SetResendTime(uint16_t resend_seq_num,
|
||||
int64_t resend_time_ms,
|
||||
int64_t now_ms) {
|
||||
CriticalSectionScoped cs(crit_sect_);
|
||||
for (RtpPacketIterator it = packets_.begin(); it != packets_.end(); ++it) {
|
||||
const uint16_t seq_num = ((*it)->data[2] << 8) +
|
||||
(*it)->data[3];
|
||||
if (resend_seq_num == seq_num) {
|
||||
if ((*it)->resend_time_ms + 10 < now_ms) {
|
||||
if (debug_file_ != NULL) {
|
||||
fprintf(debug_file_, "Resend %u at %u\n", seq_num,
|
||||
void SetResendTime(uint32_t ssrc, int16_t resendSeqNum) {
|
||||
int64_t resend_time_ms = clock_->TimeInMilliseconds() + rtt_ms_;
|
||||
int64_t now_ms = clock_->TimeInMilliseconds();
|
||||
CriticalSectionScoped cs(crit_sect_.get());
|
||||
for (RtpPacketIterator it = packets_.begin(); it != packets_.end(); ++it) {
|
||||
RawRtpPacket* packet = *it;
|
||||
if (ssrc == packet->ssrc() && resendSeqNum == packet->seq_num() &&
|
||||
packet->resend_time_ms() + 10 < now_ms) {
|
||||
if (debug_file_) {
|
||||
fprintf(debug_file_, "Resend %u at %u\n", packet->seq_num(),
|
||||
MaskWord64ToUWord32(resend_time_ms));
|
||||
}
|
||||
(*it)->resend_time_ms = resend_time_ms;
|
||||
packet->set_resend_time_ms(resend_time_ms);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
// We may get here since the captured stream may itself be missing packets.
|
||||
}
|
||||
assert(false);
|
||||
}
|
||||
|
||||
RawRtpPacket* LostPackets::NextPacketToResend(int64_t timeNow) {
|
||||
CriticalSectionScoped cs(crit_sect_);
|
||||
for (RtpPacketIterator it = packets_.begin(); it != packets_.end(); ++it) {
|
||||
if (timeNow >= (*it)->resend_time_ms && (*it)->resend_time_ms != -1) {
|
||||
RawRtpPacket* NextPacketToResend(int64_t time_now) {
|
||||
CriticalSectionScoped cs(crit_sect_.get());
|
||||
for (RtpPacketIterator it = packets_.begin(); it != packets_.end(); ++it) {
|
||||
RawRtpPacket* packet = *it;
|
||||
it = packets_.erase(it);
|
||||
return packet;
|
||||
if (time_now >= packet->resend_time_ms() &&
|
||||
packet->resend_time_ms() != -1) {
|
||||
packets_.erase(it);
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int LostPackets::NumberOfPacketsToResend() const {
|
||||
CriticalSectionScoped cs(crit_sect_);
|
||||
int count = 0;
|
||||
for (ConstRtpPacketIterator it = packets_.begin(); it != packets_.end();
|
||||
++it) {
|
||||
if ((*it)->resend_time_ms >= 0) {
|
||||
int NumberOfPacketsToResend() const {
|
||||
CriticalSectionScoped cs(crit_sect_.get());
|
||||
int count = 0;
|
||||
for (ConstRtpPacketIterator it = packets_.begin(); it != packets_.end();
|
||||
++it) {
|
||||
if ((*it)->resend_time_ms() >= 0) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void LogPacketResent(RawRtpPacket* packet) {
|
||||
int64_t now_ms = clock_->TimeInMilliseconds();
|
||||
CriticalSectionScoped cs(crit_sect_.get());
|
||||
if (debug_file_) {
|
||||
fprintf(debug_file_, "Resent %u at %u\n", packet->seq_num(),
|
||||
MaskWord64ToUWord32(now_ms));
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void LostPackets::SetPacketResent(uint16_t seq_num, int64_t now_ms) {
|
||||
CriticalSectionScoped cs(crit_sect_);
|
||||
if (debug_file_ != NULL) {
|
||||
fprintf(debug_file_, "Resent %u at %u\n", seq_num,
|
||||
MaskWord64ToUWord32(now_ms));
|
||||
}
|
||||
}
|
||||
|
||||
void LostPackets::Print() const {
|
||||
CriticalSectionScoped cs(crit_sect_);
|
||||
printf("Lost packets: %u\n", loss_count_);
|
||||
printf("Packets waiting to be resent: %u\n",
|
||||
NumberOfPacketsToResend());
|
||||
printf("Packets still lost: %u\n",
|
||||
static_cast<unsigned int>(packets_.size()));
|
||||
printf("Sequence numbers:\n");
|
||||
for (ConstRtpPacketIterator it = packets_.begin(); it != packets_.end();
|
||||
++it) {
|
||||
uint16_t seq_num = ((*it)->data[2] << 8) + (*it)->data[3];
|
||||
printf("%u, ", seq_num);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
RTPPlayer::RTPPlayer(const char* filename,
|
||||
RtpData* callback,
|
||||
Clock* clock)
|
||||
:
|
||||
_clock(clock),
|
||||
_rtpModule(NULL),
|
||||
_nextRtpTime(0),
|
||||
_dataCallback(callback),
|
||||
_firstPacket(true),
|
||||
_lossRate(0.0f),
|
||||
_nackEnabled(false),
|
||||
_resendPacketCount(0),
|
||||
_noLossStartup(100),
|
||||
_endOfFile(false),
|
||||
_rttMs(0),
|
||||
_firstPacketRtpTime(0),
|
||||
_firstPacketTimeMs(0),
|
||||
_reorderBuffer(NULL),
|
||||
_reordering(false),
|
||||
_nextPacket(),
|
||||
_nextPacketLength(0),
|
||||
_randVec(),
|
||||
_randVecPos(0)
|
||||
{
|
||||
_rtpFile = fopen(filename, "rb");
|
||||
memset(_nextPacket, 0, sizeof(_nextPacket));
|
||||
}
|
||||
|
||||
RTPPlayer::~RTPPlayer()
|
||||
{
|
||||
delete _rtpModule;
|
||||
if (_rtpFile != NULL)
|
||||
{
|
||||
fclose(_rtpFile);
|
||||
void Print() const {
|
||||
CriticalSectionScoped cs(crit_sect_.get());
|
||||
printf("Lost packets: %u\n", loss_count_);
|
||||
printf("Packets waiting to be resent: %d\n", NumberOfPacketsToResend());
|
||||
printf("Packets still lost: %zd\n", packets_.size());
|
||||
printf("Sequence numbers:\n");
|
||||
for (ConstRtpPacketIterator it = packets_.begin(); it != packets_.end();
|
||||
++it) {
|
||||
printf("%u, ", (*it)->seq_num());
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
private:
|
||||
typedef std::vector<RawRtpPacket*> RtpPacketList;
|
||||
typedef RtpPacketList::iterator RtpPacketIterator;
|
||||
typedef RtpPacketList::const_iterator ConstRtpPacketIterator;
|
||||
|
||||
scoped_ptr<CriticalSectionWrapper> crit_sect_;
|
||||
FILE* debug_file_;
|
||||
int loss_count_;
|
||||
RtpPacketList packets_;
|
||||
Clock* clock_;
|
||||
uint32_t rtt_ms_;
|
||||
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(LostPackets);
|
||||
};
|
||||
|
||||
class SsrcHandlers {
|
||||
public:
|
||||
SsrcHandlers(PayloadSinkFactoryInterface* payload_sink_factory,
|
||||
const PayloadTypes& payload_types)
|
||||
: payload_sink_factory_(payload_sink_factory),
|
||||
payload_types_(payload_types),
|
||||
handlers_() {
|
||||
assert(payload_sink_factory);
|
||||
}
|
||||
|
||||
~SsrcHandlers() {
|
||||
while (!handlers_.empty()) {
|
||||
delete handlers_.begin()->second;
|
||||
handlers_.erase(handlers_.begin());
|
||||
}
|
||||
}
|
||||
|
||||
int RegisterSsrc(uint32_t ssrc, LostPackets* lost_packets) {
|
||||
if (handlers_.count(ssrc) > 0) {
|
||||
return 0;
|
||||
}
|
||||
DEBUG_LOG1("Registering handler for ssrc=%08x", ssrc);
|
||||
|
||||
scoped_ptr<Handler> handler(
|
||||
new Handler(ssrc, payload_types_, lost_packets));
|
||||
handler->payload_sink_.reset(payload_sink_factory_->Create(handler.get()));
|
||||
if (handler->payload_sink_.get() == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (_reorderBuffer != NULL)
|
||||
{
|
||||
delete _reorderBuffer;
|
||||
_reorderBuffer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t RTPPlayer::Initialize(const PayloadTypeList* payloadList)
|
||||
{
|
||||
RtpRtcp::Configuration configuration;
|
||||
configuration.id = 1;
|
||||
configuration.audio = false;
|
||||
configuration.incoming_data = _dataCallback;
|
||||
_rtpModule = RtpRtcp::CreateRtpRtcp(configuration);
|
||||
configuration.incoming_data = handler->payload_sink_.get();
|
||||
handler->rtp_module_.reset(RtpRtcp::CreateRtpRtcp(configuration));
|
||||
if (handler->rtp_module_.get() == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::srand(321);
|
||||
for (int i=0; i < RAND_VEC_LENGTH; i++)
|
||||
{
|
||||
_randVec[i] = rand();
|
||||
if (handler->rtp_module_->SetNACKStatus(kNackOff,
|
||||
kMaxPacketAgeToNack) < 0) {
|
||||
return -1;
|
||||
}
|
||||
_randVecPos = 0;
|
||||
int32_t ret = _rtpModule->SetNACKStatus(kNackOff,
|
||||
kMaxPacketAgeToNack);
|
||||
if (ret < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
_rtpModule->SetRTCPStatus(kRtcpNonCompound);
|
||||
_rtpModule->SetTMMBRStatus(true);
|
||||
handler->rtp_module_->SetRTCPStatus(kRtcpNonCompound);
|
||||
handler->rtp_module_->SetREMBStatus(true);
|
||||
handler->rtp_module_->SetSSRCFilter(true, ssrc);
|
||||
handler->rtp_module_->RegisterReceiveRtpHeaderExtension(
|
||||
kRtpExtensionTransmissionTimeOffset,
|
||||
kDefaultTransmissionTimeOffsetExtensionId);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
for (PayloadTypesIterator it = payload_types_.begin();
|
||||
it != payload_types_.end(); ++it) {
|
||||
VideoCodec codec;
|
||||
memset(&codec, 0, sizeof(codec));
|
||||
strncpy(codec.plName, it->name().c_str(), sizeof(codec.plName)-1);
|
||||
codec.plType = it->payload_type();
|
||||
codec.codecType = it->codec_type();
|
||||
if (handler->rtp_module_->RegisterReceivePayload(codec) < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// Register payload types
|
||||
for (PayloadTypeList::const_iterator it = payloadList->begin();
|
||||
it != payloadList->end(); ++it) {
|
||||
PayloadCodecTuple* payloadType = *it;
|
||||
if (payloadType != NULL)
|
||||
{
|
||||
VideoCodec videoCodec;
|
||||
strncpy(videoCodec.plName, payloadType->name.c_str(), 32);
|
||||
videoCodec.plType = payloadType->payloadType;
|
||||
if (_rtpModule->RegisterReceivePayload(videoCodec) < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ReadHeader() < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
memset(_nextPacket, 0, sizeof(_nextPacket));
|
||||
_nextPacketLength = ReadPacket(_nextPacket, &_nextRtpTime);
|
||||
|
||||
handlers_[ssrc] = handler.release();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t RTPPlayer::ReadHeader()
|
||||
{
|
||||
char firstline[FIRSTLINELEN];
|
||||
if (_rtpFile == NULL)
|
||||
{
|
||||
return -1;
|
||||
void Process() {
|
||||
for (HandlerMapIt it = handlers_.begin(); it != handlers_.end(); ++it) {
|
||||
it->second->rtp_module_->Process();
|
||||
}
|
||||
EXPECT_TRUE(fgets(firstline, FIRSTLINELEN, _rtpFile) != NULL);
|
||||
if(strncmp(firstline,"#!rtpplay",9) == 0) {
|
||||
if(strncmp(firstline,"#!rtpplay1.0",12) != 0){
|
||||
printf("ERROR: wrong rtpplay version, must be 1.0\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
void IncomingPacket(const uint8_t* data, uint32_t length) {
|
||||
for (HandlerMapIt it = handlers_.begin(); it != handlers_.end(); ++it) {
|
||||
it->second->rtp_module_->IncomingPacket(data, length);
|
||||
}
|
||||
else if (strncmp(firstline,"#!RTPencode",11) == 0) {
|
||||
if(strncmp(firstline,"#!RTPencode1.0",14) != 0){
|
||||
printf("ERROR: wrong RTPencode version, must be 1.0\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
class Handler : public RtpStreamInterface {
|
||||
public:
|
||||
Handler(uint32_t ssrc, const PayloadTypes& payload_types,
|
||||
LostPackets* lost_packets)
|
||||
: rtp_module_(),
|
||||
payload_sink_(),
|
||||
ssrc_(ssrc),
|
||||
payload_types_(payload_types),
|
||||
lost_packets_(lost_packets) {
|
||||
assert(lost_packets);
|
||||
}
|
||||
else {
|
||||
printf("ERROR: wrong file format of input file\n");
|
||||
return -1;
|
||||
virtual ~Handler() {}
|
||||
|
||||
virtual void ResendPackets(const uint16_t* sequence_numbers,
|
||||
uint16_t length) {
|
||||
assert(sequence_numbers);
|
||||
for (uint16_t i = 0; i < length; i++) {
|
||||
lost_packets_->SetResendTime(ssrc_, sequence_numbers[i]);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t start_sec;
|
||||
uint32_t start_usec;
|
||||
uint32_t source;
|
||||
uint16_t port;
|
||||
uint16_t padding;
|
||||
|
||||
EXPECT_GT(fread(&start_sec, 4, 1, _rtpFile), 0u);
|
||||
start_sec=ntohl(start_sec);
|
||||
EXPECT_GT(fread(&start_usec, 4, 1, _rtpFile), 0u);
|
||||
start_usec=ntohl(start_usec);
|
||||
EXPECT_GT(fread(&source, 4, 1, _rtpFile), 0u);
|
||||
source=ntohl(source);
|
||||
EXPECT_GT(fread(&port, 2, 1, _rtpFile), 0u);
|
||||
port=ntohs(port);
|
||||
EXPECT_GT(fread(&padding, 2, 1, _rtpFile), 0u);
|
||||
padding=ntohs(padding);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t RTPPlayer::TimeUntilNextPacket() const
|
||||
{
|
||||
int64_t timeLeft = (_nextRtpTime - _firstPacketRtpTime) -
|
||||
(_clock->TimeInMilliseconds() - _firstPacketTimeMs);
|
||||
if (timeLeft < 0)
|
||||
{
|
||||
return 0;
|
||||
virtual uint32_t ssrc() const { return ssrc_; }
|
||||
virtual const PayloadTypes& payload_types() const {
|
||||
return payload_types_;
|
||||
}
|
||||
return static_cast<uint32_t>(timeLeft);
|
||||
}
|
||||
|
||||
int32_t RTPPlayer::NextPacket(const int64_t timeNow)
|
||||
{
|
||||
// Send any packets ready to be resent,
|
||||
RawRtpPacket* resend_packet = _lostPackets.NextPacketToResend(timeNow);
|
||||
while (resend_packet != NULL) {
|
||||
const uint16_t seqNo = (resend_packet->data[2] << 8) +
|
||||
resend_packet->data[3];
|
||||
printf("Resend: %u\n", seqNo);
|
||||
int ret = SendPacket(resend_packet->data, resend_packet->length);
|
||||
delete resend_packet;
|
||||
_resendPacketCount++;
|
||||
scoped_ptr<RtpRtcp> rtp_module_;
|
||||
scoped_ptr<PayloadSinkInterface> payload_sink_;
|
||||
|
||||
private:
|
||||
uint32_t ssrc_;
|
||||
const PayloadTypes& payload_types_;
|
||||
LostPackets* lost_packets_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Handler);
|
||||
};
|
||||
|
||||
typedef std::map<uint32_t, Handler*> HandlerMap;
|
||||
typedef std::map<uint32_t, Handler*>::iterator HandlerMapIt;
|
||||
|
||||
PayloadSinkFactoryInterface* payload_sink_factory_;
|
||||
PayloadTypes payload_types_;
|
||||
HandlerMap handlers_;
|
||||
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(SsrcHandlers);
|
||||
};
|
||||
|
||||
class RtpPlayerImpl : public RtpPlayerInterface {
|
||||
public:
|
||||
RtpPlayerImpl(PayloadSinkFactoryInterface* payload_sink_factory,
|
||||
const PayloadTypes& payload_types, Clock* clock,
|
||||
scoped_ptr<RtpPacketSourceInterface>* packet_source,
|
||||
float loss_rate, uint32_t rtt_ms, bool reordering)
|
||||
: ssrc_handlers_(payload_sink_factory, payload_types),
|
||||
clock_(clock),
|
||||
packet_source_(NULL),
|
||||
next_rtp_time_(0),
|
||||
first_packet_(true),
|
||||
first_packet_rtp_time_(0),
|
||||
first_packet_time_ms_(0),
|
||||
loss_rate_(loss_rate),
|
||||
lost_packets_(clock, rtt_ms),
|
||||
resend_packet_count_(0),
|
||||
no_loss_startup_(100),
|
||||
end_of_file_(false),
|
||||
reordering_(false),
|
||||
reorder_buffer_(),
|
||||
next_packet_(),
|
||||
next_packet_length_(0) {
|
||||
assert(clock);
|
||||
assert(packet_source);
|
||||
assert(packet_source->get());
|
||||
packet_source_.swap(*packet_source);
|
||||
srand(321);
|
||||
}
|
||||
|
||||
virtual ~RtpPlayerImpl() {}
|
||||
|
||||
virtual int NextPacket(int64_t time_now) {
|
||||
// Send any packets ready to be resent.
|
||||
RawRtpPacket* packet;
|
||||
while ((packet = lost_packets_.NextPacketToResend(time_now))) {
|
||||
int ret = SendPacket(packet->data(), packet->length());
|
||||
if (ret > 0) {
|
||||
_lostPackets.SetPacketResent(seqNo, _clock->TimeInMilliseconds());
|
||||
} else if (ret < 0) {
|
||||
printf("Resend: %08x:%u\n", packet->ssrc(), packet->seq_num());
|
||||
lost_packets_.LogPacketResent(packet);
|
||||
resend_packet_count_++;
|
||||
}
|
||||
delete packet;
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
resend_packet = _lostPackets.NextPacketToResend(timeNow);
|
||||
}
|
||||
|
||||
// Send any packets from rtp file
|
||||
if (!_endOfFile && (TimeUntilNextPacket() == 0 || _firstPacket))
|
||||
{
|
||||
_rtpModule->Process();
|
||||
if (_firstPacket)
|
||||
{
|
||||
_firstPacketRtpTime = static_cast<int64_t>(_nextRtpTime);
|
||||
_firstPacketTimeMs = _clock->TimeInMilliseconds();
|
||||
// Send any packets from packet source.
|
||||
if (!end_of_file_ && (TimeUntilNextPacket() == 0 || first_packet_)) {
|
||||
ssrc_handlers_.Process();
|
||||
|
||||
if (first_packet_) {
|
||||
next_packet_length_ = sizeof(next_packet_);
|
||||
if (packet_source_->NextPacket(next_packet_, &next_packet_length_,
|
||||
&next_rtp_time_) != 0) {
|
||||
return 0;
|
||||
}
|
||||
if (_reordering && _reorderBuffer == NULL)
|
||||
{
|
||||
_reorderBuffer = new RawRtpPacket(reinterpret_cast<uint8_t*>(_nextPacket), static_cast<uint16_t>(_nextPacketLength));
|
||||
return 0;
|
||||
}
|
||||
int32_t ret = SendPacket(reinterpret_cast<uint8_t*>(_nextPacket), static_cast<uint16_t>(_nextPacketLength));
|
||||
if (_reordering && _reorderBuffer != NULL)
|
||||
{
|
||||
RawRtpPacket* rtpPacket = _reorderBuffer;
|
||||
_reorderBuffer = NULL;
|
||||
SendPacket(rtpPacket->data, rtpPacket->length);
|
||||
delete rtpPacket;
|
||||
}
|
||||
_firstPacket = false;
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
_nextPacketLength = ReadPacket(_nextPacket, &_nextRtpTime);
|
||||
if (_nextPacketLength < 0)
|
||||
{
|
||||
_endOfFile = true;
|
||||
return 0;
|
||||
}
|
||||
else if (_nextPacketLength == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (_endOfFile && _lostPackets.NumberOfPacketsToResend() == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
first_packet_rtp_time_ = next_rtp_time_;
|
||||
first_packet_time_ms_ = clock_->TimeInMilliseconds();
|
||||
first_packet_ = false;
|
||||
}
|
||||
|
||||
int32_t RTPPlayer::SendPacket(uint8_t* rtpData, uint16_t rtpLen)
|
||||
{
|
||||
if ((_randVec[(_randVecPos++) % RAND_VEC_LENGTH] + 1.0)/(RAND_MAX + 1.0) < _lossRate &&
|
||||
_noLossStartup < 0)
|
||||
{
|
||||
if (_nackEnabled)
|
||||
{
|
||||
const uint16_t seqNo = (rtpData[2] << 8) + rtpData[3];
|
||||
printf("Throw: %u\n", seqNo);
|
||||
_lostPackets.AddPacket(new RawRtpPacket(rtpData, rtpLen));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if (rtpLen > 0)
|
||||
{
|
||||
int32_t ret = _rtpModule->IncomingPacket(rtpData, rtpLen);
|
||||
if (ret < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (_noLossStartup >= 0)
|
||||
{
|
||||
_noLossStartup--;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int32_t RTPPlayer::ReadPacket(int16_t* rtpdata, uint32_t* offset)
|
||||
{
|
||||
uint16_t length, plen;
|
||||
|
||||
if (fread(&length,2,1,_rtpFile)==0)
|
||||
return(-1);
|
||||
length=ntohs(length);
|
||||
|
||||
if (fread(&plen,2,1,_rtpFile)==0)
|
||||
return(-1);
|
||||
plen=ntohs(plen);
|
||||
|
||||
if (fread(offset,4,1,_rtpFile)==0)
|
||||
return(-1);
|
||||
*offset=ntohl(*offset);
|
||||
|
||||
// Use length here because a plen of 0 specifies rtcp
|
||||
length = (uint16_t) (length - HDR_SIZE);
|
||||
if (fread((unsigned short *) rtpdata,1,length,_rtpFile) != length)
|
||||
return(-1);
|
||||
|
||||
#ifdef JUNK_DATA
|
||||
// destroy the RTP payload with random data
|
||||
if (plen > 12) { // ensure that we have more than just a header
|
||||
for ( int ix = 12; ix < plen; ix=ix+2 ) {
|
||||
rtpdata[ix>>1] = (short) (rtpdata[ix>>1] + (short) rand());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return plen;
|
||||
}
|
||||
|
||||
int32_t RTPPlayer::SimulatePacketLoss(float lossRate, bool enableNack, uint32_t rttMs)
|
||||
{
|
||||
_nackEnabled = enableNack;
|
||||
_lossRate = lossRate;
|
||||
_rttMs = rttMs;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t RTPPlayer::SetReordering(bool enabled)
|
||||
{
|
||||
_reordering = enabled;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t RTPPlayer::ResendPackets(const uint16_t* sequenceNumbers, uint16_t length)
|
||||
{
|
||||
if (sequenceNumbers == NULL)
|
||||
{
|
||||
if (reordering_ && reorder_buffer_.get() == NULL) {
|
||||
reorder_buffer_.reset(new RawRtpPacket(next_packet_,
|
||||
next_packet_length_, 0, 0));
|
||||
return 0;
|
||||
}
|
||||
int ret = SendPacket(next_packet_, next_packet_length_);
|
||||
if (reorder_buffer_.get()) {
|
||||
SendPacket(reorder_buffer_->data(), reorder_buffer_->length());
|
||||
reorder_buffer_.reset(NULL);
|
||||
}
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
next_packet_length_ = sizeof(next_packet_);
|
||||
if (packet_source_->NextPacket(next_packet_, &next_packet_length_,
|
||||
&next_rtp_time_) != 0) {
|
||||
end_of_file_ = true;
|
||||
return 0;
|
||||
}
|
||||
else if (next_packet_length_ == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
for (int i=0; i < length; i++)
|
||||
{
|
||||
_lostPackets.SetResendTime(sequenceNumbers[i],
|
||||
_clock->TimeInMilliseconds() + _rttMs,
|
||||
_clock->TimeInMilliseconds());
|
||||
|
||||
if (end_of_file_ && lost_packets_.NumberOfPacketsToResend() == 0) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void RTPPlayer::Print() const
|
||||
{
|
||||
printf("Resent packets: %u\n", _resendPacketCount);
|
||||
_lostPackets.Print();
|
||||
virtual uint32_t TimeUntilNextPacket() const {
|
||||
int64_t time_left = (next_rtp_time_ - first_packet_rtp_time_) -
|
||||
(clock_->TimeInMilliseconds() - first_packet_time_ms_);
|
||||
if (time_left < 0) {
|
||||
return 0;
|
||||
}
|
||||
return static_cast<uint32_t>(time_left);
|
||||
}
|
||||
|
||||
virtual void Print() const {
|
||||
printf("Resent packets: %u\n", resend_packet_count_);
|
||||
lost_packets_.Print();
|
||||
}
|
||||
|
||||
private:
|
||||
int SendPacket(const uint8_t* data, uint32_t length) {
|
||||
assert(data);
|
||||
assert(length > 0);
|
||||
|
||||
ModuleRTPUtility::RTPHeaderParser rtp_header_parser(data, length);
|
||||
if (!rtp_header_parser.RTCP()) {
|
||||
WebRtcRTPHeader header;
|
||||
if (!rtp_header_parser.Parse(header, NULL)) {
|
||||
return -1;
|
||||
}
|
||||
uint32_t ssrc = header.header.ssrc;
|
||||
if (ssrc_handlers_.RegisterSsrc(ssrc, &lost_packets_) < 0) {
|
||||
DEBUG_LOG1("Unable to register ssrc: %d", ssrc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (no_loss_startup_ > 0) {
|
||||
no_loss_startup_--;
|
||||
} else if ((rand() + 1.0)/(RAND_MAX + 1.0) < loss_rate_) {
|
||||
uint16_t seq_num = header.header.sequenceNumber;
|
||||
lost_packets_.AddPacket(new RawRtpPacket(data, length, ssrc, seq_num));
|
||||
DEBUG_LOG1("Dropped packet: %d!", header.header.sequenceNumber);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
ssrc_handlers_.IncomingPacket(data, length);
|
||||
return 1;
|
||||
}
|
||||
|
||||
SsrcHandlers ssrc_handlers_;
|
||||
Clock* clock_;
|
||||
scoped_ptr<RtpPacketSourceInterface> packet_source_;
|
||||
uint32_t next_rtp_time_;
|
||||
bool first_packet_;
|
||||
int64_t first_packet_rtp_time_;
|
||||
int64_t first_packet_time_ms_;
|
||||
float loss_rate_;
|
||||
LostPackets lost_packets_;
|
||||
uint32_t resend_packet_count_;
|
||||
uint32_t no_loss_startup_;
|
||||
bool end_of_file_;
|
||||
bool reordering_;
|
||||
scoped_ptr<RawRtpPacket> reorder_buffer_;
|
||||
uint8_t next_packet_[kMaxPacketBufferSize];
|
||||
uint32_t next_packet_length_;
|
||||
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(RtpPlayerImpl);
|
||||
};
|
||||
|
||||
RtpPlayerInterface* Create(const std::string& input_filename,
|
||||
PayloadSinkFactoryInterface* payload_sink_factory, Clock* clock,
|
||||
const PayloadTypes& payload_types, float loss_rate, uint32_t rtt_ms,
|
||||
bool reordering) {
|
||||
scoped_ptr<RtpPacketSourceInterface> packet_source(
|
||||
CreateRtpFileReader(input_filename));
|
||||
if (packet_source.get() == NULL) {
|
||||
packet_source.reset(CreatePcapFileReader(input_filename));
|
||||
if (packet_source.get() == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
scoped_ptr<RtpPlayerImpl> impl(new RtpPlayerImpl(payload_sink_factory,
|
||||
payload_types, clock, &packet_source, loss_rate, rtt_ms, reordering));
|
||||
return impl.release();
|
||||
}
|
||||
} // namespace rtpplayer
|
||||
} // namespace webrtc
|
||||
|
||||
Reference in New Issue
Block a user