blob: 9c5cfe02efbbf98cf1a9bea4c159da08639c64f8 [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
pbos@webrtc.org2f446732013-04-08 11:08:41 +000024RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000025 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.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000048 srand(static_cast<uint32_t>(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()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000073 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000074 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +000085void RTPSender::SetTargetSendBitrate(const uint32_t 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +000089uint16_t RTPSender::ActualSendBitrateKbit() const {
90 return (uint16_t)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +000091}
92
pbos@webrtc.org2f446732013-04-08 11:08:41 +000093uint32_t 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000100uint32_t 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000107uint32_t 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000111int32_t RTPSender::SetTransmissionTimeOffset(
112 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000113 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000122int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
123 const uint8_t 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000128int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000129 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000134uint16_t 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000139int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000140 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000141 const int8_t payload_number, const uint32_t frequency,
142 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000143 assert(payload_name);
144 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000146 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000147 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 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000171 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000172 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000186int32_t RTPSender::DeRegisterSendPayload(
187 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000188 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000189
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000190 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000191 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000202int8_t 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000206int32_t RTPSender::SetMaxPayloadLength(
207 const uint16_t max_payload_length,
208 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000209 // 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000224uint16_t 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000234uint16_t 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000238uint16_t 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,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000241 const uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000242 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000253void RTPSender::RTXStatus(RtxMode* mode, uint32_t *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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000259int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
260 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000261 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_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000269 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000270 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 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000284 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000285 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000302int32_t RTPSender::SendOutgoingData(
303 const FrameType frame_type, const int8_t payload_type,
304 const uint32_t capture_timestamp, int64_t capture_time_ms,
305 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000306 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000343int32_t RTPSender::SendPaddingAccordingToBitrate(
344 int8_t payload_type, uint32_t 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000368int32_t RTPSender::SendPadData(
369 int8_t payload_type, uint32_t capture_timestamp,
370 int64_t capture_time_ms, int32_t bytes) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000371 // 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;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000377 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000378
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.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000394 int32_t *data =
395 reinterpret_cast<int32_t *>(&(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,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000419 const uint16_t number_to_store) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000420 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000425int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
426 uint16_t length = IP_PACKET_SIZE;
427 uint8_t data_buffer[IP_PACKET_SIZE];
428 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000429
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000430 int64_t stored_time_in_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000431 StorageType type;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000432 bool found = packet_history_->GetRTPPacket(packet_id, min_resend_time,
433 data_buffer, &length,
434 &stored_time_in_ms, &type);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000435 if (!found) {
436 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000437 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000438 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000439 if (length == 0 || type == kDontRetransmit) {
440 // No bytes copied (packet recently resent, skip resending) or
441 // packet should not be retransmitted.
442 return 0;
443 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000444 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000445 if (rtx_ != kRtxOff) {
446 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000447 buffer_to_send_ptr = data_buffer_rtx;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000448 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000449
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000450 int32_t bytes_sent = ReSendToNetwork(buffer_to_send_ptr, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000451 if (bytes_sent <= 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000452 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000453 "Transport failed to resend packet_id %u", packet_id);
454 return -1;
455 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000456 // Store the time when the packet was last resent.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000457 packet_history_->UpdateResendTime(packet_id);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000458 return bytes_sent;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000459}
460
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000461int32_t RTPSender::ReSendToNetwork(const uint8_t *packet, const uint32_t size) {
462 int32_t bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000463 if (transport_) {
464 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000465 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000466 if (bytes_sent <= 0) {
467 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000468 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000469 // Update send statistics.
470 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000471 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000472 packets_sent_++;
473 // We on purpose don't add to payload_bytes_sent_ since this is a
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000474 // re-transmit and not new payload data.
475 return bytes_sent;
niklase@google.com470e71d2011-07-07 08:21:25 +0000476}
477
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000478int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000479 if (!video_)
480 return -1;
481 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000482}
483
484int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000485 if (!video_)
486 return -1;
487 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000488}
489
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000490void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000491 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000492 const uint16_t avg_rtt) {
493 const int64_t now = clock_->TimeInMilliseconds();
494 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000495
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000496 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000497 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000498 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000499 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000500 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000501 return;
502 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000503
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000504 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
505 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000506 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000507 if (bytes_sent > 0) {
508 bytes_re_sent += bytes_sent;
509 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000510 // The packet has previously been resent.
511 // Try resending next packet in the list.
512 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000513 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000514 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000515 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000516 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000517 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000518 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000519 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000520 // Delay bandwidth estimate (RTT * BW).
521 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000522 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000523 uint32_t target_bytes =
524 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000525 if (bytes_re_sent > target_bytes) {
526 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000527 }
528 }
529 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000530 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000531 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000532 UpdateNACKBitRate(bytes_re_sent, now);
533 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000534 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000535}
536
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000537bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
538 uint32_t num = 0;
539 int32_t byte_count = 0;
540 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000541
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000542 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000543
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000544 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000545 return true;
546 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000547 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
548 if ((now - nack_byte_count_times_[num]) > avg_interval) {
549 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000550 break;
551 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000552 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000553 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000554 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000555 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000556 if (num == NACK_BYTECOUNT_SIZE) {
557 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000558 // during the last msg_interval.
559 time_interval = now - nack_byte_count_times_[num - 1];
560 if (time_interval < 0) {
561 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000562 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000563 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000564 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000565}
566
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000567void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
568 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000569 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000570
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000571 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000572 if (bytes > 0) {
573 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000574 // Add padding length.
575 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000576 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000577 if (nack_byte_count_times_[0] == 0) {
578 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000579 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000580 // Shift.
581 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
582 nack_byte_count_[i + 1] = nack_byte_count_[i];
583 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000584 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000585 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000586 nack_byte_count_[0] = bytes;
587 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000588 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000589 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000590}
591
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000592void RTPSender::TimeToSendPacket(uint16_t sequence_number,
593 int64_t capture_time_ms) {
594 StorageType type;
595 uint16_t length = IP_PACKET_SIZE;
596 uint8_t data_buffer[IP_PACKET_SIZE];
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000597 int64_t stored_time_ms; // TODO(pwestin) can we deprecate this?
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000598
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000599 if (packet_history_ == NULL) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000600 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000601 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000602 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
603 &stored_time_ms, &type)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000604 return;
605 }
606 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000607
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000608 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000609 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000610 rtp_parser.Parse(rtp_header);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000611
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000612 int64_t diff_ms = clock_->TimeInMilliseconds() - capture_time_ms;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000613 if (UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms)) {
614 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000615 packet_history_->ReplaceRTPHeader(data_buffer,
616 rtp_header.header.sequenceNumber,
617 rtp_header.header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000618 }
619 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000620 if (transport_) {
621 bytes_sent = transport_->SendPacket(id_, data_buffer, length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000622 }
623 if (bytes_sent <= 0) {
624 return;
625 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000626 // Update send statistics.
627 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000628 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000629 packets_sent_++;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000630 if (bytes_sent > rtp_header.header.headerLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000631 payload_bytes_sent_ += bytes_sent - rtp_header.header.headerLength;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000632 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000633}
634
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000635// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000636int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000637 uint8_t *buffer, int payload_length, int rtp_header_length,
638 int64_t capture_time_ms, StorageType storage) {
639 ModuleRTPUtility::RTPHeaderParser rtp_parser(
640 buffer, payload_length + rtp_header_length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000641 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000642 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000643
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000644 // |capture_time_ms| <= 0 is considered invalid.
645 // TODO(holmer): This should be changed all over Video Engine so that negative
646 // time is consider invalid, while 0 is considered a valid time.
647 if (capture_time_ms > 0) {
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000648 int64_t time_now = clock_->TimeInMilliseconds();
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000649 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
650 rtp_header, time_now - capture_time_ms);
651 }
652 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000653 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
654 max_payload_length_, capture_time_ms,
655 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000656 return -1;
657 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000658
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000659 int32_t bytes_sent = -1;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000660 // Create and send RTX Packet.
661 if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000662 uint16_t length_rtx = payload_length + rtp_header_length;
663 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000664 BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
665 if (transport_) {
666 bytes_sent += transport_->SendPacket(id_, data_buffer_rtx, length_rtx);
667 if (bytes_sent <= 0) {
668 return -1;
669 }
670 }
671 }
672
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000673 if (paced_sender_ && storage != kDontStore) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000674 if (!paced_sender_->SendPacket(
675 PacedSender::kNormalPriority, rtp_header.header.ssrc,
676 rtp_header.header.sequenceNumber, capture_time_ms,
677 payload_length + rtp_header_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000678 // We can't send the packet right now.
679 // We will be called when it is time.
680 return payload_length + rtp_header_length;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000681 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000682 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000683 // Send data packet.
684 bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000685 if (transport_) {
686 bytes_sent = transport_->SendPacket(id_, buffer,
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000687 payload_length + rtp_header_length);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000688 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000689 if (bytes_sent <= 0) {
690 return -1;
691 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000692 // Update send statistics.
693 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000694 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000695 packets_sent_++;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000696 if (bytes_sent > rtp_header_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000697 payload_bytes_sent_ += bytes_sent - rtp_header_length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000698 }
699 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000700}
701
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000702void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000703 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000704 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000705 nack_bitrate_.Process();
706 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000707 return;
708 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000709 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000710}
711
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000712uint16_t RTPSender::RTPHeaderLength() const {
713 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000714 if (include_csrcs_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000715 rtp_header_length += sizeof(uint32_t) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000716 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000717 rtp_header_length += RtpHeaderExtensionTotalLength();
718 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000719}
720
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000721uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000722 CriticalSectionScoped cs(send_critsect_);
723 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000724}
725
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000726void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000727 packets_sent_ = 0;
728 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000729}
730
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000731uint32_t RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000732 // Don't use critsect to avoid potential deadlock.
733 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000734}
735
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000736// Number of sent RTP bytes.
737// Don't use critsect to avoid potental deadlock.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000738uint32_t RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000739 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000740}
741
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000742int32_t RTPSender::BuildRTPheader(
743 uint8_t *data_buffer, const int8_t payload_type,
744 const bool marker_bit, const uint32_t capture_time_stamp,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000745 const bool time_stamp_provided, const bool inc_sequence_number) {
746 assert(payload_type >= 0);
747 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000748
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000749 data_buffer[0] = static_cast<uint8_t>(0x80); // version 2.
750 data_buffer[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000751 if (marker_bit) {
752 data_buffer[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000753 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000754 if (time_stamp_provided) {
755 time_stamp_ = start_time_stamp_ + capture_time_stamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000756 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000757 // Make a unique time stamp.
758 // We can't inc by the actual time, since then we increase the risk of back
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000759 // timing.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000760 time_stamp_++;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000761 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000762 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + 2, sequence_number_);
763 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 4, time_stamp_);
764 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 8, ssrc_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000765 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000766
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000767 // Add the CSRCs if any.
768 if (include_csrcs_ && csrcs_ > 0) {
769 if (csrcs_ > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000770 // error
771 assert(false);
772 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000773 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000774 uint8_t *ptr = &data_buffer[rtp_header_length];
775 for (uint32_t i = 0; i < csrcs_; ++i) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000776 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrc_[i]);
777 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000778 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000779 data_buffer[0] = (data_buffer[0] & 0xf0) | csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000780
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000781 // Update length of header.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000782 rtp_header_length += sizeof(uint32_t) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000783 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000784 sequence_number_++; // Prepare for next packet.
niklase@google.com470e71d2011-07-07 08:21:25 +0000785
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000786 uint16_t len = BuildRTPHeaderExtension(data_buffer + rtp_header_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000787 if (len) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000788 data_buffer[0] |= 0x10; // Set extension bit.
789 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000790 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000791 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000792}
793
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000794uint16_t RTPSender::BuildRTPHeaderExtension(
795 uint8_t *data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000796 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000797 return 0;
798 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000799 // RTP header extension, RFC 3550.
800 // 0 1 2 3
801 // 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
802 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
803 // | defined by profile | length |
804 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
805 // | header extension |
806 // | .... |
807 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000808 const uint32_t kPosLength = 2;
809 const uint32_t kHeaderLength = RTP_ONE_BYTE_HEADER_LENGTH_IN_BYTES;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000810
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000811 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000812 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000813 RTP_ONE_BYTE_HEADER_EXTENSION);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000814
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000815 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000816 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000817
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000818 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000819 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000820 uint8_t block_length = 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000821 if (type == kRtpExtensionTransmissionTimeOffset) {
822 block_length = BuildTransmissionTimeOffsetExtension(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000823 data_buffer + kHeaderLength + total_block_length);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000824 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000825 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000826 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000827 }
828 if (total_block_length == 0) {
829 // No extension added.
830 return 0;
831 }
832 // Set header length (in number of Word32, header excluded).
833 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000834 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000835 total_block_length / 4);
836 // Total added length.
837 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000838}
839
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000840uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
841 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000842 // From RFC 5450: Transmission Time Offsets in RTP Streams.
843 //
844 // The transmission time is signaled to the receiver in-band using the
845 // general mechanism for RTP header extensions [RFC5285]. The payload
846 // of this extension (the transmitted value) is a 24-bit signed integer.
847 // When added to the RTP timestamp of the packet, it represents the
848 // "effective" RTP transmission time of the packet, on the RTP
849 // timescale.
850 //
851 // The form of the transmission offset extension block:
852 //
853 // 0 1 2 3
854 // 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
855 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
856 // | ID | len=2 | transmission offset |
857 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000858
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000859 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000860 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000861 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
862 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000863 // Not registered.
864 return 0;
865 }
866 int pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000867 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000868 data_buffer[pos++] = (id << 4) + len;
869 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
870 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000871 pos += 3;
872 assert(pos == TRANSMISSION_TIME_OFFSET_LENGTH_IN_BYTES);
873 return TRANSMISSION_TIME_OFFSET_LENGTH_IN_BYTES;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000874}
875
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000876bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000877 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
878 const WebRtcRTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000879 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000880
881 // Get length until start of transmission block.
882 int transmission_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000883 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000884 kRtpExtensionTransmissionTimeOffset);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000885 if (transmission_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000886 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000887 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000888 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000889 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000890 int block_pos = 12 + rtp_header.header.numCSRCs + transmission_block_pos;
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +0000891 if (rtp_packet_length < block_pos + 4 ||
892 rtp_header.header.headerLength < block_pos + 4) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000893 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000894 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000895 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000896 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000897 // Verify that header contains extension.
898 if (!((rtp_packet[12 + rtp_header.header.numCSRCs] == 0xBE) &&
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000899 (rtp_packet[12 + rtp_header.header.numCSRCs + 1] == 0xDE))) {
900 WEBRTC_TRACE(
901 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000902 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000903 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000904 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000905 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000906 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000907 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
908 &id) != 0) {
909 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000910 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000911 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000912 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000913 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000914 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000915 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000916 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000917 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000918 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000919 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000920 // Update transmission offset field.
921 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +0000922 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000923 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000924}
925
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000926void RTPSender::SetSendingStatus(const bool enabled) {
927 if (enabled) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000928 uint32_t frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000929 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000930 uint32_t frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000931
932 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000933 switch (frequency) {
934 case 8000:
935 case 12000:
936 case 16000:
937 case 24000:
938 case 32000:
939 break;
940 default:
941 assert(false);
942 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000943 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000944 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000945 } else {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000946 frequency_hz = kDefaultVideoFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000947 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000948 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000949
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000950 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000951 SetStartTimestamp(RTPtime, false);
952 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000953 if (!ssrc_forced_) {
954 // Generate a new SSRC.
955 ssrc_db_.ReturnSSRC(ssrc_);
956 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000957 }
958 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000959 if (!sequence_number_forced_ && !ssrc_forced_) {
960 // Generate a new sequence number.
961 sequence_number_ =
962 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000963 }
964 }
965}
966
967void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000968 CriticalSectionScoped cs(send_critsect_);
969 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000970}
971
972bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000973 CriticalSectionScoped cs(send_critsect_);
974 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000975}
976
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000977uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000978 CriticalSectionScoped cs(send_critsect_);
979 return time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000980}
981
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000982void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000983 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000984 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000985 start_time_stamp_forced_ = force;
986 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000987 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000988 if (!start_time_stamp_forced_) {
989 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000990 }
991 }
992}
993
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000994uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000995 CriticalSectionScoped cs(send_critsect_);
996 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000997}
998
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000999uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001000 // If configured via API, return 0.
1001 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001002
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001003 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001004 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001005 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001006 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1007 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001008}
1009
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001010void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001011 // This is configured via the API.
1012 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001013
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001014 if (ssrc_ == ssrc && ssrc_forced_) {
1015 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001016 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001017 ssrc_forced_ = true;
1018 ssrc_db_.ReturnSSRC(ssrc_);
1019 ssrc_db_.RegisterSSRC(ssrc);
1020 ssrc_ = ssrc;
1021 if (!sequence_number_forced_) {
1022 sequence_number_ =
1023 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001024 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001025}
1026
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001027uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001028 CriticalSectionScoped cs(send_critsect_);
1029 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001030}
1031
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001032void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001033 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001034}
1035
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001036void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1037 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001038 assert(arr_length <= kRtpCsrcSize);
1039 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001040
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001041 for (int i = 0; i < arr_length; i++) {
1042 csrc_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001043 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001044 csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001045}
1046
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001047int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001048 assert(arr_of_csrc);
1049 CriticalSectionScoped cs(send_critsect_);
1050 for (int i = 0; i < csrcs_ && i < kRtpCsrcSize; i++) {
1051 arr_of_csrc[i] = csrc_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001052 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001053 return csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001054}
1055
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001056void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001057 CriticalSectionScoped cs(send_critsect_);
1058 sequence_number_forced_ = true;
1059 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001060}
1061
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001062uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001063 CriticalSectionScoped cs(send_critsect_);
1064 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001065}
1066
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001067// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001068int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1069 const uint16_t time_ms,
1070 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001071 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001072 return -1;
1073 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001074 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001075}
1076
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001077bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001078 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001079 return false;
1080 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001081 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001082}
1083
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001084int32_t RTPSender::SetAudioPacketSize(
1085 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001086 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001087 return -1;
1088 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001089 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001090}
1091
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001092int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1093 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001094 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001095 return -1;
1096 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001097 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001098}
1099
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001100int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1101 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001102 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001103}
1104
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001105int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001106 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001107}
1108
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001109int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001110 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001111 return -1;
1112 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001113 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001114}
1115
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001116int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001117 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001118 return -1;
1119 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001120 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001121}
1122
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001123// Video
1124VideoCodecInformation *RTPSender::CodecInformationVideo() {
1125 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001126 return NULL;
1127 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001128 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001129}
1130
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001131RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001132 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001133 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001134}
1135
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001136uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001137 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001138 return 0;
1139 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001140 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001141}
1142
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001143int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001144 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001145 return -1;
1146 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001147 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001148}
1149
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001150int32_t RTPSender::SetGenericFECStatus(
1151 const bool enable, const uint8_t payload_type_red,
1152 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001153 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001154 return -1;
1155 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001156 return video_->SetGenericFECStatus(enable, payload_type_red,
1157 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001158}
1159
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001160int32_t RTPSender::GenericFECStatus(
1161 bool *enable, uint8_t *payload_type_red,
1162 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001163 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001164 return -1;
1165 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001166 return video_->GenericFECStatus(
1167 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001168}
1169
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001170int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001171 const FecProtectionParams *delta_params,
1172 const FecProtectionParams *key_params) {
1173 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001174 return -1;
1175 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001176 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001177}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001178
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001179void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1180 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001181 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001182 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001183 // Add RTX header.
1184 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001185 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001186
1187 WebRtcRTPHeader rtp_header;
1188 rtp_parser.Parse(rtp_header);
1189
1190 // Add original RTP header.
1191 memcpy(data_buffer_rtx, buffer, rtp_header.header.headerLength);
1192
1193 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001194 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001195 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1196
1197 // Replace SSRC.
1198 ptr += 6;
1199 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1200
1201 // Add OSN (original sequence number).
1202 ptr = data_buffer_rtx + rtp_header.header.headerLength;
1203 ModuleRTPUtility::AssignUWord16ToBuffer(ptr,
1204 rtp_header.header.sequenceNumber);
1205 ptr += 2;
1206
1207 // Add original payload data.
1208 memcpy(ptr, buffer + rtp_header.header.headerLength,
1209 *length - rtp_header.header.headerLength);
1210 *length += 2;
1211}
1212
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001213} // namespace webrtc