blob: ac2b0d72e933a8328e27ef4fafbceece9313beb1 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000011#include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
12
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000013#include <cstdlib> // srand
niklase@google.com470e71d2011-07-07 08:21:25 +000014
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000015#include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
16#include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
17#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
18#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
19#include "webrtc/system_wrappers/interface/trace.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000020#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
stefan@webrtc.orga8179622013-06-04 13:47:36 +000024// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
25const int kMaxPaddingLength = 224;
26
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000027namespace {
28
29const char* FrameTypeToString(const FrameType frame_type) {
30 switch (frame_type) {
31 case kFrameEmpty: return "empty";
32 case kAudioFrameSpeech: return "audio_speech";
33 case kAudioFrameCN: return "audio_cn";
34 case kVideoFrameKey: return "video_key";
35 case kVideoFrameDelta: return "video_delta";
36 case kVideoFrameGolden: return "video_golden";
37 case kVideoFrameAltRef: return "video_altref";
38 }
39 return "";
40}
41
42} // namespace
43
pbos@webrtc.org2f446732013-04-08 11:08:41 +000044RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000045 Transport *transport, RtpAudioFeedback *audio_feedback,
46 PacedSender *paced_sender)
47 : Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL),
48 video_(NULL), paced_sender_(paced_sender),
49 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
50 transport_(transport), sending_media_(true), // Default to sending media.
51 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
52 target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1),
53 payload_type_map_(), rtp_header_extension_map_(),
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +000054 transmission_time_offset_(0), absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000055 // NACK.
56 nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
57 packet_history_(new RTPPacketHistory(clock)),
58 // Statistics
59 packets_sent_(0), payload_bytes_sent_(0), start_time_stamp_forced_(false),
60 start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000061 remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false),
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +000062 timestamp_(0), capture_time_ms_(0), last_packet_marker_bit_(false),
63 num_csrcs_(0), csrcs_(), include_csrcs_(true),
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +000064 rtx_(kRtxOff), payload_type_rtx_(-1) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000065 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
66 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +000067 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000068 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000069 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000070 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000071 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
72 // Random start, 16 bits. Can't be 0.
73 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
74 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +000075
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000076 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000077 audio_ = new RTPSenderAudio(id, clock_, this);
78 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000079 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000080 video_ = new RTPSenderVideo(id, clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000081 }
82 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
pwestin@webrtc.org00741872012-01-19 15:56:10 +000085RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000086 if (remote_ssrc_ != 0) {
87 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000088 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000089 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
pwestin@webrtc.org00741872012-01-19 15:56:10 +000091 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000092 delete send_critsect_;
93 while (!payload_type_map_.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000094 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000095 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +000096 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000097 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000098 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000099 delete packet_history_;
100 delete audio_;
101 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000102
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000103 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104}
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000106void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000107 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000108}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000109
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000110uint16_t RTPSender::ActualSendBitrateKbit() const {
111 return (uint16_t)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000112}
113
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000114uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000115 if (video_) {
116 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000117 }
118 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000119}
120
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000121uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000122 if (video_) {
123 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000124 }
125 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000126}
127
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000128uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000129 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000130}
131
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000132int32_t RTPSender::SetTransmissionTimeOffset(
133 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000134 if (transmission_time_offset > (0x800000 - 1) ||
135 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000136 return -1;
137 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000138 CriticalSectionScoped cs(send_critsect_);
139 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000140 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000141}
142
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000143int32_t RTPSender::SetAbsoluteSendTime(
144 const uint32_t absolute_send_time) {
145 if (absolute_send_time > 0xffffff) { // UWord24.
146 return -1;
147 }
148 CriticalSectionScoped cs(send_critsect_);
149 absolute_send_time_ = absolute_send_time;
150 return 0;
151}
152
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000153int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
154 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000155 CriticalSectionScoped cs(send_critsect_);
156 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000157}
158
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000159int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000160 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000161 CriticalSectionScoped cs(send_critsect_);
162 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000163}
164
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000165uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000166 CriticalSectionScoped cs(send_critsect_);
167 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000168}
169
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000170int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000171 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000172 const int8_t payload_number, const uint32_t frequency,
173 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000174 assert(payload_name);
175 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000177 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000178 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000180 if (payload_type_map_.end() != it) {
181 // We already use this payload type.
182 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000183 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000185 // Check if it's the same as we already have.
186 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000187 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000188 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000189 payload->typeSpecific.Audio.frequency == frequency &&
190 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000191 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000192 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000193 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000194 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000195 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000196 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000197 return 0;
198 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000199 }
200 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000201 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000202 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000203 ModuleRTPUtility::Payload *payload = NULL;
204 if (audio_configured_) {
205 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
206 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000207 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000208 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
209 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000210 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000211 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000212 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000213 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000214 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215}
216
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000217int32_t RTPSender::DeRegisterSendPayload(
218 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000219 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000220
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000221 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000222 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000223
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000224 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000225 return -1;
226 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000227 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000228 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000229 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000230 return 0;
231}
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000233int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000234
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000235int RTPSender::SendPayloadFrequency() const { return audio_->AudioFrequency(); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000237int32_t RTPSender::SetMaxPayloadLength(
238 const uint16_t max_payload_length,
239 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000240 // Sanity check.
241 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
242 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
243 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000244 return -1;
245 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000246 CriticalSectionScoped cs(send_critsect_);
247 max_payload_length_ = max_payload_length;
248 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000249
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000250 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
251 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000252 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000253}
254
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000255uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000256 if (audio_configured_) {
257 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000258 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000259 return max_payload_length_ - RTPHeaderLength() -
260 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
261 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000262 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000263}
264
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000265uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000266 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000267}
268
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000269uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000270
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000271void RTPSender::SetRTXStatus(RtxMode mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000272 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000273 rtx_ = mode;
274 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000275 if (set_ssrc) {
276 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000277 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000278 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000279 }
280 }
281}
282
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000283void RTPSender::RTXStatus(RtxMode* mode, uint32_t* ssrc,
284 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000285 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000286 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000287 *ssrc = ssrc_rtx_;
288 *payload_type = payload_type_rtx_;
289}
290
291
292void RTPSender::SetRtxPayloadType(int payload_type) {
293 CriticalSectionScoped cs(send_critsect_);
294 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000295}
296
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000297int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
298 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000299 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000301 if (payload_type < 0) {
302 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
303 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000304 return -1;
305 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000306 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000307 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000308 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000309 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000310 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000311 // And it's a match...
312 return 0;
313 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000314 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000315 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000316 if (payload_type_ == payload_type) {
317 if (!audio_configured_) {
318 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000319 }
320 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000321 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000322 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000323 payload_type_map_.find(payload_type);
324 if (it == payload_type_map_.end()) {
325 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
326 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000327 return -1;
328 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000329 payload_type_ = payload_type;
330 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000331 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000332 if (!payload->audio && !audio_configured_) {
333 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
334 *video_type = payload->typeSpecific.Video.videoCodecType;
335 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000336 }
337 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000338}
339
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000340int32_t RTPSender::SendOutgoingData(
341 const FrameType frame_type, const int8_t payload_type,
342 const uint32_t capture_timestamp, int64_t capture_time_ms,
343 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000344 const RTPFragmentationHeader *fragmentation,
345 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000346 {
347 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000348 CriticalSectionScoped cs(send_critsect_);
349 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000350 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000351 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000352 }
stefan@webrtc.org66b2e5c2013-07-05 14:30:48 +0000353 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000354 if (CheckPayloadType(payload_type, &video_type) != 0) {
355 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
356 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000357 __FUNCTION__, payload_type);
358 return -1;
359 }
360
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000361 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000362 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
363 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000364 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000365 frame_type == kFrameEmpty);
366
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000367 return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
368 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000369 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000370 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
371 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000372 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000373
374 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000375 if (paced_sender_->Enabled()) {
376 // Padding is driven by the pacer and not by the encoder.
377 return 0;
378 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000379 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000380 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000381 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000382 return video_->SendVideo(video_type, frame_type, payload_type,
383 capture_timestamp, capture_time_ms, payload_data,
384 payload_size, fragmentation, codec_info,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000385 rtp_type_hdr);
386 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000387}
388
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000389bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000390 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000391 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000392 // Current bitrate since last estimate(1 second) averaged with the
393 // estimate since then, to get the most up to date bitrate.
394 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000395 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000396 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000397 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000398 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000399 int bytes = 0;
400 if (current_bitrate == 0) {
401 // Start up phase. Send one 33.3 ms batch to start with.
402 bytes = (bitrate_diff / 8) / 30;
403 } else {
404 bytes = (bitrate_diff / 8);
405 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000406 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000407 if (bytes > bytes_cap) {
408 bytes = bytes_cap;
409 }
410 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000411 int bytes_sent = SendPadData(payload_type, capture_timestamp, capture_time_ms,
412 bytes, kDontRetransmit, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000413 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
414 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000415}
416
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000417int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
418 int32_t bytes) {
419 int padding_bytes_in_packet = kMaxPaddingLength;
420 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000421 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000422 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000423 packet[0] |= 0x20; // Set padding bit.
424 int32_t *data =
425 reinterpret_cast<int32_t *>(&(packet[header_length]));
426
427 // Fill data buffer with random data.
428 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
429 data[j] = rand(); // NOLINT
430 }
431 // Set number of padding bytes in the last byte of the packet.
432 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
433 return padding_bytes_in_packet;
434}
435
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000436int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
437 int64_t capture_time_ms, int32_t bytes,
438 StorageType store, bool force_full_size_packets) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000439 // Drop this packet if we're not sending media packets.
440 if (!sending_media_) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000441 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000442 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000443 int padding_bytes_in_packet = 0;
444 int bytes_sent = 0;
445 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000446 // Always send full padding packets.
447 if (force_full_size_packets && bytes < kMaxPaddingLength)
448 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000449 if (bytes < kMaxPaddingLength) {
450 if (force_full_size_packets) {
451 bytes = kMaxPaddingLength;
452 } else {
453 // Round to the nearest multiple of 32.
454 bytes = (bytes + 16) & 0xffe0;
455 }
456 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000457 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000458 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000459 break;
460 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000461 uint32_t ssrc;
462 uint16_t sequence_number;
463 {
464 CriticalSectionScoped cs(send_critsect_);
465 // Only send padding packets following the last packet of a frame,
466 // indicated by the marker bit.
467 if (!last_packet_marker_bit_)
468 return bytes_sent;
469 if (rtx_ == kRtxOff) {
470 ssrc = ssrc_;
471 sequence_number = sequence_number_;
472 ++sequence_number_;
473 } else {
474 ssrc = ssrc_rtx_;
475 sequence_number = sequence_number_rtx_;
476 ++sequence_number_rtx_;
477 }
478 }
479 uint8_t padding_packet[IP_PACKET_SIZE];
480 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
481 false, timestamp, sequence_number, NULL,
482 0);
483 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
484 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000485 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
486 header_length, capture_time_ms, store,
487 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000488 // Error sending the packet.
489 break;
490 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000491 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000492 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000493 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000494}
495
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000496void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000497 const uint16_t number_to_store) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000498 packet_history_->SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000499}
500
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000501bool RTPSender::StorePackets() const {
502 return packet_history_->StorePackets();
503}
niklase@google.com470e71d2011-07-07 08:21:25 +0000504
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000505int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
506 uint16_t length = IP_PACKET_SIZE;
507 uint8_t data_buffer[IP_PACKET_SIZE];
508 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000509 int64_t capture_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000510 StorageType type;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000511 if (!packet_history_->GetRTPPacket(packet_id, min_resend_time, data_buffer,
512 &length, &capture_time_ms, &type)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000513 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000514 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000515 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000516 if (length == 0 || type == kDontRetransmit) {
517 // No bytes copied (packet recently resent, skip resending) or
518 // packet should not be retransmitted.
519 return 0;
520 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000521
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000522 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000523 if (rtx_ != kRtxOff) {
524 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000525 buffer_to_send_ptr = data_buffer_rtx;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000526 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000527
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000528 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000529 RTPHeader header;
530 rtp_parser.Parse(header);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000531
532 // Store the time when the packet was last sent or added to pacer.
533 packet_history_->UpdateResendTime(packet_id);
534
535 {
536 // Update send statistics prior to pacer.
537 CriticalSectionScoped cs(send_critsect_);
538 Bitrate::Update(length);
539 packets_sent_++;
540 // We on purpose don't add to payload_bytes_sent_ since this is a
541 // re-transmit and not new payload data.
542 }
543
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000544 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000545 "timestamp", header.timestamp,
546 "seqnum", header.sequenceNumber);
547
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000548 if (paced_sender_) {
549 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000550 header.ssrc,
551 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000552 capture_time_ms,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000553 length - header.headerLength)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000554 // We can't send the packet right now.
555 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000556 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000557 }
558 }
559
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000560 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000561 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000562 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000563 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000564}
565
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000566bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
567 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000568 if (transport_) {
569 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000570 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000571 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
572 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000573 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000574 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000575 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
576 "Transport failed to send packet");
577 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000578 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000579 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000580}
581
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000582int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000583 if (!video_)
584 return -1;
585 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000586}
587
588int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000589 if (!video_)
590 return -1;
591 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000592}
593
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000594void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000595 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000596 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000597 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
598 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000599 const int64_t now = clock_->TimeInMilliseconds();
600 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000601
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000602 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000603 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000604 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000605 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000606 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000607 return;
608 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000609
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000610 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
611 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000612 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000613 if (bytes_sent > 0) {
614 bytes_re_sent += bytes_sent;
615 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000616 // The packet has previously been resent.
617 // Try resending next packet in the list.
618 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000619 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000620 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000621 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000622 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000623 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000624 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000625 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000626 // Delay bandwidth estimate (RTT * BW).
627 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000628 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000629 uint32_t target_bytes =
630 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000631 if (bytes_re_sent > target_bytes) {
632 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000633 }
634 }
635 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000636 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000637 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000638 UpdateNACKBitRate(bytes_re_sent, now);
639 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000640 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000641}
642
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000643bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
644 uint32_t num = 0;
645 int32_t byte_count = 0;
646 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000647
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000648 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000649
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000650 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000651 return true;
652 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000653 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
654 if ((now - nack_byte_count_times_[num]) > avg_interval) {
655 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000656 break;
657 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000658 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000659 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000660 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000661 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000662 if (num == NACK_BYTECOUNT_SIZE) {
663 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000664 // during the last msg_interval.
665 time_interval = now - nack_byte_count_times_[num - 1];
666 if (time_interval < 0) {
667 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000668 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000669 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000670 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000671}
672
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000673void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
674 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000675 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000676
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000677 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000678 if (bytes > 0) {
679 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000680 // Add padding length.
681 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000682 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000683 if (nack_byte_count_times_[0] == 0) {
684 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000685 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000686 // Shift.
687 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
688 nack_byte_count_[i + 1] = nack_byte_count_[i];
689 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000690 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000691 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000692 nack_byte_count_[0] = bytes;
693 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000694 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000695 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000696}
697
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000698// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000699bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000700 int64_t capture_time_ms) {
701 StorageType type;
702 uint16_t length = IP_PACKET_SIZE;
703 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000704 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000705
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000706 if (packet_history_ == NULL) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000707 // Packet cannot be found. Allow sending to continue.
708 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000709 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000710 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
711 &stored_time_ms, &type)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000712 // Packet cannot be found. Allow sending to continue.
713 return true;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000714 }
715 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000716
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000717 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000718 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000719 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000720 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000721 "timestamp", rtp_header.timestamp,
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000722 "seqnum", sequence_number);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000723
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000724 int64_t now_ms = clock_->TimeInMilliseconds();
725 int64_t diff_ms = now_ms - capture_time_ms;
726 bool updated_transmission_time_offset =
727 UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms);
728 bool updated_abs_send_time =
729 UpdateAbsoluteSendTime(data_buffer, length, rtp_header, now_ms);
730 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000731 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000732 packet_history_->ReplaceRTPHeader(data_buffer,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000733 rtp_header.sequenceNumber,
734 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000735 }
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000736 return SendPacketToNetwork(data_buffer, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000737}
738
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000739int RTPSender::TimeToSendPadding(int bytes) {
740 if (!sending_media_) {
741 return 0;
742 }
743 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000744 int64_t capture_time_ms;
745 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000746 {
747 CriticalSectionScoped cs(send_critsect_);
748 payload_type = (rtx_ == kRtxOff) ? payload_type_ : payload_type_rtx_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000749 timestamp = timestamp_;
750 capture_time_ms = capture_time_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000751 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000752 return SendPadData(payload_type, timestamp, capture_time_ms, bytes,
753 kDontStore, true);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000754}
755
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000756// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000757int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000758 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000759 int64_t capture_time_ms, StorageType storage,
760 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000761 ModuleRTPUtility::RTPHeaderParser rtp_parser(
762 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000763 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000764 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000765
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000766 int64_t now_ms = clock_->TimeInMilliseconds();
767
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000768 // |capture_time_ms| <= 0 is considered invalid.
769 // TODO(holmer): This should be changed all over Video Engine so that negative
770 // time is consider invalid, while 0 is considered a valid time.
771 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000772 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000773 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000774 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000775
776 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
777 rtp_header, now_ms);
778
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000779 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000780 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
781 max_payload_length_, capture_time_ms,
782 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000783 return -1;
784 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000785
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000786 // Create and send RTX Packet.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000787 // TODO(pwesin): This should be moved to its own code path triggered by pacer.
788 bool rtx_sent = false;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000789 if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000790 uint16_t length_rtx = payload_length + rtp_header_length;
791 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000792 BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000793 if (!SendPacketToNetwork(data_buffer_rtx, length_rtx)) return -1;
794 rtx_sent = true;
795 }
796 {
797 // Update send statistics prior to pacer.
798 CriticalSectionScoped cs(send_critsect_);
799 Bitrate::Update(payload_length + rtp_header_length);
800 ++packets_sent_;
801 payload_bytes_sent_ += payload_length;
802 if (rtx_sent) {
803 // The RTX packet.
804 ++packets_sent_;
805 payload_bytes_sent_ += payload_length;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000806 }
807 }
808
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000809 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000810 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
811 rtp_header.sequenceNumber, capture_time_ms,
812 payload_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000813 // We can't send the packet right now.
814 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000815 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000816 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000817 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000818 if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
819 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000820 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000821 return -1;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000822}
823
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000824void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000825 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000826 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000827 nack_bitrate_.Process();
828 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000829 return;
830 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000831 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000832}
833
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000834uint16_t RTPSender::RTPHeaderLength() const {
835 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000836 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000837 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000838 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000839 rtp_header_length += RtpHeaderExtensionTotalLength();
840 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000841}
842
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000843uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000844 CriticalSectionScoped cs(send_critsect_);
845 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000846}
847
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000848void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000849 packets_sent_ = 0;
850 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000851}
852
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000853uint32_t RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000854 // Don't use critsect to avoid potential deadlock.
855 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000856}
857
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000858// Number of sent RTP bytes.
859// Don't use critsect to avoid potental deadlock.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000860uint32_t RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000861 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000862}
863
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000864int RTPSender::CreateRTPHeader(
865 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
866 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
867 uint8_t num_csrcs) const {
868 header[0] = 0x80; // version 2.
869 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000870 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000871 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000872 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000873 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
874 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
875 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000876 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000877
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000878 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000879 if (num_csrcs > 0) {
880 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000881 // error
882 assert(false);
883 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000884 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000885 uint8_t *ptr = &header[rtp_header_length];
886 for (int i = 0; i < num_csrcs; ++i) {
887 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000888 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000889 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000890 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000891
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000892 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000893 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000894 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000895
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000896 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
897 if (len > 0) {
898 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000899 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000900 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000901 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000902}
903
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000904int32_t RTPSender::BuildRTPheader(
905 uint8_t *data_buffer, const int8_t payload_type,
906 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000907 int64_t capture_time_ms, const bool time_stamp_provided,
908 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000909 assert(payload_type >= 0);
910 CriticalSectionScoped cs(send_critsect_);
911
912 if (time_stamp_provided) {
913 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000914 } else {
915 // Make a unique time stamp.
916 // We can't inc by the actual time, since then we increase the risk of back
917 // timing.
918 timestamp_++;
919 }
920 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000921 capture_time_ms_ = capture_time_ms;
922 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000923 int csrcs_length = 0;
924 if (include_csrcs_)
925 csrcs_length = num_csrcs_;
926 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
927 timestamp_, sequence_number, csrcs_, csrcs_length);
928}
929
930uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000931 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000932 return 0;
933 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000934 // RTP header extension, RFC 3550.
935 // 0 1 2 3
936 // 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
937 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
938 // | defined by profile | length |
939 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
940 // | header extension |
941 // | .... |
942 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000943 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000944 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000945
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000946 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000947 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000948 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000949
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000950 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000951 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000952
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000953 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000954 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000955 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000956 switch (type) {
957 case kRtpExtensionTransmissionTimeOffset:
958 block_length = BuildTransmissionTimeOffsetExtension(
959 data_buffer + kHeaderLength + total_block_length);
960 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +0000961 case kRtpExtensionAudioLevel:
962 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
963 // we don't have to care about it here, which is true until we wan't to
964 // use it together with any of the other extensions we support.
965 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000966 case kRtpExtensionAbsoluteSendTime:
967 block_length = BuildAbsoluteSendTimeExtension(
968 data_buffer + kHeaderLength + total_block_length);
969 break;
970 default:
971 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000972 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000973 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000974 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000975 }
976 if (total_block_length == 0) {
977 // No extension added.
978 return 0;
979 }
980 // Set header length (in number of Word32, header excluded).
981 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000982 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000983 total_block_length / 4);
984 // Total added length.
985 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000986}
987
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000988uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
989 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000990 // From RFC 5450: Transmission Time Offsets in RTP Streams.
991 //
992 // The transmission time is signaled to the receiver in-band using the
993 // general mechanism for RTP header extensions [RFC5285]. The payload
994 // of this extension (the transmitted value) is a 24-bit signed integer.
995 // When added to the RTP timestamp of the packet, it represents the
996 // "effective" RTP transmission time of the packet, on the RTP
997 // timescale.
998 //
999 // The form of the transmission offset extension block:
1000 //
1001 // 0 1 2 3
1002 // 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
1003 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1004 // | ID | len=2 | transmission offset |
1005 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001006
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001007 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001008 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001009 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1010 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001011 // Not registered.
1012 return 0;
1013 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001014 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001015 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001016 data_buffer[pos++] = (id << 4) + len;
1017 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1018 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001019 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001020 assert(pos == kTransmissionTimeOffsetLength);
1021 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001022}
1023
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001024uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
1025 uint8_t* data_buffer) const {
1026 // Absolute send time in RTP streams.
1027 //
1028 // The absolute send time is signaled to the receiver in-band using the
1029 // general mechanism for RTP header extensions [RFC5285]. The payload
1030 // of this extension (the transmitted value) is a 24-bit unsigned integer
1031 // containing the sender's current time in seconds as a fixed point number
1032 // with 18 bits fractional part.
1033 //
1034 // The form of the absolute send time extension block:
1035 //
1036 // 0 1 2 3
1037 // 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
1038 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1039 // | ID | len=2 | absolute send time |
1040 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1041
1042 // Get id defined by user.
1043 uint8_t id;
1044 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1045 &id) != 0) {
1046 // Not registered.
1047 return 0;
1048 }
1049 size_t pos = 0;
1050 const uint8_t len = 2;
1051 data_buffer[pos++] = (id << 4) + len;
1052 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1053 absolute_send_time_);
1054 pos += 3;
1055 assert(pos == kAbsoluteSendTimeLength);
1056 return kAbsoluteSendTimeLength;
1057}
1058
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001059bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001060 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001061 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001062 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001063
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001064 // Get length until start of header extension block.
1065 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001066 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001067 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001068 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001069 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001070 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001071 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001072 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001073 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001074 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001075 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001076 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001077 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001078 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001079 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001080 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001081 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001082 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1083 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001084 WEBRTC_TRACE(
1085 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001086 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001087 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001088 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001089 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001090 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001091 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1092 &id) != 0) {
1093 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001094 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001095 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001096 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001097 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001098 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001099 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001100 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001101 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001102 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001103 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001104 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001105 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001106 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001107 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001108}
1109
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001110bool RTPSender::UpdateAbsoluteSendTime(
1111 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001112 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001113 CriticalSectionScoped cs(send_critsect_);
1114
1115 // Get length until start of header extension block.
1116 int extension_block_pos =
1117 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1118 kRtpExtensionAbsoluteSendTime);
1119 if (extension_block_pos < 0) {
1120 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1121 "Failed to update absolute send time, not registered.");
1122 return false;
1123 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001124 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001125 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001126 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001127 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1128 "Failed to update absolute send time, invalid length.");
1129 return false;
1130 }
1131 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001132 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1133 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001134 WEBRTC_TRACE(
1135 kTraceStream, kTraceRtpRtcp, id_,
1136 "Failed to update absolute send time, hdr extension not found.");
1137 return false;
1138 }
1139 // Get id.
1140 uint8_t id = 0;
1141 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1142 &id) != 0) {
1143 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1144 "Failed to update absolute send time, no id.");
1145 return false;
1146 }
1147 // Verify first byte in block.
1148 const uint8_t first_block_byte = (id << 4) + 2;
1149 if (rtp_packet[block_pos] != first_block_byte) {
1150 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1151 "Failed to update absolute send time.");
1152 return false;
1153 }
1154 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1155 // fractional part).
1156 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1157 ((now_ms << 18) / 1000) & 0x00ffffff);
1158 return true;
1159}
1160
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001161void RTPSender::SetSendingStatus(const bool enabled) {
1162 if (enabled) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001163 uint32_t frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001164 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001165 uint32_t frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001166
1167 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001168 switch (frequency) {
1169 case 8000:
1170 case 12000:
1171 case 16000:
1172 case 24000:
1173 case 32000:
1174 break;
1175 default:
1176 assert(false);
1177 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001178 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +00001179 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001180 } else {
stefan@webrtc.org66b2e5c2013-07-05 14:30:48 +00001181 frequency_hz = kVideoPayloadTypeFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001182 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001183 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001184
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001185 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001186 SetStartTimestamp(RTPtime, false);
1187 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001188 if (!ssrc_forced_) {
1189 // Generate a new SSRC.
1190 ssrc_db_.ReturnSSRC(ssrc_);
1191 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001192 }
1193 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001194 if (!sequence_number_forced_ && !ssrc_forced_) {
1195 // Generate a new sequence number.
1196 sequence_number_ =
1197 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001198 }
1199 }
1200}
1201
1202void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001203 CriticalSectionScoped cs(send_critsect_);
1204 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001205}
1206
1207bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001208 CriticalSectionScoped cs(send_critsect_);
1209 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001210}
1211
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001212uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001213 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001214 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001215}
1216
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001217void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001218 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001219 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001220 start_time_stamp_forced_ = force;
1221 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001222 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001223 if (!start_time_stamp_forced_) {
1224 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001225 }
1226 }
1227}
1228
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001229uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001230 CriticalSectionScoped cs(send_critsect_);
1231 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001232}
1233
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001234uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001235 // If configured via API, return 0.
1236 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001237
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001238 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001239 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001240 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001241 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1242 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001243}
1244
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001245void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001246 // This is configured via the API.
1247 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001248
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001249 if (ssrc_ == ssrc && ssrc_forced_) {
1250 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001251 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001252 ssrc_forced_ = true;
1253 ssrc_db_.ReturnSSRC(ssrc_);
1254 ssrc_db_.RegisterSSRC(ssrc);
1255 ssrc_ = ssrc;
1256 if (!sequence_number_forced_) {
1257 sequence_number_ =
1258 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001259 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001260}
1261
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001262uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001263 CriticalSectionScoped cs(send_critsect_);
1264 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001265}
1266
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001267void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001268 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001269}
1270
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001271void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1272 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001273 assert(arr_length <= kRtpCsrcSize);
1274 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001275
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001276 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001277 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001278 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001279 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001280}
1281
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001282int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001283 assert(arr_of_csrc);
1284 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001285 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1286 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001287 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001288 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001289}
1290
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001291void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001292 CriticalSectionScoped cs(send_critsect_);
1293 sequence_number_forced_ = true;
1294 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001295}
1296
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001297uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001298 CriticalSectionScoped cs(send_critsect_);
1299 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001300}
1301
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001302// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001303int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1304 const uint16_t time_ms,
1305 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001306 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001307 return -1;
1308 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001309 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001310}
1311
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001312bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001313 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001314 return false;
1315 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001316 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001317}
1318
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001319int32_t RTPSender::SetAudioPacketSize(
1320 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001321 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001322 return -1;
1323 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001324 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001325}
1326
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001327int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1328 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001329 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001330 return -1;
1331 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001332 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001333}
1334
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001335int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1336 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001337 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001338}
1339
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001340int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001341 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001342}
1343
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001344int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001345 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001346 return -1;
1347 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001348 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001349}
1350
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001351int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001352 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001353 return -1;
1354 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001355 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001356}
1357
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001358// Video
1359VideoCodecInformation *RTPSender::CodecInformationVideo() {
1360 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001361 return NULL;
1362 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001363 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001364}
1365
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001366RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001367 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001368 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001369}
1370
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001371uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001372 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001373 return 0;
1374 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001375 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001376}
1377
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001378int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001379 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001380 return -1;
1381 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001382 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001383}
1384
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001385int32_t RTPSender::SetGenericFECStatus(
1386 const bool enable, const uint8_t payload_type_red,
1387 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001388 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001389 return -1;
1390 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001391 return video_->SetGenericFECStatus(enable, payload_type_red,
1392 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001393}
1394
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001395int32_t RTPSender::GenericFECStatus(
1396 bool *enable, uint8_t *payload_type_red,
1397 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001398 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001399 return -1;
1400 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001401 return video_->GenericFECStatus(
1402 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001403}
1404
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001405int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001406 const FecProtectionParams *delta_params,
1407 const FecProtectionParams *key_params) {
1408 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001409 return -1;
1410 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001411 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001412}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001413
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001414void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1415 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001416 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001417 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001418 // Add RTX header.
1419 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001420 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001421
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001422 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001423 rtp_parser.Parse(rtp_header);
1424
1425 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001426 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001427
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001428 // Replace payload type, if a specific type is set for RTX.
1429 if (payload_type_rtx_ != -1) {
1430 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001431 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001432 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1433 }
1434
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001435 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001436 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001437 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1438
1439 // Replace SSRC.
1440 ptr += 6;
1441 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1442
1443 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001444 ptr = data_buffer_rtx + rtp_header.headerLength;
1445 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001446 ptr += 2;
1447
1448 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001449 memcpy(ptr, buffer + rtp_header.headerLength,
1450 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001451 *length += 2;
1452}
1453
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001454} // namespace webrtc