blob: e5ca8d995b8acfd92f048f1e26aaaaa2b24f4c66 [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
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <stdlib.h> // 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 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000353 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
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
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000361 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000362 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
363 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000364 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000365 frame_type == kFrameEmpty);
366
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000367 return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
368 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000369 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000370 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
371 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000372 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000373
374 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000375 if (paced_sender_->Enabled()) {
376 // Padding is driven by the pacer and not by the encoder.
377 return 0;
378 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000379 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000380 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000381 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000382 return video_->SendVideo(video_type, frame_type, payload_type,
383 capture_timestamp, capture_time_ms, payload_data,
384 payload_size, fragmentation, codec_info,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000385 rtp_type_hdr);
386 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000387}
388
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000389bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000390 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000391 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000392 // Current bitrate since last estimate(1 second) averaged with the
393 // estimate since then, to get the most up to date bitrate.
394 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000395 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000396 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000397 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000398 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000399 int bytes = 0;
400 if (current_bitrate == 0) {
401 // Start up phase. Send one 33.3 ms batch to start with.
402 bytes = (bitrate_diff / 8) / 30;
403 } else {
404 bytes = (bitrate_diff / 8);
405 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000406 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000407 if (bytes > bytes_cap) {
408 bytes = bytes_cap;
409 }
410 }
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000411 uint32_t timestamp;
412 {
413 CriticalSectionScoped cs(send_critsect_);
414 // Add the random RTP timestamp offset and store the capture time for
415 // later calculation of the send time offset.
416 timestamp = start_time_stamp_ + capture_timestamp;
417 timestamp_ = timestamp;
418 capture_time_ms_ = capture_time_ms;
419 }
420 int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
421 bytes, kDontRetransmit, false, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000422 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
423 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000424}
425
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000426int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
427 int32_t bytes) {
428 int padding_bytes_in_packet = kMaxPaddingLength;
429 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000430 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000431 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000432 packet[0] |= 0x20; // Set padding bit.
433 int32_t *data =
434 reinterpret_cast<int32_t *>(&(packet[header_length]));
435
436 // Fill data buffer with random data.
437 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
438 data[j] = rand(); // NOLINT
439 }
440 // Set number of padding bytes in the last byte of the packet.
441 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
442 return padding_bytes_in_packet;
443}
444
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000445int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
446 int64_t capture_time_ms, int32_t bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000447 StorageType store, bool force_full_size_packets,
448 bool only_pad_after_markerbit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000449 // Drop this packet if we're not sending media packets.
450 if (!sending_media_) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000451 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000452 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000453 int padding_bytes_in_packet = 0;
454 int bytes_sent = 0;
455 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000456 // Always send full padding packets.
457 if (force_full_size_packets && bytes < kMaxPaddingLength)
458 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000459 if (bytes < kMaxPaddingLength) {
460 if (force_full_size_packets) {
461 bytes = kMaxPaddingLength;
462 } else {
463 // Round to the nearest multiple of 32.
464 bytes = (bytes + 16) & 0xffe0;
465 }
466 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000467 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000468 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000469 break;
470 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000471 uint32_t ssrc;
472 uint16_t sequence_number;
473 {
474 CriticalSectionScoped cs(send_critsect_);
475 // Only send padding packets following the last packet of a frame,
476 // indicated by the marker bit.
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000477 if (only_pad_after_markerbit && !last_packet_marker_bit_)
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000478 return bytes_sent;
479 if (rtx_ == kRtxOff) {
480 ssrc = ssrc_;
481 sequence_number = sequence_number_;
482 ++sequence_number_;
483 } else {
484 ssrc = ssrc_rtx_;
485 sequence_number = sequence_number_rtx_;
486 ++sequence_number_rtx_;
487 }
488 }
489 uint8_t padding_packet[IP_PACKET_SIZE];
490 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
491 false, timestamp, sequence_number, NULL,
492 0);
493 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
494 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000495 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
496 header_length, capture_time_ms, store,
497 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000498 // Error sending the packet.
499 break;
500 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000501 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000502 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000503 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000504}
505
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000506void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000507 const uint16_t number_to_store) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000508 packet_history_->SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000509}
510
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000511bool RTPSender::StorePackets() const {
512 return packet_history_->StorePackets();
513}
niklase@google.com470e71d2011-07-07 08:21:25 +0000514
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000515int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
516 uint16_t length = IP_PACKET_SIZE;
517 uint8_t data_buffer[IP_PACKET_SIZE];
518 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000519 int64_t capture_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000520 StorageType type;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000521 if (!packet_history_->GetRTPPacket(packet_id, min_resend_time, data_buffer,
522 &length, &capture_time_ms, &type)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000523 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000524 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000525 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000526 if (length == 0 || type == kDontRetransmit) {
527 // No bytes copied (packet recently resent, skip resending) or
528 // packet should not be retransmitted.
529 return 0;
530 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000531
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000532 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000533 if (rtx_ != kRtxOff) {
534 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000535 buffer_to_send_ptr = data_buffer_rtx;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000536 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000537
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000538 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000539 RTPHeader header;
540 rtp_parser.Parse(header);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000541
542 // Store the time when the packet was last sent or added to pacer.
543 packet_history_->UpdateResendTime(packet_id);
544
545 {
546 // Update send statistics prior to pacer.
547 CriticalSectionScoped cs(send_critsect_);
548 Bitrate::Update(length);
549 packets_sent_++;
550 // We on purpose don't add to payload_bytes_sent_ since this is a
551 // re-transmit and not new payload data.
552 }
553
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000554 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000555 "timestamp", header.timestamp,
556 "seqnum", header.sequenceNumber);
557
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000558 if (paced_sender_) {
559 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000560 header.ssrc,
561 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000562 capture_time_ms,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000563 length - header.headerLength)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000564 // We can't send the packet right now.
565 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000566 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000567 }
568 }
569
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000570 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000571 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000572 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000573 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000574}
575
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000576bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
577 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000578 if (transport_) {
579 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000580 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000581 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
582 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000583 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000584 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000585 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
586 "Transport failed to send packet");
587 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000588 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000589 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000590}
591
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000592int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000593 if (!video_)
594 return -1;
595 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000596}
597
598int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000599 if (!video_)
600 return -1;
601 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000602}
603
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000604void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000605 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000606 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000607 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
608 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000609 const int64_t now = clock_->TimeInMilliseconds();
610 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000611
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000612 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000613 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000614 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000615 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000616 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000617 return;
618 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000619
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000620 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
621 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000622 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000623 if (bytes_sent > 0) {
624 bytes_re_sent += bytes_sent;
625 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000626 // The packet has previously been resent.
627 // Try resending next packet in the list.
628 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000629 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000630 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000631 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000632 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000633 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000634 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000635 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000636 // Delay bandwidth estimate (RTT * BW).
637 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000638 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000639 uint32_t target_bytes =
640 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000641 if (bytes_re_sent > target_bytes) {
642 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000643 }
644 }
645 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000646 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000647 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000648 UpdateNACKBitRate(bytes_re_sent, now);
649 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000650 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000651}
652
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000653bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
654 uint32_t num = 0;
655 int32_t byte_count = 0;
656 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000657
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000658 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000659
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000660 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000661 return true;
662 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000663 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
664 if ((now - nack_byte_count_times_[num]) > avg_interval) {
665 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000666 break;
667 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000668 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000669 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000670 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000671 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000672 if (num == NACK_BYTECOUNT_SIZE) {
673 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000674 // during the last msg_interval.
675 time_interval = now - nack_byte_count_times_[num - 1];
676 if (time_interval < 0) {
677 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000678 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000679 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000680 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000681}
682
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000683void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
684 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000685 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000686
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000687 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000688 if (bytes > 0) {
689 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000690 // Add padding length.
691 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000692 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000693 if (nack_byte_count_times_[0] == 0) {
694 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000695 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000696 // Shift.
697 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
698 nack_byte_count_[i + 1] = nack_byte_count_[i];
699 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000700 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000701 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000702 nack_byte_count_[0] = bytes;
703 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000704 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000705 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000706}
707
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000708// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000709bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000710 int64_t capture_time_ms) {
711 StorageType type;
712 uint16_t length = IP_PACKET_SIZE;
713 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000714 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000715
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000716 if (packet_history_ == NULL) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000717 // Packet cannot be found. Allow sending to continue.
718 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000719 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000720 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
721 &stored_time_ms, &type)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000722 // Packet cannot be found. Allow sending to continue.
723 return true;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000724 }
725 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000726
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000727 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000728 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000729 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000730 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000731 "timestamp", rtp_header.timestamp,
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000732 "seqnum", sequence_number);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000733
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000734 int64_t now_ms = clock_->TimeInMilliseconds();
735 int64_t diff_ms = now_ms - capture_time_ms;
736 bool updated_transmission_time_offset =
737 UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms);
738 bool updated_abs_send_time =
739 UpdateAbsoluteSendTime(data_buffer, length, rtp_header, now_ms);
740 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000741 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000742 packet_history_->ReplaceRTPHeader(data_buffer,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000743 rtp_header.sequenceNumber,
744 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000745 }
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000746 return SendPacketToNetwork(data_buffer, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000747}
748
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000749int RTPSender::TimeToSendPadding(int bytes) {
750 if (!sending_media_) {
751 return 0;
752 }
753 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000754 int64_t capture_time_ms;
755 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000756 {
757 CriticalSectionScoped cs(send_critsect_);
758 payload_type = (rtx_ == kRtxOff) ? payload_type_ : payload_type_rtx_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000759 timestamp = timestamp_;
760 capture_time_ms = capture_time_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000761 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000762 return SendPadData(payload_type, timestamp, capture_time_ms, bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000763 kDontStore, true, true);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000764}
765
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000766// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000767int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000768 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000769 int64_t capture_time_ms, StorageType storage,
770 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000771 ModuleRTPUtility::RTPHeaderParser rtp_parser(
772 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000773 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000774 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000775
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000776 int64_t now_ms = clock_->TimeInMilliseconds();
777
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000778 // |capture_time_ms| <= 0 is considered invalid.
779 // TODO(holmer): This should be changed all over Video Engine so that negative
780 // time is consider invalid, while 0 is considered a valid time.
781 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000782 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000783 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000784 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000785
786 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
787 rtp_header, now_ms);
788
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000789 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000790 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
791 max_payload_length_, capture_time_ms,
792 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000793 return -1;
794 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000795
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000796 // Create and send RTX Packet.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000797 // TODO(pwesin): This should be moved to its own code path triggered by pacer.
798 bool rtx_sent = false;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000799 if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000800 uint16_t length_rtx = payload_length + rtp_header_length;
801 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000802 BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000803 if (!SendPacketToNetwork(data_buffer_rtx, length_rtx)) return -1;
804 rtx_sent = true;
805 }
806 {
807 // Update send statistics prior to pacer.
808 CriticalSectionScoped cs(send_critsect_);
809 Bitrate::Update(payload_length + rtp_header_length);
810 ++packets_sent_;
811 payload_bytes_sent_ += payload_length;
812 if (rtx_sent) {
813 // The RTX packet.
814 ++packets_sent_;
815 payload_bytes_sent_ += payload_length;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000816 }
817 }
818
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000819 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000820 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
821 rtp_header.sequenceNumber, capture_time_ms,
822 payload_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000823 // We can't send the packet right now.
824 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000825 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000826 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000827 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000828 if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
829 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000830 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000831 return -1;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000832}
833
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000834void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000835 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000836 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000837 nack_bitrate_.Process();
838 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000839 return;
840 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000841 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000842}
843
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000844uint16_t RTPSender::RTPHeaderLength() const {
845 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000846 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000847 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000848 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000849 rtp_header_length += RtpHeaderExtensionTotalLength();
850 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000851}
852
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000853uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000854 CriticalSectionScoped cs(send_critsect_);
855 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000856}
857
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000858void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000859 packets_sent_ = 0;
860 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000861}
862
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000863uint32_t RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000864 // Don't use critsect to avoid potential deadlock.
865 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000866}
867
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000868// Number of sent RTP bytes.
869// Don't use critsect to avoid potental deadlock.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000870uint32_t RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000871 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000872}
873
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000874int RTPSender::CreateRTPHeader(
875 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
876 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
877 uint8_t num_csrcs) const {
878 header[0] = 0x80; // version 2.
879 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000880 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000881 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000882 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000883 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
884 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
885 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000886 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000887
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000888 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000889 if (num_csrcs > 0) {
890 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000891 // error
892 assert(false);
893 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000894 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000895 uint8_t *ptr = &header[rtp_header_length];
896 for (int i = 0; i < num_csrcs; ++i) {
897 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000898 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000899 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000900 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000901
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000902 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000903 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000904 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000905
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000906 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
907 if (len > 0) {
908 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000909 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000910 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000911 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000912}
913
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000914int32_t RTPSender::BuildRTPheader(
915 uint8_t *data_buffer, const int8_t payload_type,
916 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000917 int64_t capture_time_ms, const bool time_stamp_provided,
918 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000919 assert(payload_type >= 0);
920 CriticalSectionScoped cs(send_critsect_);
921
922 if (time_stamp_provided) {
923 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000924 } else {
925 // Make a unique time stamp.
926 // We can't inc by the actual time, since then we increase the risk of back
927 // timing.
928 timestamp_++;
929 }
930 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000931 capture_time_ms_ = capture_time_ms;
932 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000933 int csrcs_length = 0;
934 if (include_csrcs_)
935 csrcs_length = num_csrcs_;
936 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
937 timestamp_, sequence_number, csrcs_, csrcs_length);
938}
939
940uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000941 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000942 return 0;
943 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000944 // RTP header extension, RFC 3550.
945 // 0 1 2 3
946 // 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
947 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
948 // | defined by profile | length |
949 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
950 // | header extension |
951 // | .... |
952 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000953 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000954 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000955
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000956 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000957 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000958 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000959
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000960 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000961 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000962
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000963 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000964 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000965 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000966 switch (type) {
967 case kRtpExtensionTransmissionTimeOffset:
968 block_length = BuildTransmissionTimeOffsetExtension(
969 data_buffer + kHeaderLength + total_block_length);
970 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +0000971 case kRtpExtensionAudioLevel:
972 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
973 // we don't have to care about it here, which is true until we wan't to
974 // use it together with any of the other extensions we support.
975 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000976 case kRtpExtensionAbsoluteSendTime:
977 block_length = BuildAbsoluteSendTimeExtension(
978 data_buffer + kHeaderLength + total_block_length);
979 break;
980 default:
981 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000982 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000983 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000984 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000985 }
986 if (total_block_length == 0) {
987 // No extension added.
988 return 0;
989 }
990 // Set header length (in number of Word32, header excluded).
991 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000992 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000993 total_block_length / 4);
994 // Total added length.
995 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000996}
997
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000998uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
999 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001000 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1001 //
1002 // The transmission time is signaled to the receiver in-band using the
1003 // general mechanism for RTP header extensions [RFC5285]. The payload
1004 // of this extension (the transmitted value) is a 24-bit signed integer.
1005 // When added to the RTP timestamp of the packet, it represents the
1006 // "effective" RTP transmission time of the packet, on the RTP
1007 // timescale.
1008 //
1009 // The form of the transmission offset extension block:
1010 //
1011 // 0 1 2 3
1012 // 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
1013 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1014 // | ID | len=2 | transmission offset |
1015 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001016
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001017 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001018 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001019 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1020 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001021 // Not registered.
1022 return 0;
1023 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001024 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001025 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001026 data_buffer[pos++] = (id << 4) + len;
1027 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1028 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001029 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001030 assert(pos == kTransmissionTimeOffsetLength);
1031 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001032}
1033
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001034uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
1035 uint8_t* data_buffer) const {
1036 // Absolute send time in RTP streams.
1037 //
1038 // The absolute send time is signaled to the receiver in-band using the
1039 // general mechanism for RTP header extensions [RFC5285]. The payload
1040 // of this extension (the transmitted value) is a 24-bit unsigned integer
1041 // containing the sender's current time in seconds as a fixed point number
1042 // with 18 bits fractional part.
1043 //
1044 // The form of the absolute send time extension block:
1045 //
1046 // 0 1 2 3
1047 // 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
1048 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1049 // | ID | len=2 | absolute send time |
1050 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1051
1052 // Get id defined by user.
1053 uint8_t id;
1054 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1055 &id) != 0) {
1056 // Not registered.
1057 return 0;
1058 }
1059 size_t pos = 0;
1060 const uint8_t len = 2;
1061 data_buffer[pos++] = (id << 4) + len;
1062 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1063 absolute_send_time_);
1064 pos += 3;
1065 assert(pos == kAbsoluteSendTimeLength);
1066 return kAbsoluteSendTimeLength;
1067}
1068
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001069bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001070 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001071 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001072 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001073
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001074 // Get length until start of header extension block.
1075 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001076 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001077 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001078 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001079 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001080 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001081 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001082 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001083 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001084 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001085 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001086 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001087 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001088 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001089 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001090 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001091 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001092 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1093 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001094 WEBRTC_TRACE(
1095 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001096 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001097 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001098 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001099 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001100 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001101 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1102 &id) != 0) {
1103 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001104 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001105 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001106 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001107 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001108 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001109 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001110 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001111 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001112 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001113 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001114 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001115 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001116 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001117 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001118}
1119
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001120bool RTPSender::UpdateAbsoluteSendTime(
1121 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001122 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001123 CriticalSectionScoped cs(send_critsect_);
1124
1125 // Get length until start of header extension block.
1126 int extension_block_pos =
1127 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1128 kRtpExtensionAbsoluteSendTime);
1129 if (extension_block_pos < 0) {
1130 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1131 "Failed to update absolute send time, not registered.");
1132 return false;
1133 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001134 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001135 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001136 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001137 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1138 "Failed to update absolute send time, invalid length.");
1139 return false;
1140 }
1141 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001142 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1143 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001144 WEBRTC_TRACE(
1145 kTraceStream, kTraceRtpRtcp, id_,
1146 "Failed to update absolute send time, hdr extension not found.");
1147 return false;
1148 }
1149 // Get id.
1150 uint8_t id = 0;
1151 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1152 &id) != 0) {
1153 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1154 "Failed to update absolute send time, no id.");
1155 return false;
1156 }
1157 // Verify first byte in block.
1158 const uint8_t first_block_byte = (id << 4) + 2;
1159 if (rtp_packet[block_pos] != first_block_byte) {
1160 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1161 "Failed to update absolute send time.");
1162 return false;
1163 }
1164 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1165 // fractional part).
1166 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1167 ((now_ms << 18) / 1000) & 0x00ffffff);
1168 return true;
1169}
1170
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001171void RTPSender::SetSendingStatus(const bool enabled) {
1172 if (enabled) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001173 uint32_t frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001174 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001175 uint32_t frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001176
1177 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001178 switch (frequency) {
1179 case 8000:
1180 case 12000:
1181 case 16000:
1182 case 24000:
1183 case 32000:
1184 break;
1185 default:
1186 assert(false);
1187 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001188 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +00001189 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001190 } else {
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001191 frequency_hz = kVideoPayloadTypeFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001192 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001193 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001194
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001195 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001196 SetStartTimestamp(RTPtime, false);
1197 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001198 if (!ssrc_forced_) {
1199 // Generate a new SSRC.
1200 ssrc_db_.ReturnSSRC(ssrc_);
1201 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001202 }
1203 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001204 if (!sequence_number_forced_ && !ssrc_forced_) {
1205 // Generate a new sequence number.
1206 sequence_number_ =
1207 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001208 }
1209 }
1210}
1211
1212void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001213 CriticalSectionScoped cs(send_critsect_);
1214 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001215}
1216
1217bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001218 CriticalSectionScoped cs(send_critsect_);
1219 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001220}
1221
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001222uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001223 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001224 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001225}
1226
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001227void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001228 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001229 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001230 start_time_stamp_forced_ = force;
1231 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001232 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001233 if (!start_time_stamp_forced_) {
1234 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001235 }
1236 }
1237}
1238
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001239uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001240 CriticalSectionScoped cs(send_critsect_);
1241 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001242}
1243
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001244uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001245 // If configured via API, return 0.
1246 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001247
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001248 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001249 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001250 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001251 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1252 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001253}
1254
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001255void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001256 // This is configured via the API.
1257 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001258
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001259 if (ssrc_ == ssrc && ssrc_forced_) {
1260 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001261 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001262 ssrc_forced_ = true;
1263 ssrc_db_.ReturnSSRC(ssrc_);
1264 ssrc_db_.RegisterSSRC(ssrc);
1265 ssrc_ = ssrc;
1266 if (!sequence_number_forced_) {
1267 sequence_number_ =
1268 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001269 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001270}
1271
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001272uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001273 CriticalSectionScoped cs(send_critsect_);
1274 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001275}
1276
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001277void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001278 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001279}
1280
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001281void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1282 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001283 assert(arr_length <= kRtpCsrcSize);
1284 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001285
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001286 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001287 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001288 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001289 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001290}
1291
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001292int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001293 assert(arr_of_csrc);
1294 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001295 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1296 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001297 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001298 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001299}
1300
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001301void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001302 CriticalSectionScoped cs(send_critsect_);
1303 sequence_number_forced_ = true;
1304 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001305}
1306
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001307uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001308 CriticalSectionScoped cs(send_critsect_);
1309 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001310}
1311
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001312// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001313int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1314 const uint16_t time_ms,
1315 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001316 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001317 return -1;
1318 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001319 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001320}
1321
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001322bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001323 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001324 return false;
1325 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001326 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001327}
1328
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001329int32_t RTPSender::SetAudioPacketSize(
1330 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001331 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001332 return -1;
1333 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001334 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001335}
1336
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001337int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1338 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001339 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001340 return -1;
1341 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001342 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001343}
1344
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001345int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1346 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001347 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001348}
1349
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001350int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001351 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001352}
1353
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001354int32_t RTPSender::SetRED(const int8_t payload_type) {
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_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001359}
1360
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001361int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001362 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001363 return -1;
1364 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001365 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001366}
1367
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001368// Video
1369VideoCodecInformation *RTPSender::CodecInformationVideo() {
1370 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001371 return NULL;
1372 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001373 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001374}
1375
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001376RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001377 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001378 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001379}
1380
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001381uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001382 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001383 return 0;
1384 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001385 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001386}
1387
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001388int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001389 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001390 return -1;
1391 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001392 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001393}
1394
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001395int32_t RTPSender::SetGenericFECStatus(
1396 const bool enable, const uint8_t payload_type_red,
1397 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001398 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001399 return -1;
1400 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001401 return video_->SetGenericFECStatus(enable, payload_type_red,
1402 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001403}
1404
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001405int32_t RTPSender::GenericFECStatus(
1406 bool *enable, uint8_t *payload_type_red,
1407 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001408 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001409 return -1;
1410 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001411 return video_->GenericFECStatus(
1412 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001413}
1414
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001415int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001416 const FecProtectionParams *delta_params,
1417 const FecProtectionParams *key_params) {
1418 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001419 return -1;
1420 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001421 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001422}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001423
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001424void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1425 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001426 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001427 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001428 // Add RTX header.
1429 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001430 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001431
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001432 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001433 rtp_parser.Parse(rtp_header);
1434
1435 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001436 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001437
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001438 // Replace payload type, if a specific type is set for RTX.
1439 if (payload_type_rtx_ != -1) {
1440 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001441 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001442 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1443 }
1444
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001445 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001446 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001447 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1448
1449 // Replace SSRC.
1450 ptr += 6;
1451 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1452
1453 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001454 ptr = data_buffer_rtx + rtp_header.headerLength;
1455 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001456 ptr += 2;
1457
1458 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001459 memcpy(ptr, buffer + rtp_header.headerLength,
1460 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001461 *length += 2;
1462}
1463
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001464} // namespace webrtc