blob: ba299197140343fce79954618651ae0b7409355e [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 {
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000160 if (!SendingMedia())
161 return false;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000162 CriticalSectionScoped cs(statistics_crit_.get());
163 SendDelayMap::const_iterator it = send_delays_.upper_bound(
164 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000165 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000166 return false;
167 int num_delays = 0;
168 for (; it != send_delays_.end(); ++it) {
169 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
170 *avg_send_delay_ms += it->second;
171 ++num_delays;
172 }
173 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
174 return true;
175}
176
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000177int32_t RTPSender::SetTransmissionTimeOffset(
178 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000179 if (transmission_time_offset > (0x800000 - 1) ||
180 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000181 return -1;
182 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000183 CriticalSectionScoped cs(send_critsect_);
184 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000185 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000186}
187
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000188int32_t RTPSender::SetAbsoluteSendTime(
189 const uint32_t absolute_send_time) {
190 if (absolute_send_time > 0xffffff) { // UWord24.
191 return -1;
192 }
193 CriticalSectionScoped cs(send_critsect_);
194 absolute_send_time_ = absolute_send_time;
195 return 0;
196}
197
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000198int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
199 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000200 CriticalSectionScoped cs(send_critsect_);
201 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000202}
203
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000204int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000205 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000206 CriticalSectionScoped cs(send_critsect_);
207 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000208}
209
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000210uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000211 CriticalSectionScoped cs(send_critsect_);
212 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000213}
214
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000215int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000216 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000217 const int8_t payload_number, const uint32_t frequency,
218 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000219 assert(payload_name);
220 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000221
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000222 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000223 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000224
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000225 if (payload_type_map_.end() != it) {
226 // We already use this payload type.
227 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000228 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000229
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000230 // Check if it's the same as we already have.
231 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000232 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000233 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000234 payload->typeSpecific.Audio.frequency == frequency &&
235 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000236 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000237 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000238 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000239 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000240 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000241 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000242 return 0;
243 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000244 }
245 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000246 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000247 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000248 ModuleRTPUtility::Payload *payload = NULL;
249 if (audio_configured_) {
250 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
251 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000252 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000253 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
254 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000255 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000256 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000257 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000258 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000259 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000260}
261
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000262int32_t RTPSender::DeRegisterSendPayload(
263 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000264 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000265
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000266 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000267 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000268
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000269 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000270 return -1;
271 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000272 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000273 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000274 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000275 return 0;
276}
niklase@google.com470e71d2011-07-07 08:21:25 +0000277
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000278int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000279
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000280int RTPSender::SendPayloadFrequency() const {
281 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
282}
niklase@google.com470e71d2011-07-07 08:21:25 +0000283
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000284int32_t RTPSender::SetMaxPayloadLength(
285 const uint16_t max_payload_length,
286 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000287 // Sanity check.
288 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
289 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
290 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000291 return -1;
292 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000293 CriticalSectionScoped cs(send_critsect_);
294 max_payload_length_ = max_payload_length;
295 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000296
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000297 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
298 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000299 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000300}
301
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000302uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000303 if (audio_configured_) {
304 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000305 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000306 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
307 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
308 - ((rtx_) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000309 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000310}
311
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000312uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000313 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000314}
315
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000316uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000317
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000318void RTPSender::SetRTXStatus(int mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000319 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000320 rtx_ = mode;
321 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000322 if (set_ssrc) {
323 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000324 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000325 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000326 }
327 }
328}
329
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000330void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000331 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000332 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000333 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000334 *ssrc = ssrc_rtx_;
335 *payload_type = payload_type_rtx_;
336}
337
338
339void RTPSender::SetRtxPayloadType(int payload_type) {
340 CriticalSectionScoped cs(send_critsect_);
341 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000342}
343
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000344int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
345 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000346 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000347
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000348 if (payload_type < 0) {
349 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
350 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000351 return -1;
352 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000353 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000354 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000355 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000356 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000357 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000358 // And it's a match...
359 return 0;
360 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000361 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000362 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000363 if (payload_type_ == payload_type) {
364 if (!audio_configured_) {
365 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000366 }
367 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000368 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000369 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000370 payload_type_map_.find(payload_type);
371 if (it == payload_type_map_.end()) {
372 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
373 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000374 return -1;
375 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000376 payload_type_ = payload_type;
377 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000378 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000379 if (!payload->audio && !audio_configured_) {
380 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
381 *video_type = payload->typeSpecific.Video.videoCodecType;
382 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000383 }
384 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000385}
386
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000387int32_t RTPSender::SendOutgoingData(
388 const FrameType frame_type, const int8_t payload_type,
389 const uint32_t capture_timestamp, int64_t capture_time_ms,
390 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000391 const RTPFragmentationHeader *fragmentation,
392 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000393 {
394 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000395 CriticalSectionScoped cs(send_critsect_);
396 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000397 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000398 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000399 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000400 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000401 if (CheckPayloadType(payload_type, &video_type) != 0) {
402 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
403 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000404 __FUNCTION__, payload_type);
405 return -1;
406 }
407
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000408 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000409 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000410 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
411 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000412 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000413 frame_type == kFrameEmpty);
414
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000415 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
416 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000417 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000418 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
419 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000420 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000421
422 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000423 if (paced_sender_->Enabled()) {
424 // Padding is driven by the pacer and not by the encoder.
425 return 0;
426 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000427 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000428 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000429 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000430 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
431 capture_timestamp, capture_time_ms,
432 payload_data, payload_size,
433 fragmentation, codec_info,
434 rtp_type_hdr);
435
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000436 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000437
438 CriticalSectionScoped cs(statistics_crit_.get());
439 uint32_t frame_count = ++frame_counts_[frame_type];
440 if (frame_count_observer_) {
441 frame_count_observer_->FrameCountUpdated(frame_type,
442 frame_count,
443 ssrc_);
444 }
445
446 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000449int RTPSender::SendRedundantPayloads(int payload_type, int bytes_to_send) {
450 if (!(rtx_ & kRtxRedundantPayloads))
451 return 0;
452 uint8_t buffer[IP_PACKET_SIZE];
453 int bytes_left = bytes_to_send;
454 while (bytes_left > 0) {
455 uint16_t length = bytes_left;
456 int64_t capture_time_ms;
457 if (!packet_history_.GetBestFittingPacket(buffer, &length,
458 &capture_time_ms)) {
459 break;
460 }
461 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true))
462 return -1;
463 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
464 RTPHeader rtp_header;
465 rtp_parser.Parse(rtp_header);
466 bytes_left -= length - rtp_header.headerLength;
467 }
468 return bytes_to_send - bytes_left;
469}
470
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000471bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000472 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000473 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000474 // Current bitrate since last estimate(1 second) averaged with the
475 // estimate since then, to get the most up to date bitrate.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000476 uint32_t current_bitrate = bitrate_sent_.BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000477 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000478 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000479 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000480 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000481 int bytes = 0;
482 if (current_bitrate == 0) {
483 // Start up phase. Send one 33.3 ms batch to start with.
484 bytes = (bitrate_diff / 8) / 30;
485 } else {
486 bytes = (bitrate_diff / 8);
487 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000488 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000489 if (bytes > bytes_cap) {
490 bytes = bytes_cap;
491 }
492 }
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000493 uint32_t timestamp;
494 {
495 CriticalSectionScoped cs(send_critsect_);
496 // Add the random RTP timestamp offset and store the capture time for
497 // later calculation of the send time offset.
498 timestamp = start_time_stamp_ + capture_timestamp;
499 timestamp_ = timestamp;
500 capture_time_ms_ = capture_time_ms;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000501 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000502 }
503 int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
504 bytes, kDontRetransmit, false, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000505 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
506 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000507}
508
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000509int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
510 int32_t bytes) {
511 int padding_bytes_in_packet = kMaxPaddingLength;
512 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000513 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000514 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000515 packet[0] |= 0x20; // Set padding bit.
516 int32_t *data =
517 reinterpret_cast<int32_t *>(&(packet[header_length]));
518
519 // Fill data buffer with random data.
520 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
521 data[j] = rand(); // NOLINT
522 }
523 // Set number of padding bytes in the last byte of the packet.
524 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
525 return padding_bytes_in_packet;
526}
527
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000528int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
529 int64_t capture_time_ms, int32_t bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000530 StorageType store, bool force_full_size_packets,
531 bool only_pad_after_markerbit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000532 // Drop this packet if we're not sending media packets.
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000533 if (!SendingMedia()) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000534 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000535 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000536 int padding_bytes_in_packet = 0;
537 int bytes_sent = 0;
538 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000539 // Always send full padding packets.
540 if (force_full_size_packets && bytes < kMaxPaddingLength)
541 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000542 if (bytes < kMaxPaddingLength) {
543 if (force_full_size_packets) {
544 bytes = kMaxPaddingLength;
545 } else {
546 // Round to the nearest multiple of 32.
547 bytes = (bytes + 16) & 0xffe0;
548 }
549 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000550 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000551 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000552 break;
553 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000554 uint32_t ssrc;
555 uint16_t sequence_number;
556 {
557 CriticalSectionScoped cs(send_critsect_);
558 // Only send padding packets following the last packet of a frame,
559 // indicated by the marker bit.
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000560 if (only_pad_after_markerbit && !last_packet_marker_bit_)
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000561 return bytes_sent;
562 if (rtx_ == kRtxOff) {
563 ssrc = ssrc_;
564 sequence_number = sequence_number_;
565 ++sequence_number_;
566 } else {
567 ssrc = ssrc_rtx_;
568 sequence_number = sequence_number_rtx_;
569 ++sequence_number_rtx_;
570 }
571 }
572 uint8_t padding_packet[IP_PACKET_SIZE];
573 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
574 false, timestamp, sequence_number, NULL,
575 0);
576 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
577 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000578 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
579 header_length, capture_time_ms, store,
580 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000581 // Error sending the packet.
582 break;
583 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000584 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000585 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000586 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000587}
588
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000589void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000590 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000591 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000592}
593
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000594bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000595 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000596}
niklase@google.com470e71d2011-07-07 08:21:25 +0000597
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000598int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
599 uint16_t length = IP_PACKET_SIZE;
600 uint8_t data_buffer[IP_PACKET_SIZE];
601 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000602 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000603 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
604 data_buffer, &length,
605 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000606 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000607 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000608 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000609
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000610 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
611 RTPHeader header;
stefan@webrtc.org79b63202013-12-04 13:34:28 +0000612 if (!rtp_parser.Parse(header)) {
613 assert(false);
614 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
615 "Failed to parse RTP header of packet to be retransmitted.");
616 return -1;
617 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000618 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000619 "timestamp", header.timestamp,
620 "seqnum", header.sequenceNumber);
621
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000622 if (paced_sender_) {
623 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000624 header.ssrc,
625 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000626 capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000627 length - header.headerLength,
628 true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000629 // We can't send the packet right now.
630 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000631 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000632 }
633 }
634
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000635 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000636 if ((rtx_ & kRtxRetransmitted) > 0) {
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000637 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
638 buffer_to_send_ptr = data_buffer_rtx;
639 }
640
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000641 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000642 UpdateRtpStats(buffer_to_send_ptr, length, header, rtx_ != kRtxOff, true);
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000643 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000644 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000645 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000646}
647
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000648bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
649 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000650 if (transport_) {
651 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000652 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000653 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
654 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000655 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000656 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000657 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
658 "Transport failed to send packet");
659 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000660 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000661 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000662}
663
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000664int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000665 if (!video_)
666 return -1;
667 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000668}
669
670int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000671 if (!video_)
672 return -1;
673 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000674}
675
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000676void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000677 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000678 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000679 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
680 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000681 const int64_t now = clock_->TimeInMilliseconds();
682 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000683
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000684 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000685 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000686 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000687 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000688 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000689 return;
690 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000691
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000692 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
693 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000694 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000695 if (bytes_sent > 0) {
696 bytes_re_sent += bytes_sent;
697 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000698 // The packet has previously been resent.
699 // Try resending next packet in the list.
700 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000701 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000702 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000703 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000704 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000705 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000706 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000707 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000708 // Delay bandwidth estimate (RTT * BW).
709 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000710 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000711 uint32_t target_bytes =
712 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000713 if (bytes_re_sent > target_bytes) {
714 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000715 }
716 }
717 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000718 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000719 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000720 UpdateNACKBitRate(bytes_re_sent, now);
721 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000722 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000723}
724
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000725bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
726 uint32_t num = 0;
727 int32_t byte_count = 0;
728 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000729
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000730 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000731
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000732 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000733 return true;
734 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000735 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
736 if ((now - nack_byte_count_times_[num]) > avg_interval) {
737 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000738 break;
739 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000740 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000741 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000742 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000743 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000744 if (num == NACK_BYTECOUNT_SIZE) {
745 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000746 // during the last msg_interval.
747 time_interval = now - nack_byte_count_times_[num - 1];
748 if (time_interval < 0) {
749 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000750 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000751 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000752 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000753}
754
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000755void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
756 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000757 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000758
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000759 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000760 if (bytes > 0) {
761 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000762 // Add padding length.
763 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000764 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000765 if (nack_byte_count_times_[0] == 0) {
766 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000767 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000768 // Shift.
769 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
770 nack_byte_count_[i + 1] = nack_byte_count_[i];
771 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000772 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000773 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000774 nack_byte_count_[0] = bytes;
775 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000776 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000777 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000778}
779
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000780// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000781bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000782 int64_t capture_time_ms,
783 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000784 uint16_t length = IP_PACKET_SIZE;
785 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000786 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000787
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000788 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
789 0,
790 retransmission,
791 data_buffer,
792 &length,
793 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000794 // Packet cannot be found. Allow sending to continue.
795 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000796 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000797 if (!retransmission && capture_time_ms > 0) {
798 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
799 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000800 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
801 retransmission && (rtx_ & kRtxRetransmitted) > 0);
802}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000803
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000804bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
805 uint16_t length,
806 int64_t capture_time_ms,
807 bool send_over_rtx) {
808 uint8_t *buffer_to_send_ptr = buffer;
809
810 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000811 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000812 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000813 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000814 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000815 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000816
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000817 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000818 if (send_over_rtx) {
819 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000820 buffer_to_send_ptr = data_buffer_rtx;
821 }
822
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000823 int64_t now_ms = clock_->TimeInMilliseconds();
824 int64_t diff_ms = now_ms - capture_time_ms;
825 bool updated_transmission_time_offset =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000826 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
827 diff_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000828 bool updated_abs_send_time =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000829 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000830 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000831 // Update stored packet in case of receiving a re-transmission request.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000832 packet_history_.ReplaceRTPHeader(buffer_to_send_ptr,
833 rtp_header.sequenceNumber,
834 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000835 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000836
837 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
838 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, false, false);
839 return ret;
840}
841
842void RTPSender::UpdateRtpStats(const uint8_t* buffer,
843 uint32_t size,
844 const RTPHeader& header,
845 bool is_rtx,
846 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000847 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000848 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
849 uint32_t ssrc = SSRC();
850
851 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000852 if (is_rtx) {
853 counters = &rtx_rtp_stats_;
854 ssrc = ssrc_rtx_;
855 } else {
856 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000857 }
858
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000859 bitrate_sent_.Update(size);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000860 ++counters->packets;
861 if (IsFecPacket(buffer, header)) {
862 ++counters->fec_packets;
863 }
864
865 if (is_retransmit) {
866 ++counters->retransmitted_packets;
867 } else {
868 counters->bytes += size - (header.headerLength + header.paddingLength);
869 counters->header_bytes += header.headerLength;
870 counters->padding_bytes += header.paddingLength;
871 }
872
873 if (rtp_stats_callback_) {
874 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
875 }
876}
877
878bool RTPSender::IsFecPacket(const uint8_t* buffer,
879 const RTPHeader& header) const {
880 if (!video_) {
881 return false;
882 }
883 bool fec_enabled;
884 uint8_t pt_red;
885 uint8_t pt_fec;
886 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
887 return fec_enabled &&
888 header.payloadType == pt_red &&
889 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000890}
891
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000892int RTPSender::TimeToSendPadding(int bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000893 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_);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000898 if (!sending_media_) {
899 return 0;
900 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000901 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_ :
902 payload_type_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000903 timestamp = timestamp_;
904 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000905 if (last_timestamp_time_ms_ > 0) {
906 timestamp +=
907 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
908 capture_time_ms +=
909 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
910 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000911 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000912 int bytes_sent = SendRedundantPayloads(payload_type, bytes);
913 bytes -= bytes_sent;
914 if (bytes > 0) {
915 int padding_sent = SendPadData(payload_type, timestamp, capture_time_ms,
916 bytes, kDontStore, true, true);
917 bytes_sent += padding_sent;
918 }
919 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000920}
921
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000922// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000923int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000924 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000925 int64_t capture_time_ms, StorageType storage,
926 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000927 ModuleRTPUtility::RTPHeaderParser rtp_parser(
928 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000929 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000930 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000931
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000932 int64_t now_ms = clock_->TimeInMilliseconds();
933
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000934 // |capture_time_ms| <= 0 is considered invalid.
935 // TODO(holmer): This should be changed all over Video Engine so that negative
936 // time is consider invalid, while 0 is considered a valid time.
937 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000938 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000939 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000940 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000941
942 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
943 rtp_header, now_ms);
944
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000945 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000946 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
947 max_payload_length_, capture_time_ms,
948 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000949 return -1;
950 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000951
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000952 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000953 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
954 rtp_header.sequenceNumber, capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000955 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000956 // We can't send the packet right now.
957 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000958 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000959 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000960 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000961 if (capture_time_ms > 0) {
962 UpdateDelayStatistics(capture_time_ms, now_ms);
963 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000964 uint32_t length = payload_length + rtp_header_length;
965 if (!SendPacketToNetwork(buffer, length))
966 return -1;
967 UpdateRtpStats(buffer, length, rtp_header, false, false);
968 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000969}
970
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000971void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
972 CriticalSectionScoped cs(statistics_crit_.get());
973 send_delays_[now_ms] = now_ms - capture_time_ms;
974 send_delays_.erase(send_delays_.begin(),
975 send_delays_.lower_bound(now_ms - kSendSideDelayWindowMs));
976}
977
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000978void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000979 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000980 bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000981 nack_bitrate_.Process();
982 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000983 return;
984 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000985 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000986}
987
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000988uint16_t RTPSender::RTPHeaderLength() const {
989 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000990 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000991 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000992 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000993 rtp_header_length += RtpHeaderExtensionTotalLength();
994 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000995}
996
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000997uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000998 CriticalSectionScoped cs(send_critsect_);
999 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001000}
1001
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001002void RTPSender::ResetDataCounters() {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001003 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001004 rtp_stats_ = StreamDataCounters();
1005 rtx_rtp_stats_ = StreamDataCounters();
1006 if (rtp_stats_callback_) {
1007 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc_);
1008 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx_);
1009 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001010}
1011
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001012uint32_t RTPSender::Packets() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001013 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001014 return rtp_stats_.packets + rtx_rtp_stats_.packets;
niklase@google.com470e71d2011-07-07 08:21:25 +00001015}
1016
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001017// Number of sent RTP bytes.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001018uint32_t RTPSender::Bytes() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001019 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001020 return rtp_stats_.bytes + rtx_rtp_stats_.bytes;
niklase@google.com470e71d2011-07-07 08:21:25 +00001021}
1022
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001023int RTPSender::CreateRTPHeader(
1024 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
1025 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
1026 uint8_t num_csrcs) const {
1027 header[0] = 0x80; // version 2.
1028 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001029 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001030 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001031 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001032 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1033 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1034 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001035 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001036
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001037 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001038 if (num_csrcs > 0) {
1039 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001040 // error
1041 assert(false);
1042 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001043 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001044 uint8_t *ptr = &header[rtp_header_length];
1045 for (int i = 0; i < num_csrcs; ++i) {
1046 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001047 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001048 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001049 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001050
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001051 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001052 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001053 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001054
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001055 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1056 if (len > 0) {
1057 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001058 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001059 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001060 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001061}
1062
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001063int32_t RTPSender::BuildRTPheader(
1064 uint8_t *data_buffer, const int8_t payload_type,
1065 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001066 int64_t capture_time_ms, const bool time_stamp_provided,
1067 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001068 assert(payload_type >= 0);
1069 CriticalSectionScoped cs(send_critsect_);
1070
1071 if (time_stamp_provided) {
1072 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001073 } else {
1074 // Make a unique time stamp.
1075 // We can't inc by the actual time, since then we increase the risk of back
1076 // timing.
1077 timestamp_++;
1078 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001079 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001080 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001081 capture_time_ms_ = capture_time_ms;
1082 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001083 int csrcs_length = 0;
1084 if (include_csrcs_)
1085 csrcs_length = num_csrcs_;
1086 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1087 timestamp_, sequence_number, csrcs_, csrcs_length);
1088}
1089
1090uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001091 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001092 return 0;
1093 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001094 // RTP header extension, RFC 3550.
1095 // 0 1 2 3
1096 // 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
1097 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1098 // | defined by profile | length |
1099 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1100 // | header extension |
1101 // | .... |
1102 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001103 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001104 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001105
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001106 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001107 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001108 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001109
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001110 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001111 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001112
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001113 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001114 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001115 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001116 switch (type) {
1117 case kRtpExtensionTransmissionTimeOffset:
1118 block_length = BuildTransmissionTimeOffsetExtension(
1119 data_buffer + kHeaderLength + total_block_length);
1120 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001121 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001122 block_length = BuildAudioLevelExtension(
1123 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001124 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001125 case kRtpExtensionAbsoluteSendTime:
1126 block_length = BuildAbsoluteSendTimeExtension(
1127 data_buffer + kHeaderLength + total_block_length);
1128 break;
1129 default:
1130 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001131 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001132 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001133 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001134 }
1135 if (total_block_length == 0) {
1136 // No extension added.
1137 return 0;
1138 }
1139 // Set header length (in number of Word32, header excluded).
1140 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001141 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001142 total_block_length / 4);
1143 // Total added length.
1144 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001145}
1146
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001147uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1148 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001149 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1150 //
1151 // The transmission time is signaled to the receiver in-band using the
1152 // general mechanism for RTP header extensions [RFC5285]. The payload
1153 // of this extension (the transmitted value) is a 24-bit signed integer.
1154 // When added to the RTP timestamp of the packet, it represents the
1155 // "effective" RTP transmission time of the packet, on the RTP
1156 // timescale.
1157 //
1158 // The form of the transmission offset extension block:
1159 //
1160 // 0 1 2 3
1161 // 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
1162 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1163 // | ID | len=2 | transmission offset |
1164 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001165
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001166 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001167 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001168 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1169 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001170 // Not registered.
1171 return 0;
1172 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001173 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001174 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001175 data_buffer[pos++] = (id << 4) + len;
1176 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1177 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001178 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001179 assert(pos == kTransmissionTimeOffsetLength);
1180 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001181}
1182
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001183uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1184 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1185 //
1186 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1187 //
1188 // The form of the audio level extension block:
1189 //
1190 // 0 1 2 3
1191 // 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
1192 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1193 // | ID | len=0 |V| level | 0x00 | 0x00 |
1194 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1195 //
1196 // Note that we always include 2 pad bytes, which will result in legal and
1197 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1198 // are implemented. Right now the pad bytes would anyway be required at end
1199 // of the extension block, so it makes no difference.
1200
1201 // Get id defined by user.
1202 uint8_t id;
1203 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1204 // Not registered.
1205 return 0;
1206 }
1207 size_t pos = 0;
1208 const uint8_t len = 0;
1209 data_buffer[pos++] = (id << 4) + len;
1210 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1211 data_buffer[pos++] = 0; // Padding.
1212 data_buffer[pos++] = 0; // Padding.
1213 // kAudioLevelLength is including pad bytes.
1214 assert(pos == kAudioLevelLength);
1215 return kAudioLevelLength;
1216}
1217
1218uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001219 // Absolute send time in RTP streams.
1220 //
1221 // The absolute send time is signaled to the receiver in-band using the
1222 // general mechanism for RTP header extensions [RFC5285]. The payload
1223 // of this extension (the transmitted value) is a 24-bit unsigned integer
1224 // containing the sender's current time in seconds as a fixed point number
1225 // with 18 bits fractional part.
1226 //
1227 // The form of the absolute send time extension block:
1228 //
1229 // 0 1 2 3
1230 // 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
1231 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1232 // | ID | len=2 | absolute send time |
1233 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1234
1235 // Get id defined by user.
1236 uint8_t id;
1237 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1238 &id) != 0) {
1239 // Not registered.
1240 return 0;
1241 }
1242 size_t pos = 0;
1243 const uint8_t len = 2;
1244 data_buffer[pos++] = (id << 4) + len;
1245 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1246 absolute_send_time_);
1247 pos += 3;
1248 assert(pos == kAbsoluteSendTimeLength);
1249 return kAbsoluteSendTimeLength;
1250}
1251
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001252bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001253 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001254 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001255 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001256
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001257 // Get length until start of header extension block.
1258 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001259 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001260 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001261 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001262 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001263 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001264 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001265 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001266 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001267 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001268 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001269 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001270 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001271 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001272 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001273 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001274 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001275 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1276 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001277 WEBRTC_TRACE(
1278 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001279 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001280 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001281 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001282 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001283 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001284 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1285 &id) != 0) {
1286 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001287 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001288 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001289 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001290 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001291 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001292 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001293 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001294 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001295 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001296 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001297 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001298 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001299 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001300 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001301}
1302
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001303bool RTPSender::UpdateAudioLevel(uint8_t *rtp_packet,
1304 const uint16_t rtp_packet_length,
1305 const RTPHeader &rtp_header,
1306 const bool is_voiced,
1307 const uint8_t dBov) const {
1308 CriticalSectionScoped cs(send_critsect_);
1309
1310 // Get length until start of header extension block.
1311 int extension_block_pos =
1312 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1313 kRtpExtensionAudioLevel);
1314 if (extension_block_pos < 0) {
1315 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1316 "Failed to update audio level, not registered.");
1317 return false;
1318 }
1319 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1320 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1321 rtp_header.headerLength < block_pos + kAudioLevelLength) {
1322 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1323 "Failed to update audio level, invalid length.");
1324 return false;
1325 }
1326 // Verify that header contains extension.
1327 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1328 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
1329 WEBRTC_TRACE(
1330 kTraceStream, kTraceRtpRtcp, id_,
1331 "Failed to update audio level, hdr extension not found.");
1332 return false;
1333 }
1334 // Get id.
1335 uint8_t id = 0;
1336 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1337 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1338 "Failed to update audio level, no id.");
1339 return false;
1340 }
1341 // Verify first byte in block.
1342 const uint8_t first_block_byte = (id << 4) + 0;
1343 if (rtp_packet[block_pos] != first_block_byte) {
1344 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1345 "Failed to update audio level.");
1346 return false;
1347 }
1348 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1349 return true;
1350}
1351
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001352bool RTPSender::UpdateAbsoluteSendTime(
1353 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001354 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001355 CriticalSectionScoped cs(send_critsect_);
1356
1357 // Get length until start of header extension block.
1358 int extension_block_pos =
1359 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1360 kRtpExtensionAbsoluteSendTime);
1361 if (extension_block_pos < 0) {
1362 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1363 "Failed to update absolute send time, not registered.");
1364 return false;
1365 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001366 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001367 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001368 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001369 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1370 "Failed to update absolute send time, invalid length.");
1371 return false;
1372 }
1373 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001374 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1375 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001376 WEBRTC_TRACE(
1377 kTraceStream, kTraceRtpRtcp, id_,
1378 "Failed to update absolute send time, hdr extension not found.");
1379 return false;
1380 }
1381 // Get id.
1382 uint8_t id = 0;
1383 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1384 &id) != 0) {
1385 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1386 "Failed to update absolute send time, no id.");
1387 return false;
1388 }
1389 // Verify first byte in block.
1390 const uint8_t first_block_byte = (id << 4) + 2;
1391 if (rtp_packet[block_pos] != first_block_byte) {
1392 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1393 "Failed to update absolute send time.");
1394 return false;
1395 }
1396 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1397 // fractional part).
1398 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1399 ((now_ms << 18) / 1000) & 0x00ffffff);
1400 return true;
1401}
1402
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001403void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001404 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001405 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001406 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001407
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001408 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001409 SetStartTimestamp(RTPtime, false);
1410 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001411 if (!ssrc_forced_) {
1412 // Generate a new SSRC.
1413 ssrc_db_.ReturnSSRC(ssrc_);
1414 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001415 }
1416 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001417 if (!sequence_number_forced_ && !ssrc_forced_) {
1418 // Generate a new sequence number.
1419 sequence_number_ =
1420 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001421 }
1422 }
1423}
1424
1425void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001426 CriticalSectionScoped cs(send_critsect_);
1427 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001428}
1429
1430bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001431 CriticalSectionScoped cs(send_critsect_);
1432 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001433}
1434
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001435uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001436 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001437 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001438}
1439
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001440void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001441 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001442 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001443 start_time_stamp_forced_ = force;
1444 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001445 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001446 if (!start_time_stamp_forced_) {
1447 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001448 }
1449 }
1450}
1451
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001452uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001453 CriticalSectionScoped cs(send_critsect_);
1454 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001455}
1456
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001457uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001458 // If configured via API, return 0.
1459 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001460
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001461 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001462 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001463 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001464 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1465 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001466}
1467
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001468void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001469 // This is configured via the API.
1470 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001471
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001472 if (ssrc_ == ssrc && ssrc_forced_) {
1473 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001474 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001475 ssrc_forced_ = true;
1476 ssrc_db_.ReturnSSRC(ssrc_);
1477 ssrc_db_.RegisterSSRC(ssrc);
1478 ssrc_ = ssrc;
1479 if (!sequence_number_forced_) {
1480 sequence_number_ =
1481 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001482 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001483}
1484
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001485uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001486 CriticalSectionScoped cs(send_critsect_);
1487 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001488}
1489
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001490void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001491 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001492}
1493
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001494void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1495 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001496 assert(arr_length <= kRtpCsrcSize);
1497 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001498
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001499 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001500 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001501 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001502 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001503}
1504
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001505int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001506 assert(arr_of_csrc);
1507 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001508 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1509 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001510 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001511 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001512}
1513
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001514void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001515 CriticalSectionScoped cs(send_critsect_);
1516 sequence_number_forced_ = true;
1517 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001518}
1519
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001520uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001521 CriticalSectionScoped cs(send_critsect_);
1522 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001523}
1524
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001525// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001526int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1527 const uint16_t time_ms,
1528 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001529 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001530 return -1;
1531 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001532 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001533}
1534
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001535bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001536 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001537 return false;
1538 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001539 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001540}
1541
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001542int32_t RTPSender::SetAudioPacketSize(
1543 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001544 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001545 return -1;
1546 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001547 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001548}
1549
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001550int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001551 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001552}
1553
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001554int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001555 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001556 return -1;
1557 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001558 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001559}
1560
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001561int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001562 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001563 return -1;
1564 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001565 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001566}
1567
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001568// Video
1569VideoCodecInformation *RTPSender::CodecInformationVideo() {
1570 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001571 return NULL;
1572 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001573 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001574}
1575
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001576RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001577 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001578 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001579}
1580
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001581uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001582 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001583 return 0;
1584 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001585 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001586}
1587
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001588int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001589 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001590 return -1;
1591 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001592 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001593}
1594
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001595int32_t RTPSender::SetGenericFECStatus(
1596 const bool enable, const uint8_t payload_type_red,
1597 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001598 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001599 return -1;
1600 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001601 return video_->SetGenericFECStatus(enable, payload_type_red,
1602 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001603}
1604
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001605int32_t RTPSender::GenericFECStatus(
1606 bool *enable, uint8_t *payload_type_red,
1607 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001608 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001609 return -1;
1610 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001611 return video_->GenericFECStatus(
1612 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001613}
1614
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001615int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001616 const FecProtectionParams *delta_params,
1617 const FecProtectionParams *key_params) {
1618 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001619 return -1;
1620 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001621 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001622}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001623
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001624void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1625 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001626 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001627 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001628 // Add RTX header.
1629 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001630 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001631
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001632 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001633 rtp_parser.Parse(rtp_header);
1634
1635 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001636 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001637
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001638 // Replace payload type, if a specific type is set for RTX.
1639 if (payload_type_rtx_ != -1) {
1640 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001641 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001642 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1643 }
1644
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001645 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001646 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001647 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1648
1649 // Replace SSRC.
1650 ptr += 6;
1651 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1652
1653 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001654 ptr = data_buffer_rtx + rtp_header.headerLength;
1655 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001656 ptr += 2;
1657
1658 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001659 memcpy(ptr, buffer + rtp_header.headerLength,
1660 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001661 *length += 2;
1662}
1663
sprang@webrtc.org71f055f2013-12-04 15:09:27 +00001664void RTPSender::RegisterFrameCountObserver(FrameCountObserver* observer) {
1665 CriticalSectionScoped cs(statistics_crit_.get());
1666 if (observer != NULL)
1667 assert(frame_count_observer_ == NULL);
1668 frame_count_observer_ = observer;
1669}
1670
1671FrameCountObserver* RTPSender::GetFrameCountObserver() const {
1672 CriticalSectionScoped cs(statistics_crit_.get());
1673 return frame_count_observer_;
1674}
1675
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001676void RTPSender::RegisterRtpStatisticsCallback(
1677 StreamDataCountersCallback* callback) {
1678 CriticalSectionScoped cs(statistics_crit_.get());
1679 if (callback != NULL)
1680 assert(rtp_stats_callback_ == NULL);
1681 rtp_stats_callback_ = callback;
1682}
1683
1684StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1685 CriticalSectionScoped cs(statistics_crit_.get());
1686 return rtp_stats_callback_;
1687}
1688
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001689void RTPSender::RegisterBitrateObserver(BitrateStatisticsObserver* observer) {
1690 CriticalSectionScoped cs(statistics_crit_.get());
1691 if (observer != NULL)
1692 assert(bitrate_callback_ == NULL);
1693 bitrate_callback_ = observer;
1694}
1695
1696BitrateStatisticsObserver* RTPSender::GetBitrateObserver() const {
1697 CriticalSectionScoped cs(statistics_crit_.get());
1698 return bitrate_callback_;
1699}
1700
1701uint32_t RTPSender::BitrateSent() const { return bitrate_sent_.BitrateLast(); }
1702
1703void RTPSender::BitrateUpdated(const BitrateStatistics& stats) {
1704 CriticalSectionScoped cs(statistics_crit_.get());
1705 if (bitrate_callback_) {
1706 bitrate_callback_->Notify(stats, ssrc_);
1707 }
1708}
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001709} // namespace webrtc