blob: 0f08573efdbd94861a667ffdc0a06a73db79ecf9 [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_sender_audio.h"
16#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
17#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
18#include "webrtc/system_wrappers/interface/trace.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000019#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000022
stefan@webrtc.orga8179622013-06-04 13:47:36 +000023// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
24const int kMaxPaddingLength = 224;
25
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000026namespace {
27
28const char* FrameTypeToString(const FrameType frame_type) {
29 switch (frame_type) {
30 case kFrameEmpty: return "empty";
31 case kAudioFrameSpeech: return "audio_speech";
32 case kAudioFrameCN: return "audio_cn";
33 case kVideoFrameKey: return "video_key";
34 case kVideoFrameDelta: return "video_delta";
35 case kVideoFrameGolden: return "video_golden";
36 case kVideoFrameAltRef: return "video_altref";
37 }
38 return "";
39}
40
41} // namespace
42
pbos@webrtc.org2f446732013-04-08 11:08:41 +000043RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000044 Transport *transport, RtpAudioFeedback *audio_feedback,
45 PacedSender *paced_sender)
46 : Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL),
47 video_(NULL), paced_sender_(paced_sender),
48 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
49 transport_(transport), sending_media_(true), // Default to sending media.
50 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
51 target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1),
52 payload_type_map_(), rtp_header_extension_map_(),
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +000053 transmission_time_offset_(0), absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000054 // NACK.
55 nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000056 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000057 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +000058 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000059 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),
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +000062 timestamp_(0), capture_time_ms_(0), last_timestamp_time_ms_(0),
63 last_packet_marker_bit_(false), num_csrcs_(0), csrcs_(),
64 include_csrcs_(true), 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 audio_;
100 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000101
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000102 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000103}
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000105void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000106 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000108
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000109uint16_t RTPSender::ActualSendBitrateKbit() const {
110 return (uint16_t)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111}
112
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000113uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000114 if (video_) {
115 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000116 }
117 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000118}
119
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000120uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000121 if (video_) {
122 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000123 }
124 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000125}
126
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000127uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000128 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000129}
130
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000131int32_t RTPSender::SetTransmissionTimeOffset(
132 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000133 if (transmission_time_offset > (0x800000 - 1) ||
134 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000135 return -1;
136 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000137 CriticalSectionScoped cs(send_critsect_);
138 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000139 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000140}
141
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000142int32_t RTPSender::SetAbsoluteSendTime(
143 const uint32_t absolute_send_time) {
144 if (absolute_send_time > 0xffffff) { // UWord24.
145 return -1;
146 }
147 CriticalSectionScoped cs(send_critsect_);
148 absolute_send_time_ = absolute_send_time;
149 return 0;
150}
151
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000152int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
153 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000154 CriticalSectionScoped cs(send_critsect_);
155 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000156}
157
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000158int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000159 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000160 CriticalSectionScoped cs(send_critsect_);
161 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000162}
163
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000164uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000165 CriticalSectionScoped cs(send_critsect_);
166 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000167}
168
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000169int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000170 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000171 const int8_t payload_number, const uint32_t frequency,
172 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000173 assert(payload_name);
174 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000175
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000176 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000177 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000178
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000179 if (payload_type_map_.end() != it) {
180 // We already use this payload type.
181 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000182 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000183
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000184 // Check if it's the same as we already have.
185 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000186 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000187 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000188 payload->typeSpecific.Audio.frequency == frequency &&
189 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000190 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000191 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000192 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000193 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000194 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000195 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000196 return 0;
197 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000198 }
199 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000200 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000201 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000202 ModuleRTPUtility::Payload *payload = NULL;
203 if (audio_configured_) {
204 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
205 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000206 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000207 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
208 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000209 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000210 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000211 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000212 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000213 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000214}
215
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000216int32_t RTPSender::DeRegisterSendPayload(
217 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000218 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000219
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000220 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000221 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000222
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000223 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000224 return -1;
225 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000226 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000227 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000228 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000229 return 0;
230}
niklase@google.com470e71d2011-07-07 08:21:25 +0000231
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000232int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000233
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000234int RTPSender::SendPayloadFrequency() const {
235 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
236}
niklase@google.com470e71d2011-07-07 08:21:25 +0000237
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000238int32_t RTPSender::SetMaxPayloadLength(
239 const uint16_t max_payload_length,
240 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000241 // Sanity check.
242 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
243 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
244 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000245 return -1;
246 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000247 CriticalSectionScoped cs(send_critsect_);
248 max_payload_length_ = max_payload_length;
249 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000250
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000251 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
252 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000253 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000254}
255
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000256uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000257 if (audio_configured_) {
258 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000259 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000260 return max_payload_length_ - RTPHeaderLength() -
261 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
262 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000263 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000264}
265
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000266uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000267 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000268}
269
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000270uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000271
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000272void RTPSender::SetRTXStatus(int mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000273 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000274 rtx_ = mode;
275 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000276 if (set_ssrc) {
277 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000278 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000279 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000280 }
281 }
282}
283
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000284void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000285 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000286 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000287 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000288 *ssrc = ssrc_rtx_;
289 *payload_type = payload_type_rtx_;
290}
291
292
293void RTPSender::SetRtxPayloadType(int payload_type) {
294 CriticalSectionScoped cs(send_critsect_);
295 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000296}
297
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000298int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
299 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000300 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000301
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000302 if (payload_type < 0) {
303 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
304 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000305 return -1;
306 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000307 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000308 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000309 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000310 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000311 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000312 // And it's a match...
313 return 0;
314 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000315 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000316 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000317 if (payload_type_ == payload_type) {
318 if (!audio_configured_) {
319 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000320 }
321 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000322 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000323 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000324 payload_type_map_.find(payload_type);
325 if (it == payload_type_map_.end()) {
326 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
327 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000328 return -1;
329 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000330 payload_type_ = payload_type;
331 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000332 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000333 if (!payload->audio && !audio_configured_) {
334 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
335 *video_type = payload->typeSpecific.Video.videoCodecType;
336 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000337 }
338 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000339}
340
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000341int32_t RTPSender::SendOutgoingData(
342 const FrameType frame_type, const int8_t payload_type,
343 const uint32_t capture_timestamp, int64_t capture_time_ms,
344 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000345 const RTPFragmentationHeader *fragmentation,
346 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000347 {
348 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000349 CriticalSectionScoped cs(send_critsect_);
350 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000351 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000352 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000353 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000354 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000355 if (CheckPayloadType(payload_type, &video_type) != 0) {
356 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
357 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000358 __FUNCTION__, payload_type);
359 return -1;
360 }
361
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000362 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000363 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
364 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000365 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000366 frame_type == kFrameEmpty);
367
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000368 return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
369 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000370 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000371 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
372 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000373 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000374
375 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000376 if (paced_sender_->Enabled()) {
377 // Padding is driven by the pacer and not by the encoder.
378 return 0;
379 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000380 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000381 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000382 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000383 return video_->SendVideo(video_type, frame_type, payload_type,
384 capture_timestamp, capture_time_ms, payload_data,
385 payload_size, fragmentation, codec_info,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000386 rtp_type_hdr);
387 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000388}
389
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000390int RTPSender::SendRedundantPayloads(int payload_type, int bytes_to_send) {
391 if (!(rtx_ & kRtxRedundantPayloads))
392 return 0;
393 uint8_t buffer[IP_PACKET_SIZE];
394 int bytes_left = bytes_to_send;
395 while (bytes_left > 0) {
396 uint16_t length = bytes_left;
397 int64_t capture_time_ms;
398 if (!packet_history_.GetBestFittingPacket(buffer, &length,
399 &capture_time_ms)) {
400 break;
401 }
402 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true))
403 return -1;
404 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
405 RTPHeader rtp_header;
406 rtp_parser.Parse(rtp_header);
407 bytes_left -= length - rtp_header.headerLength;
408 }
409 return bytes_to_send - bytes_left;
410}
411
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000412bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000413 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000414 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000415 // Current bitrate since last estimate(1 second) averaged with the
416 // estimate since then, to get the most up to date bitrate.
417 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000418 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000419 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000420 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000421 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000422 int bytes = 0;
423 if (current_bitrate == 0) {
424 // Start up phase. Send one 33.3 ms batch to start with.
425 bytes = (bitrate_diff / 8) / 30;
426 } else {
427 bytes = (bitrate_diff / 8);
428 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000429 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000430 if (bytes > bytes_cap) {
431 bytes = bytes_cap;
432 }
433 }
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000434 uint32_t timestamp;
435 {
436 CriticalSectionScoped cs(send_critsect_);
437 // Add the random RTP timestamp offset and store the capture time for
438 // later calculation of the send time offset.
439 timestamp = start_time_stamp_ + capture_timestamp;
440 timestamp_ = timestamp;
441 capture_time_ms_ = capture_time_ms;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000442 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000443 }
444 int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
445 bytes, kDontRetransmit, false, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000446 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
447 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000448}
449
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000450int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
451 int32_t bytes) {
452 int padding_bytes_in_packet = kMaxPaddingLength;
453 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000454 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000455 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000456 packet[0] |= 0x20; // Set padding bit.
457 int32_t *data =
458 reinterpret_cast<int32_t *>(&(packet[header_length]));
459
460 // Fill data buffer with random data.
461 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
462 data[j] = rand(); // NOLINT
463 }
464 // Set number of padding bytes in the last byte of the packet.
465 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
466 return padding_bytes_in_packet;
467}
468
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000469int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
470 int64_t capture_time_ms, int32_t bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000471 StorageType store, bool force_full_size_packets,
472 bool only_pad_after_markerbit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000473 // Drop this packet if we're not sending media packets.
474 if (!sending_media_) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000475 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000476 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000477 int padding_bytes_in_packet = 0;
478 int bytes_sent = 0;
479 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000480 // Always send full padding packets.
481 if (force_full_size_packets && bytes < kMaxPaddingLength)
482 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000483 if (bytes < kMaxPaddingLength) {
484 if (force_full_size_packets) {
485 bytes = kMaxPaddingLength;
486 } else {
487 // Round to the nearest multiple of 32.
488 bytes = (bytes + 16) & 0xffe0;
489 }
490 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000491 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000492 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000493 break;
494 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000495 uint32_t ssrc;
496 uint16_t sequence_number;
497 {
498 CriticalSectionScoped cs(send_critsect_);
499 // Only send padding packets following the last packet of a frame,
500 // indicated by the marker bit.
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000501 if (only_pad_after_markerbit && !last_packet_marker_bit_)
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000502 return bytes_sent;
503 if (rtx_ == kRtxOff) {
504 ssrc = ssrc_;
505 sequence_number = sequence_number_;
506 ++sequence_number_;
507 } else {
508 ssrc = ssrc_rtx_;
509 sequence_number = sequence_number_rtx_;
510 ++sequence_number_rtx_;
511 }
512 }
513 uint8_t padding_packet[IP_PACKET_SIZE];
514 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
515 false, timestamp, sequence_number, NULL,
516 0);
517 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
518 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000519 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
520 header_length, capture_time_ms, store,
521 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000522 // Error sending the packet.
523 break;
524 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000525 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000526 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000527 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000528}
529
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000530void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000531 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000532 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000533}
534
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000535bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000536 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000537}
niklase@google.com470e71d2011-07-07 08:21:25 +0000538
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000539int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
540 uint16_t length = IP_PACKET_SIZE;
541 uint8_t data_buffer[IP_PACKET_SIZE];
542 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000543 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000544 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
545 data_buffer, &length,
546 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000547 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000548 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000549 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000550
551 {
552 // Update send statistics prior to pacer.
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000553 CriticalSectionScoped lock(statistics_crit_.get());
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000554 Bitrate::Update(length);
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000555 ++packets_sent_;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000556 // We on purpose don't add to payload_bytes_sent_ since this is a
557 // re-transmit and not new payload data.
558 }
559
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000560
561 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
562 RTPHeader header;
stefan@webrtc.org79b63202013-12-04 13:34:28 +0000563 if (!rtp_parser.Parse(header)) {
564 assert(false);
565 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
566 "Failed to parse RTP header of packet to be retransmitted.");
567 return -1;
568 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000569 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000570 "timestamp", header.timestamp,
571 "seqnum", header.sequenceNumber);
572
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000573 if (paced_sender_) {
574 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000575 header.ssrc,
576 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000577 capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000578 length - header.headerLength,
579 true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000580 // We can't send the packet right now.
581 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000582 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000583 }
584 }
585
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000586 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000587 if ((rtx_ & kRtxRetransmitted) > 0) {
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000588 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
589 buffer_to_send_ptr = data_buffer_rtx;
590 }
591
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000592 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000593 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000594 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000595 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000596}
597
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000598bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
599 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000600 if (transport_) {
601 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000602 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000603 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
604 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000605 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000606 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000607 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
608 "Transport failed to send packet");
609 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000610 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000611 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000612}
613
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000614int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000615 if (!video_)
616 return -1;
617 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000618}
619
620int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000621 if (!video_)
622 return -1;
623 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000624}
625
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000626void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000627 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000628 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000629 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
630 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000631 const int64_t now = clock_->TimeInMilliseconds();
632 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000633
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000634 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000635 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000636 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000637 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000638 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000639 return;
640 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000641
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000642 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
643 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000644 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000645 if (bytes_sent > 0) {
646 bytes_re_sent += bytes_sent;
647 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000648 // The packet has previously been resent.
649 // Try resending next packet in the list.
650 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000651 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000652 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000653 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000654 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000655 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000656 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000657 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000658 // Delay bandwidth estimate (RTT * BW).
659 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000660 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000661 uint32_t target_bytes =
662 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000663 if (bytes_re_sent > target_bytes) {
664 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000665 }
666 }
667 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000668 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000669 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000670 UpdateNACKBitRate(bytes_re_sent, now);
671 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000672 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000673}
674
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000675bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
676 uint32_t num = 0;
677 int32_t byte_count = 0;
678 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000679
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 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000683 return true;
684 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000685 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
686 if ((now - nack_byte_count_times_[num]) > avg_interval) {
687 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000688 break;
689 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000690 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000691 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000692 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000693 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000694 if (num == NACK_BYTECOUNT_SIZE) {
695 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000696 // during the last msg_interval.
697 time_interval = now - nack_byte_count_times_[num - 1];
698 if (time_interval < 0) {
699 time_interval = avg_interval;
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 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000703}
704
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000705void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
706 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000707 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000708
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000709 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000710 if (bytes > 0) {
711 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000712 // Add padding length.
713 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000714 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000715 if (nack_byte_count_times_[0] == 0) {
716 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000717 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000718 // Shift.
719 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
720 nack_byte_count_[i + 1] = nack_byte_count_[i];
721 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000722 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000723 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000724 nack_byte_count_[0] = bytes;
725 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000726 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000727 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000728}
729
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000730// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000731bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000732 int64_t capture_time_ms,
733 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000734 uint16_t length = IP_PACKET_SIZE;
735 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000736 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000737
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000738 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
739 0,
740 retransmission,
741 data_buffer,
742 &length,
743 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000744 // Packet cannot be found. Allow sending to continue.
745 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000746 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000747 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
748 retransmission && (rtx_ & kRtxRetransmitted) > 0);
749}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000750
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000751bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
752 uint16_t length,
753 int64_t capture_time_ms,
754 bool send_over_rtx) {
755 uint8_t *buffer_to_send_ptr = buffer;
756
757 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000758 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000759 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000760 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000761 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000762 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000763
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000764 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000765 if (send_over_rtx) {
766 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000767 buffer_to_send_ptr = data_buffer_rtx;
768 }
769
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000770 int64_t now_ms = clock_->TimeInMilliseconds();
771 int64_t diff_ms = now_ms - capture_time_ms;
772 bool updated_transmission_time_offset =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000773 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
774 diff_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000775 bool updated_abs_send_time =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000776 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000777 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000778 // Update stored packet in case of receiving a re-transmission request.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000779 packet_history_.ReplaceRTPHeader(buffer_to_send_ptr,
780 rtp_header.sequenceNumber,
781 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000782 }
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000783 return SendPacketToNetwork(buffer_to_send_ptr, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000784}
785
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000786int RTPSender::TimeToSendPadding(int bytes) {
787 if (!sending_media_) {
788 return 0;
789 }
790 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000791 int64_t capture_time_ms;
792 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000793 {
794 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000795 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_ :
796 payload_type_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000797 timestamp = timestamp_;
798 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000799 if (last_timestamp_time_ms_ > 0) {
800 timestamp +=
801 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
802 capture_time_ms +=
803 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
804 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000805 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000806 int bytes_sent = SendRedundantPayloads(payload_type, bytes);
807 bytes -= bytes_sent;
808 if (bytes > 0) {
809 int padding_sent = SendPadData(payload_type, timestamp, capture_time_ms,
810 bytes, kDontStore, true, true);
811 bytes_sent += padding_sent;
812 }
813 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000814}
815
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000816// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000817int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000818 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000819 int64_t capture_time_ms, StorageType storage,
820 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000821 ModuleRTPUtility::RTPHeaderParser rtp_parser(
822 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000823 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000824 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000825
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000826 int64_t now_ms = clock_->TimeInMilliseconds();
827
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000828 // |capture_time_ms| <= 0 is considered invalid.
829 // TODO(holmer): This should be changed all over Video Engine so that negative
830 // time is consider invalid, while 0 is considered a valid time.
831 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000832 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000833 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000834 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000835
836 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
837 rtp_header, now_ms);
838
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000839 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000840 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
841 max_payload_length_, capture_time_ms,
842 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000843 return -1;
844 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000845 {
846 // Update send statistics prior to pacer.
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000847 CriticalSectionScoped lock(statistics_crit_.get());
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000848 Bitrate::Update(payload_length + rtp_header_length);
849 ++packets_sent_;
850 payload_bytes_sent_ += payload_length;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000851 }
852
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000853 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000854 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
855 rtp_header.sequenceNumber, capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000856 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000857 // We can't send the packet right now.
858 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000859 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000860 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000861 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000862 if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
863 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000864 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000865 return -1;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000866}
867
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000868void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000869 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000870 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000871 nack_bitrate_.Process();
872 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000873 return;
874 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000875 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000876}
877
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000878uint16_t RTPSender::RTPHeaderLength() const {
879 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000880 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000881 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000882 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000883 rtp_header_length += RtpHeaderExtensionTotalLength();
884 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000885}
886
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000887uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000888 CriticalSectionScoped cs(send_critsect_);
889 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000890}
891
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000892void RTPSender::ResetDataCounters() {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000893 CriticalSectionScoped lock(statistics_crit_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000894 packets_sent_ = 0;
895 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000896}
897
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000898uint32_t RTPSender::Packets() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000899 CriticalSectionScoped lock(statistics_crit_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000900 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000901}
902
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000903// Number of sent RTP bytes.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000904uint32_t RTPSender::Bytes() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000905 CriticalSectionScoped lock(statistics_crit_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000906 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000907}
908
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000909int RTPSender::CreateRTPHeader(
910 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
911 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
912 uint8_t num_csrcs) const {
913 header[0] = 0x80; // version 2.
914 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000915 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000916 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000917 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000918 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
919 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
920 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000921 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000922
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000923 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000924 if (num_csrcs > 0) {
925 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000926 // error
927 assert(false);
928 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000929 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000930 uint8_t *ptr = &header[rtp_header_length];
931 for (int i = 0; i < num_csrcs; ++i) {
932 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000933 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000934 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000935 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000936
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000937 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000938 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000939 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000940
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000941 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
942 if (len > 0) {
943 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000944 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000945 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000946 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000947}
948
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000949int32_t RTPSender::BuildRTPheader(
950 uint8_t *data_buffer, const int8_t payload_type,
951 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000952 int64_t capture_time_ms, const bool time_stamp_provided,
953 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000954 assert(payload_type >= 0);
955 CriticalSectionScoped cs(send_critsect_);
956
957 if (time_stamp_provided) {
958 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000959 } else {
960 // Make a unique time stamp.
961 // We can't inc by the actual time, since then we increase the risk of back
962 // timing.
963 timestamp_++;
964 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000965 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000966 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000967 capture_time_ms_ = capture_time_ms;
968 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000969 int csrcs_length = 0;
970 if (include_csrcs_)
971 csrcs_length = num_csrcs_;
972 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
973 timestamp_, sequence_number, csrcs_, csrcs_length);
974}
975
976uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000977 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000978 return 0;
979 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000980 // RTP header extension, RFC 3550.
981 // 0 1 2 3
982 // 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
983 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
984 // | defined by profile | length |
985 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
986 // | header extension |
987 // | .... |
988 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000989 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000990 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000991
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000992 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000993 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000994 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000995
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000996 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000997 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000998
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000999 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001000 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001001 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001002 switch (type) {
1003 case kRtpExtensionTransmissionTimeOffset:
1004 block_length = BuildTransmissionTimeOffsetExtension(
1005 data_buffer + kHeaderLength + total_block_length);
1006 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001007 case kRtpExtensionAudioLevel:
1008 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
1009 // we don't have to care about it here, which is true until we wan't to
1010 // use it together with any of the other extensions we support.
1011 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001012 case kRtpExtensionAbsoluteSendTime:
1013 block_length = BuildAbsoluteSendTimeExtension(
1014 data_buffer + kHeaderLength + total_block_length);
1015 break;
1016 default:
1017 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001018 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001019 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001020 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001021 }
1022 if (total_block_length == 0) {
1023 // No extension added.
1024 return 0;
1025 }
1026 // Set header length (in number of Word32, header excluded).
1027 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001028 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001029 total_block_length / 4);
1030 // Total added length.
1031 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001032}
1033
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001034uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1035 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001036 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1037 //
1038 // The transmission 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 signed integer.
1041 // When added to the RTP timestamp of the packet, it represents the
1042 // "effective" RTP transmission time of the packet, on the RTP
1043 // timescale.
1044 //
1045 // The form of the transmission offset extension block:
1046 //
1047 // 0 1 2 3
1048 // 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
1049 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1050 // | ID | len=2 | transmission offset |
1051 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001052
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001053 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001054 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001055 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1056 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001057 // Not registered.
1058 return 0;
1059 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001060 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001061 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001062 data_buffer[pos++] = (id << 4) + len;
1063 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1064 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001065 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001066 assert(pos == kTransmissionTimeOffsetLength);
1067 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001068}
1069
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001070uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
1071 uint8_t* data_buffer) const {
1072 // Absolute send time in RTP streams.
1073 //
1074 // The absolute send time is signaled to the receiver in-band using the
1075 // general mechanism for RTP header extensions [RFC5285]. The payload
1076 // of this extension (the transmitted value) is a 24-bit unsigned integer
1077 // containing the sender's current time in seconds as a fixed point number
1078 // with 18 bits fractional part.
1079 //
1080 // The form of the absolute send time extension block:
1081 //
1082 // 0 1 2 3
1083 // 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
1084 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1085 // | ID | len=2 | absolute send time |
1086 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1087
1088 // Get id defined by user.
1089 uint8_t id;
1090 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1091 &id) != 0) {
1092 // Not registered.
1093 return 0;
1094 }
1095 size_t pos = 0;
1096 const uint8_t len = 2;
1097 data_buffer[pos++] = (id << 4) + len;
1098 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1099 absolute_send_time_);
1100 pos += 3;
1101 assert(pos == kAbsoluteSendTimeLength);
1102 return kAbsoluteSendTimeLength;
1103}
1104
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001105bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001106 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001107 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001108 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001109
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001110 // Get length until start of header extension block.
1111 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001112 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001113 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001114 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001115 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001116 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001117 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001118 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001119 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001120 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001121 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001122 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001123 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001124 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001125 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001126 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001127 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001128 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1129 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001130 WEBRTC_TRACE(
1131 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001132 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001133 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001134 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001135 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001136 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001137 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1138 &id) != 0) {
1139 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001140 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001141 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001142 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001143 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001144 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001145 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001146 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001147 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001148 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001149 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001150 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001151 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001152 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001153 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001154}
1155
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001156bool RTPSender::UpdateAbsoluteSendTime(
1157 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001158 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001159 CriticalSectionScoped cs(send_critsect_);
1160
1161 // Get length until start of header extension block.
1162 int extension_block_pos =
1163 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1164 kRtpExtensionAbsoluteSendTime);
1165 if (extension_block_pos < 0) {
1166 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1167 "Failed to update absolute send time, not registered.");
1168 return false;
1169 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001170 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001171 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001172 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001173 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1174 "Failed to update absolute send time, invalid length.");
1175 return false;
1176 }
1177 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001178 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1179 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001180 WEBRTC_TRACE(
1181 kTraceStream, kTraceRtpRtcp, id_,
1182 "Failed to update absolute send time, hdr extension not found.");
1183 return false;
1184 }
1185 // Get id.
1186 uint8_t id = 0;
1187 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1188 &id) != 0) {
1189 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1190 "Failed to update absolute send time, no id.");
1191 return false;
1192 }
1193 // Verify first byte in block.
1194 const uint8_t first_block_byte = (id << 4) + 2;
1195 if (rtp_packet[block_pos] != first_block_byte) {
1196 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1197 "Failed to update absolute send time.");
1198 return false;
1199 }
1200 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1201 // fractional part).
1202 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1203 ((now_ms << 18) / 1000) & 0x00ffffff);
1204 return true;
1205}
1206
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001207void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001208 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001209 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001210 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001211
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001212 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001213 SetStartTimestamp(RTPtime, false);
1214 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001215 if (!ssrc_forced_) {
1216 // Generate a new SSRC.
1217 ssrc_db_.ReturnSSRC(ssrc_);
1218 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001219 }
1220 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001221 if (!sequence_number_forced_ && !ssrc_forced_) {
1222 // Generate a new sequence number.
1223 sequence_number_ =
1224 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001225 }
1226 }
1227}
1228
1229void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001230 CriticalSectionScoped cs(send_critsect_);
1231 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001232}
1233
1234bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001235 CriticalSectionScoped cs(send_critsect_);
1236 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001237}
1238
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001239uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001240 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001241 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001242}
1243
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001244void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001245 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001246 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001247 start_time_stamp_forced_ = force;
1248 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001249 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001250 if (!start_time_stamp_forced_) {
1251 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001252 }
1253 }
1254}
1255
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001256uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001257 CriticalSectionScoped cs(send_critsect_);
1258 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001259}
1260
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001261uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001262 // If configured via API, return 0.
1263 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001264
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001265 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001266 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001267 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001268 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1269 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001270}
1271
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001272void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001273 // This is configured via the API.
1274 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001275
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001276 if (ssrc_ == ssrc && ssrc_forced_) {
1277 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001278 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001279 ssrc_forced_ = true;
1280 ssrc_db_.ReturnSSRC(ssrc_);
1281 ssrc_db_.RegisterSSRC(ssrc);
1282 ssrc_ = ssrc;
1283 if (!sequence_number_forced_) {
1284 sequence_number_ =
1285 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001286 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001287}
1288
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001289uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001290 CriticalSectionScoped cs(send_critsect_);
1291 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001292}
1293
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001294void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001295 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001296}
1297
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001298void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1299 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001300 assert(arr_length <= kRtpCsrcSize);
1301 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001302
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001303 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001304 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001305 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001306 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001307}
1308
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001309int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001310 assert(arr_of_csrc);
1311 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001312 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1313 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001314 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001315 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001316}
1317
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001318void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001319 CriticalSectionScoped cs(send_critsect_);
1320 sequence_number_forced_ = true;
1321 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001322}
1323
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001324uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001325 CriticalSectionScoped cs(send_critsect_);
1326 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001327}
1328
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001329// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001330int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1331 const uint16_t time_ms,
1332 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001333 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001334 return -1;
1335 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001336 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001337}
1338
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001339bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001340 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001341 return false;
1342 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001343 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001344}
1345
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001346int32_t RTPSender::SetAudioPacketSize(
1347 const uint16_t packet_size_samples) {
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_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001352}
1353
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001354int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1355 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001356 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001357 return -1;
1358 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001359 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001360}
1361
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001362int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1363 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001364 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001365}
1366
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001367int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001368 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001369}
1370
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001371int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001372 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001373 return -1;
1374 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001375 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001376}
1377
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001378int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001379 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001380 return -1;
1381 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001382 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001383}
1384
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001385// Video
1386VideoCodecInformation *RTPSender::CodecInformationVideo() {
1387 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001388 return NULL;
1389 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001390 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001391}
1392
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001393RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001394 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001395 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001396}
1397
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001398uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001399 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001400 return 0;
1401 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001402 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001403}
1404
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001405int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001406 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001407 return -1;
1408 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001409 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001410}
1411
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001412int32_t RTPSender::SetGenericFECStatus(
1413 const bool enable, const uint8_t payload_type_red,
1414 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001415 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001416 return -1;
1417 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001418 return video_->SetGenericFECStatus(enable, payload_type_red,
1419 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001420}
1421
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001422int32_t RTPSender::GenericFECStatus(
1423 bool *enable, uint8_t *payload_type_red,
1424 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001425 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001426 return -1;
1427 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001428 return video_->GenericFECStatus(
1429 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001430}
1431
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001432int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001433 const FecProtectionParams *delta_params,
1434 const FecProtectionParams *key_params) {
1435 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001436 return -1;
1437 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001438 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001439}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001440
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001441void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1442 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001443 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001444 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001445 // Add RTX header.
1446 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001447 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001448
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001449 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001450 rtp_parser.Parse(rtp_header);
1451
1452 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001453 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001454
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001455 // Replace payload type, if a specific type is set for RTX.
1456 if (payload_type_rtx_ != -1) {
1457 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001458 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001459 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1460 }
1461
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001462 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001463 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001464 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1465
1466 // Replace SSRC.
1467 ptr += 6;
1468 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1469
1470 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001471 ptr = data_buffer_rtx + rtp_header.headerLength;
1472 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001473 ptr += 2;
1474
1475 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001476 memcpy(ptr, buffer + rtp_header.headerLength,
1477 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001478 *length += 2;
1479}
1480
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001481} // namespace webrtc