blob: 0929fd9675ecf034c51486eedc49bf5d3dd7068b [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;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +000025const int kSendSideDelayWindowMs = 1000;
stefan@webrtc.orga8179622013-06-04 13:47:36 +000026
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";
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000036 }
37 return "";
38}
39
40} // namespace
41
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000042RTPSender::RTPSender(const int32_t id,
43 const bool audio,
44 Clock* clock,
45 Transport* transport,
46 RtpAudioFeedback* audio_feedback,
47 PacedSender* paced_sender)
48 : clock_(clock),
49 bitrate_sent_(clock, this),
50 id_(id),
51 audio_configured_(audio),
52 audio_(NULL),
53 video_(NULL),
54 paced_sender_(paced_sender),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000055 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000056 transport_(transport),
57 sending_media_(true), // Default to sending media.
58 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
59 target_send_bitrate_(0),
60 packet_over_head_(28),
61 payload_type_(-1),
62 payload_type_map_(),
63 rtp_header_extension_map_(),
64 transmission_time_offset_(0),
65 absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000066 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000067 nack_byte_count_times_(),
68 nack_byte_count_(),
69 nack_bitrate_(clock, NULL),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000070 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000071 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +000072 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000073 frame_count_observer_(NULL),
74 rtp_stats_callback_(NULL),
75 bitrate_callback_(NULL),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +000076 // RTP variables
77 start_time_stamp_forced_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000078 start_time_stamp_(0),
79 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
80 remote_ssrc_(0),
81 sequence_number_forced_(false),
82 ssrc_forced_(false),
83 timestamp_(0),
84 capture_time_ms_(0),
85 last_timestamp_time_ms_(0),
86 last_packet_marker_bit_(false),
87 num_csrcs_(0),
88 csrcs_(),
89 include_csrcs_(true),
90 rtx_(kRtxOff),
91 payload_type_rtx_(-1) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000092 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
93 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +000094 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000095 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000096 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000097 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000098 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
99 // Random start, 16 bits. Can't be 0.
100 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
101 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000103 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000104 audio_ = new RTPSenderAudio(id, clock_, this);
105 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000106 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000107 video_ = new RTPSenderVideo(id, clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000108 }
109 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000110}
111
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000112RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000113 if (remote_ssrc_ != 0) {
114 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000115 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000116 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000118 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000119 delete send_critsect_;
120 while (!payload_type_map_.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000121 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000122 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000123 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000124 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000125 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000126 delete audio_;
127 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000128
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000129 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000130}
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000132void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000133 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000134}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000135
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000136uint16_t RTPSender::ActualSendBitrateKbit() const {
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000137 return (uint16_t)(bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000138}
139
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000140uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000141 if (video_) {
142 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000143 }
144 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000145}
146
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000147uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000148 if (video_) {
149 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000150 }
151 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000152}
153
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000154uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000155 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000156}
157
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000158bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
159 int* max_send_delay_ms) const {
160 CriticalSectionScoped cs(statistics_crit_.get());
161 SendDelayMap::const_iterator it = send_delays_.upper_bound(
162 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
163 if (!sending_media_ || it == send_delays_.end())
164 return false;
165 int num_delays = 0;
166 for (; it != send_delays_.end(); ++it) {
167 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
168 *avg_send_delay_ms += it->second;
169 ++num_delays;
170 }
171 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
172 return true;
173}
174
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000175int32_t RTPSender::SetTransmissionTimeOffset(
176 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000177 if (transmission_time_offset > (0x800000 - 1) ||
178 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000179 return -1;
180 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000181 CriticalSectionScoped cs(send_critsect_);
182 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000183 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000184}
185
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000186int32_t RTPSender::SetAbsoluteSendTime(
187 const uint32_t absolute_send_time) {
188 if (absolute_send_time > 0xffffff) { // UWord24.
189 return -1;
190 }
191 CriticalSectionScoped cs(send_critsect_);
192 absolute_send_time_ = absolute_send_time;
193 return 0;
194}
195
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000196int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
197 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000198 CriticalSectionScoped cs(send_critsect_);
199 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000200}
201
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000202int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000203 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000204 CriticalSectionScoped cs(send_critsect_);
205 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000206}
207
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000208uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000209 CriticalSectionScoped cs(send_critsect_);
210 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000211}
212
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000213int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000214 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000215 const int8_t payload_number, const uint32_t frequency,
216 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000217 assert(payload_name);
218 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000219
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000220 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000221 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000222
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000223 if (payload_type_map_.end() != it) {
224 // We already use this payload type.
225 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000226 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000228 // Check if it's the same as we already have.
229 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000230 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000231 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000232 payload->typeSpecific.Audio.frequency == frequency &&
233 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000234 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000235 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000236 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000237 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000238 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000239 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000240 return 0;
241 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000242 }
243 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000244 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000245 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000246 ModuleRTPUtility::Payload *payload = NULL;
247 if (audio_configured_) {
248 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
249 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000250 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000251 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
252 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000253 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000254 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000255 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000256 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000257 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000258}
259
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000260int32_t RTPSender::DeRegisterSendPayload(
261 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000262 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000263
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000264 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000265 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000266
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000267 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000268 return -1;
269 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000270 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000271 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000272 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000273 return 0;
274}
niklase@google.com470e71d2011-07-07 08:21:25 +0000275
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000276int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000277
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000278int RTPSender::SendPayloadFrequency() const {
279 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
280}
niklase@google.com470e71d2011-07-07 08:21:25 +0000281
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000282int32_t RTPSender::SetMaxPayloadLength(
283 const uint16_t max_payload_length,
284 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000285 // Sanity check.
286 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
287 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
288 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000289 return -1;
290 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000291 CriticalSectionScoped cs(send_critsect_);
292 max_payload_length_ = max_payload_length;
293 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000294
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000295 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
296 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000297 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000298}
299
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000300uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000301 if (audio_configured_) {
302 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000303 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000304 return max_payload_length_ - RTPHeaderLength() -
305 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
306 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000307 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000308}
309
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000310uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000311 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000312}
313
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000314uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000315
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000316void RTPSender::SetRTXStatus(int mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000317 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000318 rtx_ = mode;
319 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000320 if (set_ssrc) {
321 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000322 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000323 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000324 }
325 }
326}
327
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000328void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000329 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000330 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000331 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000332 *ssrc = ssrc_rtx_;
333 *payload_type = payload_type_rtx_;
334}
335
336
337void RTPSender::SetRtxPayloadType(int payload_type) {
338 CriticalSectionScoped cs(send_critsect_);
339 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000340}
341
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000342int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
343 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000344 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000345
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000346 if (payload_type < 0) {
347 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
348 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000349 return -1;
350 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000351 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000352 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000353 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000354 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000355 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000356 // And it's a match...
357 return 0;
358 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000359 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000360 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000361 if (payload_type_ == payload_type) {
362 if (!audio_configured_) {
363 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000364 }
365 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000366 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000367 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000368 payload_type_map_.find(payload_type);
369 if (it == payload_type_map_.end()) {
370 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
371 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000372 return -1;
373 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000374 payload_type_ = payload_type;
375 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000376 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000377 if (!payload->audio && !audio_configured_) {
378 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
379 *video_type = payload->typeSpecific.Video.videoCodecType;
380 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000381 }
382 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000383}
384
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000385int32_t RTPSender::SendOutgoingData(
386 const FrameType frame_type, const int8_t payload_type,
387 const uint32_t capture_timestamp, int64_t capture_time_ms,
388 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000389 const RTPFragmentationHeader *fragmentation,
390 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000391 {
392 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000393 CriticalSectionScoped cs(send_critsect_);
394 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000395 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000396 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000397 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000398 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000399 if (CheckPayloadType(payload_type, &video_type) != 0) {
400 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
401 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000402 __FUNCTION__, payload_type);
403 return -1;
404 }
405
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000406 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000407 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000408 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
409 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000410 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000411 frame_type == kFrameEmpty);
412
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000413 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
414 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000415 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000416 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
417 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000418 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000419
420 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000421 if (paced_sender_->Enabled()) {
422 // Padding is driven by the pacer and not by the encoder.
423 return 0;
424 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000425 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000426 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000427 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000428 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
429 capture_timestamp, capture_time_ms,
430 payload_data, payload_size,
431 fragmentation, codec_info,
432 rtp_type_hdr);
433
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000434 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000435
436 CriticalSectionScoped cs(statistics_crit_.get());
437 uint32_t frame_count = ++frame_counts_[frame_type];
438 if (frame_count_observer_) {
439 frame_count_observer_->FrameCountUpdated(frame_type,
440 frame_count,
441 ssrc_);
442 }
443
444 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000445}
446
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000447int RTPSender::SendRedundantPayloads(int payload_type, int bytes_to_send) {
448 if (!(rtx_ & kRtxRedundantPayloads))
449 return 0;
450 uint8_t buffer[IP_PACKET_SIZE];
451 int bytes_left = bytes_to_send;
452 while (bytes_left > 0) {
453 uint16_t length = bytes_left;
454 int64_t capture_time_ms;
455 if (!packet_history_.GetBestFittingPacket(buffer, &length,
456 &capture_time_ms)) {
457 break;
458 }
459 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true))
460 return -1;
461 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
462 RTPHeader rtp_header;
463 rtp_parser.Parse(rtp_header);
464 bytes_left -= length - rtp_header.headerLength;
465 }
466 return bytes_to_send - bytes_left;
467}
468
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000469bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000470 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000471 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000472 // Current bitrate since last estimate(1 second) averaged with the
473 // estimate since then, to get the most up to date bitrate.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000474 uint32_t current_bitrate = bitrate_sent_.BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000475 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000476 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000477 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000478 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000479 int bytes = 0;
480 if (current_bitrate == 0) {
481 // Start up phase. Send one 33.3 ms batch to start with.
482 bytes = (bitrate_diff / 8) / 30;
483 } else {
484 bytes = (bitrate_diff / 8);
485 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000486 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000487 if (bytes > bytes_cap) {
488 bytes = bytes_cap;
489 }
490 }
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000491 uint32_t timestamp;
492 {
493 CriticalSectionScoped cs(send_critsect_);
494 // Add the random RTP timestamp offset and store the capture time for
495 // later calculation of the send time offset.
496 timestamp = start_time_stamp_ + capture_timestamp;
497 timestamp_ = timestamp;
498 capture_time_ms_ = capture_time_ms;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000499 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000500 }
501 int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
502 bytes, kDontRetransmit, false, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000503 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
504 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000505}
506
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000507int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
508 int32_t bytes) {
509 int padding_bytes_in_packet = kMaxPaddingLength;
510 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000511 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000512 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000513 packet[0] |= 0x20; // Set padding bit.
514 int32_t *data =
515 reinterpret_cast<int32_t *>(&(packet[header_length]));
516
517 // Fill data buffer with random data.
518 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
519 data[j] = rand(); // NOLINT
520 }
521 // Set number of padding bytes in the last byte of the packet.
522 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
523 return padding_bytes_in_packet;
524}
525
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000526int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
527 int64_t capture_time_ms, int32_t bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000528 StorageType store, bool force_full_size_packets,
529 bool only_pad_after_markerbit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000530 // Drop this packet if we're not sending media packets.
531 if (!sending_media_) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000532 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000533 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000534 int padding_bytes_in_packet = 0;
535 int bytes_sent = 0;
536 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000537 // Always send full padding packets.
538 if (force_full_size_packets && bytes < kMaxPaddingLength)
539 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000540 if (bytes < kMaxPaddingLength) {
541 if (force_full_size_packets) {
542 bytes = kMaxPaddingLength;
543 } else {
544 // Round to the nearest multiple of 32.
545 bytes = (bytes + 16) & 0xffe0;
546 }
547 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000548 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000549 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000550 break;
551 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000552 uint32_t ssrc;
553 uint16_t sequence_number;
554 {
555 CriticalSectionScoped cs(send_critsect_);
556 // Only send padding packets following the last packet of a frame,
557 // indicated by the marker bit.
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000558 if (only_pad_after_markerbit && !last_packet_marker_bit_)
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000559 return bytes_sent;
560 if (rtx_ == kRtxOff) {
561 ssrc = ssrc_;
562 sequence_number = sequence_number_;
563 ++sequence_number_;
564 } else {
565 ssrc = ssrc_rtx_;
566 sequence_number = sequence_number_rtx_;
567 ++sequence_number_rtx_;
568 }
569 }
570 uint8_t padding_packet[IP_PACKET_SIZE];
571 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
572 false, timestamp, sequence_number, NULL,
573 0);
574 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
575 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000576 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
577 header_length, capture_time_ms, store,
578 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000579 // Error sending the packet.
580 break;
581 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000582 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000583 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000584 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000585}
586
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000587void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000588 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000589 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000590}
591
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000592bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000593 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000594}
niklase@google.com470e71d2011-07-07 08:21:25 +0000595
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000596int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
597 uint16_t length = IP_PACKET_SIZE;
598 uint8_t data_buffer[IP_PACKET_SIZE];
599 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000600 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000601 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
602 data_buffer, &length,
603 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000604 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000605 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000606 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000607
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000608 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
609 RTPHeader header;
stefan@webrtc.org79b63202013-12-04 13:34:28 +0000610 if (!rtp_parser.Parse(header)) {
611 assert(false);
612 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
613 "Failed to parse RTP header of packet to be retransmitted.");
614 return -1;
615 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000616 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000617 "timestamp", header.timestamp,
618 "seqnum", header.sequenceNumber);
619
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000620 if (paced_sender_) {
621 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000622 header.ssrc,
623 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000624 capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000625 length - header.headerLength,
626 true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000627 // We can't send the packet right now.
628 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000629 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000630 }
631 }
632
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000633 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000634 if ((rtx_ & kRtxRetransmitted) > 0) {
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000635 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
636 buffer_to_send_ptr = data_buffer_rtx;
637 }
638
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000639 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000640 UpdateRtpStats(buffer_to_send_ptr, length, header, rtx_ != kRtxOff, true);
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000641 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000642 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000643 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000644}
645
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000646bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
647 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000648 if (transport_) {
649 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000650 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000651 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
652 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000653 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000654 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000655 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
656 "Transport failed to send packet");
657 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000658 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000659 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000660}
661
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000662int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000663 if (!video_)
664 return -1;
665 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000666}
667
668int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000669 if (!video_)
670 return -1;
671 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000672}
673
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000674void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000675 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000676 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000677 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
678 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000679 const int64_t now = clock_->TimeInMilliseconds();
680 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000681
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000682 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000683 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000684 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000685 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000686 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000687 return;
688 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000689
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000690 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
691 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000692 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000693 if (bytes_sent > 0) {
694 bytes_re_sent += bytes_sent;
695 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000696 // The packet has previously been resent.
697 // Try resending next packet in the list.
698 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000699 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000700 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000701 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000702 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000703 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000704 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000706 // Delay bandwidth estimate (RTT * BW).
707 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000708 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000709 uint32_t target_bytes =
710 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000711 if (bytes_re_sent > target_bytes) {
712 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000713 }
714 }
715 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000716 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000717 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000718 UpdateNACKBitRate(bytes_re_sent, now);
719 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000720 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000721}
722
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000723bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
724 uint32_t num = 0;
725 int32_t byte_count = 0;
726 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000727
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000728 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000729
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000730 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000731 return true;
732 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000733 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
734 if ((now - nack_byte_count_times_[num]) > avg_interval) {
735 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000736 break;
737 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000738 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000739 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000740 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000741 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000742 if (num == NACK_BYTECOUNT_SIZE) {
743 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000744 // during the last msg_interval.
745 time_interval = now - nack_byte_count_times_[num - 1];
746 if (time_interval < 0) {
747 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000748 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000749 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000750 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000751}
752
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000753void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
754 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000755 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000756
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000757 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000758 if (bytes > 0) {
759 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000760 // Add padding length.
761 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000762 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000763 if (nack_byte_count_times_[0] == 0) {
764 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000765 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000766 // Shift.
767 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
768 nack_byte_count_[i + 1] = nack_byte_count_[i];
769 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000770 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000771 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000772 nack_byte_count_[0] = bytes;
773 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000774 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000775 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000776}
777
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000778// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000779bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000780 int64_t capture_time_ms,
781 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000782 uint16_t length = IP_PACKET_SIZE;
783 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000784 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000785
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000786 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
787 0,
788 retransmission,
789 data_buffer,
790 &length,
791 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000792 // Packet cannot be found. Allow sending to continue.
793 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000794 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000795 if (!retransmission && capture_time_ms > 0) {
796 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
797 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000798 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
799 retransmission && (rtx_ & kRtxRetransmitted) > 0);
800}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000801
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000802bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
803 uint16_t length,
804 int64_t capture_time_ms,
805 bool send_over_rtx) {
806 uint8_t *buffer_to_send_ptr = buffer;
807
808 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000809 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000810 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000811 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000812 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000813 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000814
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000815 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000816 if (send_over_rtx) {
817 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000818 buffer_to_send_ptr = data_buffer_rtx;
819 }
820
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000821 int64_t now_ms = clock_->TimeInMilliseconds();
822 int64_t diff_ms = now_ms - capture_time_ms;
823 bool updated_transmission_time_offset =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000824 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
825 diff_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000826 bool updated_abs_send_time =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000827 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000828 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000829 // Update stored packet in case of receiving a re-transmission request.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000830 packet_history_.ReplaceRTPHeader(buffer_to_send_ptr,
831 rtp_header.sequenceNumber,
832 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000833 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000834
835 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
836 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, false, false);
837 return ret;
838}
839
840void RTPSender::UpdateRtpStats(const uint8_t* buffer,
841 uint32_t size,
842 const RTPHeader& header,
843 bool is_rtx,
844 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000845 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000846 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
847 uint32_t ssrc = SSRC();
848
849 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000850 if (is_rtx) {
851 counters = &rtx_rtp_stats_;
852 ssrc = ssrc_rtx_;
853 } else {
854 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000855 }
856
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000857 bitrate_sent_.Update(size);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000858 ++counters->packets;
859 if (IsFecPacket(buffer, header)) {
860 ++counters->fec_packets;
861 }
862
863 if (is_retransmit) {
864 ++counters->retransmitted_packets;
865 } else {
866 counters->bytes += size - (header.headerLength + header.paddingLength);
867 counters->header_bytes += header.headerLength;
868 counters->padding_bytes += header.paddingLength;
869 }
870
871 if (rtp_stats_callback_) {
872 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
873 }
874}
875
876bool RTPSender::IsFecPacket(const uint8_t* buffer,
877 const RTPHeader& header) const {
878 if (!video_) {
879 return false;
880 }
881 bool fec_enabled;
882 uint8_t pt_red;
883 uint8_t pt_fec;
884 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
885 return fec_enabled &&
886 header.payloadType == pt_red &&
887 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000888}
889
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000890int RTPSender::TimeToSendPadding(int bytes) {
891 if (!sending_media_) {
892 return 0;
893 }
894 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000895 int64_t capture_time_ms;
896 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000897 {
898 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000899 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_ :
900 payload_type_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000901 timestamp = timestamp_;
902 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000903 if (last_timestamp_time_ms_ > 0) {
904 timestamp +=
905 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
906 capture_time_ms +=
907 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
908 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000909 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000910 int bytes_sent = SendRedundantPayloads(payload_type, bytes);
911 bytes -= bytes_sent;
912 if (bytes > 0) {
913 int padding_sent = SendPadData(payload_type, timestamp, capture_time_ms,
914 bytes, kDontStore, true, true);
915 bytes_sent += padding_sent;
916 }
917 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000918}
919
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000920// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000921int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000922 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000923 int64_t capture_time_ms, StorageType storage,
924 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000925 ModuleRTPUtility::RTPHeaderParser rtp_parser(
926 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000927 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000928 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000929
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000930 int64_t now_ms = clock_->TimeInMilliseconds();
931
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000932 // |capture_time_ms| <= 0 is considered invalid.
933 // TODO(holmer): This should be changed all over Video Engine so that negative
934 // time is consider invalid, while 0 is considered a valid time.
935 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000936 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000937 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000938 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000939
940 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
941 rtp_header, now_ms);
942
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000943 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000944 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
945 max_payload_length_, capture_time_ms,
946 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000947 return -1;
948 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000949
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000950 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000951 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
952 rtp_header.sequenceNumber, capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000953 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000954 // We can't send the packet right now.
955 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000956 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000957 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000958 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000959 if (capture_time_ms > 0) {
960 UpdateDelayStatistics(capture_time_ms, now_ms);
961 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000962 uint32_t length = payload_length + rtp_header_length;
963 if (!SendPacketToNetwork(buffer, length))
964 return -1;
965 UpdateRtpStats(buffer, length, rtp_header, false, false);
966 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000967}
968
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000969void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
970 CriticalSectionScoped cs(statistics_crit_.get());
971 send_delays_[now_ms] = now_ms - capture_time_ms;
972 send_delays_.erase(send_delays_.begin(),
973 send_delays_.lower_bound(now_ms - kSendSideDelayWindowMs));
974}
975
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000976void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000977 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000978 bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000979 nack_bitrate_.Process();
980 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000981 return;
982 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000983 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000984}
985
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000986uint16_t RTPSender::RTPHeaderLength() const {
987 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000988 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000989 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000990 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000991 rtp_header_length += RtpHeaderExtensionTotalLength();
992 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000993}
994
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000995uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000996 CriticalSectionScoped cs(send_critsect_);
997 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000998}
999
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001000void RTPSender::ResetDataCounters() {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001001 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001002 rtp_stats_ = StreamDataCounters();
1003 rtx_rtp_stats_ = StreamDataCounters();
1004 if (rtp_stats_callback_) {
1005 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc_);
1006 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx_);
1007 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001008}
1009
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001010uint32_t RTPSender::Packets() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001011 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001012 return rtp_stats_.packets + rtx_rtp_stats_.packets;
niklase@google.com470e71d2011-07-07 08:21:25 +00001013}
1014
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001015// Number of sent RTP bytes.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001016uint32_t RTPSender::Bytes() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001017 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001018 return rtp_stats_.bytes + rtx_rtp_stats_.bytes;
niklase@google.com470e71d2011-07-07 08:21:25 +00001019}
1020
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001021int RTPSender::CreateRTPHeader(
1022 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
1023 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
1024 uint8_t num_csrcs) const {
1025 header[0] = 0x80; // version 2.
1026 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001027 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001028 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001029 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001030 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1031 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1032 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001033 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001034
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001035 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001036 if (num_csrcs > 0) {
1037 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001038 // error
1039 assert(false);
1040 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001041 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001042 uint8_t *ptr = &header[rtp_header_length];
1043 for (int i = 0; i < num_csrcs; ++i) {
1044 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001045 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001046 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001047 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001048
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001049 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001050 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001051 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001052
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001053 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1054 if (len > 0) {
1055 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001056 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001057 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001058 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001059}
1060
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001061int32_t RTPSender::BuildRTPheader(
1062 uint8_t *data_buffer, const int8_t payload_type,
1063 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001064 int64_t capture_time_ms, const bool time_stamp_provided,
1065 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001066 assert(payload_type >= 0);
1067 CriticalSectionScoped cs(send_critsect_);
1068
1069 if (time_stamp_provided) {
1070 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001071 } else {
1072 // Make a unique time stamp.
1073 // We can't inc by the actual time, since then we increase the risk of back
1074 // timing.
1075 timestamp_++;
1076 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001077 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001078 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001079 capture_time_ms_ = capture_time_ms;
1080 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001081 int csrcs_length = 0;
1082 if (include_csrcs_)
1083 csrcs_length = num_csrcs_;
1084 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1085 timestamp_, sequence_number, csrcs_, csrcs_length);
1086}
1087
1088uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001089 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001090 return 0;
1091 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001092 // RTP header extension, RFC 3550.
1093 // 0 1 2 3
1094 // 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
1095 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1096 // | defined by profile | length |
1097 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1098 // | header extension |
1099 // | .... |
1100 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001101 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001102 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001103
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001104 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001105 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001106 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001107
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001108 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001109 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001110
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001111 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001112 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001113 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001114 switch (type) {
1115 case kRtpExtensionTransmissionTimeOffset:
1116 block_length = BuildTransmissionTimeOffsetExtension(
1117 data_buffer + kHeaderLength + total_block_length);
1118 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001119 case kRtpExtensionAudioLevel:
1120 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
1121 // we don't have to care about it here, which is true until we wan't to
1122 // use it together with any of the other extensions we support.
1123 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001124 case kRtpExtensionAbsoluteSendTime:
1125 block_length = BuildAbsoluteSendTimeExtension(
1126 data_buffer + kHeaderLength + total_block_length);
1127 break;
1128 default:
1129 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001130 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001131 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001132 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001133 }
1134 if (total_block_length == 0) {
1135 // No extension added.
1136 return 0;
1137 }
1138 // Set header length (in number of Word32, header excluded).
1139 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001140 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001141 total_block_length / 4);
1142 // Total added length.
1143 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001144}
1145
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001146uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1147 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001148 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1149 //
1150 // The transmission time is signaled to the receiver in-band using the
1151 // general mechanism for RTP header extensions [RFC5285]. The payload
1152 // of this extension (the transmitted value) is a 24-bit signed integer.
1153 // When added to the RTP timestamp of the packet, it represents the
1154 // "effective" RTP transmission time of the packet, on the RTP
1155 // timescale.
1156 //
1157 // The form of the transmission offset extension block:
1158 //
1159 // 0 1 2 3
1160 // 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
1161 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1162 // | ID | len=2 | transmission offset |
1163 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001164
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001165 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001166 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001167 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1168 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001169 // Not registered.
1170 return 0;
1171 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001172 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001173 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001174 data_buffer[pos++] = (id << 4) + len;
1175 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1176 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001177 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001178 assert(pos == kTransmissionTimeOffsetLength);
1179 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001180}
1181
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001182uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
1183 uint8_t* data_buffer) const {
1184 // Absolute send time in RTP streams.
1185 //
1186 // The absolute send time is signaled to the receiver in-band using the
1187 // general mechanism for RTP header extensions [RFC5285]. The payload
1188 // of this extension (the transmitted value) is a 24-bit unsigned integer
1189 // containing the sender's current time in seconds as a fixed point number
1190 // with 18 bits fractional part.
1191 //
1192 // The form of the absolute send time extension block:
1193 //
1194 // 0 1 2 3
1195 // 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
1196 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1197 // | ID | len=2 | absolute send time |
1198 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1199
1200 // Get id defined by user.
1201 uint8_t id;
1202 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1203 &id) != 0) {
1204 // Not registered.
1205 return 0;
1206 }
1207 size_t pos = 0;
1208 const uint8_t len = 2;
1209 data_buffer[pos++] = (id << 4) + len;
1210 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1211 absolute_send_time_);
1212 pos += 3;
1213 assert(pos == kAbsoluteSendTimeLength);
1214 return kAbsoluteSendTimeLength;
1215}
1216
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001217bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001218 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001219 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001220 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001221
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001222 // Get length until start of header extension block.
1223 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001224 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001225 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001226 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001227 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001228 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001229 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001230 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001231 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001232 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001233 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001234 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001235 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001236 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001237 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001238 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001239 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001240 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1241 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001242 WEBRTC_TRACE(
1243 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001244 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001245 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001246 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001247 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001248 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001249 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1250 &id) != 0) {
1251 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001252 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001253 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001254 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001255 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001256 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001257 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001258 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001259 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001260 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001261 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001262 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001263 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001264 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001265 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001266}
1267
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001268bool RTPSender::UpdateAbsoluteSendTime(
1269 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001270 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001271 CriticalSectionScoped cs(send_critsect_);
1272
1273 // Get length until start of header extension block.
1274 int extension_block_pos =
1275 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1276 kRtpExtensionAbsoluteSendTime);
1277 if (extension_block_pos < 0) {
1278 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1279 "Failed to update absolute send time, not registered.");
1280 return false;
1281 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001282 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001283 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001284 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001285 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1286 "Failed to update absolute send time, invalid length.");
1287 return false;
1288 }
1289 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001290 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1291 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001292 WEBRTC_TRACE(
1293 kTraceStream, kTraceRtpRtcp, id_,
1294 "Failed to update absolute send time, hdr extension not found.");
1295 return false;
1296 }
1297 // Get id.
1298 uint8_t id = 0;
1299 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1300 &id) != 0) {
1301 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1302 "Failed to update absolute send time, no id.");
1303 return false;
1304 }
1305 // Verify first byte in block.
1306 const uint8_t first_block_byte = (id << 4) + 2;
1307 if (rtp_packet[block_pos] != first_block_byte) {
1308 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1309 "Failed to update absolute send time.");
1310 return false;
1311 }
1312 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1313 // fractional part).
1314 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1315 ((now_ms << 18) / 1000) & 0x00ffffff);
1316 return true;
1317}
1318
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001319void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001320 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001321 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001322 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001323
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001324 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001325 SetStartTimestamp(RTPtime, false);
1326 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001327 if (!ssrc_forced_) {
1328 // Generate a new SSRC.
1329 ssrc_db_.ReturnSSRC(ssrc_);
1330 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001331 }
1332 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001333 if (!sequence_number_forced_ && !ssrc_forced_) {
1334 // Generate a new sequence number.
1335 sequence_number_ =
1336 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001337 }
1338 }
1339}
1340
1341void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001342 CriticalSectionScoped cs(send_critsect_);
1343 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001344}
1345
1346bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001347 CriticalSectionScoped cs(send_critsect_);
1348 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001349}
1350
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001351uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001352 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001353 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001354}
1355
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001356void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001357 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001358 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001359 start_time_stamp_forced_ = force;
1360 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001361 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001362 if (!start_time_stamp_forced_) {
1363 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001364 }
1365 }
1366}
1367
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001368uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001369 CriticalSectionScoped cs(send_critsect_);
1370 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001371}
1372
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001373uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001374 // If configured via API, return 0.
1375 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001376
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001377 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001378 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001379 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001380 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1381 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001382}
1383
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001384void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001385 // This is configured via the API.
1386 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001387
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001388 if (ssrc_ == ssrc && ssrc_forced_) {
1389 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001390 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001391 ssrc_forced_ = true;
1392 ssrc_db_.ReturnSSRC(ssrc_);
1393 ssrc_db_.RegisterSSRC(ssrc);
1394 ssrc_ = ssrc;
1395 if (!sequence_number_forced_) {
1396 sequence_number_ =
1397 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001398 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001399}
1400
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001401uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001402 CriticalSectionScoped cs(send_critsect_);
1403 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001404}
1405
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001406void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001407 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001408}
1409
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001410void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1411 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001412 assert(arr_length <= kRtpCsrcSize);
1413 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001414
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001415 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001416 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001417 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001418 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001419}
1420
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001421int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001422 assert(arr_of_csrc);
1423 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001424 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1425 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001426 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001427 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001428}
1429
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001430void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001431 CriticalSectionScoped cs(send_critsect_);
1432 sequence_number_forced_ = true;
1433 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001434}
1435
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001436uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001437 CriticalSectionScoped cs(send_critsect_);
1438 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001439}
1440
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001441// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001442int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1443 const uint16_t time_ms,
1444 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001445 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001446 return -1;
1447 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001448 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001449}
1450
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001451bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001452 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001453 return false;
1454 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001455 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001456}
1457
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001458int32_t RTPSender::SetAudioPacketSize(
1459 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001460 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001461 return -1;
1462 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001463 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001464}
1465
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001466int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1467 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001468 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001469 return -1;
1470 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001471 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001472}
1473
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001474int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1475 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001476 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001477}
1478
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001479int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001480 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001481}
1482
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001483int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001484 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001485 return -1;
1486 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001487 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001488}
1489
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001490int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001491 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001492 return -1;
1493 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001494 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001495}
1496
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001497// Video
1498VideoCodecInformation *RTPSender::CodecInformationVideo() {
1499 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001500 return NULL;
1501 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001502 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001503}
1504
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001505RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001506 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001507 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001508}
1509
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001510uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001511 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001512 return 0;
1513 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001514 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001515}
1516
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001517int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001518 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001519 return -1;
1520 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001521 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001522}
1523
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001524int32_t RTPSender::SetGenericFECStatus(
1525 const bool enable, const uint8_t payload_type_red,
1526 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001527 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001528 return -1;
1529 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001530 return video_->SetGenericFECStatus(enable, payload_type_red,
1531 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001532}
1533
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001534int32_t RTPSender::GenericFECStatus(
1535 bool *enable, uint8_t *payload_type_red,
1536 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001537 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001538 return -1;
1539 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001540 return video_->GenericFECStatus(
1541 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001542}
1543
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001544int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001545 const FecProtectionParams *delta_params,
1546 const FecProtectionParams *key_params) {
1547 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001548 return -1;
1549 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001550 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001551}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001552
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001553void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1554 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001555 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001556 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001557 // Add RTX header.
1558 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001559 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001560
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001561 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001562 rtp_parser.Parse(rtp_header);
1563
1564 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001565 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001566
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001567 // Replace payload type, if a specific type is set for RTX.
1568 if (payload_type_rtx_ != -1) {
1569 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001570 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001571 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1572 }
1573
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001574 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001575 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001576 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1577
1578 // Replace SSRC.
1579 ptr += 6;
1580 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1581
1582 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001583 ptr = data_buffer_rtx + rtp_header.headerLength;
1584 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001585 ptr += 2;
1586
1587 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001588 memcpy(ptr, buffer + rtp_header.headerLength,
1589 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001590 *length += 2;
1591}
1592
sprang@webrtc.org71f055f2013-12-04 15:09:27 +00001593void RTPSender::RegisterFrameCountObserver(FrameCountObserver* observer) {
1594 CriticalSectionScoped cs(statistics_crit_.get());
1595 if (observer != NULL)
1596 assert(frame_count_observer_ == NULL);
1597 frame_count_observer_ = observer;
1598}
1599
1600FrameCountObserver* RTPSender::GetFrameCountObserver() const {
1601 CriticalSectionScoped cs(statistics_crit_.get());
1602 return frame_count_observer_;
1603}
1604
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001605void RTPSender::RegisterRtpStatisticsCallback(
1606 StreamDataCountersCallback* callback) {
1607 CriticalSectionScoped cs(statistics_crit_.get());
1608 if (callback != NULL)
1609 assert(rtp_stats_callback_ == NULL);
1610 rtp_stats_callback_ = callback;
1611}
1612
1613StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1614 CriticalSectionScoped cs(statistics_crit_.get());
1615 return rtp_stats_callback_;
1616}
1617
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001618void RTPSender::RegisterBitrateObserver(BitrateStatisticsObserver* observer) {
1619 CriticalSectionScoped cs(statistics_crit_.get());
1620 if (observer != NULL)
1621 assert(bitrate_callback_ == NULL);
1622 bitrate_callback_ = observer;
1623}
1624
1625BitrateStatisticsObserver* RTPSender::GetBitrateObserver() const {
1626 CriticalSectionScoped cs(statistics_crit_.get());
1627 return bitrate_callback_;
1628}
1629
1630uint32_t RTPSender::BitrateSent() const { return bitrate_sent_.BitrateLast(); }
1631
1632void RTPSender::BitrateUpdated(const BitrateStatistics& stats) {
1633 CriticalSectionScoped cs(statistics_crit_.get());
1634 if (bitrate_callback_) {
1635 bitrate_callback_->Notify(stats, ssrc_);
1636 }
1637}
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001638} // namespace webrtc