blob: ae67e323d478537f8596d09d469e4ecbd23f6f61 [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/pacing/include/paced_sender.h"
16#include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
17#include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
18#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
19#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
20#include "webrtc/system_wrappers/interface/trace.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000024RTPSender::RTPSender(const WebRtc_Word32 id, const bool audio, Clock *clock,
25 Transport *transport, RtpAudioFeedback *audio_feedback,
26 PacedSender *paced_sender)
27 : Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL),
28 video_(NULL), paced_sender_(paced_sender),
29 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
30 transport_(transport), sending_media_(true), // Default to sending media.
31 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
32 target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1),
33 payload_type_map_(), rtp_header_extension_map_(),
34 transmission_time_offset_(0),
35 // NACK.
36 nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
37 packet_history_(new RTPPacketHistory(clock)),
38 // Statistics
39 packets_sent_(0), payload_bytes_sent_(0), start_time_stamp_forced_(false),
40 start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
41 remote_ssrc_(0), sequence_number_forced_(false), sequence_number_(0),
42 sequence_number_rtx_(0), ssrc_forced_(false), ssrc_(0), time_stamp_(0),
43 csrcs_(0), csrc_(), include_csrcs_(true), rtx_(false), ssrc_rtx_(0) {
44 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
45 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
46 memset(csrc_, 0, sizeof(csrc_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000047 // We need to seed the random generator.
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000048 srand(static_cast<WebRtc_UWord32>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000049 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
niklase@google.com470e71d2011-07-07 08:21:25 +000050
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000051 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000052 audio_ = new RTPSenderAudio(id, clock_, this);
53 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000054 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000055 video_ = new RTPSenderVideo(id, clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000056 }
57 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000058}
59
pwestin@webrtc.org00741872012-01-19 15:56:10 +000060RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000061 if (remote_ssrc_ != 0) {
62 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000063 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000064 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +000065
pwestin@webrtc.org00741872012-01-19 15:56:10 +000066 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000067 delete send_critsect_;
68 while (!payload_type_map_.empty()) {
69 std::map<WebRtc_Word8, ModuleRTPUtility::Payload *>::iterator it =
70 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +000071 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000072 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000073 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000074 delete packet_history_;
75 delete audio_;
76 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +000077
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000078 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000079}
niklase@google.com470e71d2011-07-07 08:21:25 +000080
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000081void RTPSender::SetTargetSendBitrate(const WebRtc_UWord32 bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000082 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +000084
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000085WebRtc_UWord16 RTPSender::ActualSendBitrateKbit() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000086 return (WebRtc_UWord16)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000089WebRtc_UWord32 RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000090 if (video_) {
91 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000092 }
93 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +000094}
95
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000096WebRtc_UWord32 RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000097 if (video_) {
98 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000099 }
100 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000101}
102
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000103WebRtc_UWord32 RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000104 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000105}
106
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000107WebRtc_Word32 RTPSender::SetTransmissionTimeOffset(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000108 const WebRtc_Word32 transmission_time_offset) {
109 if (transmission_time_offset > (0x800000 - 1) ||
110 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000111 return -1;
112 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000113 CriticalSectionScoped cs(send_critsect_);
114 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000115 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000116}
117
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000118WebRtc_Word32 RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
119 const WebRtc_UWord8 id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000120 CriticalSectionScoped cs(send_critsect_);
121 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000122}
123
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000124WebRtc_Word32 RTPSender::DeregisterRtpHeaderExtension(
125 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000126 CriticalSectionScoped cs(send_critsect_);
127 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000128}
129
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000130WebRtc_UWord16 RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000131 CriticalSectionScoped cs(send_critsect_);
132 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000133}
134
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000135WebRtc_Word32 RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000136 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
137 const WebRtc_Word8 payload_number, const WebRtc_UWord32 frequency,
138 const WebRtc_UWord8 channels, const WebRtc_UWord32 rate) {
139 assert(payload_name);
140 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000141
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000142 std::map<WebRtc_Word8, ModuleRTPUtility::Payload *>::iterator it =
143 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000145 if (payload_type_map_.end() != it) {
146 // We already use this payload type.
147 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000148 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000149
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000150 // Check if it's the same as we already have.
151 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000152 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000153 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000154 payload->typeSpecific.Audio.frequency == frequency &&
155 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000156 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000157 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000158 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000159 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000160 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000161 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000162 return 0;
163 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000164 }
165 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000166 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000167 WebRtc_Word32 ret_val = -1;
168 ModuleRTPUtility::Payload *payload = NULL;
169 if (audio_configured_) {
170 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
171 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000172 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000173 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
174 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000175 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000176 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000177 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000178 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000179 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180}
181
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000182WebRtc_Word32 RTPSender::DeRegisterSendPayload(
183 const WebRtc_Word8 payload_type) {
184 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000185
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000186 std::map<WebRtc_Word8, ModuleRTPUtility::Payload *>::iterator it =
187 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000188
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000189 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000190 return -1;
191 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000192 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000193 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000194 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000195 return 0;
196}
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000198WebRtc_Word8 RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000200int RTPSender::SendPayloadFrequency() const { return audio_->AudioFrequency(); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000201
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000202WebRtc_Word32 RTPSender::SetMaxPayloadLength(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000203 const WebRtc_UWord16 max_payload_length,
204 const WebRtc_UWord16 packet_over_head) {
205 // Sanity check.
206 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
207 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
208 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000209 return -1;
210 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000211 CriticalSectionScoped cs(send_critsect_);
212 max_payload_length_ = max_payload_length;
213 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000214
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000215 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
216 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000217 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000218}
219
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000220WebRtc_UWord16 RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000221 if (audio_configured_) {
222 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000223 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000224 return max_payload_length_ - RTPHeaderLength() -
225 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
226 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000227 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000228}
229
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000230WebRtc_UWord16 RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000231 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000232}
233
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000234WebRtc_UWord16 RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000235
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000236void RTPSender::SetRTXStatus(const bool enable, const bool set_ssrc,
237 const WebRtc_UWord32 ssrc) {
238 CriticalSectionScoped cs(send_critsect_);
239 rtx_ = enable;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000240 if (enable) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000241 if (set_ssrc) {
242 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000243 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000244 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000245 }
246 }
247}
248
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000249void RTPSender::RTXStatus(bool *enable, WebRtc_UWord32 *SSRC) const {
250 CriticalSectionScoped cs(send_critsect_);
251 *enable = rtx_;
252 *SSRC = ssrc_rtx_;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000253}
254
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000255WebRtc_Word32 RTPSender::CheckPayloadType(const WebRtc_Word8 payload_type,
256 RtpVideoCodecTypes *video_type) {
257 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000258
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000259 if (payload_type < 0) {
260 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
261 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000262 return -1;
263 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000264 if (audio_configured_) {
265 WebRtc_Word8 red_pl_type = -1;
266 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000267 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000268 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000269 // And it's a match...
270 return 0;
271 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000272 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000273 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000274 if (payload_type_ == payload_type) {
275 if (!audio_configured_) {
276 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000277 }
278 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000279 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000280 std::map<WebRtc_Word8, ModuleRTPUtility::Payload *>::iterator it =
281 payload_type_map_.find(payload_type);
282 if (it == payload_type_map_.end()) {
283 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
284 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000285 return -1;
286 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000287 payload_type_ = payload_type;
288 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000289 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000290 if (!payload->audio && !audio_configured_) {
291 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
292 *video_type = payload->typeSpecific.Video.videoCodecType;
293 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000294 }
295 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000296}
297
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000298WebRtc_Word32 RTPSender::SendOutgoingData(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000299 const FrameType frame_type, const WebRtc_Word8 payload_type,
300 const WebRtc_UWord32 capture_timestamp, int64_t capture_time_ms,
301 const WebRtc_UWord8 *payload_data, const WebRtc_UWord32 payload_size,
302 const RTPFragmentationHeader *fragmentation,
303 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000304 {
305 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000306 CriticalSectionScoped cs(send_critsect_);
307 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000308 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000309 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000310 }
311 RtpVideoCodecTypes video_type = kRtpNoVideo;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000312 if (CheckPayloadType(payload_type, &video_type) != 0) {
313 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
314 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000315 __FUNCTION__, payload_type);
316 return -1;
317 }
318
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000319 if (audio_configured_) {
320 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000321 frame_type == kFrameEmpty);
322
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000323 return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
324 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000325 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000326 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000327
328 if (frame_type == kFrameEmpty) {
329 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
330 capture_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000331 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000332 return video_->SendVideo(video_type, frame_type, payload_type,
333 capture_timestamp, capture_time_ms, payload_data,
334 payload_size, fragmentation, codec_info,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000335 rtp_type_hdr);
336 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000337}
338
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000339WebRtc_Word32 RTPSender::SendPaddingAccordingToBitrate(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000340 WebRtc_Word8 payload_type, WebRtc_UWord32 capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000341 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000342 // Current bitrate since last estimate(1 second) averaged with the
343 // estimate since then, to get the most up to date bitrate.
344 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000345 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000346 if (bitrate_diff <= 0) {
347 return 0;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000348 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000349 int bytes = 0;
350 if (current_bitrate == 0) {
351 // Start up phase. Send one 33.3 ms batch to start with.
352 bytes = (bitrate_diff / 8) / 30;
353 } else {
354 bytes = (bitrate_diff / 8);
355 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000356 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000357 if (bytes > bytes_cap) {
358 bytes = bytes_cap;
359 }
360 }
361 return SendPadData(payload_type, capture_timestamp, capture_time_ms, bytes);
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000362}
363
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000364WebRtc_Word32 RTPSender::SendPadData(
365 WebRtc_Word8 payload_type, WebRtc_UWord32 capture_timestamp,
366 int64_t capture_time_ms, WebRtc_Word32 bytes) {
367 // Drop this packet if we're not sending media packets.
368 if (!sending_media_) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000369 return 0;
370 }
371 // Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
372 int max_length = 224;
373 WebRtc_UWord8 data_buffer[IP_PACKET_SIZE];
374
375 for (; bytes > 0; bytes -= max_length) {
asapersson@webrtc.org63a34f42012-04-20 13:20:27 +0000376 int padding_bytes_in_packet = max_length;
377 if (bytes < max_length) {
378 padding_bytes_in_packet = (bytes + 16) & 0xffe0; // Keep our modulus 32.
379 }
380 if (padding_bytes_in_packet < 32) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000381 // Sanity don't send empty packets.
382 break;
asapersson@webrtc.org63a34f42012-04-20 13:20:27 +0000383 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000384 // Correct seq num, timestamp and payload type.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000385 int header_length = BuildRTPheader(
386 data_buffer, payload_type, false, // No markerbit.
387 capture_timestamp, true, // Timestamp provided.
388 true); // Increment sequence number.
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000389 data_buffer[0] |= 0x20; // Set padding bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000390 WebRtc_Word32 *data =
391 reinterpret_cast<WebRtc_Word32 *>(&(data_buffer[header_length]));
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000392
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000393 // Fill data buffer with random data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000394 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
395 data[j] = rand(); // NOLINT
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000396 }
397 // Set number of padding bytes in the last byte of the packet.
398 data_buffer[header_length + padding_bytes_in_packet - 1] =
399 padding_bytes_in_packet;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000400 // Send the packet.
401 if (0 > SendToNetwork(data_buffer, padding_bytes_in_packet, header_length,
402 capture_time_ms, kDontRetransmit)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000403 // Error sending the packet.
404 break;
405 }
406 }
407 if (bytes > 31) { // 31 due to our modulus 32.
408 // We did not manage to send all bytes.
409 return -1;
410 }
411 return 0;
412}
413
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000414void RTPSender::SetStorePacketsStatus(const bool enable,
415 const WebRtc_UWord16 number_to_store) {
416 packet_history_->SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000417}
418
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000419bool RTPSender::StorePackets() const { return packet_history_->StorePackets(); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000420
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000421WebRtc_Word32 RTPSender::ReSendPacket(WebRtc_UWord16 packet_id,
422 WebRtc_UWord32 min_resend_time) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000423 WebRtc_UWord16 length = IP_PACKET_SIZE;
424 WebRtc_UWord8 data_buffer[IP_PACKET_SIZE];
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000425 WebRtc_UWord8 *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000426
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000427 int64_t stored_time_in_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000428 StorageType type;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000429 bool found = packet_history_->GetRTPPacket(packet_id, min_resend_time,
430 data_buffer, &length,
431 &stored_time_in_ms, &type);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000432 if (!found) {
433 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000434 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000435 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000436 if (length == 0 || type == kDontRetransmit) {
437 // No bytes copied (packet recently resent, skip resending) or
438 // packet should not be retransmitted.
439 return 0;
440 }
pwestin@webrtc.orgb30f0ed2012-01-23 16:23:31 +0000441 WebRtc_UWord8 data_buffer_rtx[IP_PACKET_SIZE];
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000442 if (rtx_) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000443 buffer_to_send_ptr = data_buffer_rtx;
444
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000445 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000446 // Add RTX header.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000447 ModuleRTPUtility::RTPHeaderParser rtp_parser(
448 reinterpret_cast<const WebRtc_UWord8 *>(data_buffer), length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000449
450 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000451 rtp_parser.Parse(rtp_header);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000452
453 // Add original RTP header.
454 memcpy(data_buffer_rtx, data_buffer, rtp_header.header.headerLength);
455
456 // Replace sequence number.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000457 WebRtc_UWord8 *ptr = data_buffer_rtx + 2;
458 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000459
460 // Replace SSRC.
461 ptr += 6;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000462 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000463
464 // Add OSN (original sequence number).
465 ptr = data_buffer_rtx + rtp_header.header.headerLength;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000466 ModuleRTPUtility::AssignUWord16ToBuffer(ptr,
467 rtp_header.header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000468 ptr += 2;
469
470 // Add original payload data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000471 memcpy(ptr, data_buffer + rtp_header.header.headerLength,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000472 length - rtp_header.header.headerLength);
473 length += 2;
474 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000475 WebRtc_Word32 bytes_sent = ReSendToNetwork(buffer_to_send_ptr, length);
476 if (bytes_sent <= 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000477 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000478 "Transport failed to resend packet_id %u", packet_id);
479 return -1;
480 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000481 // Store the time when the packet was last resent.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000482 packet_history_->UpdateResendTime(packet_id);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000483 return bytes_sent;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000484}
485
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000486WebRtc_Word32 RTPSender::ReSendToNetwork(const WebRtc_UWord8 *packet,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000487 const WebRtc_UWord32 size) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000488 WebRtc_Word32 bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000489 if (transport_) {
490 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000491 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000492 if (bytes_sent <= 0) {
493 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000494 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000495 // Update send statistics.
496 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000497 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000498 packets_sent_++;
499 // We on purpose don't add to payload_bytes_sent_ since this is a
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000500 // re-transmit and not new payload data.
501 return bytes_sent;
niklase@google.com470e71d2011-07-07 08:21:25 +0000502}
503
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000504int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000505 if (!video_)
506 return -1;
507 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000508}
509
510int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000511 if (!video_)
512 return -1;
513 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000514}
515
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000516void RTPSender::OnReceivedNACK(
517 const WebRtc_UWord16 nack_sequence_numbers_length,
518 const WebRtc_UWord16 *nack_sequence_numbers,
519 const WebRtc_UWord16 avg_rtt) {
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000520 const WebRtc_Word64 now = clock_->TimeInMilliseconds();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000521 WebRtc_UWord32 bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000522
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000523 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000524 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000525 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000526 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000527 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000528 return;
529 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000530
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000531 for (WebRtc_UWord16 i = 0; i < nack_sequence_numbers_length; ++i) {
532 const WebRtc_Word32 bytes_sent = ReSendPacket(nack_sequence_numbers[i],
533 5 + avg_rtt);
534 if (bytes_sent > 0) {
535 bytes_re_sent += bytes_sent;
536 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000537 // The packet has previously been resent.
538 // Try resending next packet in the list.
539 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000540 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000541 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000542 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000543 "Failed resending RTP packet %d, Discard rest of packets",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000544 nack_sequence_numbers[i]);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000545 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000546 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000547 // Delay bandwidth estimate (RTT * BW).
548 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000549 // kbits/s * ms = bits => bits/8 = bytes
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000550 WebRtc_UWord32 target_bytes =
551 (static_cast<WebRtc_UWord32>(target_send_bitrate_) * avg_rtt) >> 3;
552 if (bytes_re_sent > target_bytes) {
553 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000554 }
555 }
556 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000557 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000558 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000559 UpdateNACKBitRate(bytes_re_sent, now);
560 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000561 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000562}
563
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000564bool RTPSender::ProcessNACKBitRate(const WebRtc_UWord32 now) {
565 WebRtc_UWord32 num = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000566 WebRtc_Word32 byte_count = 0;
567 const WebRtc_UWord32 avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000568
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000569 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000570
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000571 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000572 return true;
573 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000574 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
575 if ((now - nack_byte_count_times_[num]) > avg_interval) {
576 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000577 break;
578 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000579 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000580 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000581 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000582 WebRtc_Word32 time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000583 if (num == NACK_BYTECOUNT_SIZE) {
584 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000585 // during the last msg_interval.
586 time_interval = now - nack_byte_count_times_[num - 1];
587 if (time_interval < 0) {
588 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000589 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000590 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000591 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000592}
593
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000594void RTPSender::UpdateNACKBitRate(const WebRtc_UWord32 bytes,
595 const WebRtc_UWord32 now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000596 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000597
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000598 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000599 if (bytes > 0) {
600 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000601 // Add padding length.
602 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000603 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000604 if (nack_byte_count_times_[0] == 0) {
605 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000606 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000607 // Shift.
608 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
609 nack_byte_count_[i + 1] = nack_byte_count_[i];
610 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000611 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000612 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000613 nack_byte_count_[0] = bytes;
614 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000615 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000616 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000617}
618
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000619void RTPSender::TimeToSendPacket(uint16_t sequence_number,
620 int64_t capture_time_ms) {
621 StorageType type;
622 uint16_t length = IP_PACKET_SIZE;
623 uint8_t data_buffer[IP_PACKET_SIZE];
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000624 int64_t stored_time_ms; // TODO(pwestin) can we deprecate this?
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000625
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000626 if (packet_history_ == NULL) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000627 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000628 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000629 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
630 &stored_time_ms, &type)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000631 assert(false);
632 return;
633 }
634 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000635
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000636 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000637 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000638 rtp_parser.Parse(rtp_header);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000639
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000640 int64_t diff_ms = clock_->TimeInMilliseconds() - capture_time_ms;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000641 if (UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms)) {
642 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000643 packet_history_->ReplaceRTPHeader(data_buffer,
644 rtp_header.header.sequenceNumber,
645 rtp_header.header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000646 }
647 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000648 if (transport_) {
649 bytes_sent = transport_->SendPacket(id_, data_buffer, length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000650 }
651 if (bytes_sent <= 0) {
652 return;
653 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000654 // Update send statistics.
655 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000656 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000657 packets_sent_++;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000658 if (bytes_sent > rtp_header.header.headerLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000659 payload_bytes_sent_ += bytes_sent - rtp_header.header.headerLength;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000660 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000661}
662
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000663// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
664WebRtc_Word32 RTPSender::SendToNetwork(
665 uint8_t *buffer, int payload_length, int rtp_header_length,
666 int64_t capture_time_ms, StorageType storage) {
667 ModuleRTPUtility::RTPHeaderParser rtp_parser(
668 buffer, payload_length + rtp_header_length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000669 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000670 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000671
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000672 // |capture_time_ms| <= 0 is considered invalid.
673 // TODO(holmer): This should be changed all over Video Engine so that negative
674 // time is consider invalid, while 0 is considered a valid time.
675 if (capture_time_ms > 0) {
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000676 int64_t time_now = clock_->TimeInMilliseconds();
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000677 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
678 rtp_header, time_now - capture_time_ms);
679 }
680 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000681 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
682 max_payload_length_, capture_time_ms,
683 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000684 return -1;
685 }
686 if (paced_sender_) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000687 if (!paced_sender_->SendPacket(
688 PacedSender::kNormalPriority, rtp_header.header.ssrc,
689 rtp_header.header.sequenceNumber, capture_time_ms,
690 payload_length + rtp_header_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000691 // We can't send the packet right now.
692 // We will be called when it is time.
693 return payload_length + rtp_header_length;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000694 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000695 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000696 // Send packet.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000697 WebRtc_Word32 bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000698 if (transport_) {
699 bytes_sent = transport_->SendPacket(id_, buffer,
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000700 payload_length + rtp_header_length);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000701 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000702 if (bytes_sent <= 0) {
703 return -1;
704 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000705 // Update send statistics.
706 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000707 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000708 packets_sent_++;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000709 if (bytes_sent > rtp_header_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000710 payload_bytes_sent_ += bytes_sent - rtp_header_length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000711 }
712 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000713}
714
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000715void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000716 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000717 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000718 nack_bitrate_.Process();
719 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000720 return;
721 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000722 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000723}
724
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000725WebRtc_UWord16 RTPSender::RTPHeaderLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000726 WebRtc_UWord16 rtp_header_length = 12;
727 if (include_csrcs_) {
728 rtp_header_length += sizeof(WebRtc_UWord32) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000729 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000730 rtp_header_length += RtpHeaderExtensionTotalLength();
731 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000732}
733
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000734WebRtc_UWord16 RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000735 CriticalSectionScoped cs(send_critsect_);
736 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000737}
738
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000739void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000740 packets_sent_ = 0;
741 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000742}
743
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000744WebRtc_UWord32 RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000745 // Don't use critsect to avoid potential deadlock.
746 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000747}
748
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000749// Number of sent RTP bytes.
750// Don't use critsect to avoid potental deadlock.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000751WebRtc_UWord32 RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000752 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000753}
754
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000755WebRtc_Word32 RTPSender::BuildRTPheader(
756 WebRtc_UWord8 *data_buffer, const WebRtc_Word8 payload_type,
757 const bool marker_bit, const WebRtc_UWord32 capture_time_stamp,
758 const bool time_stamp_provided, const bool inc_sequence_number) {
759 assert(payload_type >= 0);
760 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000761
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000762 data_buffer[0] = static_cast<WebRtc_UWord8>(0x80); // version 2.
763 data_buffer[1] = static_cast<WebRtc_UWord8>(payload_type);
764 if (marker_bit) {
765 data_buffer[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000766 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000767 if (time_stamp_provided) {
768 time_stamp_ = start_time_stamp_ + capture_time_stamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000769 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000770 // Make a unique time stamp.
771 // We can't inc by the actual time, since then we increase the risk of back
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000772 // timing.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000773 time_stamp_++;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000774 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000775 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + 2, sequence_number_);
776 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 4, time_stamp_);
777 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 8, ssrc_);
778 WebRtc_Word32 rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000779
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000780 // Add the CSRCs if any.
781 if (include_csrcs_ && csrcs_ > 0) {
782 if (csrcs_ > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000783 // error
784 assert(false);
785 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000786 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000787 WebRtc_UWord8 *ptr = &data_buffer[rtp_header_length];
788 for (WebRtc_UWord32 i = 0; i < csrcs_; ++i) {
789 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrc_[i]);
790 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000791 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000792 data_buffer[0] = (data_buffer[0] & 0xf0) | csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000793
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000794 // Update length of header.
795 rtp_header_length += sizeof(WebRtc_UWord32) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000796 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000797 sequence_number_++; // Prepare for next packet.
niklase@google.com470e71d2011-07-07 08:21:25 +0000798
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000799 WebRtc_UWord16 len = BuildRTPHeaderExtension(data_buffer + rtp_header_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000800 if (len) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000801 data_buffer[0] |= 0x10; // Set extension bit.
802 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000803 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000804 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000805}
806
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000807WebRtc_UWord16 RTPSender::BuildRTPHeaderExtension(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000808 WebRtc_UWord8 *data_buffer) const {
809 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000810 return 0;
811 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000812 // RTP header extension, RFC 3550.
813 // 0 1 2 3
814 // 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
815 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
816 // | defined by profile | length |
817 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
818 // | header extension |
819 // | .... |
820 //
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000821 const WebRtc_UWord32 kPosLength = 2;
822 const WebRtc_UWord32 kHeaderLength = RTP_ONE_BYTE_HEADER_LENGTH_IN_BYTES;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000823
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000824 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000825 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000826 RTP_ONE_BYTE_HEADER_EXTENSION);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000827
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000828 // Add extensions.
829 WebRtc_UWord16 total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000830
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000831 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000832 while (type != kRtpExtensionNone) {
833 WebRtc_UWord8 block_length = 0;
834 if (type == kRtpExtensionTransmissionTimeOffset) {
835 block_length = BuildTransmissionTimeOffsetExtension(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000836 data_buffer + kHeaderLength + total_block_length);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000837 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000838 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000839 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000840 }
841 if (total_block_length == 0) {
842 // No extension added.
843 return 0;
844 }
845 // Set header length (in number of Word32, header excluded).
846 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000847 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000848 total_block_length / 4);
849 // Total added length.
850 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000851}
852
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000853WebRtc_UWord8 RTPSender::BuildTransmissionTimeOffsetExtension(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000854 WebRtc_UWord8* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000855 // From RFC 5450: Transmission Time Offsets in RTP Streams.
856 //
857 // The transmission time is signaled to the receiver in-band using the
858 // general mechanism for RTP header extensions [RFC5285]. The payload
859 // of this extension (the transmitted value) is a 24-bit signed integer.
860 // When added to the RTP timestamp of the packet, it represents the
861 // "effective" RTP transmission time of the packet, on the RTP
862 // timescale.
863 //
864 // The form of the transmission offset extension block:
865 //
866 // 0 1 2 3
867 // 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
868 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
869 // | ID | len=2 | transmission offset |
870 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000871
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000872 // Get id defined by user.
873 WebRtc_UWord8 id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000874 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
875 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000876 // Not registered.
877 return 0;
878 }
879 int pos = 0;
880 const WebRtc_UWord8 len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000881 data_buffer[pos++] = (id << 4) + len;
882 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
883 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000884 pos += 3;
885 assert(pos == TRANSMISSION_TIME_OFFSET_LENGTH_IN_BYTES);
886 return TRANSMISSION_TIME_OFFSET_LENGTH_IN_BYTES;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000887}
888
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000889bool RTPSender::UpdateTransmissionTimeOffset(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000890 WebRtc_UWord8 *rtp_packet, const WebRtc_UWord16 rtp_packet_length,
891 const WebRtcRTPHeader &rtp_header, const WebRtc_Word64 time_diff_ms) const {
892 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000893
894 // Get length until start of transmission block.
895 int transmission_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000896 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000897 kRtpExtensionTransmissionTimeOffset);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000898 if (transmission_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000899 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000900 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000901 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000902 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000903 int block_pos = 12 + rtp_header.header.numCSRCs + transmission_block_pos;
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +0000904 if (rtp_packet_length < block_pos + 4 ||
905 rtp_header.header.headerLength < block_pos + 4) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000906 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000907 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000908 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000909 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000910 // Verify that header contains extension.
911 if (!((rtp_packet[12 + rtp_header.header.numCSRCs] == 0xBE) &&
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000912 (rtp_packet[12 + rtp_header.header.numCSRCs + 1] == 0xDE))) {
913 WEBRTC_TRACE(
914 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000915 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000916 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000917 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000918 // Get id.
919 WebRtc_UWord8 id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000920 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
921 &id) != 0) {
922 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000923 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000924 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000925 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000926 // Verify first byte in block.
927 const WebRtc_UWord8 first_block_byte = (id << 4) + 2;
928 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000929 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000930 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000931 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000932 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000933 // Update transmission offset field.
934 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +0000935 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000936 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000937}
938
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000939void RTPSender::SetSendingStatus(const bool enabled) {
940 if (enabled) {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000941 WebRtc_UWord32 frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000942 if (audio_configured_) {
943 WebRtc_UWord32 frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000944
945 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000946 switch (frequency) {
947 case 8000:
948 case 12000:
949 case 16000:
950 case 24000:
951 case 32000:
952 break;
953 default:
954 assert(false);
955 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000956 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000957 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000958 } else {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000959 frequency_hz = kDefaultVideoFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000960 }
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000961 WebRtc_UWord32 RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_,
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000962 frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000963
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000964 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000965 SetStartTimestamp(RTPtime, false);
966 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000967 if (!ssrc_forced_) {
968 // Generate a new SSRC.
969 ssrc_db_.ReturnSSRC(ssrc_);
970 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000971 }
972 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000973 if (!sequence_number_forced_ && !ssrc_forced_) {
974 // Generate a new sequence number.
975 sequence_number_ =
976 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000977 }
978 }
979}
980
981void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000982 CriticalSectionScoped cs(send_critsect_);
983 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000984}
985
986bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000987 CriticalSectionScoped cs(send_critsect_);
988 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000989}
990
991WebRtc_UWord32 RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000992 CriticalSectionScoped cs(send_critsect_);
993 return time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000994}
995
996void RTPSender::SetStartTimestamp(WebRtc_UWord32 timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000997 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000998 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000999 start_time_stamp_forced_ = force;
1000 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001001 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001002 if (!start_time_stamp_forced_) {
1003 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001004 }
1005 }
1006}
1007
1008WebRtc_UWord32 RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001009 CriticalSectionScoped cs(send_critsect_);
1010 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001011}
1012
1013WebRtc_UWord32 RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001014 // If configured via API, return 0.
1015 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001016
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001017 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001018 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001019 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001020 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1021 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001022}
1023
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001024void RTPSender::SetSSRC(WebRtc_UWord32 ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001025 // This is configured via the API.
1026 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001027
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001028 if (ssrc_ == ssrc && ssrc_forced_) {
1029 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001030 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001031 ssrc_forced_ = true;
1032 ssrc_db_.ReturnSSRC(ssrc_);
1033 ssrc_db_.RegisterSSRC(ssrc);
1034 ssrc_ = ssrc;
1035 if (!sequence_number_forced_) {
1036 sequence_number_ =
1037 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001038 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001039}
1040
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001041WebRtc_UWord32 RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001042 CriticalSectionScoped cs(send_critsect_);
1043 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001044}
1045
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001046void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001047 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001048}
1049
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001050void RTPSender::SetCSRCs(const WebRtc_UWord32 arr_of_csrc[kRtpCsrcSize],
1051 const WebRtc_UWord8 arr_length) {
1052 assert(arr_length <= kRtpCsrcSize);
1053 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001054
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001055 for (int i = 0; i < arr_length; i++) {
1056 csrc_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001057 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001058 csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001059}
1060
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001061WebRtc_Word32 RTPSender::CSRCs(WebRtc_UWord32 arr_of_csrc[kRtpCsrcSize]) const {
1062 assert(arr_of_csrc);
1063 CriticalSectionScoped cs(send_critsect_);
1064 for (int i = 0; i < csrcs_ && i < kRtpCsrcSize; i++) {
1065 arr_of_csrc[i] = csrc_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001066 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001067 return csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001068}
1069
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001070void RTPSender::SetSequenceNumber(WebRtc_UWord16 seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001071 CriticalSectionScoped cs(send_critsect_);
1072 sequence_number_forced_ = true;
1073 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001074}
1075
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001076WebRtc_UWord16 RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001077 CriticalSectionScoped cs(send_critsect_);
1078 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001079}
1080
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001081// Audio.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001082WebRtc_Word32 RTPSender::SendTelephoneEvent(const WebRtc_UWord8 key,
1083 const WebRtc_UWord16 time_ms,
1084 const WebRtc_UWord8 level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001085 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001086 return -1;
1087 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001088 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001089}
1090
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001091bool RTPSender::SendTelephoneEventActive(WebRtc_Word8 *telephone_event) const {
1092 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001093 return false;
1094 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001095 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001096}
1097
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001098WebRtc_Word32 RTPSender::SetAudioPacketSize(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001099 const WebRtc_UWord16 packet_size_samples) {
1100 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001101 return -1;
1102 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001103 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001104}
1105
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001106WebRtc_Word32 RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1107 const WebRtc_UWord8 ID) {
1108 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001109 return -1;
1110 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001111 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001112}
1113
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001114WebRtc_Word32 RTPSender::AudioLevelIndicationStatus(bool *enable,
1115 WebRtc_UWord8* id) const {
1116 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001117}
1118
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001119WebRtc_Word32 RTPSender::SetAudioLevel(const WebRtc_UWord8 level_d_bov) {
1120 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001121}
1122
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001123WebRtc_Word32 RTPSender::SetRED(const WebRtc_Word8 payload_type) {
1124 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001125 return -1;
1126 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001127 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001128}
1129
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001130WebRtc_Word32 RTPSender::RED(WebRtc_Word8 *payload_type) const {
1131 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001132 return -1;
1133 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001134 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001135}
1136
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001137// Video
1138VideoCodecInformation *RTPSender::CodecInformationVideo() {
1139 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001140 return NULL;
1141 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001142 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001143}
1144
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001145RtpVideoCodecTypes RTPSender::VideoCodecType() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001146 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001147 return kRtpNoVideo;
1148 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001149 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001150}
1151
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001152WebRtc_UWord32 RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001153 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001154 return 0;
1155 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001156 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001157}
1158
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001159WebRtc_Word32 RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001160 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001161 return -1;
1162 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001163 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001164}
1165
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001166WebRtc_Word32 RTPSender::SetGenericFECStatus(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001167 const bool enable, const WebRtc_UWord8 payload_type_red,
1168 const WebRtc_UWord8 payload_type_fec) {
1169 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001170 return -1;
1171 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001172 return video_->SetGenericFECStatus(enable, payload_type_red,
1173 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001174}
1175
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001176WebRtc_Word32 RTPSender::GenericFECStatus(
1177 bool *enable, WebRtc_UWord8 *payload_type_red,
1178 WebRtc_UWord8 *payload_type_fec) const {
1179 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001180 return -1;
1181 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001182 return video_->GenericFECStatus(
1183 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001184}
1185
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001186WebRtc_Word32 RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001187 const FecProtectionParams *delta_params,
1188 const FecProtectionParams *key_params) {
1189 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001190 return -1;
1191 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001192 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001193}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001194
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001195} // namespace webrtc