blob: 50cf9f9bc5acfdc671cffb48bcde32a1a8eff32e [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()),
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000041 remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false),
42 time_stamp_(0), csrcs_(0), csrc_(), include_csrcs_(true),
43 rtx_(kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000044 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.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000050 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
51 // Random start, 16 bits. Can't be 0.
52 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
53 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +000054
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000055 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000056 audio_ = new RTPSenderAudio(id, clock_, this);
57 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000058 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000059 video_ = new RTPSenderVideo(id, clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000060 }
61 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
63
pwestin@webrtc.org00741872012-01-19 15:56:10 +000064RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000065 if (remote_ssrc_ != 0) {
66 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000067 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000068 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +000069
pwestin@webrtc.org00741872012-01-19 15:56:10 +000070 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000071 delete send_critsect_;
72 while (!payload_type_map_.empty()) {
73 std::map<WebRtc_Word8, ModuleRTPUtility::Payload *>::iterator it =
74 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +000075 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000076 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000077 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000078 delete packet_history_;
79 delete audio_;
80 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +000081
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000082 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
niklase@google.com470e71d2011-07-07 08:21:25 +000084
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000085void RTPSender::SetTargetSendBitrate(const WebRtc_UWord32 bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000086 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +000088
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000089WebRtc_UWord16 RTPSender::ActualSendBitrateKbit() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000090 return (WebRtc_UWord16)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +000091}
92
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000093WebRtc_UWord32 RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000094 if (video_) {
95 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000096 }
97 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +000098}
99
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000100WebRtc_UWord32 RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000101 if (video_) {
102 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000103 }
104 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000105}
106
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000107WebRtc_UWord32 RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000108 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000109}
110
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000111WebRtc_Word32 RTPSender::SetTransmissionTimeOffset(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000112 const WebRtc_Word32 transmission_time_offset) {
113 if (transmission_time_offset > (0x800000 - 1) ||
114 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000115 return -1;
116 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000117 CriticalSectionScoped cs(send_critsect_);
118 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000119 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000120}
121
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000122WebRtc_Word32 RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
123 const WebRtc_UWord8 id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000124 CriticalSectionScoped cs(send_critsect_);
125 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000126}
127
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000128WebRtc_Word32 RTPSender::DeregisterRtpHeaderExtension(
129 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000130 CriticalSectionScoped cs(send_critsect_);
131 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000132}
133
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000134WebRtc_UWord16 RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000135 CriticalSectionScoped cs(send_critsect_);
136 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000137}
138
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000139WebRtc_Word32 RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000140 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
141 const WebRtc_Word8 payload_number, const WebRtc_UWord32 frequency,
142 const WebRtc_UWord8 channels, const WebRtc_UWord32 rate) {
143 assert(payload_name);
144 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000146 std::map<WebRtc_Word8, ModuleRTPUtility::Payload *>::iterator it =
147 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000149 if (payload_type_map_.end() != it) {
150 // We already use this payload type.
151 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000152 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000153
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000154 // Check if it's the same as we already have.
155 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000156 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000157 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000158 payload->typeSpecific.Audio.frequency == frequency &&
159 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000160 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000161 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000162 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000163 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000164 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000165 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000166 return 0;
167 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000168 }
169 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000170 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000171 WebRtc_Word32 ret_val = -1;
172 ModuleRTPUtility::Payload *payload = NULL;
173 if (audio_configured_) {
174 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
175 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000176 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000177 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
178 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000179 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000180 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000181 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000182 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000183 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000184}
185
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000186WebRtc_Word32 RTPSender::DeRegisterSendPayload(
187 const WebRtc_Word8 payload_type) {
188 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000189
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000190 std::map<WebRtc_Word8, ModuleRTPUtility::Payload *>::iterator it =
191 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000192
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000193 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000194 return -1;
195 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000196 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000197 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000198 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000199 return 0;
200}
niklase@google.com470e71d2011-07-07 08:21:25 +0000201
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000202WebRtc_Word8 RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000203
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000204int RTPSender::SendPayloadFrequency() const { return audio_->AudioFrequency(); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000206WebRtc_Word32 RTPSender::SetMaxPayloadLength(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000207 const WebRtc_UWord16 max_payload_length,
208 const WebRtc_UWord16 packet_over_head) {
209 // Sanity check.
210 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
211 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
212 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000213 return -1;
214 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000215 CriticalSectionScoped cs(send_critsect_);
216 max_payload_length_ = max_payload_length;
217 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000218
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000219 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
220 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000221 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000222}
223
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000224WebRtc_UWord16 RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000225 if (audio_configured_) {
226 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000227 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000228 return max_payload_length_ - RTPHeaderLength() -
229 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
230 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000231 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000232}
233
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000234WebRtc_UWord16 RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000235 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000236}
237
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000238WebRtc_UWord16 RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000239
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000240void RTPSender::SetRTXStatus(const RtxMode mode, const bool set_ssrc,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000241 const WebRtc_UWord32 ssrc) {
242 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000243 rtx_ = mode;
244 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000245 if (set_ssrc) {
246 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000247 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000248 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000249 }
250 }
251}
252
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000253void RTPSender::RTXStatus(RtxMode* mode, WebRtc_UWord32 *SSRC) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000254 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000255 *mode = rtx_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000256 *SSRC = ssrc_rtx_;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000257}
258
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000259WebRtc_Word32 RTPSender::CheckPayloadType(const WebRtc_Word8 payload_type,
260 RtpVideoCodecTypes *video_type) {
261 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000262
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000263 if (payload_type < 0) {
264 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
265 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000266 return -1;
267 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000268 if (audio_configured_) {
269 WebRtc_Word8 red_pl_type = -1;
270 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000271 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000272 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000273 // And it's a match...
274 return 0;
275 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000276 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000277 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000278 if (payload_type_ == payload_type) {
279 if (!audio_configured_) {
280 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000281 }
282 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000283 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000284 std::map<WebRtc_Word8, ModuleRTPUtility::Payload *>::iterator it =
285 payload_type_map_.find(payload_type);
286 if (it == payload_type_map_.end()) {
287 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
288 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000289 return -1;
290 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000291 payload_type_ = payload_type;
292 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000293 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000294 if (!payload->audio && !audio_configured_) {
295 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
296 *video_type = payload->typeSpecific.Video.videoCodecType;
297 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000298 }
299 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000300}
301
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000302WebRtc_Word32 RTPSender::SendOutgoingData(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000303 const FrameType frame_type, const WebRtc_Word8 payload_type,
304 const WebRtc_UWord32 capture_timestamp, int64_t capture_time_ms,
305 const WebRtc_UWord8 *payload_data, const WebRtc_UWord32 payload_size,
306 const RTPFragmentationHeader *fragmentation,
307 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000308 {
309 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000310 CriticalSectionScoped cs(send_critsect_);
311 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000312 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000313 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000314 }
pbos@webrtc.org8911ce42013-03-18 16:39:03 +0000315 RtpVideoCodecTypes video_type = kRtpGenericVideo;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000316 if (CheckPayloadType(payload_type, &video_type) != 0) {
317 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
318 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000319 __FUNCTION__, payload_type);
320 return -1;
321 }
322
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000323 if (audio_configured_) {
324 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000325 frame_type == kFrameEmpty);
326
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000327 return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
328 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000329 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000330 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000331
332 if (frame_type == kFrameEmpty) {
333 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
334 capture_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000335 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000336 return video_->SendVideo(video_type, frame_type, payload_type,
337 capture_timestamp, capture_time_ms, payload_data,
338 payload_size, fragmentation, codec_info,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000339 rtp_type_hdr);
340 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000341}
342
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000343WebRtc_Word32 RTPSender::SendPaddingAccordingToBitrate(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000344 WebRtc_Word8 payload_type, WebRtc_UWord32 capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000345 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000346 // Current bitrate since last estimate(1 second) averaged with the
347 // estimate since then, to get the most up to date bitrate.
348 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000349 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000350 if (bitrate_diff <= 0) {
351 return 0;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000352 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000353 int bytes = 0;
354 if (current_bitrate == 0) {
355 // Start up phase. Send one 33.3 ms batch to start with.
356 bytes = (bitrate_diff / 8) / 30;
357 } else {
358 bytes = (bitrate_diff / 8);
359 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000360 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000361 if (bytes > bytes_cap) {
362 bytes = bytes_cap;
363 }
364 }
365 return SendPadData(payload_type, capture_timestamp, capture_time_ms, bytes);
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000366}
367
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000368WebRtc_Word32 RTPSender::SendPadData(
369 WebRtc_Word8 payload_type, WebRtc_UWord32 capture_timestamp,
370 int64_t capture_time_ms, WebRtc_Word32 bytes) {
371 // Drop this packet if we're not sending media packets.
372 if (!sending_media_) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000373 return 0;
374 }
375 // Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
376 int max_length = 224;
377 WebRtc_UWord8 data_buffer[IP_PACKET_SIZE];
378
379 for (; bytes > 0; bytes -= max_length) {
asapersson@webrtc.org63a34f42012-04-20 13:20:27 +0000380 int padding_bytes_in_packet = max_length;
381 if (bytes < max_length) {
382 padding_bytes_in_packet = (bytes + 16) & 0xffe0; // Keep our modulus 32.
383 }
384 if (padding_bytes_in_packet < 32) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000385 // Sanity don't send empty packets.
386 break;
asapersson@webrtc.org63a34f42012-04-20 13:20:27 +0000387 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000388 // Correct seq num, timestamp and payload type.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000389 int header_length = BuildRTPheader(
390 data_buffer, payload_type, false, // No markerbit.
391 capture_timestamp, true, // Timestamp provided.
392 true); // Increment sequence number.
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000393 data_buffer[0] |= 0x20; // Set padding bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000394 WebRtc_Word32 *data =
395 reinterpret_cast<WebRtc_Word32 *>(&(data_buffer[header_length]));
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000396
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000397 // Fill data buffer with random data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000398 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
399 data[j] = rand(); // NOLINT
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000400 }
401 // Set number of padding bytes in the last byte of the packet.
402 data_buffer[header_length + padding_bytes_in_packet - 1] =
403 padding_bytes_in_packet;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000404 // Send the packet.
405 if (0 > SendToNetwork(data_buffer, padding_bytes_in_packet, header_length,
406 capture_time_ms, kDontRetransmit)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000407 // Error sending the packet.
408 break;
409 }
410 }
411 if (bytes > 31) { // 31 due to our modulus 32.
412 // We did not manage to send all bytes.
413 return -1;
414 }
415 return 0;
416}
417
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000418void RTPSender::SetStorePacketsStatus(const bool enable,
419 const WebRtc_UWord16 number_to_store) {
420 packet_history_->SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000421}
422
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000423bool RTPSender::StorePackets() const { return packet_history_->StorePackets(); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000424
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000425WebRtc_Word32 RTPSender::ReSendPacket(WebRtc_UWord16 packet_id,
426 WebRtc_UWord32 min_resend_time) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000427 WebRtc_UWord16 length = IP_PACKET_SIZE;
428 WebRtc_UWord8 data_buffer[IP_PACKET_SIZE];
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000429 WebRtc_UWord8 *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000430
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000431 int64_t stored_time_in_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000432 StorageType type;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000433 bool found = packet_history_->GetRTPPacket(packet_id, min_resend_time,
434 data_buffer, &length,
435 &stored_time_in_ms, &type);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000436 if (!found) {
437 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000438 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000439 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000440 if (length == 0 || type == kDontRetransmit) {
441 // No bytes copied (packet recently resent, skip resending) or
442 // packet should not be retransmitted.
443 return 0;
444 }
pwestin@webrtc.orgb30f0ed2012-01-23 16:23:31 +0000445 WebRtc_UWord8 data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000446 if (rtx_ != kRtxOff) {
447 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000448 buffer_to_send_ptr = data_buffer_rtx;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000449 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000450
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000451 WebRtc_Word32 bytes_sent = ReSendToNetwork(buffer_to_send_ptr, length);
452 if (bytes_sent <= 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000453 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000454 "Transport failed to resend packet_id %u", packet_id);
455 return -1;
456 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000457 // Store the time when the packet was last resent.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000458 packet_history_->UpdateResendTime(packet_id);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000459 return bytes_sent;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000460}
461
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000462WebRtc_Word32 RTPSender::ReSendToNetwork(const WebRtc_UWord8 *packet,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000463 const WebRtc_UWord32 size) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000464 WebRtc_Word32 bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000465 if (transport_) {
466 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000467 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000468 if (bytes_sent <= 0) {
469 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000470 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000471 // Update send statistics.
472 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000473 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000474 packets_sent_++;
475 // We on purpose don't add to payload_bytes_sent_ since this is a
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000476 // re-transmit and not new payload data.
477 return bytes_sent;
niklase@google.com470e71d2011-07-07 08:21:25 +0000478}
479
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000480int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000481 if (!video_)
482 return -1;
483 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000484}
485
486int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000487 if (!video_)
488 return -1;
489 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000490}
491
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000492void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000493 const std::list<uint16_t>& nack_sequence_numbers,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000494 const WebRtc_UWord16 avg_rtt) {
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000495 const WebRtc_Word64 now = clock_->TimeInMilliseconds();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000496 WebRtc_UWord32 bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000497
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000498 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000499 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000500 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000501 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000502 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000503 return;
504 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000505
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000506 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
507 it != nack_sequence_numbers.end(); ++it) {
508 const WebRtc_Word32 bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000509 if (bytes_sent > 0) {
510 bytes_re_sent += bytes_sent;
511 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000512 // The packet has previously been resent.
513 // Try resending next packet in the list.
514 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000515 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000516 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000517 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000518 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000519 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000520 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000521 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000522 // Delay bandwidth estimate (RTT * BW).
523 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000524 // kbits/s * ms = bits => bits/8 = bytes
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000525 WebRtc_UWord32 target_bytes =
526 (static_cast<WebRtc_UWord32>(target_send_bitrate_) * avg_rtt) >> 3;
527 if (bytes_re_sent > target_bytes) {
528 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000529 }
530 }
531 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000532 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000533 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000534 UpdateNACKBitRate(bytes_re_sent, now);
535 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000536 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000537}
538
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000539bool RTPSender::ProcessNACKBitRate(const WebRtc_UWord32 now) {
540 WebRtc_UWord32 num = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000541 WebRtc_Word32 byte_count = 0;
542 const WebRtc_UWord32 avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000543
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000544 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000545
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000546 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000547 return true;
548 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000549 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
550 if ((now - nack_byte_count_times_[num]) > avg_interval) {
551 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000552 break;
553 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000554 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000555 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000556 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000557 WebRtc_Word32 time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000558 if (num == NACK_BYTECOUNT_SIZE) {
559 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000560 // during the last msg_interval.
561 time_interval = now - nack_byte_count_times_[num - 1];
562 if (time_interval < 0) {
563 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000564 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000565 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000566 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000567}
568
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000569void RTPSender::UpdateNACKBitRate(const WebRtc_UWord32 bytes,
570 const WebRtc_UWord32 now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000571 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000572
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000573 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000574 if (bytes > 0) {
575 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000576 // Add padding length.
577 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000578 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000579 if (nack_byte_count_times_[0] == 0) {
580 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000581 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000582 // Shift.
583 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
584 nack_byte_count_[i + 1] = nack_byte_count_[i];
585 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000586 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000587 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000588 nack_byte_count_[0] = bytes;
589 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000590 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000591 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000592}
593
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000594void RTPSender::TimeToSendPacket(uint16_t sequence_number,
595 int64_t capture_time_ms) {
596 StorageType type;
597 uint16_t length = IP_PACKET_SIZE;
598 uint8_t data_buffer[IP_PACKET_SIZE];
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000599 int64_t stored_time_ms; // TODO(pwestin) can we deprecate this?
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000600
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000601 if (packet_history_ == NULL) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000602 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000603 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000604 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
605 &stored_time_ms, &type)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000606 assert(false);
607 return;
608 }
609 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000610
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000611 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000612 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000613 rtp_parser.Parse(rtp_header);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000614
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000615 int64_t diff_ms = clock_->TimeInMilliseconds() - capture_time_ms;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000616 if (UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms)) {
617 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000618 packet_history_->ReplaceRTPHeader(data_buffer,
619 rtp_header.header.sequenceNumber,
620 rtp_header.header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000621 }
622 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000623 if (transport_) {
624 bytes_sent = transport_->SendPacket(id_, data_buffer, length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000625 }
626 if (bytes_sent <= 0) {
627 return;
628 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000629 // Update send statistics.
630 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000631 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000632 packets_sent_++;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000633 if (bytes_sent > rtp_header.header.headerLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000634 payload_bytes_sent_ += bytes_sent - rtp_header.header.headerLength;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000635 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000636}
637
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000638// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
639WebRtc_Word32 RTPSender::SendToNetwork(
640 uint8_t *buffer, int payload_length, int rtp_header_length,
641 int64_t capture_time_ms, StorageType storage) {
642 ModuleRTPUtility::RTPHeaderParser rtp_parser(
643 buffer, payload_length + rtp_header_length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000644 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000645 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000646
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000647 // |capture_time_ms| <= 0 is considered invalid.
648 // TODO(holmer): This should be changed all over Video Engine so that negative
649 // time is consider invalid, while 0 is considered a valid time.
650 if (capture_time_ms > 0) {
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000651 int64_t time_now = clock_->TimeInMilliseconds();
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000652 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
653 rtp_header, time_now - capture_time_ms);
654 }
655 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000656 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
657 max_payload_length_, capture_time_ms,
658 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000659 return -1;
660 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000661
662 WebRtc_Word32 bytes_sent = -1;
663 // Create and send RTX Packet.
664 if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
665 WebRtc_UWord16 length_rtx = payload_length + rtp_header_length;
666 WebRtc_UWord8 data_buffer_rtx[IP_PACKET_SIZE];
667 BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
668 if (transport_) {
669 bytes_sent += transport_->SendPacket(id_, data_buffer_rtx, length_rtx);
670 if (bytes_sent <= 0) {
671 return -1;
672 }
673 }
674 }
675
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000676 if (paced_sender_) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000677 if (!paced_sender_->SendPacket(
678 PacedSender::kNormalPriority, rtp_header.header.ssrc,
679 rtp_header.header.sequenceNumber, capture_time_ms,
680 payload_length + rtp_header_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000681 // We can't send the packet right now.
682 // We will be called when it is time.
683 return payload_length + rtp_header_length;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000684 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000685 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000686 // Send data packet.
687 bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000688 if (transport_) {
689 bytes_sent = transport_->SendPacket(id_, buffer,
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000690 payload_length + rtp_header_length);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000691 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000692 if (bytes_sent <= 0) {
693 return -1;
694 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000695 // Update send statistics.
696 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000697 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000698 packets_sent_++;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000699 if (bytes_sent > rtp_header_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000700 payload_bytes_sent_ += bytes_sent - rtp_header_length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000701 }
702 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000703}
704
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000705void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000706 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000707 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000708 nack_bitrate_.Process();
709 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000710 return;
711 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000712 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000713}
714
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000715WebRtc_UWord16 RTPSender::RTPHeaderLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000716 WebRtc_UWord16 rtp_header_length = 12;
717 if (include_csrcs_) {
718 rtp_header_length += sizeof(WebRtc_UWord32) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000719 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000720 rtp_header_length += RtpHeaderExtensionTotalLength();
721 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000722}
723
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000724WebRtc_UWord16 RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000725 CriticalSectionScoped cs(send_critsect_);
726 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000727}
728
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000729void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000730 packets_sent_ = 0;
731 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000732}
733
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000734WebRtc_UWord32 RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000735 // Don't use critsect to avoid potential deadlock.
736 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000737}
738
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000739// Number of sent RTP bytes.
740// Don't use critsect to avoid potental deadlock.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000741WebRtc_UWord32 RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000742 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000743}
744
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000745WebRtc_Word32 RTPSender::BuildRTPheader(
746 WebRtc_UWord8 *data_buffer, const WebRtc_Word8 payload_type,
747 const bool marker_bit, const WebRtc_UWord32 capture_time_stamp,
748 const bool time_stamp_provided, const bool inc_sequence_number) {
749 assert(payload_type >= 0);
750 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000751
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000752 data_buffer[0] = static_cast<WebRtc_UWord8>(0x80); // version 2.
753 data_buffer[1] = static_cast<WebRtc_UWord8>(payload_type);
754 if (marker_bit) {
755 data_buffer[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000756 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000757 if (time_stamp_provided) {
758 time_stamp_ = start_time_stamp_ + capture_time_stamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000759 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000760 // Make a unique time stamp.
761 // We can't inc by the actual time, since then we increase the risk of back
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000762 // timing.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000763 time_stamp_++;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000764 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000765 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + 2, sequence_number_);
766 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 4, time_stamp_);
767 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 8, ssrc_);
768 WebRtc_Word32 rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000769
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000770 // Add the CSRCs if any.
771 if (include_csrcs_ && csrcs_ > 0) {
772 if (csrcs_ > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000773 // error
774 assert(false);
775 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000776 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000777 WebRtc_UWord8 *ptr = &data_buffer[rtp_header_length];
778 for (WebRtc_UWord32 i = 0; i < csrcs_; ++i) {
779 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrc_[i]);
780 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000781 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000782 data_buffer[0] = (data_buffer[0] & 0xf0) | csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000783
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000784 // Update length of header.
785 rtp_header_length += sizeof(WebRtc_UWord32) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000786 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000787 sequence_number_++; // Prepare for next packet.
niklase@google.com470e71d2011-07-07 08:21:25 +0000788
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000789 WebRtc_UWord16 len = BuildRTPHeaderExtension(data_buffer + rtp_header_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000790 if (len) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000791 data_buffer[0] |= 0x10; // Set extension bit.
792 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000793 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000794 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000795}
796
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000797WebRtc_UWord16 RTPSender::BuildRTPHeaderExtension(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000798 WebRtc_UWord8 *data_buffer) const {
799 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000800 return 0;
801 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000802 // RTP header extension, RFC 3550.
803 // 0 1 2 3
804 // 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
805 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
806 // | defined by profile | length |
807 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
808 // | header extension |
809 // | .... |
810 //
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000811 const WebRtc_UWord32 kPosLength = 2;
812 const WebRtc_UWord32 kHeaderLength = RTP_ONE_BYTE_HEADER_LENGTH_IN_BYTES;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000813
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000814 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000815 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000816 RTP_ONE_BYTE_HEADER_EXTENSION);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000817
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000818 // Add extensions.
819 WebRtc_UWord16 total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000820
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000821 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000822 while (type != kRtpExtensionNone) {
823 WebRtc_UWord8 block_length = 0;
824 if (type == kRtpExtensionTransmissionTimeOffset) {
825 block_length = BuildTransmissionTimeOffsetExtension(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000826 data_buffer + kHeaderLength + total_block_length);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000827 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000828 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000829 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000830 }
831 if (total_block_length == 0) {
832 // No extension added.
833 return 0;
834 }
835 // Set header length (in number of Word32, header excluded).
836 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000837 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000838 total_block_length / 4);
839 // Total added length.
840 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000841}
842
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000843WebRtc_UWord8 RTPSender::BuildTransmissionTimeOffsetExtension(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000844 WebRtc_UWord8* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000845 // From RFC 5450: Transmission Time Offsets in RTP Streams.
846 //
847 // The transmission time is signaled to the receiver in-band using the
848 // general mechanism for RTP header extensions [RFC5285]. The payload
849 // of this extension (the transmitted value) is a 24-bit signed integer.
850 // When added to the RTP timestamp of the packet, it represents the
851 // "effective" RTP transmission time of the packet, on the RTP
852 // timescale.
853 //
854 // The form of the transmission offset extension block:
855 //
856 // 0 1 2 3
857 // 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
858 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
859 // | ID | len=2 | transmission offset |
860 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000861
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000862 // Get id defined by user.
863 WebRtc_UWord8 id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000864 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
865 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000866 // Not registered.
867 return 0;
868 }
869 int pos = 0;
870 const WebRtc_UWord8 len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000871 data_buffer[pos++] = (id << 4) + len;
872 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
873 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000874 pos += 3;
875 assert(pos == TRANSMISSION_TIME_OFFSET_LENGTH_IN_BYTES);
876 return TRANSMISSION_TIME_OFFSET_LENGTH_IN_BYTES;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000877}
878
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000879bool RTPSender::UpdateTransmissionTimeOffset(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000880 WebRtc_UWord8 *rtp_packet, const WebRtc_UWord16 rtp_packet_length,
881 const WebRtcRTPHeader &rtp_header, const WebRtc_Word64 time_diff_ms) const {
882 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000883
884 // Get length until start of transmission block.
885 int transmission_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000886 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000887 kRtpExtensionTransmissionTimeOffset);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000888 if (transmission_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000889 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000890 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000891 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000892 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000893 int block_pos = 12 + rtp_header.header.numCSRCs + transmission_block_pos;
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +0000894 if (rtp_packet_length < block_pos + 4 ||
895 rtp_header.header.headerLength < block_pos + 4) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000896 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000897 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000898 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000899 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000900 // Verify that header contains extension.
901 if (!((rtp_packet[12 + rtp_header.header.numCSRCs] == 0xBE) &&
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000902 (rtp_packet[12 + rtp_header.header.numCSRCs + 1] == 0xDE))) {
903 WEBRTC_TRACE(
904 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000905 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000906 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000907 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000908 // Get id.
909 WebRtc_UWord8 id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000910 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
911 &id) != 0) {
912 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000913 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000914 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000915 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000916 // Verify first byte in block.
917 const WebRtc_UWord8 first_block_byte = (id << 4) + 2;
918 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000919 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000920 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000921 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000922 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000923 // Update transmission offset field.
924 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +0000925 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000926 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000927}
928
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000929void RTPSender::SetSendingStatus(const bool enabled) {
930 if (enabled) {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000931 WebRtc_UWord32 frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000932 if (audio_configured_) {
933 WebRtc_UWord32 frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000934
935 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000936 switch (frequency) {
937 case 8000:
938 case 12000:
939 case 16000:
940 case 24000:
941 case 32000:
942 break;
943 default:
944 assert(false);
945 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000946 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000947 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000948 } else {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000949 frequency_hz = kDefaultVideoFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000950 }
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000951 WebRtc_UWord32 RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_,
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000952 frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000953
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000954 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000955 SetStartTimestamp(RTPtime, false);
956 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000957 if (!ssrc_forced_) {
958 // Generate a new SSRC.
959 ssrc_db_.ReturnSSRC(ssrc_);
960 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000961 }
962 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000963 if (!sequence_number_forced_ && !ssrc_forced_) {
964 // Generate a new sequence number.
965 sequence_number_ =
966 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000967 }
968 }
969}
970
971void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000972 CriticalSectionScoped cs(send_critsect_);
973 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000974}
975
976bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000977 CriticalSectionScoped cs(send_critsect_);
978 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000979}
980
981WebRtc_UWord32 RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000982 CriticalSectionScoped cs(send_critsect_);
983 return time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000984}
985
986void RTPSender::SetStartTimestamp(WebRtc_UWord32 timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000987 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000988 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000989 start_time_stamp_forced_ = force;
990 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000991 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000992 if (!start_time_stamp_forced_) {
993 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000994 }
995 }
996}
997
998WebRtc_UWord32 RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000999 CriticalSectionScoped cs(send_critsect_);
1000 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001001}
1002
1003WebRtc_UWord32 RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001004 // If configured via API, return 0.
1005 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001006
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001007 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001008 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001009 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001010 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1011 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001012}
1013
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001014void RTPSender::SetSSRC(WebRtc_UWord32 ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001015 // This is configured via the API.
1016 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001017
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001018 if (ssrc_ == ssrc && ssrc_forced_) {
1019 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001020 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001021 ssrc_forced_ = true;
1022 ssrc_db_.ReturnSSRC(ssrc_);
1023 ssrc_db_.RegisterSSRC(ssrc);
1024 ssrc_ = ssrc;
1025 if (!sequence_number_forced_) {
1026 sequence_number_ =
1027 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001028 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001029}
1030
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001031WebRtc_UWord32 RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001032 CriticalSectionScoped cs(send_critsect_);
1033 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001034}
1035
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001036void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001037 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001038}
1039
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001040void RTPSender::SetCSRCs(const WebRtc_UWord32 arr_of_csrc[kRtpCsrcSize],
1041 const WebRtc_UWord8 arr_length) {
1042 assert(arr_length <= kRtpCsrcSize);
1043 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001044
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001045 for (int i = 0; i < arr_length; i++) {
1046 csrc_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001047 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001048 csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001049}
1050
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001051WebRtc_Word32 RTPSender::CSRCs(WebRtc_UWord32 arr_of_csrc[kRtpCsrcSize]) const {
1052 assert(arr_of_csrc);
1053 CriticalSectionScoped cs(send_critsect_);
1054 for (int i = 0; i < csrcs_ && i < kRtpCsrcSize; i++) {
1055 arr_of_csrc[i] = csrc_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001056 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001057 return csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001058}
1059
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001060void RTPSender::SetSequenceNumber(WebRtc_UWord16 seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001061 CriticalSectionScoped cs(send_critsect_);
1062 sequence_number_forced_ = true;
1063 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001064}
1065
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001066WebRtc_UWord16 RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001067 CriticalSectionScoped cs(send_critsect_);
1068 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001069}
1070
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001071// Audio.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001072WebRtc_Word32 RTPSender::SendTelephoneEvent(const WebRtc_UWord8 key,
1073 const WebRtc_UWord16 time_ms,
1074 const WebRtc_UWord8 level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001075 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001076 return -1;
1077 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001078 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001079}
1080
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001081bool RTPSender::SendTelephoneEventActive(WebRtc_Word8 *telephone_event) const {
1082 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001083 return false;
1084 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001085 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001086}
1087
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001088WebRtc_Word32 RTPSender::SetAudioPacketSize(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001089 const WebRtc_UWord16 packet_size_samples) {
1090 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001091 return -1;
1092 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001093 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001094}
1095
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001096WebRtc_Word32 RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1097 const WebRtc_UWord8 ID) {
1098 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001099 return -1;
1100 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001101 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001102}
1103
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001104WebRtc_Word32 RTPSender::AudioLevelIndicationStatus(bool *enable,
1105 WebRtc_UWord8* id) const {
1106 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001107}
1108
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001109WebRtc_Word32 RTPSender::SetAudioLevel(const WebRtc_UWord8 level_d_bov) {
1110 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001111}
1112
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001113WebRtc_Word32 RTPSender::SetRED(const WebRtc_Word8 payload_type) {
1114 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001115 return -1;
1116 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001117 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001118}
1119
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001120WebRtc_Word32 RTPSender::RED(WebRtc_Word8 *payload_type) const {
1121 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001122 return -1;
1123 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001124 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001125}
1126
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001127// Video
1128VideoCodecInformation *RTPSender::CodecInformationVideo() {
1129 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001130 return NULL;
1131 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001132 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001133}
1134
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001135RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001136 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001137 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001138}
1139
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001140WebRtc_UWord32 RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001141 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001142 return 0;
1143 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001144 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001145}
1146
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001147WebRtc_Word32 RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001148 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001149 return -1;
1150 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001151 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001152}
1153
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001154WebRtc_Word32 RTPSender::SetGenericFECStatus(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001155 const bool enable, const WebRtc_UWord8 payload_type_red,
1156 const WebRtc_UWord8 payload_type_fec) {
1157 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001158 return -1;
1159 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001160 return video_->SetGenericFECStatus(enable, payload_type_red,
1161 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001162}
1163
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001164WebRtc_Word32 RTPSender::GenericFECStatus(
1165 bool *enable, WebRtc_UWord8 *payload_type_red,
1166 WebRtc_UWord8 *payload_type_fec) const {
1167 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001168 return -1;
1169 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001170 return video_->GenericFECStatus(
1171 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001172}
1173
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001174WebRtc_Word32 RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001175 const FecProtectionParams *delta_params,
1176 const FecProtectionParams *key_params) {
1177 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001178 return -1;
1179 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001180 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001181}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001182
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001183void RTPSender::BuildRtxPacket(WebRtc_UWord8* buffer, WebRtc_UWord16* length,
1184 WebRtc_UWord8* buffer_rtx) {
1185 CriticalSectionScoped cs(send_critsect_);
1186 WebRtc_UWord8* data_buffer_rtx = buffer_rtx;
1187 // Add RTX header.
1188 ModuleRTPUtility::RTPHeaderParser rtp_parser(
1189 reinterpret_cast<const WebRtc_UWord8 *>(buffer), *length);
1190
1191 WebRtcRTPHeader rtp_header;
1192 rtp_parser.Parse(rtp_header);
1193
1194 // Add original RTP header.
1195 memcpy(data_buffer_rtx, buffer, rtp_header.header.headerLength);
1196
1197 // Replace sequence number.
1198 WebRtc_UWord8 *ptr = data_buffer_rtx + 2;
1199 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1200
1201 // Replace SSRC.
1202 ptr += 6;
1203 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1204
1205 // Add OSN (original sequence number).
1206 ptr = data_buffer_rtx + rtp_header.header.headerLength;
1207 ModuleRTPUtility::AssignUWord16ToBuffer(ptr,
1208 rtp_header.header.sequenceNumber);
1209 ptr += 2;
1210
1211 // Add original payload data.
1212 memcpy(ptr, buffer + rtp_header.header.headerLength,
1213 *length - rtp_header.header.headerLength);
1214 *length += 2;
1215}
1216
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001217} // namespace webrtc