blob: 991f12ad69eb7ff34c7cd3f0073455e663912635 [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(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000517 const std::list<uint16_t>& nack_sequence_numbers,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000518 const WebRtc_UWord16 avg_rtt) {
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000519 const WebRtc_Word64 now = clock_->TimeInMilliseconds();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000520 WebRtc_UWord32 bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000521
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000522 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000523 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000524 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000525 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000526 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000527 return;
528 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000529
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000530 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
531 it != nack_sequence_numbers.end(); ++it) {
532 const WebRtc_Word32 bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000533 if (bytes_sent > 0) {
534 bytes_re_sent += bytes_sent;
535 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000536 // The packet has previously been resent.
537 // Try resending next packet in the list.
538 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000539 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000540 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000541 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000542 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000543 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000544 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000545 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000546 // Delay bandwidth estimate (RTT * BW).
547 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000548 // kbits/s * ms = bits => bits/8 = bytes
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000549 WebRtc_UWord32 target_bytes =
550 (static_cast<WebRtc_UWord32>(target_send_bitrate_) * avg_rtt) >> 3;
551 if (bytes_re_sent > target_bytes) {
552 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000553 }
554 }
555 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000556 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000557 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000558 UpdateNACKBitRate(bytes_re_sent, now);
559 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000560 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000561}
562
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000563bool RTPSender::ProcessNACKBitRate(const WebRtc_UWord32 now) {
564 WebRtc_UWord32 num = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000565 WebRtc_Word32 byte_count = 0;
566 const WebRtc_UWord32 avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000567
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000568 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000569
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000570 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000571 return true;
572 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000573 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
574 if ((now - nack_byte_count_times_[num]) > avg_interval) {
575 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000576 break;
577 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000578 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000579 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000580 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000581 WebRtc_Word32 time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000582 if (num == NACK_BYTECOUNT_SIZE) {
583 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000584 // during the last msg_interval.
585 time_interval = now - nack_byte_count_times_[num - 1];
586 if (time_interval < 0) {
587 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000588 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000589 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000590 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000591}
592
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000593void RTPSender::UpdateNACKBitRate(const WebRtc_UWord32 bytes,
594 const WebRtc_UWord32 now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000595 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000596
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000597 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000598 if (bytes > 0) {
599 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000600 // Add padding length.
601 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000602 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000603 if (nack_byte_count_times_[0] == 0) {
604 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000605 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000606 // Shift.
607 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
608 nack_byte_count_[i + 1] = nack_byte_count_[i];
609 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000610 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000611 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000612 nack_byte_count_[0] = bytes;
613 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000614 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000615 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000616}
617
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000618void RTPSender::TimeToSendPacket(uint16_t sequence_number,
619 int64_t capture_time_ms) {
620 StorageType type;
621 uint16_t length = IP_PACKET_SIZE;
622 uint8_t data_buffer[IP_PACKET_SIZE];
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000623 int64_t stored_time_ms; // TODO(pwestin) can we deprecate this?
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000624
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000625 if (packet_history_ == NULL) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000626 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000627 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000628 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
629 &stored_time_ms, &type)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000630 assert(false);
631 return;
632 }
633 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000634
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000635 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000636 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000637 rtp_parser.Parse(rtp_header);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000638
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000639 int64_t diff_ms = clock_->TimeInMilliseconds() - capture_time_ms;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000640 if (UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms)) {
641 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000642 packet_history_->ReplaceRTPHeader(data_buffer,
643 rtp_header.header.sequenceNumber,
644 rtp_header.header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000645 }
646 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000647 if (transport_) {
648 bytes_sent = transport_->SendPacket(id_, data_buffer, length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000649 }
650 if (bytes_sent <= 0) {
651 return;
652 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000653 // Update send statistics.
654 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000655 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000656 packets_sent_++;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000657 if (bytes_sent > rtp_header.header.headerLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000658 payload_bytes_sent_ += bytes_sent - rtp_header.header.headerLength;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000659 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000660}
661
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000662// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
663WebRtc_Word32 RTPSender::SendToNetwork(
664 uint8_t *buffer, int payload_length, int rtp_header_length,
665 int64_t capture_time_ms, StorageType storage) {
666 ModuleRTPUtility::RTPHeaderParser rtp_parser(
667 buffer, payload_length + rtp_header_length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000668 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000669 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000670
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000671 // |capture_time_ms| <= 0 is considered invalid.
672 // TODO(holmer): This should be changed all over Video Engine so that negative
673 // time is consider invalid, while 0 is considered a valid time.
674 if (capture_time_ms > 0) {
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000675 int64_t time_now = clock_->TimeInMilliseconds();
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000676 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
677 rtp_header, time_now - capture_time_ms);
678 }
679 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000680 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
681 max_payload_length_, capture_time_ms,
682 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000683 return -1;
684 }
685 if (paced_sender_) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000686 if (!paced_sender_->SendPacket(
687 PacedSender::kNormalPriority, rtp_header.header.ssrc,
688 rtp_header.header.sequenceNumber, capture_time_ms,
689 payload_length + rtp_header_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000690 // We can't send the packet right now.
691 // We will be called when it is time.
692 return payload_length + rtp_header_length;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000693 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000694 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000695 // Send packet.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000696 WebRtc_Word32 bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000697 if (transport_) {
698 bytes_sent = transport_->SendPacket(id_, buffer,
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000699 payload_length + rtp_header_length);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000700 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000701 if (bytes_sent <= 0) {
702 return -1;
703 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000704 // Update send statistics.
705 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000706 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000707 packets_sent_++;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000708 if (bytes_sent > rtp_header_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000709 payload_bytes_sent_ += bytes_sent - rtp_header_length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000710 }
711 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000712}
713
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000714void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000715 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000716 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000717 nack_bitrate_.Process();
718 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000719 return;
720 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000721 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000722}
723
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000724WebRtc_UWord16 RTPSender::RTPHeaderLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000725 WebRtc_UWord16 rtp_header_length = 12;
726 if (include_csrcs_) {
727 rtp_header_length += sizeof(WebRtc_UWord32) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000728 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000729 rtp_header_length += RtpHeaderExtensionTotalLength();
730 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000731}
732
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000733WebRtc_UWord16 RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000734 CriticalSectionScoped cs(send_critsect_);
735 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000736}
737
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000738void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000739 packets_sent_ = 0;
740 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000741}
742
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000743WebRtc_UWord32 RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000744 // Don't use critsect to avoid potential deadlock.
745 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000746}
747
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000748// Number of sent RTP bytes.
749// Don't use critsect to avoid potental deadlock.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000750WebRtc_UWord32 RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000751 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000752}
753
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000754WebRtc_Word32 RTPSender::BuildRTPheader(
755 WebRtc_UWord8 *data_buffer, const WebRtc_Word8 payload_type,
756 const bool marker_bit, const WebRtc_UWord32 capture_time_stamp,
757 const bool time_stamp_provided, const bool inc_sequence_number) {
758 assert(payload_type >= 0);
759 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000760
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000761 data_buffer[0] = static_cast<WebRtc_UWord8>(0x80); // version 2.
762 data_buffer[1] = static_cast<WebRtc_UWord8>(payload_type);
763 if (marker_bit) {
764 data_buffer[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000765 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000766 if (time_stamp_provided) {
767 time_stamp_ = start_time_stamp_ + capture_time_stamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000768 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000769 // Make a unique time stamp.
770 // We can't inc by the actual time, since then we increase the risk of back
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000771 // timing.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000772 time_stamp_++;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000773 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000774 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + 2, sequence_number_);
775 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 4, time_stamp_);
776 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 8, ssrc_);
777 WebRtc_Word32 rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000778
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000779 // Add the CSRCs if any.
780 if (include_csrcs_ && csrcs_ > 0) {
781 if (csrcs_ > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000782 // error
783 assert(false);
784 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000785 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000786 WebRtc_UWord8 *ptr = &data_buffer[rtp_header_length];
787 for (WebRtc_UWord32 i = 0; i < csrcs_; ++i) {
788 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrc_[i]);
789 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000790 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000791 data_buffer[0] = (data_buffer[0] & 0xf0) | csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000792
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000793 // Update length of header.
794 rtp_header_length += sizeof(WebRtc_UWord32) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000795 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000796 sequence_number_++; // Prepare for next packet.
niklase@google.com470e71d2011-07-07 08:21:25 +0000797
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000798 WebRtc_UWord16 len = BuildRTPHeaderExtension(data_buffer + rtp_header_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000799 if (len) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000800 data_buffer[0] |= 0x10; // Set extension bit.
801 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000802 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000803 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000804}
805
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000806WebRtc_UWord16 RTPSender::BuildRTPHeaderExtension(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000807 WebRtc_UWord8 *data_buffer) const {
808 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000809 return 0;
810 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000811 // RTP header extension, RFC 3550.
812 // 0 1 2 3
813 // 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
814 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
815 // | defined by profile | length |
816 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
817 // | header extension |
818 // | .... |
819 //
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000820 const WebRtc_UWord32 kPosLength = 2;
821 const WebRtc_UWord32 kHeaderLength = RTP_ONE_BYTE_HEADER_LENGTH_IN_BYTES;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000822
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000823 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000824 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000825 RTP_ONE_BYTE_HEADER_EXTENSION);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000826
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000827 // Add extensions.
828 WebRtc_UWord16 total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000829
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000830 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000831 while (type != kRtpExtensionNone) {
832 WebRtc_UWord8 block_length = 0;
833 if (type == kRtpExtensionTransmissionTimeOffset) {
834 block_length = BuildTransmissionTimeOffsetExtension(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000835 data_buffer + kHeaderLength + total_block_length);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000836 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000837 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000838 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000839 }
840 if (total_block_length == 0) {
841 // No extension added.
842 return 0;
843 }
844 // Set header length (in number of Word32, header excluded).
845 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000846 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000847 total_block_length / 4);
848 // Total added length.
849 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000850}
851
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000852WebRtc_UWord8 RTPSender::BuildTransmissionTimeOffsetExtension(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000853 WebRtc_UWord8* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000854 // From RFC 5450: Transmission Time Offsets in RTP Streams.
855 //
856 // The transmission time is signaled to the receiver in-band using the
857 // general mechanism for RTP header extensions [RFC5285]. The payload
858 // of this extension (the transmitted value) is a 24-bit signed integer.
859 // When added to the RTP timestamp of the packet, it represents the
860 // "effective" RTP transmission time of the packet, on the RTP
861 // timescale.
862 //
863 // The form of the transmission offset extension block:
864 //
865 // 0 1 2 3
866 // 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
867 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
868 // | ID | len=2 | transmission offset |
869 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000870
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000871 // Get id defined by user.
872 WebRtc_UWord8 id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000873 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
874 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000875 // Not registered.
876 return 0;
877 }
878 int pos = 0;
879 const WebRtc_UWord8 len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000880 data_buffer[pos++] = (id << 4) + len;
881 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
882 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000883 pos += 3;
884 assert(pos == TRANSMISSION_TIME_OFFSET_LENGTH_IN_BYTES);
885 return TRANSMISSION_TIME_OFFSET_LENGTH_IN_BYTES;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000886}
887
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000888bool RTPSender::UpdateTransmissionTimeOffset(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000889 WebRtc_UWord8 *rtp_packet, const WebRtc_UWord16 rtp_packet_length,
890 const WebRtcRTPHeader &rtp_header, const WebRtc_Word64 time_diff_ms) const {
891 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000892
893 // Get length until start of transmission block.
894 int transmission_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000895 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000896 kRtpExtensionTransmissionTimeOffset);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000897 if (transmission_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000898 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000899 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000900 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000901 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000902 int block_pos = 12 + rtp_header.header.numCSRCs + transmission_block_pos;
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +0000903 if (rtp_packet_length < block_pos + 4 ||
904 rtp_header.header.headerLength < block_pos + 4) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000905 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000906 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000907 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000908 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000909 // Verify that header contains extension.
910 if (!((rtp_packet[12 + rtp_header.header.numCSRCs] == 0xBE) &&
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000911 (rtp_packet[12 + rtp_header.header.numCSRCs + 1] == 0xDE))) {
912 WEBRTC_TRACE(
913 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000914 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000915 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000916 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000917 // Get id.
918 WebRtc_UWord8 id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000919 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
920 &id) != 0) {
921 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000922 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000923 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000924 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000925 // Verify first byte in block.
926 const WebRtc_UWord8 first_block_byte = (id << 4) + 2;
927 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000928 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000929 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000930 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000931 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000932 // Update transmission offset field.
933 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +0000934 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000935 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000936}
937
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000938void RTPSender::SetSendingStatus(const bool enabled) {
939 if (enabled) {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000940 WebRtc_UWord32 frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000941 if (audio_configured_) {
942 WebRtc_UWord32 frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000943
944 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000945 switch (frequency) {
946 case 8000:
947 case 12000:
948 case 16000:
949 case 24000:
950 case 32000:
951 break;
952 default:
953 assert(false);
954 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000955 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000956 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000957 } else {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000958 frequency_hz = kDefaultVideoFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000959 }
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000960 WebRtc_UWord32 RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_,
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000961 frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000962
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000963 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000964 SetStartTimestamp(RTPtime, false);
965 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000966 if (!ssrc_forced_) {
967 // Generate a new SSRC.
968 ssrc_db_.ReturnSSRC(ssrc_);
969 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000970 }
971 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000972 if (!sequence_number_forced_ && !ssrc_forced_) {
973 // Generate a new sequence number.
974 sequence_number_ =
975 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000976 }
977 }
978}
979
980void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000981 CriticalSectionScoped cs(send_critsect_);
982 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000983}
984
985bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000986 CriticalSectionScoped cs(send_critsect_);
987 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000988}
989
990WebRtc_UWord32 RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000991 CriticalSectionScoped cs(send_critsect_);
992 return time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000993}
994
995void RTPSender::SetStartTimestamp(WebRtc_UWord32 timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000996 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000997 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000998 start_time_stamp_forced_ = force;
999 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001000 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001001 if (!start_time_stamp_forced_) {
1002 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001003 }
1004 }
1005}
1006
1007WebRtc_UWord32 RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001008 CriticalSectionScoped cs(send_critsect_);
1009 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001010}
1011
1012WebRtc_UWord32 RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001013 // If configured via API, return 0.
1014 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001015
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001016 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001017 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001018 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001019 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1020 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001021}
1022
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001023void RTPSender::SetSSRC(WebRtc_UWord32 ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001024 // This is configured via the API.
1025 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001026
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001027 if (ssrc_ == ssrc && ssrc_forced_) {
1028 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001029 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001030 ssrc_forced_ = true;
1031 ssrc_db_.ReturnSSRC(ssrc_);
1032 ssrc_db_.RegisterSSRC(ssrc);
1033 ssrc_ = ssrc;
1034 if (!sequence_number_forced_) {
1035 sequence_number_ =
1036 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001037 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001038}
1039
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001040WebRtc_UWord32 RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001041 CriticalSectionScoped cs(send_critsect_);
1042 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001043}
1044
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001045void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001046 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001047}
1048
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001049void RTPSender::SetCSRCs(const WebRtc_UWord32 arr_of_csrc[kRtpCsrcSize],
1050 const WebRtc_UWord8 arr_length) {
1051 assert(arr_length <= kRtpCsrcSize);
1052 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001053
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001054 for (int i = 0; i < arr_length; i++) {
1055 csrc_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001056 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001057 csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001058}
1059
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001060WebRtc_Word32 RTPSender::CSRCs(WebRtc_UWord32 arr_of_csrc[kRtpCsrcSize]) const {
1061 assert(arr_of_csrc);
1062 CriticalSectionScoped cs(send_critsect_);
1063 for (int i = 0; i < csrcs_ && i < kRtpCsrcSize; i++) {
1064 arr_of_csrc[i] = csrc_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001065 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001066 return csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001067}
1068
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001069void RTPSender::SetSequenceNumber(WebRtc_UWord16 seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001070 CriticalSectionScoped cs(send_critsect_);
1071 sequence_number_forced_ = true;
1072 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001073}
1074
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001075WebRtc_UWord16 RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001076 CriticalSectionScoped cs(send_critsect_);
1077 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001078}
1079
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001080// Audio.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001081WebRtc_Word32 RTPSender::SendTelephoneEvent(const WebRtc_UWord8 key,
1082 const WebRtc_UWord16 time_ms,
1083 const WebRtc_UWord8 level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001084 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001085 return -1;
1086 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001087 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001088}
1089
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001090bool RTPSender::SendTelephoneEventActive(WebRtc_Word8 *telephone_event) const {
1091 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001092 return false;
1093 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001094 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001095}
1096
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001097WebRtc_Word32 RTPSender::SetAudioPacketSize(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001098 const WebRtc_UWord16 packet_size_samples) {
1099 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001100 return -1;
1101 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001102 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001103}
1104
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001105WebRtc_Word32 RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1106 const WebRtc_UWord8 ID) {
1107 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001108 return -1;
1109 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001110 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001111}
1112
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001113WebRtc_Word32 RTPSender::AudioLevelIndicationStatus(bool *enable,
1114 WebRtc_UWord8* id) const {
1115 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001116}
1117
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001118WebRtc_Word32 RTPSender::SetAudioLevel(const WebRtc_UWord8 level_d_bov) {
1119 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001120}
1121
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001122WebRtc_Word32 RTPSender::SetRED(const WebRtc_Word8 payload_type) {
1123 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001124 return -1;
1125 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001126 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001127}
1128
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001129WebRtc_Word32 RTPSender::RED(WebRtc_Word8 *payload_type) const {
1130 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001131 return -1;
1132 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001133 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001134}
1135
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001136// Video
1137VideoCodecInformation *RTPSender::CodecInformationVideo() {
1138 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001139 return NULL;
1140 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001141 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001142}
1143
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001144RtpVideoCodecTypes RTPSender::VideoCodecType() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001145 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001146 return kRtpNoVideo;
1147 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001148 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001149}
1150
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001151WebRtc_UWord32 RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001152 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001153 return 0;
1154 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001155 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001156}
1157
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001158WebRtc_Word32 RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001159 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001160 return -1;
1161 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001162 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001163}
1164
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001165WebRtc_Word32 RTPSender::SetGenericFECStatus(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001166 const bool enable, const WebRtc_UWord8 payload_type_red,
1167 const WebRtc_UWord8 payload_type_fec) {
1168 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001169 return -1;
1170 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001171 return video_->SetGenericFECStatus(enable, payload_type_red,
1172 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001173}
1174
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001175WebRtc_Word32 RTPSender::GenericFECStatus(
1176 bool *enable, WebRtc_UWord8 *payload_type_red,
1177 WebRtc_UWord8 *payload_type_fec) const {
1178 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001179 return -1;
1180 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001181 return video_->GenericFECStatus(
1182 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001183}
1184
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001185WebRtc_Word32 RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001186 const FecProtectionParams *delta_params,
1187 const FecProtectionParams *key_params) {
1188 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001189 return -1;
1190 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001191 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001192}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001193
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001194} // namespace webrtc