blob: fd320324b8a6cb84fa79e3aca01e6bd35c8a26b8 [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) {
845 CriticalSectionScoped lock(statistics_crit_.get());
846 StreamDataCounters* counters;
847 uint32_t ssrc;
848 if (is_rtx) {
849 counters = &rtx_rtp_stats_;
850 ssrc = ssrc_rtx_;
851 } else {
852 counters = &rtp_stats_;
853 ssrc = ssrc_;
854 }
855
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000856 bitrate_sent_.Update(size);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000857 ++counters->packets;
858 if (IsFecPacket(buffer, header)) {
859 ++counters->fec_packets;
860 }
861
862 if (is_retransmit) {
863 ++counters->retransmitted_packets;
864 } else {
865 counters->bytes += size - (header.headerLength + header.paddingLength);
866 counters->header_bytes += header.headerLength;
867 counters->padding_bytes += header.paddingLength;
868 }
869
870 if (rtp_stats_callback_) {
871 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
872 }
873}
874
875bool RTPSender::IsFecPacket(const uint8_t* buffer,
876 const RTPHeader& header) const {
877 if (!video_) {
878 return false;
879 }
880 bool fec_enabled;
881 uint8_t pt_red;
882 uint8_t pt_fec;
883 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
884 return fec_enabled &&
885 header.payloadType == pt_red &&
886 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000887}
888
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000889int RTPSender::TimeToSendPadding(int bytes) {
890 if (!sending_media_) {
891 return 0;
892 }
893 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000894 int64_t capture_time_ms;
895 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000896 {
897 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000898 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_ :
899 payload_type_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000900 timestamp = timestamp_;
901 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000902 if (last_timestamp_time_ms_ > 0) {
903 timestamp +=
904 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
905 capture_time_ms +=
906 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
907 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000908 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000909 int bytes_sent = SendRedundantPayloads(payload_type, bytes);
910 bytes -= bytes_sent;
911 if (bytes > 0) {
912 int padding_sent = SendPadData(payload_type, timestamp, capture_time_ms,
913 bytes, kDontStore, true, true);
914 bytes_sent += padding_sent;
915 }
916 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000917}
918
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000919// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000920int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000921 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000922 int64_t capture_time_ms, StorageType storage,
923 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000924 ModuleRTPUtility::RTPHeaderParser rtp_parser(
925 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000926 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000927 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000928
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000929 int64_t now_ms = clock_->TimeInMilliseconds();
930
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000931 // |capture_time_ms| <= 0 is considered invalid.
932 // TODO(holmer): This should be changed all over Video Engine so that negative
933 // time is consider invalid, while 0 is considered a valid time.
934 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000935 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000936 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000937 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000938
939 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
940 rtp_header, now_ms);
941
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000942 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000943 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
944 max_payload_length_, capture_time_ms,
945 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000946 return -1;
947 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000948
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000949 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000950 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
951 rtp_header.sequenceNumber, capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000952 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000953 // We can't send the packet right now.
954 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000955 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000956 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000957 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000958 if (capture_time_ms > 0) {
959 UpdateDelayStatistics(capture_time_ms, now_ms);
960 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000961 uint32_t length = payload_length + rtp_header_length;
962 if (!SendPacketToNetwork(buffer, length))
963 return -1;
964 UpdateRtpStats(buffer, length, rtp_header, false, false);
965 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000966}
967
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000968void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
969 CriticalSectionScoped cs(statistics_crit_.get());
970 send_delays_[now_ms] = now_ms - capture_time_ms;
971 send_delays_.erase(send_delays_.begin(),
972 send_delays_.lower_bound(now_ms - kSendSideDelayWindowMs));
973}
974
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000975void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000976 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000977 bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000978 nack_bitrate_.Process();
979 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000980 return;
981 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000982 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000983}
984
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000985uint16_t RTPSender::RTPHeaderLength() const {
986 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000987 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000988 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000989 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000990 rtp_header_length += RtpHeaderExtensionTotalLength();
991 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000992}
993
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000994uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000995 CriticalSectionScoped cs(send_critsect_);
996 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000997}
998
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000999void RTPSender::ResetDataCounters() {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001000 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001001 rtp_stats_ = StreamDataCounters();
1002 rtx_rtp_stats_ = StreamDataCounters();
1003 if (rtp_stats_callback_) {
1004 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc_);
1005 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx_);
1006 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001007}
1008
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001009uint32_t RTPSender::Packets() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001010 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001011 return rtp_stats_.packets + rtx_rtp_stats_.packets;
niklase@google.com470e71d2011-07-07 08:21:25 +00001012}
1013
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001014// Number of sent RTP bytes.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001015uint32_t RTPSender::Bytes() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001016 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001017 return rtp_stats_.bytes + rtx_rtp_stats_.bytes;
niklase@google.com470e71d2011-07-07 08:21:25 +00001018}
1019
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001020int RTPSender::CreateRTPHeader(
1021 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
1022 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
1023 uint8_t num_csrcs) const {
1024 header[0] = 0x80; // version 2.
1025 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001026 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001027 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001028 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001029 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1030 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1031 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001032 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001033
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001034 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001035 if (num_csrcs > 0) {
1036 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001037 // error
1038 assert(false);
1039 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001040 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001041 uint8_t *ptr = &header[rtp_header_length];
1042 for (int i = 0; i < num_csrcs; ++i) {
1043 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001044 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001045 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001046 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001047
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001048 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001049 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001050 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001051
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001052 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1053 if (len > 0) {
1054 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001055 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001056 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001057 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001058}
1059
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001060int32_t RTPSender::BuildRTPheader(
1061 uint8_t *data_buffer, const int8_t payload_type,
1062 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001063 int64_t capture_time_ms, const bool time_stamp_provided,
1064 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001065 assert(payload_type >= 0);
1066 CriticalSectionScoped cs(send_critsect_);
1067
1068 if (time_stamp_provided) {
1069 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001070 } else {
1071 // Make a unique time stamp.
1072 // We can't inc by the actual time, since then we increase the risk of back
1073 // timing.
1074 timestamp_++;
1075 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001076 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001077 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001078 capture_time_ms_ = capture_time_ms;
1079 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001080 int csrcs_length = 0;
1081 if (include_csrcs_)
1082 csrcs_length = num_csrcs_;
1083 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1084 timestamp_, sequence_number, csrcs_, csrcs_length);
1085}
1086
1087uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001088 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001089 return 0;
1090 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001091 // RTP header extension, RFC 3550.
1092 // 0 1 2 3
1093 // 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
1094 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1095 // | defined by profile | length |
1096 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1097 // | header extension |
1098 // | .... |
1099 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001100 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001101 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001102
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001103 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001104 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001105 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001106
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001107 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001108 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001109
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001110 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001111 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001112 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001113 switch (type) {
1114 case kRtpExtensionTransmissionTimeOffset:
1115 block_length = BuildTransmissionTimeOffsetExtension(
1116 data_buffer + kHeaderLength + total_block_length);
1117 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001118 case kRtpExtensionAudioLevel:
1119 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
1120 // we don't have to care about it here, which is true until we wan't to
1121 // use it together with any of the other extensions we support.
1122 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001123 case kRtpExtensionAbsoluteSendTime:
1124 block_length = BuildAbsoluteSendTimeExtension(
1125 data_buffer + kHeaderLength + total_block_length);
1126 break;
1127 default:
1128 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001129 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001130 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001131 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001132 }
1133 if (total_block_length == 0) {
1134 // No extension added.
1135 return 0;
1136 }
1137 // Set header length (in number of Word32, header excluded).
1138 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001139 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001140 total_block_length / 4);
1141 // Total added length.
1142 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001143}
1144
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001145uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1146 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001147 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1148 //
1149 // The transmission time is signaled to the receiver in-band using the
1150 // general mechanism for RTP header extensions [RFC5285]. The payload
1151 // of this extension (the transmitted value) is a 24-bit signed integer.
1152 // When added to the RTP timestamp of the packet, it represents the
1153 // "effective" RTP transmission time of the packet, on the RTP
1154 // timescale.
1155 //
1156 // The form of the transmission offset extension block:
1157 //
1158 // 0 1 2 3
1159 // 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
1160 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1161 // | ID | len=2 | transmission offset |
1162 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001163
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001164 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001165 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001166 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1167 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001168 // Not registered.
1169 return 0;
1170 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001171 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001172 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001173 data_buffer[pos++] = (id << 4) + len;
1174 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1175 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001176 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001177 assert(pos == kTransmissionTimeOffsetLength);
1178 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001179}
1180
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001181uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
1182 uint8_t* data_buffer) const {
1183 // Absolute send time in RTP streams.
1184 //
1185 // The absolute send time is signaled to the receiver in-band using the
1186 // general mechanism for RTP header extensions [RFC5285]. The payload
1187 // of this extension (the transmitted value) is a 24-bit unsigned integer
1188 // containing the sender's current time in seconds as a fixed point number
1189 // with 18 bits fractional part.
1190 //
1191 // The form of the absolute send time extension block:
1192 //
1193 // 0 1 2 3
1194 // 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
1195 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1196 // | ID | len=2 | absolute send time |
1197 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1198
1199 // Get id defined by user.
1200 uint8_t id;
1201 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1202 &id) != 0) {
1203 // Not registered.
1204 return 0;
1205 }
1206 size_t pos = 0;
1207 const uint8_t len = 2;
1208 data_buffer[pos++] = (id << 4) + len;
1209 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1210 absolute_send_time_);
1211 pos += 3;
1212 assert(pos == kAbsoluteSendTimeLength);
1213 return kAbsoluteSendTimeLength;
1214}
1215
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001216bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001217 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001218 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001219 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001220
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001221 // Get length until start of header extension block.
1222 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001223 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001224 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001225 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001226 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001227 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001228 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001229 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001230 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001231 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001232 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001233 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001234 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001235 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001236 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001237 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001238 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001239 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1240 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001241 WEBRTC_TRACE(
1242 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001243 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001244 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001245 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001246 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001247 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001248 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1249 &id) != 0) {
1250 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001251 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001252 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001253 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001254 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001255 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001256 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001257 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001258 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001259 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001260 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001261 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001262 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001263 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001264 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001265}
1266
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001267bool RTPSender::UpdateAbsoluteSendTime(
1268 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001269 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001270 CriticalSectionScoped cs(send_critsect_);
1271
1272 // Get length until start of header extension block.
1273 int extension_block_pos =
1274 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1275 kRtpExtensionAbsoluteSendTime);
1276 if (extension_block_pos < 0) {
1277 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1278 "Failed to update absolute send time, not registered.");
1279 return false;
1280 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001281 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001282 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001283 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001284 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1285 "Failed to update absolute send time, invalid length.");
1286 return false;
1287 }
1288 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001289 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1290 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001291 WEBRTC_TRACE(
1292 kTraceStream, kTraceRtpRtcp, id_,
1293 "Failed to update absolute send time, hdr extension not found.");
1294 return false;
1295 }
1296 // Get id.
1297 uint8_t id = 0;
1298 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1299 &id) != 0) {
1300 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1301 "Failed to update absolute send time, no id.");
1302 return false;
1303 }
1304 // Verify first byte in block.
1305 const uint8_t first_block_byte = (id << 4) + 2;
1306 if (rtp_packet[block_pos] != first_block_byte) {
1307 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1308 "Failed to update absolute send time.");
1309 return false;
1310 }
1311 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1312 // fractional part).
1313 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1314 ((now_ms << 18) / 1000) & 0x00ffffff);
1315 return true;
1316}
1317
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001318void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001319 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001320 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001321 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001322
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001323 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001324 SetStartTimestamp(RTPtime, false);
1325 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001326 if (!ssrc_forced_) {
1327 // Generate a new SSRC.
1328 ssrc_db_.ReturnSSRC(ssrc_);
1329 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001330 }
1331 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001332 if (!sequence_number_forced_ && !ssrc_forced_) {
1333 // Generate a new sequence number.
1334 sequence_number_ =
1335 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001336 }
1337 }
1338}
1339
1340void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001341 CriticalSectionScoped cs(send_critsect_);
1342 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001343}
1344
1345bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001346 CriticalSectionScoped cs(send_critsect_);
1347 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001348}
1349
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001350uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001351 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001352 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001353}
1354
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001355void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001356 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001357 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001358 start_time_stamp_forced_ = force;
1359 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001360 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001361 if (!start_time_stamp_forced_) {
1362 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001363 }
1364 }
1365}
1366
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001367uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001368 CriticalSectionScoped cs(send_critsect_);
1369 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001370}
1371
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001372uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001373 // If configured via API, return 0.
1374 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001375
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001376 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001377 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001378 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001379 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1380 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001381}
1382
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001383void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001384 // This is configured via the API.
1385 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001386
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001387 if (ssrc_ == ssrc && ssrc_forced_) {
1388 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001389 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001390 ssrc_forced_ = true;
1391 ssrc_db_.ReturnSSRC(ssrc_);
1392 ssrc_db_.RegisterSSRC(ssrc);
1393 ssrc_ = ssrc;
1394 if (!sequence_number_forced_) {
1395 sequence_number_ =
1396 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001397 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001398}
1399
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001400uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001401 CriticalSectionScoped cs(send_critsect_);
1402 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001403}
1404
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001405void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001406 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001407}
1408
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001409void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1410 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001411 assert(arr_length <= kRtpCsrcSize);
1412 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001413
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001414 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001415 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001416 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001417 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001418}
1419
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001420int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001421 assert(arr_of_csrc);
1422 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001423 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1424 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001425 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001426 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001427}
1428
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001429void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001430 CriticalSectionScoped cs(send_critsect_);
1431 sequence_number_forced_ = true;
1432 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001433}
1434
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001435uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001436 CriticalSectionScoped cs(send_critsect_);
1437 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001438}
1439
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001440// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001441int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1442 const uint16_t time_ms,
1443 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001444 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001445 return -1;
1446 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001447 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001448}
1449
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001450bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001451 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001452 return false;
1453 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001454 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001455}
1456
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001457int32_t RTPSender::SetAudioPacketSize(
1458 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001459 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001460 return -1;
1461 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001462 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001463}
1464
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001465int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1466 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001467 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001468 return -1;
1469 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001470 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001471}
1472
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001473int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1474 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001475 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001476}
1477
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001478int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001479 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001480}
1481
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001482int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001483 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001484 return -1;
1485 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001486 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001487}
1488
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001489int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001490 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001491 return -1;
1492 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001493 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001494}
1495
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001496// Video
1497VideoCodecInformation *RTPSender::CodecInformationVideo() {
1498 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001499 return NULL;
1500 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001501 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001502}
1503
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001504RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001505 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001506 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001507}
1508
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001509uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001510 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001511 return 0;
1512 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001513 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001514}
1515
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001516int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001517 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001518 return -1;
1519 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001520 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001521}
1522
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001523int32_t RTPSender::SetGenericFECStatus(
1524 const bool enable, const uint8_t payload_type_red,
1525 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001526 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001527 return -1;
1528 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001529 return video_->SetGenericFECStatus(enable, payload_type_red,
1530 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001531}
1532
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001533int32_t RTPSender::GenericFECStatus(
1534 bool *enable, uint8_t *payload_type_red,
1535 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001536 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001537 return -1;
1538 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001539 return video_->GenericFECStatus(
1540 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001541}
1542
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001543int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001544 const FecProtectionParams *delta_params,
1545 const FecProtectionParams *key_params) {
1546 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001547 return -1;
1548 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001549 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001550}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001551
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001552void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1553 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001554 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001555 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001556 // Add RTX header.
1557 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001558 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001559
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001560 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001561 rtp_parser.Parse(rtp_header);
1562
1563 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001564 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001565
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001566 // Replace payload type, if a specific type is set for RTX.
1567 if (payload_type_rtx_ != -1) {
1568 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001569 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001570 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1571 }
1572
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001573 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001574 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001575 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1576
1577 // Replace SSRC.
1578 ptr += 6;
1579 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1580
1581 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001582 ptr = data_buffer_rtx + rtp_header.headerLength;
1583 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001584 ptr += 2;
1585
1586 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001587 memcpy(ptr, buffer + rtp_header.headerLength,
1588 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001589 *length += 2;
1590}
1591
sprang@webrtc.org71f055f2013-12-04 15:09:27 +00001592void RTPSender::RegisterFrameCountObserver(FrameCountObserver* observer) {
1593 CriticalSectionScoped cs(statistics_crit_.get());
1594 if (observer != NULL)
1595 assert(frame_count_observer_ == NULL);
1596 frame_count_observer_ = observer;
1597}
1598
1599FrameCountObserver* RTPSender::GetFrameCountObserver() const {
1600 CriticalSectionScoped cs(statistics_crit_.get());
1601 return frame_count_observer_;
1602}
1603
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001604void RTPSender::RegisterRtpStatisticsCallback(
1605 StreamDataCountersCallback* callback) {
1606 CriticalSectionScoped cs(statistics_crit_.get());
1607 if (callback != NULL)
1608 assert(rtp_stats_callback_ == NULL);
1609 rtp_stats_callback_ = callback;
1610}
1611
1612StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1613 CriticalSectionScoped cs(statistics_crit_.get());
1614 return rtp_stats_callback_;
1615}
1616
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001617void RTPSender::RegisterBitrateObserver(BitrateStatisticsObserver* observer) {
1618 CriticalSectionScoped cs(statistics_crit_.get());
1619 if (observer != NULL)
1620 assert(bitrate_callback_ == NULL);
1621 bitrate_callback_ = observer;
1622}
1623
1624BitrateStatisticsObserver* RTPSender::GetBitrateObserver() const {
1625 CriticalSectionScoped cs(statistics_crit_.get());
1626 return bitrate_callback_;
1627}
1628
1629uint32_t RTPSender::BitrateSent() const { return bitrate_sent_.BitrateLast(); }
1630
1631void RTPSender::BitrateUpdated(const BitrateStatistics& stats) {
1632 CriticalSectionScoped cs(statistics_crit_.get());
1633 if (bitrate_callback_) {
1634 bitrate_callback_->Notify(stats, ssrc_);
1635 }
1636}
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001637} // namespace webrtc