blob: c7179646b5f131e870f6848149f7e46873985fda [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";
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000035 }
36 return "";
37}
38
39} // namespace
40
pbos@webrtc.org2f446732013-04-08 11:08:41 +000041RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000042 Transport *transport, RtpAudioFeedback *audio_feedback,
43 PacedSender *paced_sender)
44 : Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL),
45 video_(NULL), paced_sender_(paced_sender),
46 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
47 transport_(transport), sending_media_(true), // Default to sending media.
48 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
49 target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1),
50 payload_type_map_(), rtp_header_extension_map_(),
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +000051 transmission_time_offset_(0), absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000052 // NACK.
53 nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000054 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000055 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +000056 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000057 packets_sent_(0), payload_bytes_sent_(0), start_time_stamp_forced_(false),
58 start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000059 remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false),
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +000060 timestamp_(0), capture_time_ms_(0), last_timestamp_time_ms_(0),
61 last_packet_marker_bit_(false), num_csrcs_(0), csrcs_(),
sprang@webrtc.org71f055f2013-12-04 15:09:27 +000062 include_csrcs_(true), rtx_(kRtxOff), payload_type_rtx_(-1),
63 frame_counts_(), frame_count_observer_(NULL) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000064 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
65 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +000066 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000067 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000068 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000069 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000070 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
71 // Random start, 16 bits. Can't be 0.
72 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
73 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +000074
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000075 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000076 audio_ = new RTPSenderAudio(id, clock_, this);
77 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000078 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000079 video_ = new RTPSenderVideo(id, clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000080 }
81 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000082}
83
pwestin@webrtc.org00741872012-01-19 15:56:10 +000084RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000085 if (remote_ssrc_ != 0) {
86 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000087 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000088 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +000089
pwestin@webrtc.org00741872012-01-19 15:56:10 +000090 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000091 delete send_critsect_;
92 while (!payload_type_map_.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000093 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000094 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +000095 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000096 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000097 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000098 delete audio_;
99 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000100
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000101 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000102}
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000104void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000105 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000106}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000107
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000108uint16_t RTPSender::ActualSendBitrateKbit() const {
109 return (uint16_t)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000110}
111
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000112uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000113 if (video_) {
114 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000115 }
116 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000117}
118
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000119uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000120 if (video_) {
121 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000122 }
123 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000124}
125
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000126uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000127 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000128}
129
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000130int32_t RTPSender::SetTransmissionTimeOffset(
131 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000132 if (transmission_time_offset > (0x800000 - 1) ||
133 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000134 return -1;
135 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000136 CriticalSectionScoped cs(send_critsect_);
137 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000138 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000139}
140
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000141int32_t RTPSender::SetAbsoluteSendTime(
142 const uint32_t absolute_send_time) {
143 if (absolute_send_time > 0xffffff) { // UWord24.
144 return -1;
145 }
146 CriticalSectionScoped cs(send_critsect_);
147 absolute_send_time_ = absolute_send_time;
148 return 0;
149}
150
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000151int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
152 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000153 CriticalSectionScoped cs(send_critsect_);
154 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000155}
156
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000157int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000158 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000159 CriticalSectionScoped cs(send_critsect_);
160 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000161}
162
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000163uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000164 CriticalSectionScoped cs(send_critsect_);
165 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000166}
167
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000168int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000169 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000170 const int8_t payload_number, const uint32_t frequency,
171 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000172 assert(payload_name);
173 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000175 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000176 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000178 if (payload_type_map_.end() != it) {
179 // We already use this payload type.
180 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000181 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000183 // Check if it's the same as we already have.
184 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000185 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000186 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000187 payload->typeSpecific.Audio.frequency == frequency &&
188 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000189 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000190 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000191 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000192 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000193 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000194 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000195 return 0;
196 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000197 }
198 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000199 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000200 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000201 ModuleRTPUtility::Payload *payload = NULL;
202 if (audio_configured_) {
203 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
204 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000205 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000206 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
207 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000208 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000209 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000210 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000211 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000212 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213}
214
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000215int32_t RTPSender::DeRegisterSendPayload(
216 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000217 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000218
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000219 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000220 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000221
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000222 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000223 return -1;
224 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000225 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000226 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000227 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000228 return 0;
229}
niklase@google.com470e71d2011-07-07 08:21:25 +0000230
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000231int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000233int RTPSender::SendPayloadFrequency() const {
234 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
235}
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
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000271void RTPSender::SetRTXStatus(int 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
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000283void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000284 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000285 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000286 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000287 *ssrc = ssrc_rtx_;
288 *payload_type = payload_type_rtx_;
289}
290
291
292void RTPSender::SetRtxPayloadType(int payload_type) {
293 CriticalSectionScoped cs(send_critsect_);
294 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000295}
296
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000297int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
298 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000299 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000301 if (payload_type < 0) {
302 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
303 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000304 return -1;
305 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000306 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000307 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000308 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000309 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000310 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000311 // And it's a match...
312 return 0;
313 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000314 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000315 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000316 if (payload_type_ == payload_type) {
317 if (!audio_configured_) {
318 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000319 }
320 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000321 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000322 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000323 payload_type_map_.find(payload_type);
324 if (it == payload_type_map_.end()) {
325 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
326 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000327 return -1;
328 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000329 payload_type_ = payload_type;
330 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000331 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000332 if (!payload->audio && !audio_configured_) {
333 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
334 *video_type = payload->typeSpecific.Video.videoCodecType;
335 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000336 }
337 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000338}
339
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000340int32_t RTPSender::SendOutgoingData(
341 const FrameType frame_type, const int8_t payload_type,
342 const uint32_t capture_timestamp, int64_t capture_time_ms,
343 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000344 const RTPFragmentationHeader *fragmentation,
345 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000346 {
347 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000348 CriticalSectionScoped cs(send_critsect_);
349 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000350 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000351 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000352 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000353 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000354 if (CheckPayloadType(payload_type, &video_type) != 0) {
355 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
356 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000357 __FUNCTION__, payload_type);
358 return -1;
359 }
360
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000361 uint32_t ret_val;
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
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000368 ret_val = 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 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000383 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
384 capture_timestamp, capture_time_ms,
385 payload_data, payload_size,
386 fragmentation, codec_info,
387 rtp_type_hdr);
388
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000389 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000390
391 CriticalSectionScoped cs(statistics_crit_.get());
392 uint32_t frame_count = ++frame_counts_[frame_type];
393 if (frame_count_observer_) {
394 frame_count_observer_->FrameCountUpdated(frame_type,
395 frame_count,
396 ssrc_);
397 }
398
399 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000400}
401
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000402int RTPSender::SendRedundantPayloads(int payload_type, int bytes_to_send) {
403 if (!(rtx_ & kRtxRedundantPayloads))
404 return 0;
405 uint8_t buffer[IP_PACKET_SIZE];
406 int bytes_left = bytes_to_send;
407 while (bytes_left > 0) {
408 uint16_t length = bytes_left;
409 int64_t capture_time_ms;
410 if (!packet_history_.GetBestFittingPacket(buffer, &length,
411 &capture_time_ms)) {
412 break;
413 }
414 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true))
415 return -1;
416 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
417 RTPHeader rtp_header;
418 rtp_parser.Parse(rtp_header);
419 bytes_left -= length - rtp_header.headerLength;
420 }
421 return bytes_to_send - bytes_left;
422}
423
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000424bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000425 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000426 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000427 // Current bitrate since last estimate(1 second) averaged with the
428 // estimate since then, to get the most up to date bitrate.
429 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000430 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000431 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000432 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000433 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000434 int bytes = 0;
435 if (current_bitrate == 0) {
436 // Start up phase. Send one 33.3 ms batch to start with.
437 bytes = (bitrate_diff / 8) / 30;
438 } else {
439 bytes = (bitrate_diff / 8);
440 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000441 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000442 if (bytes > bytes_cap) {
443 bytes = bytes_cap;
444 }
445 }
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000446 uint32_t timestamp;
447 {
448 CriticalSectionScoped cs(send_critsect_);
449 // Add the random RTP timestamp offset and store the capture time for
450 // later calculation of the send time offset.
451 timestamp = start_time_stamp_ + capture_timestamp;
452 timestamp_ = timestamp;
453 capture_time_ms_ = capture_time_ms;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000454 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000455 }
456 int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
457 bytes, kDontRetransmit, false, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000458 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
459 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000460}
461
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000462int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
463 int32_t bytes) {
464 int padding_bytes_in_packet = kMaxPaddingLength;
465 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000466 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000467 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000468 packet[0] |= 0x20; // Set padding bit.
469 int32_t *data =
470 reinterpret_cast<int32_t *>(&(packet[header_length]));
471
472 // Fill data buffer with random data.
473 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
474 data[j] = rand(); // NOLINT
475 }
476 // Set number of padding bytes in the last byte of the packet.
477 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
478 return padding_bytes_in_packet;
479}
480
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000481int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
482 int64_t capture_time_ms, int32_t bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000483 StorageType store, bool force_full_size_packets,
484 bool only_pad_after_markerbit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000485 // Drop this packet if we're not sending media packets.
486 if (!sending_media_) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000487 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000488 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000489 int padding_bytes_in_packet = 0;
490 int bytes_sent = 0;
491 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000492 // Always send full padding packets.
493 if (force_full_size_packets && bytes < kMaxPaddingLength)
494 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000495 if (bytes < kMaxPaddingLength) {
496 if (force_full_size_packets) {
497 bytes = kMaxPaddingLength;
498 } else {
499 // Round to the nearest multiple of 32.
500 bytes = (bytes + 16) & 0xffe0;
501 }
502 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000503 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000504 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000505 break;
506 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000507 uint32_t ssrc;
508 uint16_t sequence_number;
509 {
510 CriticalSectionScoped cs(send_critsect_);
511 // Only send padding packets following the last packet of a frame,
512 // indicated by the marker bit.
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000513 if (only_pad_after_markerbit && !last_packet_marker_bit_)
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000514 return bytes_sent;
515 if (rtx_ == kRtxOff) {
516 ssrc = ssrc_;
517 sequence_number = sequence_number_;
518 ++sequence_number_;
519 } else {
520 ssrc = ssrc_rtx_;
521 sequence_number = sequence_number_rtx_;
522 ++sequence_number_rtx_;
523 }
524 }
525 uint8_t padding_packet[IP_PACKET_SIZE];
526 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
527 false, timestamp, sequence_number, NULL,
528 0);
529 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
530 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000531 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
532 header_length, capture_time_ms, store,
533 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000534 // Error sending the packet.
535 break;
536 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000537 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000538 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000539 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000540}
541
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000542void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000543 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000544 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000545}
546
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000547bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000548 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000549}
niklase@google.com470e71d2011-07-07 08:21:25 +0000550
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000551int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
552 uint16_t length = IP_PACKET_SIZE;
553 uint8_t data_buffer[IP_PACKET_SIZE];
554 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000555 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000556 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
557 data_buffer, &length,
558 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000559 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000560 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000561 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000562
563 {
564 // Update send statistics prior to pacer.
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000565 CriticalSectionScoped lock(statistics_crit_.get());
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000566 Bitrate::Update(length);
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000567 ++packets_sent_;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000568 // We on purpose don't add to payload_bytes_sent_ since this is a
569 // re-transmit and not new payload data.
570 }
571
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000572
573 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
574 RTPHeader header;
stefan@webrtc.org79b63202013-12-04 13:34:28 +0000575 if (!rtp_parser.Parse(header)) {
576 assert(false);
577 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
578 "Failed to parse RTP header of packet to be retransmitted.");
579 return -1;
580 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000581 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000582 "timestamp", header.timestamp,
583 "seqnum", header.sequenceNumber);
584
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000585 if (paced_sender_) {
586 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000587 header.ssrc,
588 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000589 capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000590 length - header.headerLength,
591 true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000592 // We can't send the packet right now.
593 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000594 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000595 }
596 }
597
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000598 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000599 if ((rtx_ & kRtxRetransmitted) > 0) {
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000600 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
601 buffer_to_send_ptr = data_buffer_rtx;
602 }
603
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000604 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000605 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000606 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000607 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000608}
609
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000610bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
611 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000612 if (transport_) {
613 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000614 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000615 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
616 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000617 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000618 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000619 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
620 "Transport failed to send packet");
621 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000622 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000623 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000624}
625
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000626int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000627 if (!video_)
628 return -1;
629 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000630}
631
632int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000633 if (!video_)
634 return -1;
635 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000636}
637
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000638void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000639 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000640 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000641 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
642 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000643 const int64_t now = clock_->TimeInMilliseconds();
644 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000645
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000646 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000647 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000648 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000649 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000650 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000651 return;
652 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000653
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000654 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
655 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000656 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000657 if (bytes_sent > 0) {
658 bytes_re_sent += bytes_sent;
659 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000660 // The packet has previously been resent.
661 // Try resending next packet in the list.
662 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000663 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000664 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000665 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000666 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000667 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000668 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000669 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000670 // Delay bandwidth estimate (RTT * BW).
671 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000672 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000673 uint32_t target_bytes =
674 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000675 if (bytes_re_sent > target_bytes) {
676 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000677 }
678 }
679 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000680 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000681 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000682 UpdateNACKBitRate(bytes_re_sent, now);
683 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000684 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000685}
686
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000687bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
688 uint32_t num = 0;
689 int32_t byte_count = 0;
690 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000691
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000692 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000693
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000694 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000695 return true;
696 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000697 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
698 if ((now - nack_byte_count_times_[num]) > avg_interval) {
699 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000700 break;
701 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000702 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000703 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000704 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000705 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000706 if (num == NACK_BYTECOUNT_SIZE) {
707 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000708 // during the last msg_interval.
709 time_interval = now - nack_byte_count_times_[num - 1];
710 if (time_interval < 0) {
711 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000712 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000713 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000714 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000715}
716
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000717void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
718 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000719 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000720
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000721 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000722 if (bytes > 0) {
723 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000724 // Add padding length.
725 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000726 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000727 if (nack_byte_count_times_[0] == 0) {
728 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000729 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000730 // Shift.
731 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
732 nack_byte_count_[i + 1] = nack_byte_count_[i];
733 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000734 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000735 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000736 nack_byte_count_[0] = bytes;
737 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000738 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000739 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000740}
741
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000742// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000743bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000744 int64_t capture_time_ms,
745 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000746 uint16_t length = IP_PACKET_SIZE;
747 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000748 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000749
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000750 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
751 0,
752 retransmission,
753 data_buffer,
754 &length,
755 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000756 // Packet cannot be found. Allow sending to continue.
757 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000758 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000759 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
760 retransmission && (rtx_ & kRtxRetransmitted) > 0);
761}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000762
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000763bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
764 uint16_t length,
765 int64_t capture_time_ms,
766 bool send_over_rtx) {
767 uint8_t *buffer_to_send_ptr = buffer;
768
769 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000770 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000771 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000772 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000773 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000774 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000775
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000776 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000777 if (send_over_rtx) {
778 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000779 buffer_to_send_ptr = data_buffer_rtx;
780 }
781
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000782 int64_t now_ms = clock_->TimeInMilliseconds();
783 int64_t diff_ms = now_ms - capture_time_ms;
784 bool updated_transmission_time_offset =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000785 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
786 diff_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000787 bool updated_abs_send_time =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000788 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000789 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000790 // Update stored packet in case of receiving a re-transmission request.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000791 packet_history_.ReplaceRTPHeader(buffer_to_send_ptr,
792 rtp_header.sequenceNumber,
793 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000794 }
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000795 return SendPacketToNetwork(buffer_to_send_ptr, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000796}
797
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000798int RTPSender::TimeToSendPadding(int bytes) {
799 if (!sending_media_) {
800 return 0;
801 }
802 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000803 int64_t capture_time_ms;
804 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000805 {
806 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000807 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_ :
808 payload_type_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000809 timestamp = timestamp_;
810 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000811 if (last_timestamp_time_ms_ > 0) {
812 timestamp +=
813 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
814 capture_time_ms +=
815 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
816 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000817 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000818 int bytes_sent = SendRedundantPayloads(payload_type, bytes);
819 bytes -= bytes_sent;
820 if (bytes > 0) {
821 int padding_sent = SendPadData(payload_type, timestamp, capture_time_ms,
822 bytes, kDontStore, true, true);
823 bytes_sent += padding_sent;
824 }
825 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000826}
827
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000828// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000829int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000830 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000831 int64_t capture_time_ms, StorageType storage,
832 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000833 ModuleRTPUtility::RTPHeaderParser rtp_parser(
834 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000835 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000836 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000837
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000838 int64_t now_ms = clock_->TimeInMilliseconds();
839
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000840 // |capture_time_ms| <= 0 is considered invalid.
841 // TODO(holmer): This should be changed all over Video Engine so that negative
842 // time is consider invalid, while 0 is considered a valid time.
843 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000844 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000845 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000846 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000847
848 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
849 rtp_header, now_ms);
850
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000851 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000852 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
853 max_payload_length_, capture_time_ms,
854 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000855 return -1;
856 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000857 {
858 // Update send statistics prior to pacer.
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000859 CriticalSectionScoped lock(statistics_crit_.get());
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000860 Bitrate::Update(payload_length + rtp_header_length);
861 ++packets_sent_;
862 payload_bytes_sent_ += payload_length;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000863 }
864
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000865 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000866 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
867 rtp_header.sequenceNumber, capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000868 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000869 // We can't send the packet right now.
870 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000871 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000872 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000873 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000874 if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
875 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000876 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000877 return -1;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000878}
879
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000880void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000881 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000882 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000883 nack_bitrate_.Process();
884 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000885 return;
886 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000887 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000888}
889
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000890uint16_t RTPSender::RTPHeaderLength() const {
891 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000892 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000893 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000894 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000895 rtp_header_length += RtpHeaderExtensionTotalLength();
896 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000897}
898
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000899uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000900 CriticalSectionScoped cs(send_critsect_);
901 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000902}
903
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000904void RTPSender::ResetDataCounters() {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000905 CriticalSectionScoped lock(statistics_crit_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000906 packets_sent_ = 0;
907 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000908}
909
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000910uint32_t RTPSender::Packets() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000911 CriticalSectionScoped lock(statistics_crit_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000912 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000913}
914
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000915// Number of sent RTP bytes.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000916uint32_t RTPSender::Bytes() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000917 CriticalSectionScoped lock(statistics_crit_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000918 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000919}
920
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000921int RTPSender::CreateRTPHeader(
922 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
923 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
924 uint8_t num_csrcs) const {
925 header[0] = 0x80; // version 2.
926 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000927 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000928 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000929 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000930 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
931 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
932 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000933 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000934
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000935 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000936 if (num_csrcs > 0) {
937 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000938 // error
939 assert(false);
940 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000941 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000942 uint8_t *ptr = &header[rtp_header_length];
943 for (int i = 0; i < num_csrcs; ++i) {
944 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000945 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000946 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000947 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000948
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000949 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000950 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000951 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000952
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000953 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
954 if (len > 0) {
955 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000956 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000957 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000958 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000959}
960
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000961int32_t RTPSender::BuildRTPheader(
962 uint8_t *data_buffer, const int8_t payload_type,
963 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000964 int64_t capture_time_ms, const bool time_stamp_provided,
965 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000966 assert(payload_type >= 0);
967 CriticalSectionScoped cs(send_critsect_);
968
969 if (time_stamp_provided) {
970 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000971 } else {
972 // Make a unique time stamp.
973 // We can't inc by the actual time, since then we increase the risk of back
974 // timing.
975 timestamp_++;
976 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000977 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000978 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000979 capture_time_ms_ = capture_time_ms;
980 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000981 int csrcs_length = 0;
982 if (include_csrcs_)
983 csrcs_length = num_csrcs_;
984 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
985 timestamp_, sequence_number, csrcs_, csrcs_length);
986}
987
988uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000989 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000990 return 0;
991 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000992 // RTP header extension, RFC 3550.
993 // 0 1 2 3
994 // 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
995 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
996 // | defined by profile | length |
997 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
998 // | header extension |
999 // | .... |
1000 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001001 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001002 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001003
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001004 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001005 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001006 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001007
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001008 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001009 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001010
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001011 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001012 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001013 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001014 switch (type) {
1015 case kRtpExtensionTransmissionTimeOffset:
1016 block_length = BuildTransmissionTimeOffsetExtension(
1017 data_buffer + kHeaderLength + total_block_length);
1018 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001019 case kRtpExtensionAudioLevel:
1020 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
1021 // we don't have to care about it here, which is true until we wan't to
1022 // use it together with any of the other extensions we support.
1023 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001024 case kRtpExtensionAbsoluteSendTime:
1025 block_length = BuildAbsoluteSendTimeExtension(
1026 data_buffer + kHeaderLength + total_block_length);
1027 break;
1028 default:
1029 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001030 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001031 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001032 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001033 }
1034 if (total_block_length == 0) {
1035 // No extension added.
1036 return 0;
1037 }
1038 // Set header length (in number of Word32, header excluded).
1039 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001040 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001041 total_block_length / 4);
1042 // Total added length.
1043 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001044}
1045
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001046uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1047 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001048 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1049 //
1050 // The transmission time is signaled to the receiver in-band using the
1051 // general mechanism for RTP header extensions [RFC5285]. The payload
1052 // of this extension (the transmitted value) is a 24-bit signed integer.
1053 // When added to the RTP timestamp of the packet, it represents the
1054 // "effective" RTP transmission time of the packet, on the RTP
1055 // timescale.
1056 //
1057 // The form of the transmission offset extension block:
1058 //
1059 // 0 1 2 3
1060 // 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
1061 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1062 // | ID | len=2 | transmission offset |
1063 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001064
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001065 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001066 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001067 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1068 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001069 // Not registered.
1070 return 0;
1071 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001072 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001073 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001074 data_buffer[pos++] = (id << 4) + len;
1075 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1076 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001077 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001078 assert(pos == kTransmissionTimeOffsetLength);
1079 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001080}
1081
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001082uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
1083 uint8_t* data_buffer) const {
1084 // Absolute send time in RTP streams.
1085 //
1086 // The absolute send time is signaled to the receiver in-band using the
1087 // general mechanism for RTP header extensions [RFC5285]. The payload
1088 // of this extension (the transmitted value) is a 24-bit unsigned integer
1089 // containing the sender's current time in seconds as a fixed point number
1090 // with 18 bits fractional part.
1091 //
1092 // The form of the absolute send time extension block:
1093 //
1094 // 0 1 2 3
1095 // 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
1096 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1097 // | ID | len=2 | absolute send time |
1098 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1099
1100 // Get id defined by user.
1101 uint8_t id;
1102 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1103 &id) != 0) {
1104 // Not registered.
1105 return 0;
1106 }
1107 size_t pos = 0;
1108 const uint8_t len = 2;
1109 data_buffer[pos++] = (id << 4) + len;
1110 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1111 absolute_send_time_);
1112 pos += 3;
1113 assert(pos == kAbsoluteSendTimeLength);
1114 return kAbsoluteSendTimeLength;
1115}
1116
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001117bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001118 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001119 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001120 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001121
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001122 // Get length until start of header extension block.
1123 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001124 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001125 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001126 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001127 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001128 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001129 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001130 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001131 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001132 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001133 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001134 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001135 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001136 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001137 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001138 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001139 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001140 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1141 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001142 WEBRTC_TRACE(
1143 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001144 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001145 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001146 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001147 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001148 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001149 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1150 &id) != 0) {
1151 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001152 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001153 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001154 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001155 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001156 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001157 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001158 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001159 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001160 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001161 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001162 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001163 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001164 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001165 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001166}
1167
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001168bool RTPSender::UpdateAbsoluteSendTime(
1169 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001170 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001171 CriticalSectionScoped cs(send_critsect_);
1172
1173 // Get length until start of header extension block.
1174 int extension_block_pos =
1175 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1176 kRtpExtensionAbsoluteSendTime);
1177 if (extension_block_pos < 0) {
1178 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1179 "Failed to update absolute send time, not registered.");
1180 return false;
1181 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001182 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001183 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001184 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001185 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1186 "Failed to update absolute send time, invalid length.");
1187 return false;
1188 }
1189 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001190 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1191 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001192 WEBRTC_TRACE(
1193 kTraceStream, kTraceRtpRtcp, id_,
1194 "Failed to update absolute send time, hdr extension not found.");
1195 return false;
1196 }
1197 // Get id.
1198 uint8_t id = 0;
1199 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1200 &id) != 0) {
1201 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1202 "Failed to update absolute send time, no id.");
1203 return false;
1204 }
1205 // Verify first byte in block.
1206 const uint8_t first_block_byte = (id << 4) + 2;
1207 if (rtp_packet[block_pos] != first_block_byte) {
1208 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1209 "Failed to update absolute send time.");
1210 return false;
1211 }
1212 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1213 // fractional part).
1214 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1215 ((now_ms << 18) / 1000) & 0x00ffffff);
1216 return true;
1217}
1218
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001219void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001220 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001221 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001222 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001223
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001224 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001225 SetStartTimestamp(RTPtime, false);
1226 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001227 if (!ssrc_forced_) {
1228 // Generate a new SSRC.
1229 ssrc_db_.ReturnSSRC(ssrc_);
1230 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001231 }
1232 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001233 if (!sequence_number_forced_ && !ssrc_forced_) {
1234 // Generate a new sequence number.
1235 sequence_number_ =
1236 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001237 }
1238 }
1239}
1240
1241void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001242 CriticalSectionScoped cs(send_critsect_);
1243 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001244}
1245
1246bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001247 CriticalSectionScoped cs(send_critsect_);
1248 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001249}
1250
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001251uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001252 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001253 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001254}
1255
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001256void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001257 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001258 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001259 start_time_stamp_forced_ = force;
1260 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001261 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001262 if (!start_time_stamp_forced_) {
1263 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001264 }
1265 }
1266}
1267
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001268uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001269 CriticalSectionScoped cs(send_critsect_);
1270 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001271}
1272
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001273uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001274 // If configured via API, return 0.
1275 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001276
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001277 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001278 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001279 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001280 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1281 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001282}
1283
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001284void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001285 // This is configured via the API.
1286 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001287
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001288 if (ssrc_ == ssrc && ssrc_forced_) {
1289 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001290 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001291 ssrc_forced_ = true;
1292 ssrc_db_.ReturnSSRC(ssrc_);
1293 ssrc_db_.RegisterSSRC(ssrc);
1294 ssrc_ = ssrc;
1295 if (!sequence_number_forced_) {
1296 sequence_number_ =
1297 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001298 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001299}
1300
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001301uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001302 CriticalSectionScoped cs(send_critsect_);
1303 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001304}
1305
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001306void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001307 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001308}
1309
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001310void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1311 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001312 assert(arr_length <= kRtpCsrcSize);
1313 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001314
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001315 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001316 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001317 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001318 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001319}
1320
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001321int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001322 assert(arr_of_csrc);
1323 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001324 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1325 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001326 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001327 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001328}
1329
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001330void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001331 CriticalSectionScoped cs(send_critsect_);
1332 sequence_number_forced_ = true;
1333 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001334}
1335
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001336uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001337 CriticalSectionScoped cs(send_critsect_);
1338 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001339}
1340
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001341// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001342int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1343 const uint16_t time_ms,
1344 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001345 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001346 return -1;
1347 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001348 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001349}
1350
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001351bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001352 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001353 return false;
1354 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001355 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001356}
1357
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001358int32_t RTPSender::SetAudioPacketSize(
1359 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001360 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001361 return -1;
1362 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001363 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001364}
1365
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001366int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1367 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001368 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001369 return -1;
1370 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001371 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001372}
1373
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001374int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1375 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001376 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001377}
1378
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001379int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001380 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001381}
1382
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001383int32_t RTPSender::SetRED(const int8_t payload_type) {
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 audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001388}
1389
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001390int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001391 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001392 return -1;
1393 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001394 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001395}
1396
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001397// Video
1398VideoCodecInformation *RTPSender::CodecInformationVideo() {
1399 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001400 return NULL;
1401 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001402 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001403}
1404
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001405RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001406 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001407 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001408}
1409
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001410uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001411 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001412 return 0;
1413 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001414 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001415}
1416
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001417int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001418 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001419 return -1;
1420 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001421 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001422}
1423
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001424int32_t RTPSender::SetGenericFECStatus(
1425 const bool enable, const uint8_t payload_type_red,
1426 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001427 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001428 return -1;
1429 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001430 return video_->SetGenericFECStatus(enable, payload_type_red,
1431 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001432}
1433
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001434int32_t RTPSender::GenericFECStatus(
1435 bool *enable, uint8_t *payload_type_red,
1436 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001437 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001438 return -1;
1439 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001440 return video_->GenericFECStatus(
1441 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001442}
1443
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001444int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001445 const FecProtectionParams *delta_params,
1446 const FecProtectionParams *key_params) {
1447 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001448 return -1;
1449 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001450 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001451}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001452
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001453void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1454 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001455 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001456 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001457 // Add RTX header.
1458 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001459 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001460
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001461 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001462 rtp_parser.Parse(rtp_header);
1463
1464 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001465 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001466
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001467 // Replace payload type, if a specific type is set for RTX.
1468 if (payload_type_rtx_ != -1) {
1469 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001470 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001471 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1472 }
1473
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001474 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001475 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001476 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1477
1478 // Replace SSRC.
1479 ptr += 6;
1480 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1481
1482 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001483 ptr = data_buffer_rtx + rtp_header.headerLength;
1484 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001485 ptr += 2;
1486
1487 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001488 memcpy(ptr, buffer + rtp_header.headerLength,
1489 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001490 *length += 2;
1491}
1492
sprang@webrtc.org71f055f2013-12-04 15:09:27 +00001493void RTPSender::RegisterFrameCountObserver(FrameCountObserver* observer) {
1494 CriticalSectionScoped cs(statistics_crit_.get());
1495 if (observer != NULL)
1496 assert(frame_count_observer_ == NULL);
1497 frame_count_observer_ = observer;
1498}
1499
1500FrameCountObserver* RTPSender::GetFrameCountObserver() const {
1501 CriticalSectionScoped cs(statistics_crit_.get());
1502 return frame_count_observer_;
1503}
1504
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001505} // namespace webrtc