blob: 278cada7f02c18bc9ff72a89706fe450e30125d4 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000011#include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
12
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000013#include <cstdlib> // srand
niklase@google.com470e71d2011-07-07 08:21:25 +000014
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000015#include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
16#include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
17#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
18#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
19#include "webrtc/system_wrappers/interface/trace.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000020#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
stefan@webrtc.orga8179622013-06-04 13:47:36 +000024// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
25const int kMaxPaddingLength = 224;
26
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000027namespace {
28
29const char* FrameTypeToString(const FrameType frame_type) {
30 switch (frame_type) {
31 case kFrameEmpty: return "empty";
32 case kAudioFrameSpeech: return "audio_speech";
33 case kAudioFrameCN: return "audio_cn";
34 case kVideoFrameKey: return "video_key";
35 case kVideoFrameDelta: return "video_delta";
36 case kVideoFrameGolden: return "video_golden";
37 case kVideoFrameAltRef: return "video_altref";
38 }
39 return "";
40}
41
42} // namespace
43
pbos@webrtc.org2f446732013-04-08 11:08:41 +000044RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000045 Transport *transport, RtpAudioFeedback *audio_feedback,
46 PacedSender *paced_sender)
47 : Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL),
48 video_(NULL), paced_sender_(paced_sender),
49 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
50 transport_(transport), sending_media_(true), // Default to sending media.
51 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
52 target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1),
53 payload_type_map_(), rtp_header_extension_map_(),
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +000054 transmission_time_offset_(0), absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000055 // NACK.
56 nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
57 packet_history_(new RTPPacketHistory(clock)),
58 // Statistics
59 packets_sent_(0), payload_bytes_sent_(0), start_time_stamp_forced_(false),
60 start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000061 remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false),
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +000062 timestamp_(0), capture_time_ms_(0), last_packet_marker_bit_(false),
63 num_csrcs_(0), csrcs_(), include_csrcs_(true),
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +000064 rtx_(kRtxOff), payload_type_rtx_(-1) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000065 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
66 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +000067 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000068 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000069 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000070 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000071 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
72 // Random start, 16 bits. Can't be 0.
73 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
74 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +000075
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000076 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000077 audio_ = new RTPSenderAudio(id, clock_, this);
78 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000079 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000080 video_ = new RTPSenderVideo(id, clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000081 }
82 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
pwestin@webrtc.org00741872012-01-19 15:56:10 +000085RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000086 if (remote_ssrc_ != 0) {
87 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000088 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000089 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
pwestin@webrtc.org00741872012-01-19 15:56:10 +000091 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000092 delete send_critsect_;
93 while (!payload_type_map_.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000094 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000095 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +000096 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000097 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000098 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000099 delete packet_history_;
100 delete audio_;
101 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000102
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000103 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104}
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000106void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000107 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000108}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000109
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000110uint16_t RTPSender::ActualSendBitrateKbit() const {
111 return (uint16_t)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000112}
113
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000114uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000115 if (video_) {
116 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000117 }
118 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000119}
120
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000121uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000122 if (video_) {
123 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000124 }
125 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000126}
127
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000128uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000129 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000130}
131
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000132int32_t RTPSender::SetTransmissionTimeOffset(
133 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000134 if (transmission_time_offset > (0x800000 - 1) ||
135 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000136 return -1;
137 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000138 CriticalSectionScoped cs(send_critsect_);
139 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000140 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000141}
142
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000143int32_t RTPSender::SetAbsoluteSendTime(
144 const uint32_t absolute_send_time) {
145 if (absolute_send_time > 0xffffff) { // UWord24.
146 return -1;
147 }
148 CriticalSectionScoped cs(send_critsect_);
149 absolute_send_time_ = absolute_send_time;
150 return 0;
151}
152
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000153int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
154 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000155 CriticalSectionScoped cs(send_critsect_);
156 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000157}
158
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000159int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000160 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000161 CriticalSectionScoped cs(send_critsect_);
162 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000163}
164
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000165uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000166 CriticalSectionScoped cs(send_critsect_);
167 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000168}
169
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000170int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000171 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000172 const int8_t payload_number, const uint32_t frequency,
173 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000174 assert(payload_name);
175 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000177 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000178 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000180 if (payload_type_map_.end() != it) {
181 // We already use this payload type.
182 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000183 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000185 // Check if it's the same as we already have.
186 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000187 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000188 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000189 payload->typeSpecific.Audio.frequency == frequency &&
190 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000191 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000192 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000193 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000194 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000195 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000196 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000197 return 0;
198 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000199 }
200 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000201 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000202 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000203 ModuleRTPUtility::Payload *payload = NULL;
204 if (audio_configured_) {
205 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
206 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000207 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000208 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
209 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000210 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000211 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000212 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000213 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000214 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215}
216
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000217int32_t RTPSender::DeRegisterSendPayload(
218 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000219 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000220
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000221 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000222 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000223
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000224 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000225 return -1;
226 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000227 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000228 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000229 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000230 return 0;
231}
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000233int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000234
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000235int RTPSender::SendPayloadFrequency() const { return audio_->AudioFrequency(); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000237int32_t RTPSender::SetMaxPayloadLength(
238 const uint16_t max_payload_length,
239 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000240 // Sanity check.
241 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
242 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
243 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000244 return -1;
245 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000246 CriticalSectionScoped cs(send_critsect_);
247 max_payload_length_ = max_payload_length;
248 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000249
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000250 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
251 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000252 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000253}
254
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000255uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000256 if (audio_configured_) {
257 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000258 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000259 return max_payload_length_ - RTPHeaderLength() -
260 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
261 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000262 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000263}
264
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000265uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000266 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000267}
268
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000269uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000270
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000271void RTPSender::SetRTXStatus(RtxMode mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000272 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000273 rtx_ = mode;
274 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000275 if (set_ssrc) {
276 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000277 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000278 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000279 }
280 }
281}
282
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000283void RTPSender::RTXStatus(RtxMode* mode, uint32_t* ssrc,
284 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000285 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000286 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000287 *ssrc = ssrc_rtx_;
288 *payload_type = payload_type_rtx_;
289}
290
291
292void RTPSender::SetRtxPayloadType(int payload_type) {
293 CriticalSectionScoped cs(send_critsect_);
294 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000295}
296
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000297int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
298 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000299 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000301 if (payload_type < 0) {
302 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
303 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000304 return -1;
305 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000306 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000307 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000308 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000309 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000310 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000311 // And it's a match...
312 return 0;
313 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000314 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000315 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000316 if (payload_type_ == payload_type) {
317 if (!audio_configured_) {
318 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000319 }
320 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000321 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000322 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000323 payload_type_map_.find(payload_type);
324 if (it == payload_type_map_.end()) {
325 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
326 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000327 return -1;
328 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000329 payload_type_ = payload_type;
330 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000331 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000332 if (!payload->audio && !audio_configured_) {
333 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
334 *video_type = payload->typeSpecific.Video.videoCodecType;
335 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000336 }
337 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000338}
339
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000340int32_t RTPSender::SendOutgoingData(
341 const FrameType frame_type, const int8_t payload_type,
342 const uint32_t capture_timestamp, int64_t capture_time_ms,
343 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000344 const RTPFragmentationHeader *fragmentation,
345 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000346 {
347 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000348 CriticalSectionScoped cs(send_critsect_);
349 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000350 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000351 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000352 }
stefan@webrtc.org66b2e5c2013-07-05 14:30:48 +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
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000361 if (frame_type == kVideoFrameKey) {
362 TRACE_EVENT_INSTANT1("webrtc_rtp", "SendKeyFrame",
363 "timestamp", capture_timestamp);
364 } else {
365 TRACE_EVENT_INSTANT2("webrtc_rtp", "SendFrame",
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000366 "timestamp", capture_timestamp,
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000367 "frame_type", FrameTypeToString(frame_type));
368 }
369
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000370 if (audio_configured_) {
371 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000372 frame_type == kFrameEmpty);
373
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000374 return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
375 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000376 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000377 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000378
379 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000380 if (paced_sender_->Enabled()) {
381 // Padding is driven by the pacer and not by the encoder.
382 return 0;
383 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000384 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000385 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000386 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000387 return video_->SendVideo(video_type, frame_type, payload_type,
388 capture_timestamp, capture_time_ms, payload_data,
389 payload_size, fragmentation, codec_info,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000390 rtp_type_hdr);
391 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000392}
393
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000394bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000395 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000396 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000397 // Current bitrate since last estimate(1 second) averaged with the
398 // estimate since then, to get the most up to date bitrate.
399 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000400 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000401 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000402 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000403 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000404 int bytes = 0;
405 if (current_bitrate == 0) {
406 // Start up phase. Send one 33.3 ms batch to start with.
407 bytes = (bitrate_diff / 8) / 30;
408 } else {
409 bytes = (bitrate_diff / 8);
410 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000411 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000412 if (bytes > bytes_cap) {
413 bytes = bytes_cap;
414 }
415 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000416 int bytes_sent = SendPadData(payload_type, capture_timestamp, capture_time_ms,
417 bytes, kDontRetransmit, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000418 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
419 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000420}
421
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000422int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
423 int32_t bytes) {
424 int padding_bytes_in_packet = kMaxPaddingLength;
425 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000426 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000427 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000428 packet[0] |= 0x20; // Set padding bit.
429 int32_t *data =
430 reinterpret_cast<int32_t *>(&(packet[header_length]));
431
432 // Fill data buffer with random data.
433 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
434 data[j] = rand(); // NOLINT
435 }
436 // Set number of padding bytes in the last byte of the packet.
437 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
438 return padding_bytes_in_packet;
439}
440
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000441int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
442 int64_t capture_time_ms, int32_t bytes,
443 StorageType store, bool force_full_size_packets) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000444 // Drop this packet if we're not sending media packets.
445 if (!sending_media_) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000446 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000447 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000448 int padding_bytes_in_packet = 0;
449 int bytes_sent = 0;
450 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000451 // Always send full padding packets.
452 if (force_full_size_packets && bytes < kMaxPaddingLength)
453 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000454 if (bytes < kMaxPaddingLength) {
455 if (force_full_size_packets) {
456 bytes = kMaxPaddingLength;
457 } else {
458 // Round to the nearest multiple of 32.
459 bytes = (bytes + 16) & 0xffe0;
460 }
461 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000462 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000463 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000464 break;
465 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000466 uint32_t ssrc;
467 uint16_t sequence_number;
468 {
469 CriticalSectionScoped cs(send_critsect_);
470 // Only send padding packets following the last packet of a frame,
471 // indicated by the marker bit.
472 if (!last_packet_marker_bit_)
473 return bytes_sent;
474 if (rtx_ == kRtxOff) {
475 ssrc = ssrc_;
476 sequence_number = sequence_number_;
477 ++sequence_number_;
478 } else {
479 ssrc = ssrc_rtx_;
480 sequence_number = sequence_number_rtx_;
481 ++sequence_number_rtx_;
482 }
483 }
484 uint8_t padding_packet[IP_PACKET_SIZE];
485 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
486 false, timestamp, sequence_number, NULL,
487 0);
488 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
489 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000490 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
491 header_length, capture_time_ms, store,
492 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000493 // Error sending the packet.
494 break;
495 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000496 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000497 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000498 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000499}
500
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000501void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000502 const uint16_t number_to_store) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000503 packet_history_->SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000504}
505
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000506bool RTPSender::StorePackets() const {
507 return packet_history_->StorePackets();
508}
niklase@google.com470e71d2011-07-07 08:21:25 +0000509
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000510int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
511 uint16_t length = IP_PACKET_SIZE;
512 uint8_t data_buffer[IP_PACKET_SIZE];
513 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000514 int64_t capture_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000515 StorageType type;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000516 if (!packet_history_->GetRTPPacket(packet_id, min_resend_time, data_buffer,
517 &length, &capture_time_ms, &type)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000518 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000519 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000520 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000521 if (length == 0 || type == kDontRetransmit) {
522 // No bytes copied (packet recently resent, skip resending) or
523 // packet should not be retransmitted.
524 return 0;
525 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000526
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000527 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000528 if (rtx_ != kRtxOff) {
529 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000530 buffer_to_send_ptr = data_buffer_rtx;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000531 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000532
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000533 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000534 RTPHeader header;
535 rtp_parser.Parse(header);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000536
537 // Store the time when the packet was last sent or added to pacer.
538 packet_history_->UpdateResendTime(packet_id);
539
540 {
541 // Update send statistics prior to pacer.
542 CriticalSectionScoped cs(send_critsect_);
543 Bitrate::Update(length);
544 packets_sent_++;
545 // We on purpose don't add to payload_bytes_sent_ since this is a
546 // re-transmit and not new payload data.
547 }
548
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000549 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000550 "timestamp", header.timestamp,
551 "seqnum", header.sequenceNumber);
552
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000553 if (paced_sender_) {
554 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000555 header.ssrc,
556 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000557 capture_time_ms,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000558 length - header.headerLength)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000559 // We can't send the packet right now.
560 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000561 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000562 }
563 }
564
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000565 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000566 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000567 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000568 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000569}
570
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000571bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
572 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000573 if (transport_) {
574 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000575 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000576 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
577 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000578 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000579 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000580 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
581 "Transport failed to send packet");
582 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000583 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000584 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000585}
586
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000587int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000588 if (!video_)
589 return -1;
590 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000591}
592
593int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000594 if (!video_)
595 return -1;
596 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000597}
598
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000599void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000600 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000601 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000602 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
603 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000604 const int64_t now = clock_->TimeInMilliseconds();
605 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000606
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000607 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000608 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000609 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000610 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000611 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000612 return;
613 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000614
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000615 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
616 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000617 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000618 if (bytes_sent > 0) {
619 bytes_re_sent += bytes_sent;
620 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000621 // The packet has previously been resent.
622 // Try resending next packet in the list.
623 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000624 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000625 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000626 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000627 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000628 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000629 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000630 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000631 // Delay bandwidth estimate (RTT * BW).
632 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000633 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000634 uint32_t target_bytes =
635 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000636 if (bytes_re_sent > target_bytes) {
637 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000638 }
639 }
640 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000641 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000642 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000643 UpdateNACKBitRate(bytes_re_sent, now);
644 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000645 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000646}
647
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000648bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
649 uint32_t num = 0;
650 int32_t byte_count = 0;
651 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000652
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000653 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000654
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000655 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000656 return true;
657 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000658 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
659 if ((now - nack_byte_count_times_[num]) > avg_interval) {
660 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000661 break;
662 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000663 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000664 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000665 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000666 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000667 if (num == NACK_BYTECOUNT_SIZE) {
668 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000669 // during the last msg_interval.
670 time_interval = now - nack_byte_count_times_[num - 1];
671 if (time_interval < 0) {
672 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000673 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000674 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000675 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000676}
677
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000678void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
679 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000680 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000681
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000682 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000683 if (bytes > 0) {
684 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000685 // Add padding length.
686 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000687 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000688 if (nack_byte_count_times_[0] == 0) {
689 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000690 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000691 // Shift.
692 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
693 nack_byte_count_[i + 1] = nack_byte_count_[i];
694 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000695 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000696 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000697 nack_byte_count_[0] = bytes;
698 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000699 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000700 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000701}
702
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000703// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000704bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000705 int64_t capture_time_ms) {
706 StorageType type;
707 uint16_t length = IP_PACKET_SIZE;
708 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000709 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000710
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000711 if (packet_history_ == NULL) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000712 // Packet cannot be found. Allow sending to continue.
713 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000714 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000715 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
716 &stored_time_ms, &type)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000717 // Packet cannot be found. Allow sending to continue.
718 return true;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000719 }
720 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000721
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000722 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000723 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000724 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000725 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000726 "timestamp", rtp_header.timestamp,
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000727 "seqnum", sequence_number);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000728
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000729 int64_t now_ms = clock_->TimeInMilliseconds();
730 int64_t diff_ms = now_ms - capture_time_ms;
731 bool updated_transmission_time_offset =
732 UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms);
733 bool updated_abs_send_time =
734 UpdateAbsoluteSendTime(data_buffer, length, rtp_header, now_ms);
735 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000736 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000737 packet_history_->ReplaceRTPHeader(data_buffer,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000738 rtp_header.sequenceNumber,
739 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000740 }
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000741 return SendPacketToNetwork(data_buffer, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000742}
743
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000744int RTPSender::TimeToSendPadding(int bytes) {
745 if (!sending_media_) {
746 return 0;
747 }
748 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000749 int64_t capture_time_ms;
750 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000751 {
752 CriticalSectionScoped cs(send_critsect_);
753 payload_type = (rtx_ == kRtxOff) ? payload_type_ : payload_type_rtx_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000754 timestamp = timestamp_;
755 capture_time_ms = capture_time_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000756 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000757 return SendPadData(payload_type, timestamp, capture_time_ms, bytes,
758 kDontStore, true);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000759}
760
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000761// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000762int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000763 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000764 int64_t capture_time_ms, StorageType storage,
765 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000766 ModuleRTPUtility::RTPHeaderParser rtp_parser(
767 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000768 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000769 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000770
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000771 int64_t now_ms = clock_->TimeInMilliseconds();
772
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000773 // |capture_time_ms| <= 0 is considered invalid.
774 // TODO(holmer): This should be changed all over Video Engine so that negative
775 // time is consider invalid, while 0 is considered a valid time.
776 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000777 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000778 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000779 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000780
781 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
782 rtp_header, now_ms);
783
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000784 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000785 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
786 max_payload_length_, capture_time_ms,
787 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000788 return -1;
789 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000790
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000791 // Create and send RTX Packet.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000792 // TODO(pwesin): This should be moved to its own code path triggered by pacer.
793 bool rtx_sent = false;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000794 if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000795 uint16_t length_rtx = payload_length + rtp_header_length;
796 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000797 BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000798 if (!SendPacketToNetwork(data_buffer_rtx, length_rtx)) return -1;
799 rtx_sent = true;
800 }
801 {
802 // Update send statistics prior to pacer.
803 CriticalSectionScoped cs(send_critsect_);
804 Bitrate::Update(payload_length + rtp_header_length);
805 ++packets_sent_;
806 payload_bytes_sent_ += payload_length;
807 if (rtx_sent) {
808 // The RTX packet.
809 ++packets_sent_;
810 payload_bytes_sent_ += payload_length;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000811 }
812 }
813
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000814 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000815 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
816 rtp_header.sequenceNumber, capture_time_ms,
817 payload_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000818 // We can't send the packet right now.
819 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000820 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000821 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000822 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000823 if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
824 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000825 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000826 return -1;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000827}
828
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000829void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000830 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000831 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000832 nack_bitrate_.Process();
833 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000834 return;
835 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000836 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000837}
838
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000839uint16_t RTPSender::RTPHeaderLength() const {
840 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000841 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000842 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000843 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000844 rtp_header_length += RtpHeaderExtensionTotalLength();
845 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000846}
847
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000848uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000849 CriticalSectionScoped cs(send_critsect_);
850 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000851}
852
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000853void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000854 packets_sent_ = 0;
855 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000856}
857
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000858uint32_t RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000859 // Don't use critsect to avoid potential deadlock.
860 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000861}
862
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000863// Number of sent RTP bytes.
864// Don't use critsect to avoid potental deadlock.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000865uint32_t RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000866 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000867}
868
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000869int RTPSender::CreateRTPHeader(
870 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
871 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
872 uint8_t num_csrcs) const {
873 header[0] = 0x80; // version 2.
874 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000875 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000876 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000877 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000878 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
879 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
880 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000881 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000882
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000883 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000884 if (num_csrcs > 0) {
885 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000886 // error
887 assert(false);
888 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000889 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000890 uint8_t *ptr = &header[rtp_header_length];
891 for (int i = 0; i < num_csrcs; ++i) {
892 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000893 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000894 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000895 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000896
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000897 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000898 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000899 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000900
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000901 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
902 if (len > 0) {
903 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000904 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000905 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000906 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000907}
908
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000909int32_t RTPSender::BuildRTPheader(
910 uint8_t *data_buffer, const int8_t payload_type,
911 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000912 int64_t capture_time_ms, const bool time_stamp_provided,
913 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000914 assert(payload_type >= 0);
915 CriticalSectionScoped cs(send_critsect_);
916
917 if (time_stamp_provided) {
918 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000919 } else {
920 // Make a unique time stamp.
921 // We can't inc by the actual time, since then we increase the risk of back
922 // timing.
923 timestamp_++;
924 }
925 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000926 capture_time_ms_ = capture_time_ms;
927 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000928 int csrcs_length = 0;
929 if (include_csrcs_)
930 csrcs_length = num_csrcs_;
931 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
932 timestamp_, sequence_number, csrcs_, csrcs_length);
933}
934
935uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000936 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000937 return 0;
938 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000939 // RTP header extension, RFC 3550.
940 // 0 1 2 3
941 // 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
942 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
943 // | defined by profile | length |
944 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
945 // | header extension |
946 // | .... |
947 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000948 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000949 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000950
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000951 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000952 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000953 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000954
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000955 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000956 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000957
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000958 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000959 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000960 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000961 switch (type) {
962 case kRtpExtensionTransmissionTimeOffset:
963 block_length = BuildTransmissionTimeOffsetExtension(
964 data_buffer + kHeaderLength + total_block_length);
965 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +0000966 case kRtpExtensionAudioLevel:
967 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
968 // we don't have to care about it here, which is true until we wan't to
969 // use it together with any of the other extensions we support.
970 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000971 case kRtpExtensionAbsoluteSendTime:
972 block_length = BuildAbsoluteSendTimeExtension(
973 data_buffer + kHeaderLength + total_block_length);
974 break;
975 default:
976 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000977 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000978 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000979 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000980 }
981 if (total_block_length == 0) {
982 // No extension added.
983 return 0;
984 }
985 // Set header length (in number of Word32, header excluded).
986 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000987 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000988 total_block_length / 4);
989 // Total added length.
990 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000991}
992
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000993uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
994 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000995 // From RFC 5450: Transmission Time Offsets in RTP Streams.
996 //
997 // The transmission time is signaled to the receiver in-band using the
998 // general mechanism for RTP header extensions [RFC5285]. The payload
999 // of this extension (the transmitted value) is a 24-bit signed integer.
1000 // When added to the RTP timestamp of the packet, it represents the
1001 // "effective" RTP transmission time of the packet, on the RTP
1002 // timescale.
1003 //
1004 // The form of the transmission offset extension block:
1005 //
1006 // 0 1 2 3
1007 // 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
1008 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1009 // | ID | len=2 | transmission offset |
1010 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001011
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001012 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001013 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001014 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1015 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001016 // Not registered.
1017 return 0;
1018 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001019 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001020 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001021 data_buffer[pos++] = (id << 4) + len;
1022 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1023 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001024 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001025 assert(pos == kTransmissionTimeOffsetLength);
1026 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001027}
1028
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001029uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
1030 uint8_t* data_buffer) const {
1031 // Absolute send time in RTP streams.
1032 //
1033 // The absolute send time is signaled to the receiver in-band using the
1034 // general mechanism for RTP header extensions [RFC5285]. The payload
1035 // of this extension (the transmitted value) is a 24-bit unsigned integer
1036 // containing the sender's current time in seconds as a fixed point number
1037 // with 18 bits fractional part.
1038 //
1039 // The form of the absolute send time extension block:
1040 //
1041 // 0 1 2 3
1042 // 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
1043 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1044 // | ID | len=2 | absolute send time |
1045 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1046
1047 // Get id defined by user.
1048 uint8_t id;
1049 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1050 &id) != 0) {
1051 // Not registered.
1052 return 0;
1053 }
1054 size_t pos = 0;
1055 const uint8_t len = 2;
1056 data_buffer[pos++] = (id << 4) + len;
1057 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1058 absolute_send_time_);
1059 pos += 3;
1060 assert(pos == kAbsoluteSendTimeLength);
1061 return kAbsoluteSendTimeLength;
1062}
1063
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001064bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001065 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001066 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001067 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001068
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001069 // Get length until start of header extension block.
1070 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001071 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001072 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001073 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001074 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001075 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001076 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001077 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001078 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001079 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001080 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001081 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001082 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001083 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001084 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001085 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001086 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001087 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1088 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001089 WEBRTC_TRACE(
1090 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001091 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001092 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001093 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001094 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001095 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001096 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1097 &id) != 0) {
1098 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001099 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001100 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001101 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001102 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001103 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001104 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001105 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001106 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001107 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001108 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001109 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001110 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001111 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001112 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001113}
1114
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001115bool RTPSender::UpdateAbsoluteSendTime(
1116 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001117 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001118 CriticalSectionScoped cs(send_critsect_);
1119
1120 // Get length until start of header extension block.
1121 int extension_block_pos =
1122 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1123 kRtpExtensionAbsoluteSendTime);
1124 if (extension_block_pos < 0) {
1125 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1126 "Failed to update absolute send time, not registered.");
1127 return false;
1128 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001129 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001130 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001131 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001132 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1133 "Failed to update absolute send time, invalid length.");
1134 return false;
1135 }
1136 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001137 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1138 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001139 WEBRTC_TRACE(
1140 kTraceStream, kTraceRtpRtcp, id_,
1141 "Failed to update absolute send time, hdr extension not found.");
1142 return false;
1143 }
1144 // Get id.
1145 uint8_t id = 0;
1146 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1147 &id) != 0) {
1148 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1149 "Failed to update absolute send time, no id.");
1150 return false;
1151 }
1152 // Verify first byte in block.
1153 const uint8_t first_block_byte = (id << 4) + 2;
1154 if (rtp_packet[block_pos] != first_block_byte) {
1155 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1156 "Failed to update absolute send time.");
1157 return false;
1158 }
1159 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1160 // fractional part).
1161 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1162 ((now_ms << 18) / 1000) & 0x00ffffff);
1163 return true;
1164}
1165
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001166void RTPSender::SetSendingStatus(const bool enabled) {
1167 if (enabled) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001168 uint32_t frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001169 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001170 uint32_t frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001171
1172 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001173 switch (frequency) {
1174 case 8000:
1175 case 12000:
1176 case 16000:
1177 case 24000:
1178 case 32000:
1179 break;
1180 default:
1181 assert(false);
1182 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001183 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +00001184 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001185 } else {
stefan@webrtc.org66b2e5c2013-07-05 14:30:48 +00001186 frequency_hz = kVideoPayloadTypeFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001187 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001188 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001189
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001190 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001191 SetStartTimestamp(RTPtime, false);
1192 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001193 if (!ssrc_forced_) {
1194 // Generate a new SSRC.
1195 ssrc_db_.ReturnSSRC(ssrc_);
1196 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001197 }
1198 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001199 if (!sequence_number_forced_ && !ssrc_forced_) {
1200 // Generate a new sequence number.
1201 sequence_number_ =
1202 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001203 }
1204 }
1205}
1206
1207void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001208 CriticalSectionScoped cs(send_critsect_);
1209 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001210}
1211
1212bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001213 CriticalSectionScoped cs(send_critsect_);
1214 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001215}
1216
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001217uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001218 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001219 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001220}
1221
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001222void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001223 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001224 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001225 start_time_stamp_forced_ = force;
1226 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001227 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001228 if (!start_time_stamp_forced_) {
1229 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001230 }
1231 }
1232}
1233
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001234uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001235 CriticalSectionScoped cs(send_critsect_);
1236 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001237}
1238
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001239uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001240 // If configured via API, return 0.
1241 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001242
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001243 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001244 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001245 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001246 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1247 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001248}
1249
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001250void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001251 // This is configured via the API.
1252 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001253
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001254 if (ssrc_ == ssrc && ssrc_forced_) {
1255 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001256 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001257 ssrc_forced_ = true;
1258 ssrc_db_.ReturnSSRC(ssrc_);
1259 ssrc_db_.RegisterSSRC(ssrc);
1260 ssrc_ = ssrc;
1261 if (!sequence_number_forced_) {
1262 sequence_number_ =
1263 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001264 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001265}
1266
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001267uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001268 CriticalSectionScoped cs(send_critsect_);
1269 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001270}
1271
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001272void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001273 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001274}
1275
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001276void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1277 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001278 assert(arr_length <= kRtpCsrcSize);
1279 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001280
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001281 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001282 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001283 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001284 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001285}
1286
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001287int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001288 assert(arr_of_csrc);
1289 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001290 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1291 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001292 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001293 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001294}
1295
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001296void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001297 CriticalSectionScoped cs(send_critsect_);
1298 sequence_number_forced_ = true;
1299 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001300}
1301
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001302uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001303 CriticalSectionScoped cs(send_critsect_);
1304 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001305}
1306
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001307// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001308int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1309 const uint16_t time_ms,
1310 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001311 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001312 return -1;
1313 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001314 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001315}
1316
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001317bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001318 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001319 return false;
1320 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001321 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001322}
1323
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001324int32_t RTPSender::SetAudioPacketSize(
1325 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001326 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001327 return -1;
1328 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001329 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001330}
1331
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001332int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1333 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001334 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001335 return -1;
1336 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001337 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001338}
1339
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001340int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1341 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001342 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001343}
1344
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001345int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001346 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001347}
1348
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001349int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001350 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001351 return -1;
1352 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001353 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001354}
1355
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001356int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001357 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001358 return -1;
1359 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001360 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001361}
1362
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001363// Video
1364VideoCodecInformation *RTPSender::CodecInformationVideo() {
1365 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001366 return NULL;
1367 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001368 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001369}
1370
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001371RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001372 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001373 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001374}
1375
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001376uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001377 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001378 return 0;
1379 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001380 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001381}
1382
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001383int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001384 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001385 return -1;
1386 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001387 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001388}
1389
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001390int32_t RTPSender::SetGenericFECStatus(
1391 const bool enable, const uint8_t payload_type_red,
1392 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001393 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001394 return -1;
1395 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001396 return video_->SetGenericFECStatus(enable, payload_type_red,
1397 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001398}
1399
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001400int32_t RTPSender::GenericFECStatus(
1401 bool *enable, uint8_t *payload_type_red,
1402 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001403 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001404 return -1;
1405 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001406 return video_->GenericFECStatus(
1407 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001408}
1409
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001410int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001411 const FecProtectionParams *delta_params,
1412 const FecProtectionParams *key_params) {
1413 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001414 return -1;
1415 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001416 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001417}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001418
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001419void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1420 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001421 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001422 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001423 // Add RTX header.
1424 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001425 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001426
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001427 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001428 rtp_parser.Parse(rtp_header);
1429
1430 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001431 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001432
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001433 // Replace payload type, if a specific type is set for RTX.
1434 if (payload_type_rtx_ != -1) {
1435 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001436 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001437 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1438 }
1439
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001440 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001441 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001442 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1443
1444 // Replace SSRC.
1445 ptr += 6;
1446 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1447
1448 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001449 ptr = data_buffer_rtx + rtp_header.headerLength;
1450 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001451 ptr += 2;
1452
1453 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001454 memcpy(ptr, buffer + rtp_header.headerLength,
1455 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001456 *length += 2;
1457}
1458
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001459} // namespace webrtc