blob: 29e4616e1790d4381be93bd4174417287e6227ed [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.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000059 packet_over_head_(28),
60 payload_type_(-1),
61 payload_type_map_(),
62 rtp_header_extension_map_(),
63 transmission_time_offset_(0),
64 absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000065 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000066 nack_byte_count_times_(),
67 nack_byte_count_(),
68 nack_bitrate_(clock, NULL),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000069 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000070 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +000071 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000072 frame_count_observer_(NULL),
73 rtp_stats_callback_(NULL),
74 bitrate_callback_(NULL),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +000075 // RTP variables
76 start_time_stamp_forced_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000077 start_time_stamp_(0),
78 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
79 remote_ssrc_(0),
80 sequence_number_forced_(false),
81 ssrc_forced_(false),
82 timestamp_(0),
83 capture_time_ms_(0),
84 last_timestamp_time_ms_(0),
85 last_packet_marker_bit_(false),
86 num_csrcs_(0),
87 csrcs_(),
88 include_csrcs_(true),
89 rtx_(kRtxOff),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +000090 payload_type_rtx_(-1),
91 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
92 target_bitrate_kbps_(0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000093 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
94 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +000095 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000096 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000097 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000098 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000099 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
100 // Random start, 16 bits. Can't be 0.
101 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
102 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000104 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000105 audio_ = new RTPSenderAudio(id, clock_, this);
106 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000107 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000108 video_ = new RTPSenderVideo(id, clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000109 }
110 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111}
112
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000113RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000114 if (remote_ssrc_ != 0) {
115 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000116 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000117 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000118
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000119 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000120 delete send_critsect_;
121 while (!payload_type_map_.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000122 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000123 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000124 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000125 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000126 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000127 delete audio_;
128 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000129
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000130 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000131}
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000133void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000134 SetTargetBitrateKbps(static_cast<uint16_t>(bits / 1000));
niklase@google.com470e71d2011-07-07 08:21:25 +0000135}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000136
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000137uint16_t RTPSender::ActualSendBitrateKbit() const {
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000138 return (uint16_t)(bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000139}
140
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000141uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000142 if (video_) {
143 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000144 }
145 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000146}
147
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000148uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000149 if (video_) {
150 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000151 }
152 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000153}
154
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000155uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000156 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000157}
158
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000159bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
160 int* max_send_delay_ms) const {
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000161 if (!SendingMedia())
162 return false;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000163 CriticalSectionScoped cs(statistics_crit_.get());
164 SendDelayMap::const_iterator it = send_delays_.upper_bound(
165 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000166 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000167 return false;
168 int num_delays = 0;
169 for (; it != send_delays_.end(); ++it) {
170 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
171 *avg_send_delay_ms += it->second;
172 ++num_delays;
173 }
174 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
175 return true;
176}
177
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000178int32_t RTPSender::SetTransmissionTimeOffset(
179 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000180 if (transmission_time_offset > (0x800000 - 1) ||
181 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000182 return -1;
183 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000184 CriticalSectionScoped cs(send_critsect_);
185 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000186 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000187}
188
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000189int32_t RTPSender::SetAbsoluteSendTime(
190 const uint32_t absolute_send_time) {
191 if (absolute_send_time > 0xffffff) { // UWord24.
192 return -1;
193 }
194 CriticalSectionScoped cs(send_critsect_);
195 absolute_send_time_ = absolute_send_time;
196 return 0;
197}
198
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000199int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
200 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000201 CriticalSectionScoped cs(send_critsect_);
202 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000203}
204
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000205int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000206 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000207 CriticalSectionScoped cs(send_critsect_);
208 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000209}
210
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000211uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000212 CriticalSectionScoped cs(send_critsect_);
213 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000214}
215
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000216int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000217 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000218 const int8_t payload_number, const uint32_t frequency,
219 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000220 assert(payload_name);
221 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000222
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000223 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000224 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000225
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000226 if (payload_type_map_.end() != it) {
227 // We already use this payload type.
228 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000229 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000230
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000231 // Check if it's the same as we already have.
232 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000233 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000234 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000235 payload->typeSpecific.Audio.frequency == frequency &&
236 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000237 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000238 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000239 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000240 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000241 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000242 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000243 return 0;
244 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000245 }
246 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000247 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000248 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000249 ModuleRTPUtility::Payload *payload = NULL;
250 if (audio_configured_) {
251 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
252 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000253 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000254 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
255 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000256 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000257 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000258 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000259 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000260 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000261}
262
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000263int32_t RTPSender::DeRegisterSendPayload(
264 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000265 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000266
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000267 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000268 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000269
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000270 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000271 return -1;
272 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000273 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000274 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000275 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000276 return 0;
277}
niklase@google.com470e71d2011-07-07 08:21:25 +0000278
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000279int8_t RTPSender::SendPayloadType() const {
280 CriticalSectionScoped cs(send_critsect_);
281 return payload_type_;
282}
niklase@google.com470e71d2011-07-07 08:21:25 +0000283
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000284int RTPSender::SendPayloadFrequency() const {
285 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
286}
niklase@google.com470e71d2011-07-07 08:21:25 +0000287
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000288int32_t RTPSender::SetMaxPayloadLength(
289 const uint16_t max_payload_length,
290 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000291 // Sanity check.
292 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
293 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
294 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000295 return -1;
296 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000297 CriticalSectionScoped cs(send_critsect_);
298 max_payload_length_ = max_payload_length;
299 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000301 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
302 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000303 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000304}
305
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000306uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000307 if (audio_configured_) {
308 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000309 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000310 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
311 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
312 - ((rtx_) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000313 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000314}
315
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000316uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000317 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000318}
319
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000320uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000321
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000322void RTPSender::SetRTXStatus(int mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000323 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000324 rtx_ = mode;
325 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000326 if (set_ssrc) {
327 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000328 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000329 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000330 }
331 }
332}
333
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000334void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000335 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000336 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000337 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000338 *ssrc = ssrc_rtx_;
339 *payload_type = payload_type_rtx_;
340}
341
342
343void RTPSender::SetRtxPayloadType(int payload_type) {
344 CriticalSectionScoped cs(send_critsect_);
345 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000346}
347
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000348int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
349 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000350 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000351
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000352 if (payload_type < 0) {
353 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
354 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000355 return -1;
356 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000357 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000358 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000359 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000360 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000361 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000362 // And it's a match...
363 return 0;
364 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000365 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000366 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000367 if (payload_type_ == payload_type) {
368 if (!audio_configured_) {
369 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000370 }
371 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000372 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000373 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000374 payload_type_map_.find(payload_type);
375 if (it == payload_type_map_.end()) {
376 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
377 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000378 return -1;
379 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000380 payload_type_ = payload_type;
381 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000382 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000383 if (!payload->audio && !audio_configured_) {
384 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
385 *video_type = payload->typeSpecific.Video.videoCodecType;
386 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000387 }
388 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000389}
390
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000391int32_t RTPSender::SendOutgoingData(
392 const FrameType frame_type, const int8_t payload_type,
393 const uint32_t capture_timestamp, int64_t capture_time_ms,
394 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000395 const RTPFragmentationHeader *fragmentation,
396 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000397 {
398 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000399 CriticalSectionScoped cs(send_critsect_);
400 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000401 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000402 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000403 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000404 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000405 if (CheckPayloadType(payload_type, &video_type) != 0) {
406 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
407 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000408 __FUNCTION__, payload_type);
409 return -1;
410 }
411
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000412 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000413 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000414 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
415 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000416 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000417 frame_type == kFrameEmpty);
418
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000419 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
420 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000421 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000422 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
423 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000424 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000425
426 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000427 if (paced_sender_->Enabled()) {
428 // Padding is driven by the pacer and not by the encoder.
429 return 0;
430 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000431 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000432 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000433 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000434 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
435 capture_timestamp, capture_time_ms,
436 payload_data, payload_size,
437 fragmentation, codec_info,
438 rtp_type_hdr);
439
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000440 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000441
442 CriticalSectionScoped cs(statistics_crit_.get());
443 uint32_t frame_count = ++frame_counts_[frame_type];
444 if (frame_count_observer_) {
445 frame_count_observer_->FrameCountUpdated(frame_type,
446 frame_count,
447 ssrc_);
448 }
449
450 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000451}
452
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000453int RTPSender::SendRedundantPayloads(int payload_type, int bytes_to_send) {
454 if (!(rtx_ & kRtxRedundantPayloads))
455 return 0;
456 uint8_t buffer[IP_PACKET_SIZE];
457 int bytes_left = bytes_to_send;
458 while (bytes_left > 0) {
459 uint16_t length = bytes_left;
460 int64_t capture_time_ms;
461 if (!packet_history_.GetBestFittingPacket(buffer, &length,
462 &capture_time_ms)) {
463 break;
464 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000465 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000466 return -1;
467 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
468 RTPHeader rtp_header;
469 rtp_parser.Parse(rtp_header);
470 bytes_left -= length - rtp_header.headerLength;
471 }
472 return bytes_to_send - bytes_left;
473}
474
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000475bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000476 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000477 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000478 // Current bitrate since last estimate(1 second) averaged with the
479 // estimate since then, to get the most up to date bitrate.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000480 uint32_t current_bitrate = bitrate_sent_.BitrateNow();
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000481 uint16_t target_bitrate_kbps = GetTargetBitrateKbps();
482 int bitrate_diff = target_bitrate_kbps * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000483 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000484 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000485 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000486 int bytes = 0;
487 if (current_bitrate == 0) {
488 // Start up phase. Send one 33.3 ms batch to start with.
489 bytes = (bitrate_diff / 8) / 30;
490 } else {
491 bytes = (bitrate_diff / 8);
492 // Cap at 200 ms of target send data.
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000493 int bytes_cap = target_bitrate_kbps * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000494 if (bytes > bytes_cap) {
495 bytes = bytes_cap;
496 }
497 }
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000498 uint32_t timestamp;
499 {
500 CriticalSectionScoped cs(send_critsect_);
501 // Add the random RTP timestamp offset and store the capture time for
502 // later calculation of the send time offset.
503 timestamp = start_time_stamp_ + capture_timestamp;
504 timestamp_ = timestamp;
505 capture_time_ms_ = capture_time_ms;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000506 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000507 }
508 int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
509 bytes, kDontRetransmit, false, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000510 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
511 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000512}
513
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000514int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
515 int32_t bytes) {
516 int padding_bytes_in_packet = kMaxPaddingLength;
517 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000518 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000519 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000520 packet[0] |= 0x20; // Set padding bit.
521 int32_t *data =
522 reinterpret_cast<int32_t *>(&(packet[header_length]));
523
524 // Fill data buffer with random data.
525 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
526 data[j] = rand(); // NOLINT
527 }
528 // Set number of padding bytes in the last byte of the packet.
529 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
530 return padding_bytes_in_packet;
531}
532
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000533int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
534 int64_t capture_time_ms, int32_t bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000535 StorageType store, bool force_full_size_packets,
536 bool only_pad_after_markerbit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000537 // Drop this packet if we're not sending media packets.
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000538 if (!SendingMedia()) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000539 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000540 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000541 int padding_bytes_in_packet = 0;
542 int bytes_sent = 0;
543 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000544 // Always send full padding packets.
545 if (force_full_size_packets && bytes < kMaxPaddingLength)
546 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000547 if (bytes < kMaxPaddingLength) {
548 if (force_full_size_packets) {
549 bytes = kMaxPaddingLength;
550 } else {
551 // Round to the nearest multiple of 32.
552 bytes = (bytes + 16) & 0xffe0;
553 }
554 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000555 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000556 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000557 break;
558 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000559 uint32_t ssrc;
560 uint16_t sequence_number;
561 {
562 CriticalSectionScoped cs(send_critsect_);
563 // Only send padding packets following the last packet of a frame,
564 // indicated by the marker bit.
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000565 if (only_pad_after_markerbit && !last_packet_marker_bit_)
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000566 return bytes_sent;
567 if (rtx_ == kRtxOff) {
568 ssrc = ssrc_;
569 sequence_number = sequence_number_;
570 ++sequence_number_;
571 } else {
572 ssrc = ssrc_rtx_;
573 sequence_number = sequence_number_rtx_;
574 ++sequence_number_rtx_;
575 }
576 }
577 uint8_t padding_packet[IP_PACKET_SIZE];
578 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
579 false, timestamp, sequence_number, NULL,
580 0);
581 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
582 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000583 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
584 header_length, capture_time_ms, store,
585 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000586 // Error sending the packet.
587 break;
588 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000589 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000590 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000591 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000592}
593
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000594void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000595 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000596 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000597}
598
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000599bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000600 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000601}
niklase@google.com470e71d2011-07-07 08:21:25 +0000602
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000603int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
604 uint16_t length = IP_PACKET_SIZE;
605 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000606 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000607 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
608 data_buffer, &length,
609 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000610 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000611 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000612 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000613
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000614 if (paced_sender_) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000615 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
616 RTPHeader header;
617 if (!rtp_parser.Parse(header)) {
618 assert(false);
619 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
620 "Failed to parse RTP header of packet to be retransmitted.");
621 return -1;
622 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000623 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.org7c6ff2d2014-03-19 18:14:52 +0000635 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org16395222014-03-19 19:34:07 +0000636 (rtx_ & kRtxRetransmitted) > 0, true) ?
637 length : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000638}
639
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000640bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
641 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000642 if (transport_) {
643 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000644 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000645 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
646 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000647 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000648 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000649 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
650 "Transport failed to send packet");
651 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000652 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000653 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000654}
655
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000656int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000657 if (!video_)
658 return -1;
659 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000660}
661
662int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000663 if (!video_)
664 return -1;
665 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000666}
667
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000668void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000669 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000670 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000671 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
672 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000673 const int64_t now = clock_->TimeInMilliseconds();
674 uint32_t bytes_re_sent = 0;
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000675 uint16_t target_bitrate_kbps = GetTargetBitrateKbps();
niklase@google.com470e71d2011-07-07 08:21:25 +0000676
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000677 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000678 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000679 WEBRTC_TRACE(kTraceStream,
680 kTraceRtpRtcp,
681 id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000682 "NACK bitrate reached. Skip sending NACK response. Target %d",
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000683 target_bitrate_kbps);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000684 return;
685 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000686
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000687 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
688 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000689 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000690 if (bytes_sent > 0) {
691 bytes_re_sent += bytes_sent;
692 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000693 // The packet has previously been resent.
694 // Try resending next packet in the list.
695 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000696 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000697 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000698 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000699 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000700 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000701 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000702 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000703 // Delay bandwidth estimate (RTT * BW).
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000704 if (target_bitrate_kbps != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000705 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000706 uint32_t target_bytes =
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000707 (static_cast<uint32_t>(target_bitrate_kbps) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000708 if (bytes_re_sent > target_bytes) {
709 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000710 }
711 }
712 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000713 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000714 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000715 UpdateNACKBitRate(bytes_re_sent, now);
716 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000717 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000718}
719
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000720bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
721 uint32_t num = 0;
722 int32_t byte_count = 0;
723 const uint32_t avg_interval = 1000;
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000724 uint16_t target_bitrate_kbps = GetTargetBitrateKbps();
niklase@google.com470e71d2011-07-07 08:21:25 +0000725
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000726 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000727
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000728 if (target_bitrate_kbps == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000729 return true;
730 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000731 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
732 if ((now - nack_byte_count_times_[num]) > avg_interval) {
733 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000734 break;
735 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000736 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000737 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000738 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000739 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000740 if (num == NACK_BYTECOUNT_SIZE) {
741 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000742 // during the last msg_interval.
743 time_interval = now - nack_byte_count_times_[num - 1];
744 if (time_interval < 0) {
745 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000746 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000747 }
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000748 return (byte_count * 8) < (target_bitrate_kbps * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000749}
750
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000751void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
752 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000753 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000754
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000755 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000756 if (bytes > 0) {
757 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000758 // Add padding length.
759 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000760 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000761 if (nack_byte_count_times_[0] == 0) {
762 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000763 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000764 // Shift.
765 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
766 nack_byte_count_[i + 1] = nack_byte_count_[i];
767 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000768 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000769 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000770 nack_byte_count_[0] = bytes;
771 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000772 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000773 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000774}
775
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000776// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000777bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000778 int64_t capture_time_ms,
779 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000780 uint16_t length = IP_PACKET_SIZE;
781 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000782 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000783
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000784 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
785 0,
786 retransmission,
787 data_buffer,
788 &length,
789 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000790 // Packet cannot be found. Allow sending to continue.
791 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000792 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000793 if (!retransmission && capture_time_ms > 0) {
794 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
795 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000796 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000797 retransmission && (rtx_ & kRtxRetransmitted) > 0,
798 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000799}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000800
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000801bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
802 uint16_t length,
803 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000804 bool send_over_rtx,
805 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000806 uint8_t *buffer_to_send_ptr = buffer;
807
808 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000809 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000810 rtp_parser.Parse(rtp_header);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000811 TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000812 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000813 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000814
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000815 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000816 if (send_over_rtx) {
817 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000818 buffer_to_send_ptr = data_buffer_rtx;
819 }
820
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000821 int64_t now_ms = clock_->TimeInMilliseconds();
822 int64_t diff_ms = now_ms - capture_time_ms;
823 bool updated_transmission_time_offset =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000824 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
825 diff_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000826 bool updated_abs_send_time =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000827 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000828 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000829 // Update stored packet in case of receiving a re-transmission request.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000830 packet_history_.ReplaceRTPHeader(buffer_to_send_ptr,
831 rtp_header.sequenceNumber,
832 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000833 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000834
835 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000836 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
837 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000838 return ret;
839}
840
841void RTPSender::UpdateRtpStats(const uint8_t* buffer,
842 uint32_t size,
843 const RTPHeader& header,
844 bool is_rtx,
845 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000846 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000847 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
848 uint32_t ssrc = SSRC();
849
850 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000851 if (is_rtx) {
852 counters = &rtx_rtp_stats_;
853 ssrc = ssrc_rtx_;
854 } else {
855 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000856 }
857
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000858 bitrate_sent_.Update(size);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000859 ++counters->packets;
860 if (IsFecPacket(buffer, header)) {
861 ++counters->fec_packets;
862 }
863
864 if (is_retransmit) {
865 ++counters->retransmitted_packets;
866 } else {
867 counters->bytes += size - (header.headerLength + header.paddingLength);
868 counters->header_bytes += header.headerLength;
869 counters->padding_bytes += header.paddingLength;
870 }
871
872 if (rtp_stats_callback_) {
873 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
874 }
875}
876
877bool RTPSender::IsFecPacket(const uint8_t* buffer,
878 const RTPHeader& header) const {
879 if (!video_) {
880 return false;
881 }
882 bool fec_enabled;
883 uint8_t pt_red;
884 uint8_t pt_fec;
885 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
886 return fec_enabled &&
887 header.payloadType == pt_red &&
888 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000889}
890
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000891int RTPSender::TimeToSendPadding(int bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000892 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000893 int64_t capture_time_ms;
894 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000895 {
896 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000897 if (!sending_media_) {
898 return 0;
899 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000900 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_ :
901 payload_type_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000902 timestamp = timestamp_;
903 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000904 if (last_timestamp_time_ms_ > 0) {
905 timestamp +=
906 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
907 capture_time_ms +=
908 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
909 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000910 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000911 int bytes_sent = SendRedundantPayloads(payload_type, bytes);
912 bytes -= bytes_sent;
913 if (bytes > 0) {
914 int padding_sent = SendPadData(payload_type, timestamp, capture_time_ms,
915 bytes, kDontStore, true, true);
916 bytes_sent += padding_sent;
917 }
918 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000919}
920
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000921// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000922int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000923 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000924 int64_t capture_time_ms, StorageType storage,
925 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000926 ModuleRTPUtility::RTPHeaderParser rtp_parser(
927 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000928 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000929 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000930
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000931 int64_t now_ms = clock_->TimeInMilliseconds();
932
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000933 // |capture_time_ms| <= 0 is considered invalid.
934 // TODO(holmer): This should be changed all over Video Engine so that negative
935 // time is consider invalid, while 0 is considered a valid time.
936 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000937 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000938 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000939 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000940
941 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
942 rtp_header, now_ms);
943
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000944 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000945 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
946 max_payload_length_, capture_time_ms,
947 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000948 return -1;
949 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000950
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000951 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000952 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
953 rtp_header.sequenceNumber, capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000954 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000955 // We can't send the packet right now.
956 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000957 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000958 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000959 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000960 if (capture_time_ms > 0) {
961 UpdateDelayStatistics(capture_time_ms, now_ms);
962 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000963 uint32_t length = payload_length + rtp_header_length;
964 if (!SendPacketToNetwork(buffer, length))
965 return -1;
966 UpdateRtpStats(buffer, length, rtp_header, false, false);
967 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000968}
969
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000970void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
971 CriticalSectionScoped cs(statistics_crit_.get());
972 send_delays_[now_ms] = now_ms - capture_time_ms;
973 send_delays_.erase(send_delays_.begin(),
974 send_delays_.lower_bound(now_ms - kSendSideDelayWindowMs));
975}
976
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000977void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000978 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000979 bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000980 nack_bitrate_.Process();
981 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000982 return;
983 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000984 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000985}
986
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000987uint16_t RTPSender::RTPHeaderLength() const {
988 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000989 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000990 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000991 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000992 rtp_header_length += RtpHeaderExtensionTotalLength();
993 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000994}
995
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000996uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000997 CriticalSectionScoped cs(send_critsect_);
998 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000999}
1000
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001001void RTPSender::ResetDataCounters() {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001002 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001003 rtp_stats_ = StreamDataCounters();
1004 rtx_rtp_stats_ = StreamDataCounters();
1005 if (rtp_stats_callback_) {
1006 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc_);
1007 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx_);
1008 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001009}
1010
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001011uint32_t RTPSender::Packets() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001012 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001013 return rtp_stats_.packets + rtx_rtp_stats_.packets;
niklase@google.com470e71d2011-07-07 08:21:25 +00001014}
1015
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001016// Number of sent RTP bytes.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001017uint32_t RTPSender::Bytes() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001018 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001019 return rtp_stats_.bytes + rtx_rtp_stats_.bytes;
niklase@google.com470e71d2011-07-07 08:21:25 +00001020}
1021
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001022int RTPSender::CreateRTPHeader(
1023 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
1024 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
1025 uint8_t num_csrcs) const {
1026 header[0] = 0x80; // version 2.
1027 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001028 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001029 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001030 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001031 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1032 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1033 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001034 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001035
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001036 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001037 if (num_csrcs > 0) {
1038 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001039 // error
1040 assert(false);
1041 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001042 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001043 uint8_t *ptr = &header[rtp_header_length];
1044 for (int i = 0; i < num_csrcs; ++i) {
1045 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001046 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001047 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001048 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001049
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001050 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001051 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001052 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001053
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001054 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1055 if (len > 0) {
1056 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001057 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001058 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001059 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001060}
1061
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001062int32_t RTPSender::BuildRTPheader(
1063 uint8_t *data_buffer, const int8_t payload_type,
1064 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001065 int64_t capture_time_ms, const bool time_stamp_provided,
1066 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001067 assert(payload_type >= 0);
1068 CriticalSectionScoped cs(send_critsect_);
1069
1070 if (time_stamp_provided) {
1071 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001072 } else {
1073 // Make a unique time stamp.
1074 // We can't inc by the actual time, since then we increase the risk of back
1075 // timing.
1076 timestamp_++;
1077 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001078 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001079 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001080 capture_time_ms_ = capture_time_ms;
1081 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001082 int csrcs_length = 0;
1083 if (include_csrcs_)
1084 csrcs_length = num_csrcs_;
1085 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1086 timestamp_, sequence_number, csrcs_, csrcs_length);
1087}
1088
1089uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001090 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001091 return 0;
1092 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001093 // RTP header extension, RFC 3550.
1094 // 0 1 2 3
1095 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1096 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1097 // | defined by profile | length |
1098 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1099 // | header extension |
1100 // | .... |
1101 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001102 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001103 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001104
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001105 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001106 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001107 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001108
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001109 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001110 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001111
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001112 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001113 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001114 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001115 switch (type) {
1116 case kRtpExtensionTransmissionTimeOffset:
1117 block_length = BuildTransmissionTimeOffsetExtension(
1118 data_buffer + kHeaderLength + total_block_length);
1119 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001120 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001121 block_length = BuildAudioLevelExtension(
1122 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001123 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001124 case kRtpExtensionAbsoluteSendTime:
1125 block_length = BuildAbsoluteSendTimeExtension(
1126 data_buffer + kHeaderLength + total_block_length);
1127 break;
1128 default:
1129 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001130 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001131 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001132 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001133 }
1134 if (total_block_length == 0) {
1135 // No extension added.
1136 return 0;
1137 }
1138 // Set header length (in number of Word32, header excluded).
1139 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001140 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001141 total_block_length / 4);
1142 // Total added length.
1143 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001144}
1145
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001146uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1147 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001148 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1149 //
1150 // The transmission time is signaled to the receiver in-band using the
1151 // general mechanism for RTP header extensions [RFC5285]. The payload
1152 // of this extension (the transmitted value) is a 24-bit signed integer.
1153 // When added to the RTP timestamp of the packet, it represents the
1154 // "effective" RTP transmission time of the packet, on the RTP
1155 // timescale.
1156 //
1157 // The form of the transmission offset extension block:
1158 //
1159 // 0 1 2 3
1160 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1161 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1162 // | ID | len=2 | transmission offset |
1163 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001164
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001165 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001166 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001167 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1168 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001169 // Not registered.
1170 return 0;
1171 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001172 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001173 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001174 data_buffer[pos++] = (id << 4) + len;
1175 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1176 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001177 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001178 assert(pos == kTransmissionTimeOffsetLength);
1179 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001180}
1181
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001182uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1183 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1184 //
1185 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1186 //
1187 // The form of the audio level extension block:
1188 //
1189 // 0 1 2 3
1190 // 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
1191 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1192 // | ID | len=0 |V| level | 0x00 | 0x00 |
1193 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1194 //
1195 // Note that we always include 2 pad bytes, which will result in legal and
1196 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1197 // are implemented. Right now the pad bytes would anyway be required at end
1198 // of the extension block, so it makes no difference.
1199
1200 // Get id defined by user.
1201 uint8_t id;
1202 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1203 // Not registered.
1204 return 0;
1205 }
1206 size_t pos = 0;
1207 const uint8_t len = 0;
1208 data_buffer[pos++] = (id << 4) + len;
1209 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1210 data_buffer[pos++] = 0; // Padding.
1211 data_buffer[pos++] = 0; // Padding.
1212 // kAudioLevelLength is including pad bytes.
1213 assert(pos == kAudioLevelLength);
1214 return kAudioLevelLength;
1215}
1216
1217uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001218 // Absolute send time in RTP streams.
1219 //
1220 // The absolute send time is signaled to the receiver in-band using the
1221 // general mechanism for RTP header extensions [RFC5285]. The payload
1222 // of this extension (the transmitted value) is a 24-bit unsigned integer
1223 // containing the sender's current time in seconds as a fixed point number
1224 // with 18 bits fractional part.
1225 //
1226 // The form of the absolute send time extension block:
1227 //
1228 // 0 1 2 3
1229 // 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
1230 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1231 // | ID | len=2 | absolute send time |
1232 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1233
1234 // Get id defined by user.
1235 uint8_t id;
1236 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1237 &id) != 0) {
1238 // Not registered.
1239 return 0;
1240 }
1241 size_t pos = 0;
1242 const uint8_t len = 2;
1243 data_buffer[pos++] = (id << 4) + len;
1244 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1245 absolute_send_time_);
1246 pos += 3;
1247 assert(pos == kAbsoluteSendTimeLength);
1248 return kAbsoluteSendTimeLength;
1249}
1250
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001251bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001252 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001253 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001254 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001255
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001256 // Get length until start of header extension block.
1257 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001258 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001259 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001260 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001261 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001262 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001263 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001264 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001265 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001266 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001267 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001268 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001269 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001270 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001271 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001272 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001273 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001274 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1275 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001276 WEBRTC_TRACE(
1277 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001278 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001279 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001280 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001281 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001282 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001283 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1284 &id) != 0) {
1285 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001286 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001287 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001288 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001289 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001290 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001291 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001292 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001293 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001294 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001295 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001296 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001297 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001298 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001299 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001300}
1301
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001302bool RTPSender::UpdateAudioLevel(uint8_t *rtp_packet,
1303 const uint16_t rtp_packet_length,
1304 const RTPHeader &rtp_header,
1305 const bool is_voiced,
1306 const uint8_t dBov) const {
1307 CriticalSectionScoped cs(send_critsect_);
1308
1309 // Get length until start of header extension block.
1310 int extension_block_pos =
1311 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1312 kRtpExtensionAudioLevel);
1313 if (extension_block_pos < 0) {
1314 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1315 "Failed to update audio level, not registered.");
1316 return false;
1317 }
1318 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1319 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1320 rtp_header.headerLength < block_pos + kAudioLevelLength) {
1321 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1322 "Failed to update audio level, invalid length.");
1323 return false;
1324 }
1325 // Verify that header contains extension.
1326 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1327 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
1328 WEBRTC_TRACE(
1329 kTraceStream, kTraceRtpRtcp, id_,
1330 "Failed to update audio level, hdr extension not found.");
1331 return false;
1332 }
1333 // Get id.
1334 uint8_t id = 0;
1335 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1336 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1337 "Failed to update audio level, no id.");
1338 return false;
1339 }
1340 // Verify first byte in block.
1341 const uint8_t first_block_byte = (id << 4) + 0;
1342 if (rtp_packet[block_pos] != first_block_byte) {
1343 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1344 "Failed to update audio level.");
1345 return false;
1346 }
1347 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1348 return true;
1349}
1350
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001351bool RTPSender::UpdateAbsoluteSendTime(
1352 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001353 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001354 CriticalSectionScoped cs(send_critsect_);
1355
1356 // Get length until start of header extension block.
1357 int extension_block_pos =
1358 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1359 kRtpExtensionAbsoluteSendTime);
1360 if (extension_block_pos < 0) {
1361 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1362 "Failed to update absolute send time, not registered.");
1363 return false;
1364 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001365 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001366 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001367 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001368 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1369 "Failed to update absolute send time, invalid length.");
1370 return false;
1371 }
1372 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001373 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1374 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001375 WEBRTC_TRACE(
1376 kTraceStream, kTraceRtpRtcp, id_,
1377 "Failed to update absolute send time, hdr extension not found.");
1378 return false;
1379 }
1380 // Get id.
1381 uint8_t id = 0;
1382 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1383 &id) != 0) {
1384 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1385 "Failed to update absolute send time, no id.");
1386 return false;
1387 }
1388 // Verify first byte in block.
1389 const uint8_t first_block_byte = (id << 4) + 2;
1390 if (rtp_packet[block_pos] != first_block_byte) {
1391 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1392 "Failed to update absolute send time.");
1393 return false;
1394 }
1395 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1396 // fractional part).
1397 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1398 ((now_ms << 18) / 1000) & 0x00ffffff);
1399 return true;
1400}
1401
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001402void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001403 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001404 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001405 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001406
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001407 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001408 SetStartTimestamp(RTPtime, false);
1409 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001410 if (!ssrc_forced_) {
1411 // Generate a new SSRC.
1412 ssrc_db_.ReturnSSRC(ssrc_);
1413 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001414 }
1415 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001416 if (!sequence_number_forced_ && !ssrc_forced_) {
1417 // Generate a new sequence number.
1418 sequence_number_ =
1419 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001420 }
1421 }
1422}
1423
1424void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001425 CriticalSectionScoped cs(send_critsect_);
1426 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001427}
1428
1429bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001430 CriticalSectionScoped cs(send_critsect_);
1431 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001432}
1433
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001434uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001435 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001436 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001437}
1438
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001439void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001440 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001441 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001442 start_time_stamp_forced_ = force;
1443 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001444 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001445 if (!start_time_stamp_forced_) {
1446 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001447 }
1448 }
1449}
1450
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001451uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001452 CriticalSectionScoped cs(send_critsect_);
1453 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001454}
1455
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001456uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001457 // If configured via API, return 0.
1458 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001459
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001460 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001461 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001462 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001463 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1464 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001465}
1466
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001467void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001468 // This is configured via the API.
1469 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001470
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001471 if (ssrc_ == ssrc && ssrc_forced_) {
1472 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001473 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001474 ssrc_forced_ = true;
1475 ssrc_db_.ReturnSSRC(ssrc_);
1476 ssrc_db_.RegisterSSRC(ssrc);
1477 ssrc_ = ssrc;
1478 if (!sequence_number_forced_) {
1479 sequence_number_ =
1480 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001481 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001482}
1483
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001484uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001485 CriticalSectionScoped cs(send_critsect_);
1486 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001487}
1488
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001489void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001490 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001491}
1492
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001493void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1494 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001495 assert(arr_length <= kRtpCsrcSize);
1496 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001497
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001498 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001499 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001500 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001501 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001502}
1503
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001504int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001505 assert(arr_of_csrc);
1506 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001507 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1508 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001509 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001510 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001511}
1512
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001513void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001514 CriticalSectionScoped cs(send_critsect_);
1515 sequence_number_forced_ = true;
1516 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001517}
1518
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001519uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001520 CriticalSectionScoped cs(send_critsect_);
1521 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001522}
1523
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001524// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001525int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1526 const uint16_t time_ms,
1527 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001528 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001529 return -1;
1530 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001531 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001532}
1533
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001534bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001535 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001536 return false;
1537 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001538 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001539}
1540
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001541int32_t RTPSender::SetAudioPacketSize(
1542 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001543 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001544 return -1;
1545 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001546 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001547}
1548
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001549int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001550 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001551}
1552
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001553int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001554 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001555 return -1;
1556 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001557 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001558}
1559
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001560int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001561 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001562 return -1;
1563 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001564 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001565}
1566
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001567// Video
1568VideoCodecInformation *RTPSender::CodecInformationVideo() {
1569 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001570 return NULL;
1571 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001572 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001573}
1574
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001575RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001576 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001577 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001578}
1579
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001580uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001581 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001582 return 0;
1583 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001584 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001585}
1586
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001587int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001588 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001589 return -1;
1590 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001591 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001592}
1593
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001594int32_t RTPSender::SetGenericFECStatus(
1595 const bool enable, const uint8_t payload_type_red,
1596 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001597 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001598 return -1;
1599 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001600 return video_->SetGenericFECStatus(enable, payload_type_red,
1601 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001602}
1603
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001604int32_t RTPSender::GenericFECStatus(
1605 bool *enable, uint8_t *payload_type_red,
1606 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001607 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001608 return -1;
1609 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001610 return video_->GenericFECStatus(
1611 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001612}
1613
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001614int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001615 const FecProtectionParams *delta_params,
1616 const FecProtectionParams *key_params) {
1617 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001618 return -1;
1619 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001620 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001621}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001622
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001623void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1624 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001625 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001626 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001627 // Add RTX header.
1628 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001629 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001630
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001631 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001632 rtp_parser.Parse(rtp_header);
1633
1634 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001635 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001636
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001637 // Replace payload type, if a specific type is set for RTX.
1638 if (payload_type_rtx_ != -1) {
1639 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001640 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001641 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1642 }
1643
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001644 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001645 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001646 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1647
1648 // Replace SSRC.
1649 ptr += 6;
1650 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1651
1652 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001653 ptr = data_buffer_rtx + rtp_header.headerLength;
1654 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001655 ptr += 2;
1656
1657 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001658 memcpy(ptr, buffer + rtp_header.headerLength,
1659 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001660 *length += 2;
1661}
1662
sprang@webrtc.org71f055f2013-12-04 15:09:27 +00001663void RTPSender::RegisterFrameCountObserver(FrameCountObserver* observer) {
1664 CriticalSectionScoped cs(statistics_crit_.get());
1665 if (observer != NULL)
1666 assert(frame_count_observer_ == NULL);
1667 frame_count_observer_ = observer;
1668}
1669
1670FrameCountObserver* RTPSender::GetFrameCountObserver() const {
1671 CriticalSectionScoped cs(statistics_crit_.get());
1672 return frame_count_observer_;
1673}
1674
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001675void RTPSender::RegisterRtpStatisticsCallback(
1676 StreamDataCountersCallback* callback) {
1677 CriticalSectionScoped cs(statistics_crit_.get());
1678 if (callback != NULL)
1679 assert(rtp_stats_callback_ == NULL);
1680 rtp_stats_callback_ = callback;
1681}
1682
1683StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1684 CriticalSectionScoped cs(statistics_crit_.get());
1685 return rtp_stats_callback_;
1686}
1687
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001688void RTPSender::RegisterBitrateObserver(BitrateStatisticsObserver* observer) {
1689 CriticalSectionScoped cs(statistics_crit_.get());
1690 if (observer != NULL)
1691 assert(bitrate_callback_ == NULL);
1692 bitrate_callback_ = observer;
1693}
1694
1695BitrateStatisticsObserver* RTPSender::GetBitrateObserver() const {
1696 CriticalSectionScoped cs(statistics_crit_.get());
1697 return bitrate_callback_;
1698}
1699
1700uint32_t RTPSender::BitrateSent() const { return bitrate_sent_.BitrateLast(); }
1701
1702void RTPSender::BitrateUpdated(const BitrateStatistics& stats) {
1703 CriticalSectionScoped cs(statistics_crit_.get());
1704 if (bitrate_callback_) {
1705 bitrate_callback_->Notify(stats, ssrc_);
1706 }
1707}
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +00001708
1709void RTPSender::SetTargetBitrateKbps(uint16_t bitrate_kbps) {
1710 CriticalSectionScoped cs(target_bitrate_critsect_.get());
1711 target_bitrate_kbps_ = bitrate_kbps;
1712}
1713
1714uint16_t RTPSender::GetTargetBitrateKbps() {
1715 CriticalSectionScoped cs(target_bitrate_critsect_.get());
1716 return target_bitrate_kbps_;
1717}
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001718} // namespace webrtc