blob: 07a3f1b9f6e57ed6182babe77f375ae657cdda1e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000011#include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
12
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000013#include <cstdlib> // srand
niklase@google.com470e71d2011-07-07 08:21:25 +000014
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000015#include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
16#include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
17#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
18#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
19#include "webrtc/system_wrappers/interface/trace.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000020#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
stefan@webrtc.orga8179622013-06-04 13:47:36 +000024// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
25const int kMaxPaddingLength = 224;
26
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000027namespace {
28
29const char* FrameTypeToString(const FrameType frame_type) {
30 switch (frame_type) {
31 case kFrameEmpty: return "empty";
32 case kAudioFrameSpeech: return "audio_speech";
33 case kAudioFrameCN: return "audio_cn";
34 case kVideoFrameKey: return "video_key";
35 case kVideoFrameDelta: return "video_delta";
36 case kVideoFrameGolden: return "video_golden";
37 case kVideoFrameAltRef: return "video_altref";
38 }
39 return "";
40}
41
42} // namespace
43
pbos@webrtc.org2f446732013-04-08 11:08:41 +000044RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000045 Transport *transport, RtpAudioFeedback *audio_feedback,
46 PacedSender *paced_sender)
47 : Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL),
48 video_(NULL), paced_sender_(paced_sender),
49 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
50 transport_(transport), sending_media_(true), // Default to sending media.
51 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
52 target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1),
53 payload_type_map_(), rtp_header_extension_map_(),
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +000054 transmission_time_offset_(0), absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000055 // NACK.
56 nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
57 packet_history_(new RTPPacketHistory(clock)),
58 // Statistics
59 packets_sent_(0), payload_bytes_sent_(0), start_time_stamp_forced_(false),
60 start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000061 remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false),
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +000062 timestamp_(0), capture_time_ms_(0), last_packet_marker_bit_(false),
63 num_csrcs_(0), csrcs_(), include_csrcs_(true),
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +000064 rtx_(kRtxOff), payload_type_rtx_(-1) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000065 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
66 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +000067 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000068 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000069 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000070 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000071 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
72 // Random start, 16 bits. Can't be 0.
73 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
74 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +000075
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000076 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000077 audio_ = new RTPSenderAudio(id, clock_, this);
78 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000079 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000080 video_ = new RTPSenderVideo(id, clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000081 }
82 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
pwestin@webrtc.org00741872012-01-19 15:56:10 +000085RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000086 if (remote_ssrc_ != 0) {
87 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000088 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000089 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
pwestin@webrtc.org00741872012-01-19 15:56:10 +000091 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000092 delete send_critsect_;
93 while (!payload_type_map_.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000094 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000095 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +000096 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000097 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000098 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000099 delete packet_history_;
100 delete audio_;
101 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000102
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000103 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104}
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000106void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000107 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000108}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000109
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000110uint16_t RTPSender::ActualSendBitrateKbit() const {
111 return (uint16_t)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000112}
113
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000114uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000115 if (video_) {
116 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000117 }
118 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000119}
120
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000121uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000122 if (video_) {
123 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000124 }
125 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000126}
127
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000128uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000129 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000130}
131
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000132int32_t RTPSender::SetTransmissionTimeOffset(
133 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000134 if (transmission_time_offset > (0x800000 - 1) ||
135 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000136 return -1;
137 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000138 CriticalSectionScoped cs(send_critsect_);
139 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000140 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000141}
142
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000143int32_t RTPSender::SetAbsoluteSendTime(
144 const uint32_t absolute_send_time) {
145 if (absolute_send_time > 0xffffff) { // UWord24.
146 return -1;
147 }
148 CriticalSectionScoped cs(send_critsect_);
149 absolute_send_time_ = absolute_send_time;
150 return 0;
151}
152
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000153int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
154 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000155 CriticalSectionScoped cs(send_critsect_);
156 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000157}
158
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000159int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000160 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000161 CriticalSectionScoped cs(send_critsect_);
162 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000163}
164
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000165uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000166 CriticalSectionScoped cs(send_critsect_);
167 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000168}
169
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000170int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000171 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000172 const int8_t payload_number, const uint32_t frequency,
173 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000174 assert(payload_name);
175 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000177 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000178 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000180 if (payload_type_map_.end() != it) {
181 // We already use this payload type.
182 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000183 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000185 // Check if it's the same as we already have.
186 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000187 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000188 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000189 payload->typeSpecific.Audio.frequency == frequency &&
190 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000191 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000192 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000193 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000194 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000195 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000196 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000197 return 0;
198 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000199 }
200 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000201 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000202 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000203 ModuleRTPUtility::Payload *payload = NULL;
204 if (audio_configured_) {
205 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
206 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000207 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000208 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
209 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000210 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000211 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000212 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000213 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000214 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215}
216
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000217int32_t RTPSender::DeRegisterSendPayload(
218 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000219 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000220
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000221 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000222 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000223
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000224 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000225 return -1;
226 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000227 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000228 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000229 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000230 return 0;
231}
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000233int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000234
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000235int RTPSender::SendPayloadFrequency() const { return audio_->AudioFrequency(); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000237int32_t RTPSender::SetMaxPayloadLength(
238 const uint16_t max_payload_length,
239 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000240 // Sanity check.
241 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
242 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
243 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000244 return -1;
245 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000246 CriticalSectionScoped cs(send_critsect_);
247 max_payload_length_ = max_payload_length;
248 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000249
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000250 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
251 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000252 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000253}
254
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000255uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000256 if (audio_configured_) {
257 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000258 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000259 return max_payload_length_ - RTPHeaderLength() -
260 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
261 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000262 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000263}
264
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000265uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000266 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000267}
268
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000269uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000270
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000271void RTPSender::SetRTXStatus(RtxMode mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000272 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000273 rtx_ = mode;
274 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000275 if (set_ssrc) {
276 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000277 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000278 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000279 }
280 }
281}
282
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000283void RTPSender::RTXStatus(RtxMode* mode, uint32_t* ssrc,
284 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000285 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000286 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000287 *ssrc = ssrc_rtx_;
288 *payload_type = payload_type_rtx_;
289}
290
291
292void RTPSender::SetRtxPayloadType(int payload_type) {
293 CriticalSectionScoped cs(send_critsect_);
294 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000295}
296
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000297int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
298 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000299 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000301 if (payload_type < 0) {
302 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
303 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000304 return -1;
305 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000306 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000307 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000308 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000309 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000310 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000311 // And it's a match...
312 return 0;
313 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000314 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000315 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000316 if (payload_type_ == payload_type) {
317 if (!audio_configured_) {
318 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000319 }
320 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000321 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000322 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000323 payload_type_map_.find(payload_type);
324 if (it == payload_type_map_.end()) {
325 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
326 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000327 return -1;
328 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000329 payload_type_ = payload_type;
330 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000331 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000332 if (!payload->audio && !audio_configured_) {
333 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
334 *video_type = payload->typeSpecific.Video.videoCodecType;
335 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000336 }
337 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000338}
339
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000340int32_t RTPSender::SendOutgoingData(
341 const FrameType frame_type, const int8_t payload_type,
342 const uint32_t capture_timestamp, int64_t capture_time_ms,
343 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000344 const RTPFragmentationHeader *fragmentation,
345 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000346 {
347 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000348 CriticalSectionScoped cs(send_critsect_);
349 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000350 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000351 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000352 }
pbos@webrtc.org8911ce42013-03-18 16:39:03 +0000353 RtpVideoCodecTypes video_type = kRtpGenericVideo;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000354 if (CheckPayloadType(payload_type, &video_type) != 0) {
355 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
356 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000357 __FUNCTION__, payload_type);
358 return -1;
359 }
360
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000361 if (frame_type == kVideoFrameKey) {
362 TRACE_EVENT_INSTANT1("webrtc_rtp", "SendKeyFrame",
363 "timestamp", capture_timestamp);
364 } else {
365 TRACE_EVENT_INSTANT2("webrtc_rtp", "SendFrame",
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000366 "timestamp", capture_timestamp,
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000367 "frame_type", FrameTypeToString(frame_type));
368 }
369
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000370 if (audio_configured_) {
371 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000372 frame_type == kFrameEmpty);
373
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000374 return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
375 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000376 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000377 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000378
379 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000380 if (paced_sender_->Enabled()) {
381 // Padding is driven by the pacer and not by the encoder.
382 return 0;
383 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000384 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000385 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000386 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000387 return video_->SendVideo(video_type, frame_type, payload_type,
388 capture_timestamp, capture_time_ms, payload_data,
389 payload_size, fragmentation, codec_info,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000390 rtp_type_hdr);
391 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000392}
393
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000394bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000395 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000396 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000397 // Current bitrate since last estimate(1 second) averaged with the
398 // estimate since then, to get the most up to date bitrate.
399 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000400 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000401 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000402 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000403 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000404 int bytes = 0;
405 if (current_bitrate == 0) {
406 // Start up phase. Send one 33.3 ms batch to start with.
407 bytes = (bitrate_diff / 8) / 30;
408 } else {
409 bytes = (bitrate_diff / 8);
410 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000411 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000412 if (bytes > bytes_cap) {
413 bytes = bytes_cap;
414 }
415 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000416 int bytes_sent = SendPadData(payload_type, capture_timestamp, capture_time_ms,
417 bytes, kDontRetransmit, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000418 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
419 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000420}
421
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000422int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
423 int32_t bytes) {
424 int padding_bytes_in_packet = kMaxPaddingLength;
425 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000426 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000427 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000428 packet[0] |= 0x20; // Set padding bit.
429 int32_t *data =
430 reinterpret_cast<int32_t *>(&(packet[header_length]));
431
432 // Fill data buffer with random data.
433 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
434 data[j] = rand(); // NOLINT
435 }
436 // Set number of padding bytes in the last byte of the packet.
437 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
438 return padding_bytes_in_packet;
439}
440
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000441int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
442 int64_t capture_time_ms, int32_t bytes,
443 StorageType store, bool force_full_size_packets) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000444 // Drop this packet if we're not sending media packets.
445 if (!sending_media_) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000446 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000447 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000448 int padding_bytes_in_packet = 0;
449 int bytes_sent = 0;
450 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000451 // Always send full padding packets.
452 if (force_full_size_packets && bytes < kMaxPaddingLength)
453 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000454 if (bytes < kMaxPaddingLength) {
455 if (force_full_size_packets) {
456 bytes = kMaxPaddingLength;
457 } else {
458 // Round to the nearest multiple of 32.
459 bytes = (bytes + 16) & 0xffe0;
460 }
461 }
462 if (padding_bytes_in_packet < 32) {
463 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000464 break;
465 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000466 uint32_t ssrc;
467 uint16_t sequence_number;
468 {
469 CriticalSectionScoped cs(send_critsect_);
470 // Only send padding packets following the last packet of a frame,
471 // indicated by the marker bit.
472 if (!last_packet_marker_bit_)
473 return bytes_sent;
474 if (rtx_ == kRtxOff) {
475 ssrc = ssrc_;
476 sequence_number = sequence_number_;
477 ++sequence_number_;
478 } else {
479 ssrc = ssrc_rtx_;
480 sequence_number = sequence_number_rtx_;
481 ++sequence_number_rtx_;
482 }
483 }
484 uint8_t padding_packet[IP_PACKET_SIZE];
485 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
486 false, timestamp, sequence_number, NULL,
487 0);
488 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
489 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000490 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
491 header_length, capture_time_ms, store,
492 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000493 // Error sending the packet.
494 break;
495 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000496 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000497 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000498 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000499}
500
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000501void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000502 const uint16_t number_to_store) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000503 packet_history_->SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000504}
505
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000506bool RTPSender::StorePackets() const {
507 return packet_history_->StorePackets();
508}
niklase@google.com470e71d2011-07-07 08:21:25 +0000509
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000510int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
511 uint16_t length = IP_PACKET_SIZE;
512 uint8_t data_buffer[IP_PACKET_SIZE];
513 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000514 int64_t capture_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000515 StorageType type;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000516 if (!packet_history_->GetRTPPacket(packet_id, min_resend_time, data_buffer,
517 &length, &capture_time_ms, &type)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000518 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000519 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000520 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000521 if (length == 0 || type == kDontRetransmit) {
522 // No bytes copied (packet recently resent, skip resending) or
523 // packet should not be retransmitted.
524 return 0;
525 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000526
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000527 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000528 if (rtx_ != kRtxOff) {
529 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000530 buffer_to_send_ptr = data_buffer_rtx;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000531 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000532
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000533 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000534 RTPHeader header;
535 rtp_parser.Parse(header);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000536
537 // Store the time when the packet was last sent or added to pacer.
538 packet_history_->UpdateResendTime(packet_id);
539
540 {
541 // Update send statistics prior to pacer.
542 CriticalSectionScoped cs(send_critsect_);
543 Bitrate::Update(length);
544 packets_sent_++;
545 // We on purpose don't add to payload_bytes_sent_ since this is a
546 // re-transmit and not new payload data.
547 }
548
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000549 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000550 "timestamp", header.timestamp,
551 "seqnum", header.sequenceNumber);
552
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000553 if (paced_sender_) {
554 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000555 header.ssrc,
556 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000557 capture_time_ms,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000558 length - header.headerLength)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000559 // We can't send the packet right now.
560 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000561 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000562 }
563 }
564
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000565 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000566 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000567 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000568 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000569}
570
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000571bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
572 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000573 if (transport_) {
574 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000575 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000576 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
577 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000578 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000579 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000580 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
581 "Transport failed to send packet");
582 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000583 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000584 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000585}
586
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000587int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000588 if (!video_)
589 return -1;
590 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000591}
592
593int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000594 if (!video_)
595 return -1;
596 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000597}
598
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000599void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000600 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000601 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000602 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
603 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000604 const int64_t now = clock_->TimeInMilliseconds();
605 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000606
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000607 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000608 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000609 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000610 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000611 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000612 return;
613 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000614
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000615 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
616 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000617 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000618 if (bytes_sent > 0) {
619 bytes_re_sent += bytes_sent;
620 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000621 // The packet has previously been resent.
622 // Try resending next packet in the list.
623 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000624 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000625 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000626 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000627 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000628 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000629 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000630 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000631 // Delay bandwidth estimate (RTT * BW).
632 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000633 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000634 uint32_t target_bytes =
635 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000636 if (bytes_re_sent > target_bytes) {
637 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000638 }
639 }
640 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000641 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000642 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000643 UpdateNACKBitRate(bytes_re_sent, now);
644 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000645 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000646}
647
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000648bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
649 uint32_t num = 0;
650 int32_t byte_count = 0;
651 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000652
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000653 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000654
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000655 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000656 return true;
657 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000658 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
659 if ((now - nack_byte_count_times_[num]) > avg_interval) {
660 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000661 break;
662 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000663 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000664 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000665 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000666 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000667 if (num == NACK_BYTECOUNT_SIZE) {
668 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000669 // during the last msg_interval.
670 time_interval = now - nack_byte_count_times_[num - 1];
671 if (time_interval < 0) {
672 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000673 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000674 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000675 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000676}
677
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000678void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
679 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000680 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000681
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000682 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000683 if (bytes > 0) {
684 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000685 // Add padding length.
686 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000687 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000688 if (nack_byte_count_times_[0] == 0) {
689 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000690 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000691 // Shift.
692 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
693 nack_byte_count_[i + 1] = nack_byte_count_[i];
694 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000695 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000696 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000697 nack_byte_count_[0] = bytes;
698 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000699 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000700 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000701}
702
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000703// Called from pacer when we can send the packet.
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000704void RTPSender::TimeToSendPacket(uint16_t sequence_number,
705 int64_t capture_time_ms) {
706 StorageType type;
707 uint16_t length = IP_PACKET_SIZE;
708 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000709 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000710
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000711 if (packet_history_ == NULL) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000712 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000713 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000714 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
715 &stored_time_ms, &type)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000716 return;
717 }
718 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000719
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000720 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000721 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000722 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000723 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000724 "timestamp", rtp_header.timestamp,
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000725 "seqnum", sequence_number);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000726
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000727 int64_t now_ms = clock_->TimeInMilliseconds();
728 int64_t diff_ms = now_ms - capture_time_ms;
729 bool updated_transmission_time_offset =
730 UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms);
731 bool updated_abs_send_time =
732 UpdateAbsoluteSendTime(data_buffer, length, rtp_header, now_ms);
733 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000734 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000735 packet_history_->ReplaceRTPHeader(data_buffer,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000736 rtp_header.sequenceNumber,
737 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000738 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000739 SendPacketToNetwork(data_buffer, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000740}
741
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000742int RTPSender::TimeToSendPadding(int bytes) {
743 if (!sending_media_) {
744 return 0;
745 }
746 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000747 int64_t capture_time_ms;
748 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000749 {
750 CriticalSectionScoped cs(send_critsect_);
751 payload_type = (rtx_ == kRtxOff) ? payload_type_ : payload_type_rtx_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000752 timestamp = timestamp_;
753 capture_time_ms = capture_time_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000754 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000755 return SendPadData(payload_type, timestamp, capture_time_ms, bytes,
756 kDontStore, true);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000757}
758
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000759// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000760int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000761 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000762 int64_t capture_time_ms, StorageType storage,
763 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000764 ModuleRTPUtility::RTPHeaderParser rtp_parser(
765 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000766 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000767 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000768
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000769 int64_t now_ms = clock_->TimeInMilliseconds();
770
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000771 // |capture_time_ms| <= 0 is considered invalid.
772 // TODO(holmer): This should be changed all over Video Engine so that negative
773 // time is consider invalid, while 0 is considered a valid time.
774 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000775 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000776 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000777 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000778
779 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
780 rtp_header, now_ms);
781
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000782 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000783 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
784 max_payload_length_, capture_time_ms,
785 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000786 return -1;
787 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000788
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000789 // Create and send RTX Packet.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000790 // TODO(pwesin): This should be moved to its own code path triggered by pacer.
791 bool rtx_sent = false;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000792 if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000793 uint16_t length_rtx = payload_length + rtp_header_length;
794 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000795 BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000796 if (!SendPacketToNetwork(data_buffer_rtx, length_rtx)) return -1;
797 rtx_sent = true;
798 }
799 {
800 // Update send statistics prior to pacer.
801 CriticalSectionScoped cs(send_critsect_);
802 Bitrate::Update(payload_length + rtp_header_length);
803 ++packets_sent_;
804 payload_bytes_sent_ += payload_length;
805 if (rtx_sent) {
806 // The RTX packet.
807 ++packets_sent_;
808 payload_bytes_sent_ += payload_length;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000809 }
810 }
811
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000812 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000813 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
814 rtp_header.sequenceNumber, capture_time_ms,
815 payload_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000816 // We can't send the packet right now.
817 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000818 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000819 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000820 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000821 if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
822 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000823 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000824 return -1;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000825}
826
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000827void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000828 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000829 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000830 nack_bitrate_.Process();
831 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000832 return;
833 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000834 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000835}
836
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000837uint16_t RTPSender::RTPHeaderLength() const {
838 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000839 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000840 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000841 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000842 rtp_header_length += RtpHeaderExtensionTotalLength();
843 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000844}
845
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000846uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000847 CriticalSectionScoped cs(send_critsect_);
848 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000849}
850
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000851void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000852 packets_sent_ = 0;
853 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000854}
855
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000856uint32_t RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000857 // Don't use critsect to avoid potential deadlock.
858 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000859}
860
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000861// Number of sent RTP bytes.
862// Don't use critsect to avoid potental deadlock.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000863uint32_t RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000864 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000865}
866
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000867int RTPSender::CreateRTPHeader(
868 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
869 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
870 uint8_t num_csrcs) const {
871 header[0] = 0x80; // version 2.
872 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000873 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000874 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000875 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000876 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
877 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
878 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000879 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000880
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000881 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000882 if (num_csrcs > 0) {
883 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000884 // error
885 assert(false);
886 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000887 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000888 uint8_t *ptr = &header[rtp_header_length];
889 for (int i = 0; i < num_csrcs; ++i) {
890 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000891 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000892 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000893 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000894
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000895 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000896 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000897 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000898
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000899 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
900 if (len > 0) {
901 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000902 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000903 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000904 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000905}
906
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000907int32_t RTPSender::BuildRTPheader(
908 uint8_t *data_buffer, const int8_t payload_type,
909 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000910 int64_t capture_time_ms, const bool time_stamp_provided,
911 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000912 assert(payload_type >= 0);
913 CriticalSectionScoped cs(send_critsect_);
914
915 if (time_stamp_provided) {
916 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000917 } else {
918 // Make a unique time stamp.
919 // We can't inc by the actual time, since then we increase the risk of back
920 // timing.
921 timestamp_++;
922 }
923 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000924 capture_time_ms_ = capture_time_ms;
925 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000926 int csrcs_length = 0;
927 if (include_csrcs_)
928 csrcs_length = num_csrcs_;
929 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
930 timestamp_, sequence_number, csrcs_, csrcs_length);
931}
932
933uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000934 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000935 return 0;
936 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000937 // RTP header extension, RFC 3550.
938 // 0 1 2 3
939 // 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
940 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
941 // | defined by profile | length |
942 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
943 // | header extension |
944 // | .... |
945 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000946 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000947 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000948
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000949 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000950 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000951 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000952
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000953 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000954 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000955
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000956 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000957 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000958 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000959 switch (type) {
960 case kRtpExtensionTransmissionTimeOffset:
961 block_length = BuildTransmissionTimeOffsetExtension(
962 data_buffer + kHeaderLength + total_block_length);
963 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +0000964 case kRtpExtensionAudioLevel:
965 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
966 // we don't have to care about it here, which is true until we wan't to
967 // use it together with any of the other extensions we support.
968 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000969 case kRtpExtensionAbsoluteSendTime:
970 block_length = BuildAbsoluteSendTimeExtension(
971 data_buffer + kHeaderLength + total_block_length);
972 break;
973 default:
974 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000975 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000976 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000977 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000978 }
979 if (total_block_length == 0) {
980 // No extension added.
981 return 0;
982 }
983 // Set header length (in number of Word32, header excluded).
984 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000985 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000986 total_block_length / 4);
987 // Total added length.
988 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000989}
990
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000991uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
992 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000993 // From RFC 5450: Transmission Time Offsets in RTP Streams.
994 //
995 // The transmission time is signaled to the receiver in-band using the
996 // general mechanism for RTP header extensions [RFC5285]. The payload
997 // of this extension (the transmitted value) is a 24-bit signed integer.
998 // When added to the RTP timestamp of the packet, it represents the
999 // "effective" RTP transmission time of the packet, on the RTP
1000 // timescale.
1001 //
1002 // The form of the transmission offset extension block:
1003 //
1004 // 0 1 2 3
1005 // 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
1006 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1007 // | ID | len=2 | transmission offset |
1008 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001009
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001010 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001011 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001012 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1013 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001014 // Not registered.
1015 return 0;
1016 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001017 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001018 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001019 data_buffer[pos++] = (id << 4) + len;
1020 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1021 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001022 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001023 assert(pos == kTransmissionTimeOffsetLength);
1024 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001025}
1026
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001027uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
1028 uint8_t* data_buffer) const {
1029 // Absolute send time in RTP streams.
1030 //
1031 // The absolute send time is signaled to the receiver in-band using the
1032 // general mechanism for RTP header extensions [RFC5285]. The payload
1033 // of this extension (the transmitted value) is a 24-bit unsigned integer
1034 // containing the sender's current time in seconds as a fixed point number
1035 // with 18 bits fractional part.
1036 //
1037 // The form of the absolute send time extension block:
1038 //
1039 // 0 1 2 3
1040 // 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
1041 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1042 // | ID | len=2 | absolute send time |
1043 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1044
1045 // Get id defined by user.
1046 uint8_t id;
1047 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1048 &id) != 0) {
1049 // Not registered.
1050 return 0;
1051 }
1052 size_t pos = 0;
1053 const uint8_t len = 2;
1054 data_buffer[pos++] = (id << 4) + len;
1055 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1056 absolute_send_time_);
1057 pos += 3;
1058 assert(pos == kAbsoluteSendTimeLength);
1059 return kAbsoluteSendTimeLength;
1060}
1061
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001062bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001063 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001064 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001065 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001066
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001067 // Get length until start of header extension block.
1068 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001069 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001070 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001071 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001072 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001073 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001074 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001075 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001076 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001077 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001078 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001079 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001080 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001081 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001082 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001083 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001084 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001085 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1086 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001087 WEBRTC_TRACE(
1088 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001089 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001090 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001091 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001092 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001093 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001094 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1095 &id) != 0) {
1096 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001097 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001098 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001099 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001100 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001101 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001102 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001103 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001104 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001105 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001106 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001107 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001108 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001109 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001110 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001111}
1112
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001113bool RTPSender::UpdateAbsoluteSendTime(
1114 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001115 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001116 CriticalSectionScoped cs(send_critsect_);
1117
1118 // Get length until start of header extension block.
1119 int extension_block_pos =
1120 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1121 kRtpExtensionAbsoluteSendTime);
1122 if (extension_block_pos < 0) {
1123 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1124 "Failed to update absolute send time, not registered.");
1125 return false;
1126 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001127 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001128 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001129 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001130 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1131 "Failed to update absolute send time, invalid length.");
1132 return false;
1133 }
1134 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001135 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1136 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001137 WEBRTC_TRACE(
1138 kTraceStream, kTraceRtpRtcp, id_,
1139 "Failed to update absolute send time, hdr extension not found.");
1140 return false;
1141 }
1142 // Get id.
1143 uint8_t id = 0;
1144 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1145 &id) != 0) {
1146 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1147 "Failed to update absolute send time, no id.");
1148 return false;
1149 }
1150 // Verify first byte in block.
1151 const uint8_t first_block_byte = (id << 4) + 2;
1152 if (rtp_packet[block_pos] != first_block_byte) {
1153 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1154 "Failed to update absolute send time.");
1155 return false;
1156 }
1157 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1158 // fractional part).
1159 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1160 ((now_ms << 18) / 1000) & 0x00ffffff);
1161 return true;
1162}
1163
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001164void RTPSender::SetSendingStatus(const bool enabled) {
1165 if (enabled) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001166 uint32_t frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001167 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001168 uint32_t frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001169
1170 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001171 switch (frequency) {
1172 case 8000:
1173 case 12000:
1174 case 16000:
1175 case 24000:
1176 case 32000:
1177 break;
1178 default:
1179 assert(false);
1180 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001181 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +00001182 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001183 } else {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +00001184 frequency_hz = kDefaultVideoFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001185 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001186 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001187
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001188 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001189 SetStartTimestamp(RTPtime, false);
1190 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001191 if (!ssrc_forced_) {
1192 // Generate a new SSRC.
1193 ssrc_db_.ReturnSSRC(ssrc_);
1194 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001195 }
1196 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001197 if (!sequence_number_forced_ && !ssrc_forced_) {
1198 // Generate a new sequence number.
1199 sequence_number_ =
1200 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001201 }
1202 }
1203}
1204
1205void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001206 CriticalSectionScoped cs(send_critsect_);
1207 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001208}
1209
1210bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001211 CriticalSectionScoped cs(send_critsect_);
1212 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001213}
1214
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001215uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001216 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001217 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001218}
1219
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001220void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001221 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001222 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001223 start_time_stamp_forced_ = force;
1224 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001225 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001226 if (!start_time_stamp_forced_) {
1227 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001228 }
1229 }
1230}
1231
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001232uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001233 CriticalSectionScoped cs(send_critsect_);
1234 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001235}
1236
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001237uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001238 // If configured via API, return 0.
1239 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001240
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001241 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001242 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001243 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001244 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1245 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001246}
1247
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001248void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001249 // This is configured via the API.
1250 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001251
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001252 if (ssrc_ == ssrc && ssrc_forced_) {
1253 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001254 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001255 ssrc_forced_ = true;
1256 ssrc_db_.ReturnSSRC(ssrc_);
1257 ssrc_db_.RegisterSSRC(ssrc);
1258 ssrc_ = ssrc;
1259 if (!sequence_number_forced_) {
1260 sequence_number_ =
1261 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001262 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001263}
1264
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001265uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001266 CriticalSectionScoped cs(send_critsect_);
1267 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001268}
1269
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001270void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001271 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001272}
1273
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001274void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1275 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001276 assert(arr_length <= kRtpCsrcSize);
1277 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001278
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001279 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001280 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001281 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001282 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001283}
1284
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001285int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001286 assert(arr_of_csrc);
1287 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001288 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1289 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001290 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001291 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001292}
1293
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001294void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001295 CriticalSectionScoped cs(send_critsect_);
1296 sequence_number_forced_ = true;
1297 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001298}
1299
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001300uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001301 CriticalSectionScoped cs(send_critsect_);
1302 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001303}
1304
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001305// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001306int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1307 const uint16_t time_ms,
1308 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001309 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001310 return -1;
1311 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001312 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001313}
1314
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001315bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001316 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001317 return false;
1318 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001319 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001320}
1321
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001322int32_t RTPSender::SetAudioPacketSize(
1323 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001324 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001325 return -1;
1326 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001327 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001328}
1329
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001330int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1331 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001332 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001333 return -1;
1334 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001335 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001336}
1337
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001338int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1339 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001340 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001341}
1342
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001343int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001344 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001345}
1346
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001347int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001348 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001349 return -1;
1350 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001351 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001352}
1353
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001354int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001355 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001356 return -1;
1357 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001358 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001359}
1360
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001361// Video
1362VideoCodecInformation *RTPSender::CodecInformationVideo() {
1363 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001364 return NULL;
1365 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001366 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001367}
1368
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001369RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001370 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001371 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001372}
1373
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001374uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001375 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001376 return 0;
1377 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001378 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001379}
1380
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001381int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001382 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001383 return -1;
1384 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001385 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001386}
1387
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001388int32_t RTPSender::SetGenericFECStatus(
1389 const bool enable, const uint8_t payload_type_red,
1390 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001391 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001392 return -1;
1393 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001394 return video_->SetGenericFECStatus(enable, payload_type_red,
1395 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001396}
1397
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001398int32_t RTPSender::GenericFECStatus(
1399 bool *enable, uint8_t *payload_type_red,
1400 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001401 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001402 return -1;
1403 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001404 return video_->GenericFECStatus(
1405 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001406}
1407
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001408int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001409 const FecProtectionParams *delta_params,
1410 const FecProtectionParams *key_params) {
1411 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001412 return -1;
1413 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001414 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001415}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001416
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001417void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1418 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001419 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001420 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001421 // Add RTX header.
1422 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001423 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001424
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001425 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001426 rtp_parser.Parse(rtp_header);
1427
1428 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001429 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001430
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001431 // Replace payload type, if a specific type is set for RTX.
1432 if (payload_type_rtx_ != -1) {
1433 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001434 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001435 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1436 }
1437
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001438 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001439 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001440 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1441
1442 // Replace SSRC.
1443 ptr += 6;
1444 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1445
1446 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001447 ptr = data_buffer_rtx + rtp_header.headerLength;
1448 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001449 ptr += 2;
1450
1451 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001452 memcpy(ptr, buffer + rtp_header.headerLength,
1453 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001454 *length += 2;
1455}
1456
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001457} // namespace webrtc