2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-01-24 17:16:59 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-11-13 21:12:39 +00:00
|
|
|
#include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-08-05 16:22:53 +00:00
|
|
|
#include <stdlib.h> // srand
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-11-13 21:12:39 +00:00
|
|
|
#include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
|
|
|
|
|
#include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
|
|
|
|
|
#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
|
|
|
|
|
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
|
|
|
|
|
#include "webrtc/system_wrappers/interface/trace.h"
|
2013-04-09 19:54:10 +00:00
|
|
|
#include "webrtc/system_wrappers/interface/trace_event.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2013-01-25 10:53:38 +00:00
|
|
|
|
2013-06-04 13:47:36 +00:00
|
|
|
// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
|
|
|
|
|
const int kMaxPaddingLength = 224;
|
|
|
|
|
|
2013-04-09 19:54:10 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
const char* FrameTypeToString(const FrameType frame_type) {
|
|
|
|
|
switch (frame_type) {
|
|
|
|
|
case kFrameEmpty: return "empty";
|
|
|
|
|
case kAudioFrameSpeech: return "audio_speech";
|
|
|
|
|
case kAudioFrameCN: return "audio_cn";
|
|
|
|
|
case kVideoFrameKey: return "video_key";
|
|
|
|
|
case kVideoFrameDelta: return "video_delta";
|
|
|
|
|
case kVideoFrameGolden: return "video_golden";
|
|
|
|
|
case kVideoFrameAltRef: return "video_altref";
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
|
2013-01-25 10:53:38 +00:00
|
|
|
Transport *transport, RtpAudioFeedback *audio_feedback,
|
|
|
|
|
PacedSender *paced_sender)
|
|
|
|
|
: Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL),
|
|
|
|
|
video_(NULL), paced_sender_(paced_sender),
|
|
|
|
|
send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
|
|
|
|
|
transport_(transport), sending_media_(true), // Default to sending media.
|
|
|
|
|
max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
|
|
|
|
|
target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1),
|
|
|
|
|
payload_type_map_(), rtp_header_extension_map_(),
|
2013-05-16 11:10:31 +00:00
|
|
|
transmission_time_offset_(0), absolute_send_time_(0),
|
2013-01-25 10:53:38 +00:00
|
|
|
// NACK.
|
|
|
|
|
nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
|
|
|
|
|
packet_history_(new RTPPacketHistory(clock)),
|
|
|
|
|
// Statistics
|
|
|
|
|
packets_sent_(0), payload_bytes_sent_(0), start_time_stamp_forced_(false),
|
|
|
|
|
start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
|
2013-03-15 23:21:52 +00:00
|
|
|
remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false),
|
2013-06-19 14:13:42 +00:00
|
|
|
timestamp_(0), capture_time_ms_(0), last_packet_marker_bit_(false),
|
|
|
|
|
num_csrcs_(0), csrcs_(), include_csrcs_(true),
|
2013-04-12 14:55:46 +00:00
|
|
|
rtx_(kRtxOff), payload_type_rtx_(-1) {
|
2013-01-25 10:53:38 +00:00
|
|
|
memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
|
|
|
|
|
memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
|
2013-06-04 13:47:36 +00:00
|
|
|
memset(csrcs_, 0, sizeof(csrcs_));
|
2012-11-07 17:01:04 +00:00
|
|
|
// We need to seed the random generator.
|
2013-04-08 11:08:41 +00:00
|
|
|
srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
|
2013-01-25 10:53:38 +00:00
|
|
|
ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
|
2013-03-15 23:21:52 +00:00
|
|
|
ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
|
|
|
|
|
// Random start, 16 bits. Can't be 0.
|
|
|
|
|
sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
|
|
|
|
|
sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
|
2012-11-07 17:01:04 +00:00
|
|
|
|
|
|
|
|
if (audio) {
|
2013-01-25 10:53:38 +00:00
|
|
|
audio_ = new RTPSenderAudio(id, clock_, this);
|
|
|
|
|
audio_->RegisterAudioCallback(audio_feedback);
|
2012-11-07 17:01:04 +00:00
|
|
|
} else {
|
2013-01-25 10:53:38 +00:00
|
|
|
video_ = new RTPSenderVideo(id, clock_, this);
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-01-19 15:56:10 +00:00
|
|
|
RTPSender::~RTPSender() {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (remote_ssrc_ != 0) {
|
|
|
|
|
ssrc_db_.ReturnSSRC(remote_ssrc_);
|
2012-01-19 15:56:10 +00:00
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
ssrc_db_.ReturnSSRC(ssrc_);
|
2012-01-19 15:56:10 +00:00
|
|
|
|
|
|
|
|
SSRCDatabase::ReturnSSRCDatabase();
|
2013-01-25 10:53:38 +00:00
|
|
|
delete send_critsect_;
|
|
|
|
|
while (!payload_type_map_.empty()) {
|
2013-04-08 11:08:41 +00:00
|
|
|
std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
|
2013-01-25 10:53:38 +00:00
|
|
|
payload_type_map_.begin();
|
2012-01-19 15:56:10 +00:00
|
|
|
delete it->second;
|
2013-01-25 10:53:38 +00:00
|
|
|
payload_type_map_.erase(it);
|
2012-01-19 15:56:10 +00:00
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
delete packet_history_;
|
|
|
|
|
delete audio_;
|
|
|
|
|
delete video_;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
|
2013-01-25 10:53:38 +00:00
|
|
|
target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2011-10-14 14:24:54 +00:00
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint16_t RTPSender::ActualSendBitrateKbit() const {
|
|
|
|
|
return (uint16_t)(Bitrate::BitrateNow() / 1000);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t RTPSender::VideoBitrateSent() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (video_) {
|
|
|
|
|
return video_->VideoBitrateSent();
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
return 0;
|
2011-10-27 16:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t RTPSender::FecOverheadRate() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (video_) {
|
|
|
|
|
return video_->FecOverheadRate();
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
return 0;
|
2011-10-14 14:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t RTPSender::NackOverheadRate() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
return nack_bitrate_.BitrateLast();
|
2011-10-14 14:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::SetTransmissionTimeOffset(
|
|
|
|
|
const int32_t transmission_time_offset) {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (transmission_time_offset > (0x800000 - 1) ||
|
|
|
|
|
transmission_time_offset < -(0x800000 - 1)) { // Word24.
|
2012-11-07 17:01:04 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
transmission_time_offset_ = transmission_time_offset;
|
2012-11-07 17:01:04 +00:00
|
|
|
return 0;
|
2011-12-16 14:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-16 11:10:31 +00:00
|
|
|
int32_t RTPSender::SetAbsoluteSendTime(
|
|
|
|
|
const uint32_t absolute_send_time) {
|
|
|
|
|
if (absolute_send_time > 0xffffff) { // UWord24.
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
absolute_send_time_ = absolute_send_time;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
|
|
|
|
|
const uint8_t id) {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
return rtp_header_extension_map_.Register(type, id);
|
2011-12-16 14:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::DeregisterRtpHeaderExtension(
|
2012-11-07 17:01:04 +00:00
|
|
|
const RTPExtensionType type) {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
return rtp_header_extension_map_.Deregister(type);
|
2011-12-16 14:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
return rtp_header_extension_map_.GetTotalLengthInBytes();
|
2011-12-16 14:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::RegisterPayload(
|
2013-01-25 10:53:38 +00:00
|
|
|
const char payload_name[RTP_PAYLOAD_NAME_SIZE],
|
2013-04-08 11:08:41 +00:00
|
|
|
const int8_t payload_number, const uint32_t frequency,
|
|
|
|
|
const uint8_t channels, const uint32_t rate) {
|
2013-01-25 10:53:38 +00:00
|
|
|
assert(payload_name);
|
|
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
|
2013-01-25 10:53:38 +00:00
|
|
|
payload_type_map_.find(payload_number);
|
|
|
|
|
|
|
|
|
|
if (payload_type_map_.end() != it) {
|
|
|
|
|
// We already use this payload type.
|
|
|
|
|
ModuleRTPUtility::Payload *payload = it->second;
|
2012-01-19 15:56:10 +00:00
|
|
|
assert(payload);
|
|
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
// Check if it's the same as we already have.
|
|
|
|
|
if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
|
2012-01-24 17:16:59 +00:00
|
|
|
RTP_PAYLOAD_NAME_SIZE - 1)) {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (audio_configured_ && payload->audio &&
|
2012-01-19 15:56:10 +00:00
|
|
|
payload->typeSpecific.Audio.frequency == frequency &&
|
|
|
|
|
(payload->typeSpecific.Audio.rate == rate ||
|
2013-01-25 10:53:38 +00:00
|
|
|
payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
|
2012-01-19 15:56:10 +00:00
|
|
|
payload->typeSpecific.Audio.rate = rate;
|
2013-01-25 10:53:38 +00:00
|
|
|
// Ensure that we update the rate if new or old is zero.
|
2012-01-19 15:56:10 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!audio_configured_ && !payload->audio) {
|
2012-01-19 15:56:10 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2012-01-19 15:56:10 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t ret_val = -1;
|
2013-01-25 10:53:38 +00:00
|
|
|
ModuleRTPUtility::Payload *payload = NULL;
|
|
|
|
|
if (audio_configured_) {
|
|
|
|
|
ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
|
|
|
|
|
frequency, channels, rate, payload);
|
2012-01-19 15:56:10 +00:00
|
|
|
} else {
|
2013-01-25 10:53:38 +00:00
|
|
|
ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
|
|
|
|
|
payload);
|
2012-01-19 15:56:10 +00:00
|
|
|
}
|
2012-11-07 17:01:04 +00:00
|
|
|
if (payload) {
|
2013-01-25 10:53:38 +00:00
|
|
|
payload_type_map_[payload_number] = payload;
|
2012-01-19 15:56:10 +00:00
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return ret_val;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::DeRegisterSendPayload(
|
|
|
|
|
const int8_t payload_type) {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped lock(send_critsect_);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
|
2013-01-25 10:53:38 +00:00
|
|
|
payload_type_map_.find(payload_type);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
if (payload_type_map_.end() == it) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
ModuleRTPUtility::Payload *payload = it->second;
|
2012-01-19 15:56:10 +00:00
|
|
|
delete payload;
|
2013-01-25 10:53:38 +00:00
|
|
|
payload_type_map_.erase(it);
|
2012-01-19 15:56:10 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int8_t RTPSender::SendPayloadType() const { return payload_type_; }
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
int RTPSender::SendPayloadFrequency() const { return audio_->AudioFrequency(); }
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::SetMaxPayloadLength(
|
|
|
|
|
const uint16_t max_payload_length,
|
|
|
|
|
const uint16_t packet_over_head) {
|
2013-01-25 10:53:38 +00:00
|
|
|
// Sanity check.
|
|
|
|
|
if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
|
|
|
|
|
WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
|
|
|
|
|
__FUNCTION__);
|
2012-11-07 17:01:04 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
max_payload_length_ = max_payload_length;
|
|
|
|
|
packet_over_head_ = packet_over_head;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
|
|
|
|
|
max_payload_length);
|
2012-11-07 17:01:04 +00:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint16_t RTPSender::MaxDataPayloadLength() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (audio_configured_) {
|
|
|
|
|
return max_payload_length_ - RTPHeaderLength();
|
2012-01-10 14:09:18 +00:00
|
|
|
} else {
|
2013-01-25 10:53:38 +00:00
|
|
|
return max_payload_length_ - RTPHeaderLength() -
|
|
|
|
|
video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
|
|
|
|
|
// Include the FEC/ULP/RED overhead.
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint16_t RTPSender::MaxPayloadLength() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
return max_payload_length_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-04-12 14:55:46 +00:00
|
|
|
void RTPSender::SetRTXStatus(RtxMode mode, bool set_ssrc, uint32_t ssrc) {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2013-03-15 23:21:52 +00:00
|
|
|
rtx_ = mode;
|
|
|
|
|
if (rtx_ != kRtxOff) {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (set_ssrc) {
|
|
|
|
|
ssrc_rtx_ = ssrc;
|
2012-01-10 14:09:18 +00:00
|
|
|
} else {
|
2013-01-25 10:53:38 +00:00
|
|
|
ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-12 14:55:46 +00:00
|
|
|
void RTPSender::RTXStatus(RtxMode* mode, uint32_t* ssrc,
|
|
|
|
|
int* payload_type) const {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2013-03-15 23:21:52 +00:00
|
|
|
*mode = rtx_;
|
2013-04-12 14:55:46 +00:00
|
|
|
*ssrc = ssrc_rtx_;
|
|
|
|
|
*payload_type = payload_type_rtx_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void RTPSender::SetRtxPayloadType(int payload_type) {
|
|
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
payload_type_rtx_ = payload_type;
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
|
|
|
|
|
RtpVideoCodecTypes *video_type) {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
if (payload_type < 0) {
|
|
|
|
|
WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
|
|
|
|
|
payload_type);
|
2012-01-19 15:56:10 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
if (audio_configured_) {
|
2013-04-08 11:08:41 +00:00
|
|
|
int8_t red_pl_type = -1;
|
2013-01-25 10:53:38 +00:00
|
|
|
if (audio_->RED(red_pl_type) == 0) {
|
2012-01-19 15:56:10 +00:00
|
|
|
// We have configured RED.
|
2013-01-25 10:53:38 +00:00
|
|
|
if (red_pl_type == payload_type) {
|
2012-01-19 15:56:10 +00:00
|
|
|
// And it's a match...
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2012-01-19 15:56:10 +00:00
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
if (payload_type_ == payload_type) {
|
|
|
|
|
if (!audio_configured_) {
|
|
|
|
|
*video_type = video_->VideoCodecType();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
return 0;
|
2012-01-19 15:56:10 +00:00
|
|
|
}
|
2013-04-08 11:08:41 +00:00
|
|
|
std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
|
2013-01-25 10:53:38 +00:00
|
|
|
payload_type_map_.find(payload_type);
|
|
|
|
|
if (it == payload_type_map_.end()) {
|
|
|
|
|
WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
|
|
|
|
|
"\tpayloadType:%d not registered", payload_type);
|
2012-01-19 15:56:10 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
payload_type_ = payload_type;
|
|
|
|
|
ModuleRTPUtility::Payload *payload = it->second;
|
2012-01-19 15:56:10 +00:00
|
|
|
assert(payload);
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!payload->audio && !audio_configured_) {
|
|
|
|
|
video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
|
|
|
|
|
*video_type = payload->typeSpecific.Video.videoCodecType;
|
|
|
|
|
video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
|
2012-01-19 15:56:10 +00:00
|
|
|
}
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::SendOutgoingData(
|
|
|
|
|
const FrameType frame_type, const int8_t payload_type,
|
|
|
|
|
const uint32_t capture_timestamp, int64_t capture_time_ms,
|
|
|
|
|
const uint8_t *payload_data, const uint32_t payload_size,
|
2013-01-25 10:53:38 +00:00
|
|
|
const RTPFragmentationHeader *fragmentation,
|
|
|
|
|
VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
|
2012-11-07 17:01:04 +00:00
|
|
|
{
|
|
|
|
|
// Drop this packet if we're not sending media packets.
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
if (!sending_media_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
2013-07-16 19:25:04 +00:00
|
|
|
RtpVideoCodecTypes video_type = kRtpGenericVideo;
|
2013-01-25 10:53:38 +00:00
|
|
|
if (CheckPayloadType(payload_type, &video_type) != 0) {
|
|
|
|
|
WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
|
|
|
|
|
"%s invalid argument failed to find payload_type:%d",
|
2012-11-07 17:01:04 +00:00
|
|
|
__FUNCTION__, payload_type);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
if (audio_configured_) {
|
2013-07-08 21:31:18 +00:00
|
|
|
TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
|
|
|
|
|
"Send", "type", FrameTypeToString(frame_type));
|
2013-01-25 10:53:38 +00:00
|
|
|
assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
|
2012-11-07 17:01:04 +00:00
|
|
|
frame_type == kFrameEmpty);
|
2012-05-31 10:47:35 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
|
|
|
|
|
payload_data, payload_size, fragmentation);
|
2012-11-07 17:01:04 +00:00
|
|
|
} else {
|
2013-07-08 21:31:18 +00:00
|
|
|
TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
|
|
|
|
|
"Send", "type", FrameTypeToString(frame_type));
|
2013-01-25 10:53:38 +00:00
|
|
|
assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
|
2012-11-07 17:01:04 +00:00
|
|
|
|
|
|
|
|
if (frame_type == kFrameEmpty) {
|
2013-06-17 12:53:37 +00:00
|
|
|
if (paced_sender_->Enabled()) {
|
|
|
|
|
// Padding is driven by the pacer and not by the encoder.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2012-11-07 17:01:04 +00:00
|
|
|
return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
|
2013-06-04 13:47:36 +00:00
|
|
|
capture_time_ms) ? 0 : -1;
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return video_->SendVideo(video_type, frame_type, payload_type,
|
|
|
|
|
capture_timestamp, capture_time_ms, payload_data,
|
|
|
|
|
payload_size, fragmentation, codec_info,
|
2012-11-07 17:01:04 +00:00
|
|
|
rtp_type_hdr);
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-04 13:47:36 +00:00
|
|
|
bool RTPSender::SendPaddingAccordingToBitrate(
|
2013-04-08 11:08:41 +00:00
|
|
|
int8_t payload_type, uint32_t capture_timestamp,
|
2012-07-03 13:21:22 +00:00
|
|
|
int64_t capture_time_ms) {
|
2012-05-31 10:47:35 +00:00
|
|
|
// Current bitrate since last estimate(1 second) averaged with the
|
|
|
|
|
// estimate since then, to get the most up to date bitrate.
|
|
|
|
|
uint32_t current_bitrate = BitrateNow();
|
2013-01-25 10:53:38 +00:00
|
|
|
int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
|
2012-11-07 17:01:04 +00:00
|
|
|
if (bitrate_diff <= 0) {
|
2013-06-04 13:47:36 +00:00
|
|
|
return true;
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
int bytes = 0;
|
|
|
|
|
if (current_bitrate == 0) {
|
|
|
|
|
// Start up phase. Send one 33.3 ms batch to start with.
|
|
|
|
|
bytes = (bitrate_diff / 8) / 30;
|
|
|
|
|
} else {
|
|
|
|
|
bytes = (bitrate_diff / 8);
|
|
|
|
|
// Cap at 200 ms of target send data.
|
2013-01-25 10:53:38 +00:00
|
|
|
int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
|
2012-11-07 17:01:04 +00:00
|
|
|
if (bytes > bytes_cap) {
|
|
|
|
|
bytes = bytes_cap;
|
2012-05-31 10:47:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-06-19 14:13:42 +00:00
|
|
|
int bytes_sent = SendPadData(payload_type, capture_timestamp, capture_time_ms,
|
|
|
|
|
bytes, kDontRetransmit, false);
|
2013-06-17 12:53:37 +00:00
|
|
|
// We did not manage to send all bytes. Comparing with 31 due to modulus 32.
|
|
|
|
|
return bytes - bytes_sent < 31;
|
2012-05-31 10:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-04 13:47:36 +00:00
|
|
|
int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
|
|
|
|
|
int32_t bytes) {
|
|
|
|
|
int padding_bytes_in_packet = kMaxPaddingLength;
|
|
|
|
|
if (bytes < kMaxPaddingLength) {
|
2013-06-19 14:13:42 +00:00
|
|
|
padding_bytes_in_packet = bytes;
|
2013-06-04 13:47:36 +00:00
|
|
|
}
|
|
|
|
|
packet[0] |= 0x20; // Set padding bit.
|
|
|
|
|
int32_t *data =
|
|
|
|
|
reinterpret_cast<int32_t *>(&(packet[header_length]));
|
|
|
|
|
|
|
|
|
|
// Fill data buffer with random data.
|
|
|
|
|
for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
|
|
|
|
|
data[j] = rand(); // NOLINT
|
|
|
|
|
}
|
|
|
|
|
// Set number of padding bytes in the last byte of the packet.
|
|
|
|
|
packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
|
|
|
|
|
return padding_bytes_in_packet;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-19 14:13:42 +00:00
|
|
|
int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
|
|
|
|
|
int64_t capture_time_ms, int32_t bytes,
|
|
|
|
|
StorageType store, bool force_full_size_packets) {
|
2013-01-25 10:53:38 +00:00
|
|
|
// Drop this packet if we're not sending media packets.
|
|
|
|
|
if (!sending_media_) {
|
2013-06-17 12:53:37 +00:00
|
|
|
return bytes;
|
2012-01-05 10:54:44 +00:00
|
|
|
}
|
2013-06-17 12:53:37 +00:00
|
|
|
int padding_bytes_in_packet = 0;
|
|
|
|
|
int bytes_sent = 0;
|
|
|
|
|
for (; bytes > 0; bytes -= padding_bytes_in_packet) {
|
|
|
|
|
// Always send full padding packets.
|
|
|
|
|
if (force_full_size_packets && bytes < kMaxPaddingLength)
|
|
|
|
|
bytes = kMaxPaddingLength;
|
2013-06-19 14:13:42 +00:00
|
|
|
if (bytes < kMaxPaddingLength) {
|
|
|
|
|
if (force_full_size_packets) {
|
|
|
|
|
bytes = kMaxPaddingLength;
|
|
|
|
|
} else {
|
|
|
|
|
// Round to the nearest multiple of 32.
|
|
|
|
|
bytes = (bytes + 16) & 0xffe0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-25 15:46:16 +00:00
|
|
|
if (bytes < 32) {
|
2013-06-19 14:13:42 +00:00
|
|
|
// Sanity don't send empty packets.
|
2013-06-17 12:53:37 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2013-06-19 14:13:42 +00:00
|
|
|
uint32_t ssrc;
|
|
|
|
|
uint16_t sequence_number;
|
|
|
|
|
{
|
|
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
// Only send padding packets following the last packet of a frame,
|
|
|
|
|
// indicated by the marker bit.
|
|
|
|
|
if (!last_packet_marker_bit_)
|
|
|
|
|
return bytes_sent;
|
|
|
|
|
if (rtx_ == kRtxOff) {
|
|
|
|
|
ssrc = ssrc_;
|
|
|
|
|
sequence_number = sequence_number_;
|
|
|
|
|
++sequence_number_;
|
|
|
|
|
} else {
|
|
|
|
|
ssrc = ssrc_rtx_;
|
|
|
|
|
sequence_number = sequence_number_rtx_;
|
|
|
|
|
++sequence_number_rtx_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
uint8_t padding_packet[IP_PACKET_SIZE];
|
|
|
|
|
int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
|
|
|
|
|
false, timestamp, sequence_number, NULL,
|
|
|
|
|
0);
|
|
|
|
|
padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
|
|
|
|
|
bytes);
|
2013-06-17 12:53:37 +00:00
|
|
|
if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
|
|
|
|
|
header_length, capture_time_ms, store,
|
|
|
|
|
PacedSender::kLowPriority)) {
|
2012-01-05 10:54:44 +00:00
|
|
|
// Error sending the packet.
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-06-17 12:53:37 +00:00
|
|
|
bytes_sent += padding_bytes_in_packet;
|
2012-01-05 10:54:44 +00:00
|
|
|
}
|
2013-06-17 12:53:37 +00:00
|
|
|
return bytes_sent;
|
2012-01-05 10:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
void RTPSender::SetStorePacketsStatus(const bool enable,
|
2013-04-08 11:08:41 +00:00
|
|
|
const uint16_t number_to_store) {
|
2013-01-25 10:53:38 +00:00
|
|
|
packet_history_->SetStorePacketsStatus(enable, number_to_store);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-27 00:41:08 +00:00
|
|
|
bool RTPSender::StorePackets() const {
|
|
|
|
|
return packet_history_->StorePackets();
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
|
|
|
|
|
uint16_t length = IP_PACKET_SIZE;
|
|
|
|
|
uint8_t data_buffer[IP_PACKET_SIZE];
|
|
|
|
|
uint8_t *buffer_to_send_ptr = data_buffer;
|
2013-04-27 00:41:08 +00:00
|
|
|
int64_t capture_time_ms;
|
2012-01-16 11:06:31 +00:00
|
|
|
StorageType type;
|
2013-04-27 00:41:08 +00:00
|
|
|
if (!packet_history_->GetRTPPacket(packet_id, min_resend_time, data_buffer,
|
|
|
|
|
&length, &capture_time_ms, &type)) {
|
2012-01-16 11:06:31 +00:00
|
|
|
// Packet not found.
|
2012-04-23 12:43:05 +00:00
|
|
|
return 0;
|
2012-01-16 11:06:31 +00:00
|
|
|
}
|
|
|
|
|
if (length == 0 || type == kDontRetransmit) {
|
|
|
|
|
// No bytes copied (packet recently resent, skip resending) or
|
|
|
|
|
// packet should not be retransmitted.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2013-04-27 00:41:08 +00:00
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint8_t data_buffer_rtx[IP_PACKET_SIZE];
|
2013-03-15 23:21:52 +00:00
|
|
|
if (rtx_ != kRtxOff) {
|
|
|
|
|
BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
|
2012-01-16 11:06:31 +00:00
|
|
|
buffer_to_send_ptr = data_buffer_rtx;
|
|
|
|
|
}
|
2013-03-15 23:21:52 +00:00
|
|
|
|
2013-04-09 19:54:10 +00:00
|
|
|
ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
|
2013-05-29 12:12:51 +00:00
|
|
|
RTPHeader header;
|
|
|
|
|
rtp_parser.Parse(header);
|
2013-04-27 00:41:08 +00:00
|
|
|
|
|
|
|
|
// Store the time when the packet was last sent or added to pacer.
|
|
|
|
|
packet_history_->UpdateResendTime(packet_id);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// Update send statistics prior to pacer.
|
|
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
Bitrate::Update(length);
|
|
|
|
|
packets_sent_++;
|
|
|
|
|
// We on purpose don't add to payload_bytes_sent_ since this is a
|
|
|
|
|
// re-transmit and not new payload data.
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-02 19:02:17 +00:00
|
|
|
TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
|
2013-05-29 12:12:51 +00:00
|
|
|
"timestamp", header.timestamp,
|
|
|
|
|
"seqnum", header.sequenceNumber);
|
|
|
|
|
|
2013-04-27 00:41:08 +00:00
|
|
|
if (paced_sender_) {
|
|
|
|
|
if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
|
2013-05-29 12:12:51 +00:00
|
|
|
header.ssrc,
|
|
|
|
|
header.sequenceNumber,
|
2013-04-27 00:41:08 +00:00
|
|
|
capture_time_ms,
|
2013-06-17 12:53:37 +00:00
|
|
|
length - header.headerLength)) {
|
2013-04-27 00:41:08 +00:00
|
|
|
// We can't send the packet right now.
|
|
|
|
|
// We will be called when it is time.
|
2013-05-23 13:36:55 +00:00
|
|
|
return length;
|
2013-04-27 00:41:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
|
2013-05-23 13:36:55 +00:00
|
|
|
return length;
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
2013-04-27 00:41:08 +00:00
|
|
|
return -1;
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-04-27 00:41:08 +00:00
|
|
|
bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
|
|
|
|
|
int bytes_sent = -1;
|
2013-01-25 10:53:38 +00:00
|
|
|
if (transport_) {
|
|
|
|
|
bytes_sent = transport_->SendPacket(id_, packet, size);
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
2013-05-02 19:02:17 +00:00
|
|
|
TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
|
|
|
|
|
"size", size, "sent", bytes_sent);
|
2013-04-27 00:41:08 +00:00
|
|
|
// TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
|
2012-01-16 11:06:31 +00:00
|
|
|
if (bytes_sent <= 0) {
|
2013-04-27 00:41:08 +00:00
|
|
|
WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
|
|
|
|
|
"Transport failed to send packet");
|
|
|
|
|
return false;
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
2013-04-27 00:41:08 +00:00
|
|
|
return true;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-22 12:52:41 +00:00
|
|
|
int RTPSender::SelectiveRetransmissions() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!video_)
|
|
|
|
|
return -1;
|
|
|
|
|
return video_->SelectiveRetransmissions();
|
2011-12-22 12:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!video_)
|
|
|
|
|
return -1;
|
|
|
|
|
return video_->SetSelectiveRetransmissions(settings);
|
2011-12-22 12:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
void RTPSender::OnReceivedNACK(
|
2013-02-01 15:09:57 +00:00
|
|
|
const std::list<uint16_t>& nack_sequence_numbers,
|
2013-04-08 11:08:41 +00:00
|
|
|
const uint16_t avg_rtt) {
|
2013-04-09 19:54:10 +00:00
|
|
|
TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
|
|
|
|
|
"num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
|
2013-04-08 11:08:41 +00:00
|
|
|
const int64_t now = clock_->TimeInMilliseconds();
|
|
|
|
|
uint32_t bytes_re_sent = 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-01-16 11:06:31 +00:00
|
|
|
// Enough bandwidth to send NACK?
|
2012-01-10 14:09:18 +00:00
|
|
|
if (!ProcessNACKBitRate(now)) {
|
2013-01-25 10:53:38 +00:00
|
|
|
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
|
2012-01-16 11:06:31 +00:00
|
|
|
"NACK bitrate reached. Skip sending NACK response. Target %d",
|
2013-01-25 10:53:38 +00:00
|
|
|
target_send_bitrate_);
|
2012-01-10 14:09:18 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-02-01 15:09:57 +00:00
|
|
|
for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
|
|
|
|
|
it != nack_sequence_numbers.end(); ++it) {
|
2013-04-08 11:08:41 +00:00
|
|
|
const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
|
2013-01-25 10:53:38 +00:00
|
|
|
if (bytes_sent > 0) {
|
|
|
|
|
bytes_re_sent += bytes_sent;
|
|
|
|
|
} else if (bytes_sent == 0) {
|
2012-01-10 14:09:18 +00:00
|
|
|
// The packet has previously been resent.
|
|
|
|
|
// Try resending next packet in the list.
|
|
|
|
|
continue;
|
2013-01-25 10:53:38 +00:00
|
|
|
} else if (bytes_sent < 0) {
|
2012-01-10 14:09:18 +00:00
|
|
|
// Failed to send one Sequence number. Give up the rest in this nack.
|
2013-01-25 10:53:38 +00:00
|
|
|
WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
|
2012-01-10 14:09:18 +00:00
|
|
|
"Failed resending RTP packet %d, Discard rest of packets",
|
2013-02-01 15:09:57 +00:00
|
|
|
*it);
|
2012-01-10 14:09:18 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
// Delay bandwidth estimate (RTT * BW).
|
|
|
|
|
if (target_send_bitrate_ != 0 && avg_rtt) {
|
2012-01-10 14:09:18 +00:00
|
|
|
// kbits/s * ms = bits => bits/8 = bytes
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t target_bytes =
|
|
|
|
|
(static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
|
2013-01-25 10:53:38 +00:00
|
|
|
if (bytes_re_sent > target_bytes) {
|
|
|
|
|
break; // Ignore the rest of the packets in the list.
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
if (bytes_re_sent > 0) {
|
2012-01-10 14:09:18 +00:00
|
|
|
// TODO(pwestin) consolidate these two methods.
|
2013-01-25 10:53:38 +00:00
|
|
|
UpdateNACKBitRate(bytes_re_sent, now);
|
|
|
|
|
nack_bitrate_.Update(bytes_re_sent);
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
|
|
|
|
|
uint32_t num = 0;
|
|
|
|
|
int32_t byte_count = 0;
|
|
|
|
|
const uint32_t avg_interval = 1000;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
if (target_send_bitrate_ == 0) {
|
2012-01-10 14:09:18 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
|
|
|
|
|
if ((now - nack_byte_count_times_[num]) > avg_interval) {
|
|
|
|
|
// Don't use data older than 1sec.
|
2012-01-10 14:09:18 +00:00
|
|
|
break;
|
|
|
|
|
} else {
|
2013-01-25 10:53:38 +00:00
|
|
|
byte_count += nack_byte_count_[num];
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t time_interval = avg_interval;
|
2012-01-10 14:09:18 +00:00
|
|
|
if (num == NACK_BYTECOUNT_SIZE) {
|
|
|
|
|
// More than NACK_BYTECOUNT_SIZE nack messages has been received
|
2013-01-25 10:53:38 +00:00
|
|
|
// during the last msg_interval.
|
|
|
|
|
time_interval = now - nack_byte_count_times_[num - 1];
|
|
|
|
|
if (time_interval < 0) {
|
|
|
|
|
time_interval = avg_interval;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return (byte_count * 8) < (target_send_bitrate_ * time_interval);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
|
|
|
|
|
const uint32_t now) {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2012-01-10 14:09:18 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
// Save bitrate statistics.
|
2012-11-07 17:01:04 +00:00
|
|
|
if (bytes > 0) {
|
|
|
|
|
if (now == 0) {
|
2013-01-25 10:53:38 +00:00
|
|
|
// Add padding length.
|
|
|
|
|
nack_byte_count_[0] += bytes;
|
2012-01-10 14:09:18 +00:00
|
|
|
} else {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (nack_byte_count_times_[0] == 0) {
|
|
|
|
|
// First no shift.
|
2012-01-10 14:09:18 +00:00
|
|
|
} else {
|
2013-01-25 10:53:38 +00:00
|
|
|
// Shift.
|
|
|
|
|
for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
|
|
|
|
|
nack_byte_count_[i + 1] = nack_byte_count_[i];
|
|
|
|
|
nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
nack_byte_count_[0] = bytes;
|
|
|
|
|
nack_byte_count_times_[0] = now;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2012-01-10 14:09:18 +00:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-27 00:41:08 +00:00
|
|
|
// Called from pacer when we can send the packet.
|
2013-06-20 20:18:31 +00:00
|
|
|
bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
|
2012-11-13 21:12:39 +00:00
|
|
|
int64_t capture_time_ms) {
|
|
|
|
|
StorageType type;
|
|
|
|
|
uint16_t length = IP_PACKET_SIZE;
|
|
|
|
|
uint8_t data_buffer[IP_PACKET_SIZE];
|
2013-04-27 00:41:08 +00:00
|
|
|
int64_t stored_time_ms;
|
2012-01-16 11:06:31 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
if (packet_history_ == NULL) {
|
2013-06-20 20:18:31 +00:00
|
|
|
// Packet cannot be found. Allow sending to continue.
|
|
|
|
|
return true;
|
2012-01-16 11:06:31 +00:00
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
|
|
|
|
|
&stored_time_ms, &type)) {
|
2013-06-20 20:18:31 +00:00
|
|
|
// Packet cannot be found. Allow sending to continue.
|
|
|
|
|
return true;
|
2012-11-13 21:12:39 +00:00
|
|
|
}
|
|
|
|
|
assert(length > 0);
|
2012-01-16 11:06:31 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
|
2013-05-29 12:12:51 +00:00
|
|
|
RTPHeader rtp_header;
|
2013-01-25 10:53:38 +00:00
|
|
|
rtp_parser.Parse(rtp_header);
|
2013-04-09 19:54:10 +00:00
|
|
|
TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
|
2013-05-29 12:12:51 +00:00
|
|
|
"timestamp", rtp_header.timestamp,
|
2013-04-09 19:54:10 +00:00
|
|
|
"seqnum", sequence_number);
|
2011-12-22 12:52:41 +00:00
|
|
|
|
2013-05-16 11:10:31 +00:00
|
|
|
int64_t now_ms = clock_->TimeInMilliseconds();
|
|
|
|
|
int64_t diff_ms = now_ms - capture_time_ms;
|
|
|
|
|
bool updated_transmission_time_offset =
|
|
|
|
|
UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms);
|
|
|
|
|
bool updated_abs_send_time =
|
|
|
|
|
UpdateAbsoluteSendTime(data_buffer, length, rtp_header, now_ms);
|
|
|
|
|
if (updated_transmission_time_offset || updated_abs_send_time) {
|
2012-11-13 21:12:39 +00:00
|
|
|
// Update stored packet in case of receiving a re-transmission request.
|
2013-01-25 10:53:38 +00:00
|
|
|
packet_history_->ReplaceRTPHeader(data_buffer,
|
2013-05-29 12:12:51 +00:00
|
|
|
rtp_header.sequenceNumber,
|
|
|
|
|
rtp_header.headerLength);
|
2012-11-13 21:12:39 +00:00
|
|
|
}
|
2013-06-20 20:18:31 +00:00
|
|
|
return SendPacketToNetwork(data_buffer, length);
|
2012-01-16 11:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-17 12:53:37 +00:00
|
|
|
int RTPSender::TimeToSendPadding(int bytes) {
|
|
|
|
|
if (!sending_media_) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int payload_type;
|
2013-06-19 14:13:42 +00:00
|
|
|
int64_t capture_time_ms;
|
|
|
|
|
uint32_t timestamp;
|
2013-06-17 12:53:37 +00:00
|
|
|
{
|
|
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
payload_type = (rtx_ == kRtxOff) ? payload_type_ : payload_type_rtx_;
|
2013-06-19 14:13:42 +00:00
|
|
|
timestamp = timestamp_;
|
|
|
|
|
capture_time_ms = capture_time_ms_;
|
2013-06-17 12:53:37 +00:00
|
|
|
}
|
2013-06-19 14:13:42 +00:00
|
|
|
return SendPadData(payload_type, timestamp, capture_time_ms, bytes,
|
|
|
|
|
kDontStore, true);
|
2013-06-17 12:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::SendToNetwork(
|
2013-01-25 10:53:38 +00:00
|
|
|
uint8_t *buffer, int payload_length, int rtp_header_length,
|
2013-06-17 12:53:37 +00:00
|
|
|
int64_t capture_time_ms, StorageType storage,
|
|
|
|
|
PacedSender::Priority priority) {
|
2013-01-25 10:53:38 +00:00
|
|
|
ModuleRTPUtility::RTPHeaderParser rtp_parser(
|
|
|
|
|
buffer, payload_length + rtp_header_length);
|
2013-05-29 12:12:51 +00:00
|
|
|
RTPHeader rtp_header;
|
2013-01-25 10:53:38 +00:00
|
|
|
rtp_parser.Parse(rtp_header);
|
2012-11-13 21:12:39 +00:00
|
|
|
|
2013-05-16 11:10:31 +00:00
|
|
|
int64_t now_ms = clock_->TimeInMilliseconds();
|
|
|
|
|
|
2012-08-28 15:20:39 +00:00
|
|
|
// |capture_time_ms| <= 0 is considered invalid.
|
|
|
|
|
// TODO(holmer): This should be changed all over Video Engine so that negative
|
|
|
|
|
// time is consider invalid, while 0 is considered a valid time.
|
|
|
|
|
if (capture_time_ms > 0) {
|
2012-11-13 21:12:39 +00:00
|
|
|
UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
|
2013-05-16 11:10:31 +00:00
|
|
|
rtp_header, now_ms - capture_time_ms);
|
2012-11-13 21:12:39 +00:00
|
|
|
}
|
2013-05-16 11:10:31 +00:00
|
|
|
|
|
|
|
|
UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
|
|
|
|
|
rtp_header, now_ms);
|
|
|
|
|
|
2012-11-13 21:12:39 +00:00
|
|
|
// Used for NACK and to spread out the transmission of packets.
|
2013-01-25 10:53:38 +00:00
|
|
|
if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
|
|
|
|
|
max_payload_length_, capture_time_ms,
|
|
|
|
|
storage) != 0) {
|
2012-11-13 21:12:39 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-03-15 23:21:52 +00:00
|
|
|
|
|
|
|
|
// Create and send RTX Packet.
|
2013-04-27 00:41:08 +00:00
|
|
|
// TODO(pwesin): This should be moved to its own code path triggered by pacer.
|
|
|
|
|
bool rtx_sent = false;
|
2013-03-15 23:21:52 +00:00
|
|
|
if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
|
2013-04-08 11:08:41 +00:00
|
|
|
uint16_t length_rtx = payload_length + rtp_header_length;
|
|
|
|
|
uint8_t data_buffer_rtx[IP_PACKET_SIZE];
|
2013-03-15 23:21:52 +00:00
|
|
|
BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
|
2013-04-27 00:41:08 +00:00
|
|
|
if (!SendPacketToNetwork(data_buffer_rtx, length_rtx)) return -1;
|
|
|
|
|
rtx_sent = true;
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
// Update send statistics prior to pacer.
|
|
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
Bitrate::Update(payload_length + rtp_header_length);
|
|
|
|
|
++packets_sent_;
|
|
|
|
|
payload_bytes_sent_ += payload_length;
|
|
|
|
|
if (rtx_sent) {
|
|
|
|
|
// The RTX packet.
|
|
|
|
|
++packets_sent_;
|
|
|
|
|
payload_bytes_sent_ += payload_length;
|
2013-03-15 23:21:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-27 16:36:01 +00:00
|
|
|
if (paced_sender_ && storage != kDontStore) {
|
2013-06-17 12:53:37 +00:00
|
|
|
if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
|
|
|
|
|
rtp_header.sequenceNumber, capture_time_ms,
|
|
|
|
|
payload_length)) {
|
2012-11-13 21:12:39 +00:00
|
|
|
// We can't send the packet right now.
|
|
|
|
|
// We will be called when it is time.
|
2013-04-27 00:41:08 +00:00
|
|
|
return 0;
|
2012-11-06 13:09:39 +00:00
|
|
|
}
|
2012-07-03 13:21:22 +00:00
|
|
|
}
|
2013-04-27 00:41:08 +00:00
|
|
|
if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
|
|
|
|
|
return 0;
|
2012-01-16 11:06:31 +00:00
|
|
|
}
|
2013-04-27 00:41:08 +00:00
|
|
|
return -1;
|
2011-12-22 12:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-07 17:01:04 +00:00
|
|
|
void RTPSender::ProcessBitrate() {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2012-11-07 17:01:04 +00:00
|
|
|
Bitrate::Process();
|
2013-01-25 10:53:38 +00:00
|
|
|
nack_bitrate_.Process();
|
|
|
|
|
if (audio_configured_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
video_->ProcessBitrate();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint16_t RTPSender::RTPHeaderLength() const {
|
|
|
|
|
uint16_t rtp_header_length = 12;
|
2013-01-25 10:53:38 +00:00
|
|
|
if (include_csrcs_) {
|
2013-06-04 13:47:36 +00:00
|
|
|
rtp_header_length += sizeof(uint32_t) * num_csrcs_;
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
rtp_header_length += RtpHeaderExtensionTotalLength();
|
|
|
|
|
return rtp_header_length;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint16_t RTPSender::IncrementSequenceNumber() {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
return sequence_number_++;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-07 17:01:04 +00:00
|
|
|
void RTPSender::ResetDataCounters() {
|
2013-01-25 10:53:38 +00:00
|
|
|
packets_sent_ = 0;
|
|
|
|
|
payload_bytes_sent_ = 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t RTPSender::Packets() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
// Don't use critsect to avoid potential deadlock.
|
|
|
|
|
return packets_sent_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
// Number of sent RTP bytes.
|
|
|
|
|
// Don't use critsect to avoid potental deadlock.
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t RTPSender::Bytes() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
return payload_bytes_sent_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-04 13:47:36 +00:00
|
|
|
int RTPSender::CreateRTPHeader(
|
|
|
|
|
uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
|
|
|
|
|
uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
|
|
|
|
|
uint8_t num_csrcs) const {
|
|
|
|
|
header[0] = 0x80; // version 2.
|
|
|
|
|
header[1] = static_cast<uint8_t>(payload_type);
|
2013-01-25 10:53:38 +00:00
|
|
|
if (marker_bit) {
|
2013-06-04 13:47:36 +00:00
|
|
|
header[1] |= kRtpMarkerBitMask; // Marker bit is set.
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
2013-06-04 13:47:36 +00:00
|
|
|
ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
|
|
|
|
|
ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
|
|
|
|
|
ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t rtp_header_length = 12;
|
2012-11-07 17:01:04 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
// Add the CSRCs if any.
|
2013-06-04 13:47:36 +00:00
|
|
|
if (num_csrcs > 0) {
|
|
|
|
|
if (num_csrcs > kRtpCsrcSize) {
|
2012-11-07 17:01:04 +00:00
|
|
|
// error
|
|
|
|
|
assert(false);
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2013-06-04 13:47:36 +00:00
|
|
|
uint8_t *ptr = &header[rtp_header_length];
|
|
|
|
|
for (int i = 0; i < num_csrcs; ++i) {
|
|
|
|
|
ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
|
2013-01-25 10:53:38 +00:00
|
|
|
ptr += 4;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2013-06-04 13:47:36 +00:00
|
|
|
header[0] = (header[0] & 0xf0) | num_csrcs;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
// Update length of header.
|
2013-06-04 13:47:36 +00:00
|
|
|
rtp_header_length += sizeof(uint32_t) * num_csrcs;
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
2011-12-16 14:31:37 +00:00
|
|
|
|
2013-06-04 13:47:36 +00:00
|
|
|
uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
|
|
|
|
|
if (len > 0) {
|
|
|
|
|
header[0] |= 0x10; // Set extension bit.
|
2013-01-25 10:53:38 +00:00
|
|
|
rtp_header_length += len;
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return rtp_header_length;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-04 13:47:36 +00:00
|
|
|
int32_t RTPSender::BuildRTPheader(
|
|
|
|
|
uint8_t *data_buffer, const int8_t payload_type,
|
|
|
|
|
const bool marker_bit, const uint32_t capture_timestamp,
|
2013-06-19 14:13:42 +00:00
|
|
|
int64_t capture_time_ms, const bool time_stamp_provided,
|
|
|
|
|
const bool inc_sequence_number) {
|
2013-06-04 13:47:36 +00:00
|
|
|
assert(payload_type >= 0);
|
|
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
|
|
|
|
|
if (time_stamp_provided) {
|
|
|
|
|
timestamp_ = start_time_stamp_ + capture_timestamp;
|
|
|
|
|
} else {
|
|
|
|
|
// Make a unique time stamp.
|
|
|
|
|
// We can't inc by the actual time, since then we increase the risk of back
|
|
|
|
|
// timing.
|
|
|
|
|
timestamp_++;
|
|
|
|
|
}
|
|
|
|
|
uint32_t sequence_number = sequence_number_++;
|
2013-06-19 14:13:42 +00:00
|
|
|
capture_time_ms_ = capture_time_ms;
|
|
|
|
|
last_packet_marker_bit_ = marker_bit;
|
2013-06-04 13:47:36 +00:00
|
|
|
int csrcs_length = 0;
|
|
|
|
|
if (include_csrcs_)
|
|
|
|
|
csrcs_length = num_csrcs_;
|
|
|
|
|
return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
|
|
|
|
|
timestamp_, sequence_number, csrcs_, csrcs_length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (rtp_header_extension_map_.Size() <= 0) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
// RTP header extension, RFC 3550.
|
|
|
|
|
// 0 1 2 3
|
|
|
|
|
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
// | defined by profile | length |
|
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
// | header extension |
|
|
|
|
|
// | .... |
|
|
|
|
|
//
|
2013-04-08 11:08:41 +00:00
|
|
|
const uint32_t kPosLength = 2;
|
2013-05-07 12:36:21 +00:00
|
|
|
const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
|
2012-11-07 17:01:04 +00:00
|
|
|
|
|
|
|
|
// Add extension ID (0xBEDE).
|
2013-01-25 10:53:38 +00:00
|
|
|
ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
|
2013-05-07 12:36:21 +00:00
|
|
|
kRtpOneByteHeaderExtensionId);
|
2012-11-07 17:01:04 +00:00
|
|
|
|
|
|
|
|
// Add extensions.
|
2013-04-08 11:08:41 +00:00
|
|
|
uint16_t total_block_length = 0;
|
2012-11-07 17:01:04 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
RTPExtensionType type = rtp_header_extension_map_.First();
|
2012-11-07 17:01:04 +00:00
|
|
|
while (type != kRtpExtensionNone) {
|
2013-04-08 11:08:41 +00:00
|
|
|
uint8_t block_length = 0;
|
2013-05-16 11:10:31 +00:00
|
|
|
switch (type) {
|
|
|
|
|
case kRtpExtensionTransmissionTimeOffset:
|
|
|
|
|
block_length = BuildTransmissionTimeOffsetExtension(
|
|
|
|
|
data_buffer + kHeaderLength + total_block_length);
|
|
|
|
|
break;
|
2013-05-20 20:55:07 +00:00
|
|
|
case kRtpExtensionAudioLevel:
|
|
|
|
|
// Because AudioLevel is handled specially by RTPSenderAudio, we pretend
|
|
|
|
|
// we don't have to care about it here, which is true until we wan't to
|
|
|
|
|
// use it together with any of the other extensions we support.
|
|
|
|
|
break;
|
2013-05-16 11:10:31 +00:00
|
|
|
case kRtpExtensionAbsoluteSendTime:
|
|
|
|
|
block_length = BuildAbsoluteSendTimeExtension(
|
|
|
|
|
data_buffer + kHeaderLength + total_block_length);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(false);
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
total_block_length += block_length;
|
2013-01-25 10:53:38 +00:00
|
|
|
type = rtp_header_extension_map_.Next(type);
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
if (total_block_length == 0) {
|
|
|
|
|
// No extension added.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// Set header length (in number of Word32, header excluded).
|
|
|
|
|
assert(total_block_length % 4 == 0);
|
2013-01-25 10:53:38 +00:00
|
|
|
ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
|
2012-11-07 17:01:04 +00:00
|
|
|
total_block_length / 4);
|
|
|
|
|
// Total added length.
|
|
|
|
|
return kHeaderLength + total_block_length;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
|
|
|
|
|
uint8_t* data_buffer) const {
|
2012-11-07 17:01:04 +00:00
|
|
|
// From RFC 5450: Transmission Time Offsets in RTP Streams.
|
|
|
|
|
//
|
|
|
|
|
// The transmission time is signaled to the receiver in-band using the
|
|
|
|
|
// general mechanism for RTP header extensions [RFC5285]. The payload
|
|
|
|
|
// of this extension (the transmitted value) is a 24-bit signed integer.
|
|
|
|
|
// When added to the RTP timestamp of the packet, it represents the
|
|
|
|
|
// "effective" RTP transmission time of the packet, on the RTP
|
|
|
|
|
// timescale.
|
|
|
|
|
//
|
|
|
|
|
// The form of the transmission offset extension block:
|
|
|
|
|
//
|
|
|
|
|
// 0 1 2 3
|
|
|
|
|
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
// | ID | len=2 | transmission offset |
|
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
|
|
|
|
|
// Get id defined by user.
|
2013-04-08 11:08:41 +00:00
|
|
|
uint8_t id;
|
2013-01-25 10:53:38 +00:00
|
|
|
if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
|
|
|
|
|
&id) != 0) {
|
2012-11-07 17:01:04 +00:00
|
|
|
// Not registered.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2013-05-07 12:36:21 +00:00
|
|
|
size_t pos = 0;
|
2013-04-08 11:08:41 +00:00
|
|
|
const uint8_t len = 2;
|
2013-01-25 10:53:38 +00:00
|
|
|
data_buffer[pos++] = (id << 4) + len;
|
|
|
|
|
ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
|
|
|
|
|
transmission_time_offset_);
|
2012-11-07 17:01:04 +00:00
|
|
|
pos += 3;
|
2013-05-07 12:36:21 +00:00
|
|
|
assert(pos == kTransmissionTimeOffsetLength);
|
|
|
|
|
return kTransmissionTimeOffsetLength;
|
2011-12-16 14:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-16 11:10:31 +00:00
|
|
|
uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
|
|
|
|
|
uint8_t* data_buffer) const {
|
|
|
|
|
// Absolute send time in RTP streams.
|
|
|
|
|
//
|
|
|
|
|
// The absolute send time is signaled to the receiver in-band using the
|
|
|
|
|
// general mechanism for RTP header extensions [RFC5285]. The payload
|
|
|
|
|
// of this extension (the transmitted value) is a 24-bit unsigned integer
|
|
|
|
|
// containing the sender's current time in seconds as a fixed point number
|
|
|
|
|
// with 18 bits fractional part.
|
|
|
|
|
//
|
|
|
|
|
// The form of the absolute send time extension block:
|
|
|
|
|
//
|
|
|
|
|
// 0 1 2 3
|
|
|
|
|
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
// | ID | len=2 | absolute send time |
|
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
|
|
|
|
|
// Get id defined by user.
|
|
|
|
|
uint8_t id;
|
|
|
|
|
if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
|
|
|
|
|
&id) != 0) {
|
|
|
|
|
// Not registered.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
size_t pos = 0;
|
|
|
|
|
const uint8_t len = 2;
|
|
|
|
|
data_buffer[pos++] = (id << 4) + len;
|
|
|
|
|
ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
|
|
|
|
|
absolute_send_time_);
|
|
|
|
|
pos += 3;
|
|
|
|
|
assert(pos == kAbsoluteSendTimeLength);
|
|
|
|
|
return kAbsoluteSendTimeLength;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-06 13:09:39 +00:00
|
|
|
bool RTPSender::UpdateTransmissionTimeOffset(
|
2013-04-08 11:08:41 +00:00
|
|
|
uint8_t *rtp_packet, const uint16_t rtp_packet_length,
|
2013-05-29 12:12:51 +00:00
|
|
|
const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2012-01-16 11:06:31 +00:00
|
|
|
|
2013-05-16 11:10:31 +00:00
|
|
|
// Get length until start of header extension block.
|
|
|
|
|
int extension_block_pos =
|
2013-01-25 10:53:38 +00:00
|
|
|
rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
|
2012-11-07 17:01:04 +00:00
|
|
|
kRtpExtensionTransmissionTimeOffset);
|
2013-05-16 11:10:31 +00:00
|
|
|
if (extension_block_pos < 0) {
|
2013-01-25 10:53:38 +00:00
|
|
|
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
|
2012-11-07 17:01:04 +00:00
|
|
|
"Failed to update transmission time offset, not registered.");
|
2012-11-06 13:09:39 +00:00
|
|
|
return false;
|
2012-01-16 11:06:31 +00:00
|
|
|
}
|
2013-05-29 12:12:51 +00:00
|
|
|
int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
|
2013-05-16 11:10:31 +00:00
|
|
|
if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
|
2013-05-29 12:12:51 +00:00
|
|
|
rtp_header.headerLength <
|
2013-05-16 11:10:31 +00:00
|
|
|
block_pos + kTransmissionTimeOffsetLength) {
|
2013-01-25 10:53:38 +00:00
|
|
|
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
|
2012-11-07 17:01:04 +00:00
|
|
|
"Failed to update transmission time offset, invalid length.");
|
2012-11-06 13:09:39 +00:00
|
|
|
return false;
|
2012-01-16 11:06:31 +00:00
|
|
|
}
|
|
|
|
|
// Verify that header contains extension.
|
2013-05-29 12:12:51 +00:00
|
|
|
if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
|
|
|
|
|
(rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
|
2013-01-25 10:53:38 +00:00
|
|
|
WEBRTC_TRACE(
|
|
|
|
|
kTraceStream, kTraceRtpRtcp, id_,
|
2012-01-16 11:06:31 +00:00
|
|
|
"Failed to update transmission time offset, hdr extension not found.");
|
2012-11-06 13:09:39 +00:00
|
|
|
return false;
|
2012-01-16 11:06:31 +00:00
|
|
|
}
|
|
|
|
|
// Get id.
|
2013-04-08 11:08:41 +00:00
|
|
|
uint8_t id = 0;
|
2013-01-25 10:53:38 +00:00
|
|
|
if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
|
|
|
|
|
&id) != 0) {
|
|
|
|
|
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
|
2012-11-07 17:01:04 +00:00
|
|
|
"Failed to update transmission time offset, no id.");
|
2012-11-06 13:09:39 +00:00
|
|
|
return false;
|
2012-01-16 11:06:31 +00:00
|
|
|
}
|
|
|
|
|
// Verify first byte in block.
|
2013-04-08 11:08:41 +00:00
|
|
|
const uint8_t first_block_byte = (id << 4) + 2;
|
2012-01-16 11:06:31 +00:00
|
|
|
if (rtp_packet[block_pos] != first_block_byte) {
|
2013-01-25 10:53:38 +00:00
|
|
|
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
|
2012-11-07 17:01:04 +00:00
|
|
|
"Failed to update transmission time offset.");
|
2012-11-06 13:09:39 +00:00
|
|
|
return false;
|
2012-01-16 11:06:31 +00:00
|
|
|
}
|
2013-05-16 11:10:31 +00:00
|
|
|
// Update transmission offset field (converting to a 90 kHz timestamp).
|
2012-01-16 11:06:31 +00:00
|
|
|
ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
|
2012-08-10 14:30:53 +00:00
|
|
|
time_diff_ms * 90); // RTP timestamp.
|
2012-11-06 13:09:39 +00:00
|
|
|
return true;
|
2012-01-16 11:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-16 11:10:31 +00:00
|
|
|
bool RTPSender::UpdateAbsoluteSendTime(
|
|
|
|
|
uint8_t *rtp_packet, const uint16_t rtp_packet_length,
|
2013-05-29 12:12:51 +00:00
|
|
|
const RTPHeader &rtp_header, const int64_t now_ms) const {
|
2013-05-16 11:10:31 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
|
|
|
|
|
// Get length until start of header extension block.
|
|
|
|
|
int extension_block_pos =
|
|
|
|
|
rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
|
|
|
|
|
kRtpExtensionAbsoluteSendTime);
|
|
|
|
|
if (extension_block_pos < 0) {
|
|
|
|
|
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
|
|
|
|
|
"Failed to update absolute send time, not registered.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-05-29 12:12:51 +00:00
|
|
|
int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
|
2013-05-16 11:10:31 +00:00
|
|
|
if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
|
2013-05-29 12:12:51 +00:00
|
|
|
rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
|
2013-05-16 11:10:31 +00:00
|
|
|
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
|
|
|
|
|
"Failed to update absolute send time, invalid length.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Verify that header contains extension.
|
2013-05-29 12:12:51 +00:00
|
|
|
if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
|
|
|
|
|
(rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
|
2013-05-16 11:10:31 +00:00
|
|
|
WEBRTC_TRACE(
|
|
|
|
|
kTraceStream, kTraceRtpRtcp, id_,
|
|
|
|
|
"Failed to update absolute send time, hdr extension not found.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Get id.
|
|
|
|
|
uint8_t id = 0;
|
|
|
|
|
if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
|
|
|
|
|
&id) != 0) {
|
|
|
|
|
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
|
|
|
|
|
"Failed to update absolute send time, no id.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Verify first byte in block.
|
|
|
|
|
const uint8_t first_block_byte = (id << 4) + 2;
|
|
|
|
|
if (rtp_packet[block_pos] != first_block_byte) {
|
|
|
|
|
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
|
|
|
|
|
"Failed to update absolute send time.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
|
|
|
|
|
// fractional part).
|
|
|
|
|
ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
|
|
|
|
|
((now_ms << 18) / 1000) & 0x00ffffff);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-07 17:01:04 +00:00
|
|
|
void RTPSender::SetSendingStatus(const bool enabled) {
|
|
|
|
|
if (enabled) {
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t frequency_hz;
|
2013-01-25 10:53:38 +00:00
|
|
|
if (audio_configured_) {
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t frequency = audio_->AudioFrequency();
|
2012-11-07 17:01:04 +00:00
|
|
|
|
|
|
|
|
// sanity
|
2013-01-25 10:53:38 +00:00
|
|
|
switch (frequency) {
|
|
|
|
|
case 8000:
|
|
|
|
|
case 12000:
|
|
|
|
|
case 16000:
|
|
|
|
|
case 24000:
|
|
|
|
|
case 32000:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(false);
|
|
|
|
|
return;
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
2013-01-07 10:18:30 +00:00
|
|
|
frequency_hz = frequency;
|
2012-11-07 17:01:04 +00:00
|
|
|
} else {
|
2013-07-16 19:25:04 +00:00
|
|
|
frequency_hz = kDefaultVideoFrequency;
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
// Will be ignored if it's already configured via API.
|
2012-11-07 17:01:04 +00:00
|
|
|
SetStartTimestamp(RTPtime, false);
|
|
|
|
|
} else {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!ssrc_forced_) {
|
|
|
|
|
// Generate a new SSRC.
|
|
|
|
|
ssrc_db_.ReturnSSRC(ssrc_);
|
|
|
|
|
ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2012-11-07 17:01:04 +00:00
|
|
|
// Don't initialize seq number if SSRC passed externally.
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!sequence_number_forced_ && !ssrc_forced_) {
|
|
|
|
|
// Generate a new sequence number.
|
|
|
|
|
sequence_number_ =
|
|
|
|
|
rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-07 17:01:04 +00:00
|
|
|
void RTPSender::SetSendingMediaStatus(const bool enabled) {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
sending_media_ = enabled;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-07 17:01:04 +00:00
|
|
|
bool RTPSender::SendingMedia() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
return sending_media_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t RTPSender::Timestamp() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2013-06-04 13:47:36 +00:00
|
|
|
return timestamp_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2012-11-07 17:01:04 +00:00
|
|
|
if (force) {
|
2013-01-25 10:53:38 +00:00
|
|
|
start_time_stamp_forced_ = force;
|
|
|
|
|
start_time_stamp_ = timestamp;
|
2012-11-07 17:01:04 +00:00
|
|
|
} else {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!start_time_stamp_forced_) {
|
|
|
|
|
start_time_stamp_ = timestamp;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t RTPSender::StartTimestamp() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
return start_time_stamp_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t RTPSender::GenerateNewSSRC() {
|
2013-01-25 10:53:38 +00:00
|
|
|
// If configured via API, return 0.
|
|
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
if (ssrc_forced_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
|
|
|
|
|
return ssrc_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
void RTPSender::SetSSRC(uint32_t ssrc) {
|
2013-01-25 10:53:38 +00:00
|
|
|
// This is configured via the API.
|
|
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
if (ssrc_ == ssrc && ssrc_forced_) {
|
|
|
|
|
return; // Since it's same ssrc, don't reset anything.
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
ssrc_forced_ = true;
|
|
|
|
|
ssrc_db_.ReturnSSRC(ssrc_);
|
|
|
|
|
ssrc_db_.RegisterSSRC(ssrc);
|
|
|
|
|
ssrc_ = ssrc;
|
|
|
|
|
if (!sequence_number_forced_) {
|
|
|
|
|
sequence_number_ =
|
|
|
|
|
rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t RTPSender::SSRC() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
return ssrc_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-07 17:01:04 +00:00
|
|
|
void RTPSender::SetCSRCStatus(const bool include) {
|
2013-01-25 10:53:38 +00:00
|
|
|
include_csrcs_ = include;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
|
|
|
|
|
const uint8_t arr_length) {
|
2013-01-25 10:53:38 +00:00
|
|
|
assert(arr_length <= kRtpCsrcSize);
|
|
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
for (int i = 0; i < arr_length; i++) {
|
2013-06-04 13:47:36 +00:00
|
|
|
csrcs_[i] = arr_of_csrc[i];
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
2013-06-04 13:47:36 +00:00
|
|
|
num_csrcs_ = arr_length;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
|
2013-01-25 10:53:38 +00:00
|
|
|
assert(arr_of_csrc);
|
|
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2013-06-04 13:47:36 +00:00
|
|
|
for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
|
|
|
|
|
arr_of_csrc[i] = csrcs_[i];
|
2012-11-07 17:01:04 +00:00
|
|
|
}
|
2013-06-04 13:47:36 +00:00
|
|
|
return num_csrcs_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
void RTPSender::SetSequenceNumber(uint16_t seq) {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
sequence_number_forced_ = true;
|
|
|
|
|
sequence_number_ = seq;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint16_t RTPSender::SequenceNumber() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
|
|
|
|
return sequence_number_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
// Audio.
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
|
|
|
|
|
const uint16_t time_ms,
|
|
|
|
|
const uint8_t level) {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!audio_configured_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return audio_->SendTelephoneEvent(key, time_ms, level);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!audio_configured_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return audio_->SendTelephoneEventActive(*telephone_event);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::SetAudioPacketSize(
|
|
|
|
|
const uint16_t packet_size_samples) {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!audio_configured_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return audio_->SetAudioPacketSize(packet_size_samples);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
|
|
|
|
|
const uint8_t ID) {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!audio_configured_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return audio_->SetAudioLevelIndicationStatus(enable, ID);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
|
|
|
|
|
uint8_t* id) const {
|
2013-01-25 10:53:38 +00:00
|
|
|
return audio_->AudioLevelIndicationStatus(*enable, *id);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
|
2013-01-25 10:53:38 +00:00
|
|
|
return audio_->SetAudioLevel(level_d_bov);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::SetRED(const int8_t payload_type) {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!audio_configured_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return audio_->SetRED(payload_type);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::RED(int8_t *payload_type) const {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (!audio_configured_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return audio_->RED(*payload_type);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-25 10:53:38 +00:00
|
|
|
// Video
|
|
|
|
|
VideoCodecInformation *RTPSender::CodecInformationVideo() {
|
|
|
|
|
if (audio_configured_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return video_->CodecInformationVideo();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-07 17:01:04 +00:00
|
|
|
RtpVideoCodecTypes RTPSender::VideoCodecType() const {
|
2013-03-18 16:39:03 +00:00
|
|
|
assert(!audio_configured_ && "Sender is an audio stream!");
|
2013-01-25 10:53:38 +00:00
|
|
|
return video_->VideoCodecType();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (audio_configured_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return video_->MaxConfiguredBitrateVideo();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::SendRTPIntraRequest() {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (audio_configured_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return video_->SendRTPIntraRequest();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::SetGenericFECStatus(
|
|
|
|
|
const bool enable, const uint8_t payload_type_red,
|
|
|
|
|
const uint8_t payload_type_fec) {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (audio_configured_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return video_->SetGenericFECStatus(enable, payload_type_red,
|
|
|
|
|
payload_type_fec);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::GenericFECStatus(
|
|
|
|
|
bool *enable, uint8_t *payload_type_red,
|
|
|
|
|
uint8_t *payload_type_fec) const {
|
2013-01-25 10:53:38 +00:00
|
|
|
if (audio_configured_) {
|
2012-11-07 17:01:04 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return video_->GenericFECStatus(
|
|
|
|
|
*enable, *payload_type_red, *payload_type_fec);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
int32_t RTPSender::SetFecParameters(
|
2013-01-25 10:53:38 +00:00
|
|
|
const FecProtectionParams *delta_params,
|
|
|
|
|
const FecProtectionParams *key_params) {
|
|
|
|
|
if (audio_configured_) {
|
2012-03-20 22:10:56 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
return video_->SetFecParameters(delta_params, key_params);
|
2011-07-15 21:32:40 +00:00
|
|
|
}
|
2013-01-25 10:53:38 +00:00
|
|
|
|
2013-04-08 11:08:41 +00:00
|
|
|
void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
|
|
|
|
|
uint8_t* buffer_rtx) {
|
2013-03-15 23:21:52 +00:00
|
|
|
CriticalSectionScoped cs(send_critsect_);
|
2013-04-08 11:08:41 +00:00
|
|
|
uint8_t* data_buffer_rtx = buffer_rtx;
|
2013-03-15 23:21:52 +00:00
|
|
|
// Add RTX header.
|
|
|
|
|
ModuleRTPUtility::RTPHeaderParser rtp_parser(
|
2013-04-08 11:08:41 +00:00
|
|
|
reinterpret_cast<const uint8_t *>(buffer), *length);
|
2013-03-15 23:21:52 +00:00
|
|
|
|
2013-05-29 12:12:51 +00:00
|
|
|
RTPHeader rtp_header;
|
2013-03-15 23:21:52 +00:00
|
|
|
rtp_parser.Parse(rtp_header);
|
|
|
|
|
|
|
|
|
|
// Add original RTP header.
|
2013-05-29 12:12:51 +00:00
|
|
|
memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
|
2013-03-15 23:21:52 +00:00
|
|
|
|
2013-04-12 14:55:46 +00:00
|
|
|
// Replace payload type, if a specific type is set for RTX.
|
|
|
|
|
if (payload_type_rtx_ != -1) {
|
|
|
|
|
data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
|
2013-05-29 12:12:51 +00:00
|
|
|
if (rtp_header.markerBit)
|
2013-04-12 14:55:46 +00:00
|
|
|
data_buffer_rtx[1] |= kRtpMarkerBitMask;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-15 23:21:52 +00:00
|
|
|
// Replace sequence number.
|
2013-04-08 11:08:41 +00:00
|
|
|
uint8_t *ptr = data_buffer_rtx + 2;
|
2013-03-15 23:21:52 +00:00
|
|
|
ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
|
|
|
|
|
|
|
|
|
|
// Replace SSRC.
|
|
|
|
|
ptr += 6;
|
|
|
|
|
ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
|
|
|
|
|
|
|
|
|
|
// Add OSN (original sequence number).
|
2013-05-29 12:12:51 +00:00
|
|
|
ptr = data_buffer_rtx + rtp_header.headerLength;
|
|
|
|
|
ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
|
2013-03-15 23:21:52 +00:00
|
|
|
ptr += 2;
|
|
|
|
|
|
|
|
|
|
// Add original payload data.
|
2013-05-29 12:12:51 +00:00
|
|
|
memcpy(ptr, buffer + rtp_header.headerLength,
|
|
|
|
|
*length - rtp_header.headerLength);
|
2013-03-15 23:21:52 +00:00
|
|
|
*length += 2;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-07 17:01:04 +00:00
|
|
|
} // namespace webrtc
|