blob: c98d498173dfd61b728ae923a48cf864e50cf747 [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"
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000018#include "webrtc/system_wrappers/interface/logging.h"
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000019#include "webrtc/system_wrappers/interface/tick_util.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000020#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
stefan@webrtc.orga8179622013-06-04 13:47:36 +000024// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
25const int kMaxPaddingLength = 224;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +000026const int kSendSideDelayWindowMs = 1000;
stefan@webrtc.orga8179622013-06-04 13:47:36 +000027
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000028namespace {
29
30const char* FrameTypeToString(const FrameType frame_type) {
31 switch (frame_type) {
32 case kFrameEmpty: return "empty";
33 case kAudioFrameSpeech: return "audio_speech";
34 case kAudioFrameCN: return "audio_cn";
35 case kVideoFrameKey: return "video_key";
36 case kVideoFrameDelta: return "video_delta";
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000037 }
38 return "";
39}
40
41} // namespace
42
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000043RTPSender::RTPSender(const int32_t id,
44 const bool audio,
45 Clock* clock,
46 Transport* transport,
47 RtpAudioFeedback* audio_feedback,
andresp@webrtc.orgd11bec42014-07-08 14:32:58 +000048 PacedSender* paced_sender,
andresp@webrtc.org8f151212014-07-10 09:39:23 +000049 BitrateStatisticsObserver* bitrate_callback,
50 FrameCountObserver* frame_count_observer)
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000051 : clock_(clock),
52 bitrate_sent_(clock, this),
53 id_(id),
54 audio_configured_(audio),
55 audio_(NULL),
56 video_(NULL),
57 paced_sender_(paced_sender),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000058 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000059 transport_(transport),
60 sending_media_(true), // Default to sending media.
61 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000062 packet_over_head_(28),
63 payload_type_(-1),
64 payload_type_map_(),
65 rtp_header_extension_map_(),
66 transmission_time_offset_(0),
67 absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000068 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000069 nack_byte_count_times_(),
70 nack_byte_count_(),
71 nack_bitrate_(clock, NULL),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000072 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000073 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +000074 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000075 rtp_stats_callback_(NULL),
andresp@webrtc.orgd11bec42014-07-08 14:32:58 +000076 bitrate_callback_(bitrate_callback),
andresp@webrtc.org8f151212014-07-10 09:39:23 +000077 frame_count_observer_(frame_count_observer),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +000078 // RTP variables
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +000079 start_timestamp_forced_(false),
80 start_timestamp_(0),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000081 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
82 remote_ssrc_(0),
83 sequence_number_forced_(false),
84 ssrc_forced_(false),
85 timestamp_(0),
86 capture_time_ms_(0),
87 last_timestamp_time_ms_(0),
88 last_packet_marker_bit_(false),
89 num_csrcs_(0),
90 csrcs_(),
91 include_csrcs_(true),
92 rtx_(kRtxOff),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +000093 payload_type_rtx_(-1),
94 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +000095 target_bitrate_(0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000096 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
97 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +000098 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000099 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000100 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000101 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000102 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
103 // Random start, 16 bits. Can't be 0.
104 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
105 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000107 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000108 audio_ = new RTPSenderAudio(id, clock_, this);
109 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000110 } else {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000111 video_ = new RTPSenderVideo(clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000112 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000113}
114
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000115RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000116 if (remote_ssrc_ != 0) {
117 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000118 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000119 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000121 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000122 delete send_critsect_;
123 while (!payload_type_map_.empty()) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000124 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000125 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000126 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000127 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000128 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000129 delete audio_;
130 delete video_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000131}
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000133void RTPSender::SetTargetBitrate(uint32_t bitrate) {
134 CriticalSectionScoped cs(target_bitrate_critsect_.get());
135 target_bitrate_ = bitrate;
136}
137
138uint32_t RTPSender::GetTargetBitrate() {
139 CriticalSectionScoped cs(target_bitrate_critsect_.get());
140 return target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000141}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000142
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000143uint16_t RTPSender::ActualSendBitrateKbit() const {
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000144 return (uint16_t)(bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000145}
146
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000147uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000148 if (video_) {
149 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000150 }
151 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000152}
153
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000154uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000155 if (video_) {
156 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000157 }
158 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000159}
160
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000161uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000162 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000163}
164
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000165bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
166 int* max_send_delay_ms) const {
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000167 if (!SendingMedia())
168 return false;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000169 CriticalSectionScoped cs(statistics_crit_.get());
170 SendDelayMap::const_iterator it = send_delays_.upper_bound(
171 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000172 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000173 return false;
174 int num_delays = 0;
175 for (; it != send_delays_.end(); ++it) {
176 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
177 *avg_send_delay_ms += it->second;
178 ++num_delays;
179 }
180 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
181 return true;
182}
183
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000184int32_t RTPSender::SetTransmissionTimeOffset(
185 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000186 if (transmission_time_offset > (0x800000 - 1) ||
187 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000188 return -1;
189 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000190 CriticalSectionScoped cs(send_critsect_);
191 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000192 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000193}
194
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000195int32_t RTPSender::SetAbsoluteSendTime(
196 const uint32_t absolute_send_time) {
197 if (absolute_send_time > 0xffffff) { // UWord24.
198 return -1;
199 }
200 CriticalSectionScoped cs(send_critsect_);
201 absolute_send_time_ = absolute_send_time;
202 return 0;
203}
204
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000205int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
206 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000207 CriticalSectionScoped cs(send_critsect_);
208 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000209}
210
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000211int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000212 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000213 CriticalSectionScoped cs(send_critsect_);
214 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000215}
216
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000217uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000218 CriticalSectionScoped cs(send_critsect_);
219 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000220}
221
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000222int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000223 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000224 const int8_t payload_number, const uint32_t frequency,
225 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000226 assert(payload_name);
227 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000228
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000229 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000230 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000231
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000232 if (payload_type_map_.end() != it) {
233 // We already use this payload type.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000234 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000235 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000237 // Check if it's the same as we already have.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000238 if (RtpUtility::StringCompare(
239 payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000240 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000241 payload->typeSpecific.Audio.frequency == frequency &&
242 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000243 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000244 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000245 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000246 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000247 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000248 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000249 return 0;
250 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000251 }
252 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000253 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000254 int32_t ret_val = -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000255 RtpUtility::Payload* payload = NULL;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000256 if (audio_configured_) {
257 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
258 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000259 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000260 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
261 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000262 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000263 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000264 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000265 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000266 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000267}
268
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000269int32_t RTPSender::DeRegisterSendPayload(
270 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000271 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000272
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000273 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000274 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000275
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000276 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000277 return -1;
278 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000279 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000280 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000281 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000282 return 0;
283}
niklase@google.com470e71d2011-07-07 08:21:25 +0000284
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000285int8_t RTPSender::SendPayloadType() const {
286 CriticalSectionScoped cs(send_critsect_);
287 return payload_type_;
288}
niklase@google.com470e71d2011-07-07 08:21:25 +0000289
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000290int RTPSender::SendPayloadFrequency() const {
291 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
292}
niklase@google.com470e71d2011-07-07 08:21:25 +0000293
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000294int32_t RTPSender::SetMaxPayloadLength(
295 const uint16_t max_payload_length,
296 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000297 // Sanity check.
298 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000299 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000300 return -1;
301 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000302 CriticalSectionScoped cs(send_critsect_);
303 max_payload_length_ = max_payload_length;
304 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000305 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000306}
307
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000308uint16_t RTPSender::MaxDataPayloadLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000309 int rtx;
310 {
311 CriticalSectionScoped rtx_lock(send_critsect_);
312 rtx = rtx_;
313 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000314 if (audio_configured_) {
315 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000316 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000317 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
318 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000319 - ((rtx) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000320 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000321}
322
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000323uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000324 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000325}
326
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000327uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000328
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000329void RTPSender::SetRTXStatus(int mode) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000330 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000331 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000332}
333
334void RTPSender::SetRtxSsrc(uint32_t ssrc) {
335 CriticalSectionScoped cs(send_critsect_);
336 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000337}
338
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000339uint32_t RTPSender::RtxSsrc() const {
340 CriticalSectionScoped cs(send_critsect_);
341 return ssrc_rtx_;
342}
343
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000344void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000345 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000346 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000347 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000348 *ssrc = ssrc_rtx_;
349 *payload_type = payload_type_rtx_;
350}
351
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000352void RTPSender::SetRtxPayloadType(int payload_type) {
353 CriticalSectionScoped cs(send_critsect_);
354 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000355}
356
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000357int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
358 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000359 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000360
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000361 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000362 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000363 return -1;
364 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000365 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000366 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000367 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000368 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000369 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000370 // And it's a match...
371 return 0;
372 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000373 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000374 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000375 if (payload_type_ == payload_type) {
376 if (!audio_configured_) {
377 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000378 }
379 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000380 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000381 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000382 payload_type_map_.find(payload_type);
383 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000384 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000385 return -1;
386 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000387 payload_type_ = payload_type;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000388 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000389 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000390 if (!payload->audio && !audio_configured_) {
391 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
392 *video_type = payload->typeSpecific.Video.videoCodecType;
393 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000394 }
395 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000396}
397
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000398int32_t RTPSender::SendOutgoingData(
399 const FrameType frame_type, const int8_t payload_type,
400 const uint32_t capture_timestamp, int64_t capture_time_ms,
401 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000402 const RTPFragmentationHeader *fragmentation,
403 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000404 uint32_t ssrc;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000405 {
406 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000407 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000408 ssrc = ssrc_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000409 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000410 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000411 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000412 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000413 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000414 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000415 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000416 return -1;
417 }
418
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000419 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000420 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000421 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
422 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000423 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000424 frame_type == kFrameEmpty);
425
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000426 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
427 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000428 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000429 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
430 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000431 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000432
433 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000434 if (paced_sender_->Enabled()) {
435 // Padding is driven by the pacer and not by the encoder.
436 return 0;
437 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000438 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000439 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000440 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000441 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
442 capture_timestamp, capture_time_ms,
443 payload_data, payload_size,
444 fragmentation, codec_info,
445 rtp_type_hdr);
446
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000447 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000448
449 CriticalSectionScoped cs(statistics_crit_.get());
450 uint32_t frame_count = ++frame_counts_[frame_type];
451 if (frame_count_observer_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000452 frame_count_observer_->FrameCountUpdated(frame_type, frame_count, ssrc);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000453 }
454
455 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000456}
457
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000458int RTPSender::SendRedundantPayloads(int payload_type, int bytes_to_send) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000459 uint8_t buffer[IP_PACKET_SIZE];
460 int bytes_left = bytes_to_send;
461 while (bytes_left > 0) {
462 uint16_t length = bytes_left;
463 int64_t capture_time_ms;
464 if (!packet_history_.GetBestFittingPacket(buffer, &length,
465 &capture_time_ms)) {
466 break;
467 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000468 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000469 return -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000470 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000471 RTPHeader rtp_header;
472 rtp_parser.Parse(rtp_header);
473 bytes_left -= length - rtp_header.headerLength;
474 }
475 return bytes_to_send - bytes_left;
476}
477
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000478bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000479 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000480 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000481 // Current bitrate since last estimate(1 second) averaged with the
482 // estimate since then, to get the most up to date bitrate.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000483 uint32_t current_bitrate = bitrate_sent_.BitrateNow();
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000484 uint32_t target_bitrate = GetTargetBitrate();
485 int bitrate_diff = target_bitrate - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000486 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000487 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000488 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000489 int bytes = 0;
490 if (current_bitrate == 0) {
491 // Start up phase. Send one 33.3 ms batch to start with.
492 bytes = (bitrate_diff / 8) / 30;
493 } else {
494 bytes = (bitrate_diff / 8);
495 // Cap at 200 ms of target send data.
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000496 int bytes_cap = target_bitrate / 1000 * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000497 if (bytes > bytes_cap) {
498 bytes = bytes_cap;
499 }
500 }
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000501 uint32_t timestamp;
502 {
503 CriticalSectionScoped cs(send_critsect_);
504 // Add the random RTP timestamp offset and store the capture time for
505 // later calculation of the send time offset.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000506 timestamp = start_timestamp_ + capture_timestamp;
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000507 timestamp_ = timestamp;
508 capture_time_ms_ = capture_time_ms;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000509 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000510 }
511 int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
512 bytes, kDontRetransmit, false, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000513 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
514 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000515}
516
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000517int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
518 int32_t bytes) {
519 int padding_bytes_in_packet = kMaxPaddingLength;
520 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000521 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000522 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000523 packet[0] |= 0x20; // Set padding bit.
524 int32_t *data =
525 reinterpret_cast<int32_t *>(&(packet[header_length]));
526
527 // Fill data buffer with random data.
528 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
529 data[j] = rand(); // NOLINT
530 }
531 // Set number of padding bytes in the last byte of the packet.
532 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
533 return padding_bytes_in_packet;
534}
535
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000536int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
537 int64_t capture_time_ms, int32_t bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000538 StorageType store, bool force_full_size_packets,
539 bool only_pad_after_markerbit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000540 // Drop this packet if we're not sending media packets.
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000541 if (!SendingMedia()) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000542 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000543 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000544 int padding_bytes_in_packet = 0;
545 int bytes_sent = 0;
546 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000547 // Always send full padding packets.
548 if (force_full_size_packets && bytes < kMaxPaddingLength)
549 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000550 if (bytes < kMaxPaddingLength) {
551 if (force_full_size_packets) {
552 bytes = kMaxPaddingLength;
553 } else {
554 // Round to the nearest multiple of 32.
555 bytes = (bytes + 16) & 0xffe0;
556 }
557 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000558 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000559 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000560 break;
561 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000562 uint32_t ssrc;
563 uint16_t sequence_number;
564 {
565 CriticalSectionScoped cs(send_critsect_);
566 // Only send padding packets following the last packet of a frame,
567 // indicated by the marker bit.
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000568 if (only_pad_after_markerbit && !last_packet_marker_bit_)
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000569 return bytes_sent;
570 if (rtx_ == kRtxOff) {
571 ssrc = ssrc_;
572 sequence_number = sequence_number_;
573 ++sequence_number_;
574 } else {
575 ssrc = ssrc_rtx_;
576 sequence_number = sequence_number_rtx_;
577 ++sequence_number_rtx_;
578 }
579 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000580
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000581 uint8_t padding_packet[IP_PACKET_SIZE];
582 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
583 false, timestamp, sequence_number, NULL,
584 0);
585 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
586 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000587 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
588 header_length, capture_time_ms, store,
589 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000590 // Error sending the packet.
591 break;
592 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000593 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000594 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000595 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000596}
597
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000598void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000599 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000600 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000601}
602
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000603bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000604 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000605}
niklase@google.com470e71d2011-07-07 08:21:25 +0000606
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000607int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
608 uint16_t length = IP_PACKET_SIZE;
609 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000610 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000611 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
612 data_buffer, &length,
613 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000614 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000615 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000616 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000617
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000618 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000619 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000620 RTPHeader header;
621 if (!rtp_parser.Parse(header)) {
622 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000623 return -1;
624 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000625 // Convert from TickTime to Clock since capture_time_ms is based on
626 // TickTime.
627 // TODO(holmer): Remove this conversion when we remove the use of TickTime.
628 int64_t clock_delta_ms = clock_->TimeInMilliseconds() -
629 TickTime::MillisecondTimestamp();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000630 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000631 header.ssrc,
632 header.sequenceNumber,
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000633 capture_time_ms + clock_delta_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000634 length - header.headerLength,
635 true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000636 // We can't send the packet right now.
637 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000638 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000639 }
640 }
641
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000642 CriticalSectionScoped lock(send_critsect_);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000643 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org16395222014-03-19 19:34:07 +0000644 (rtx_ & kRtxRetransmitted) > 0, true) ?
645 length : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000646}
647
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000648bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
649 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000650 if (transport_) {
651 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000652 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000653 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
654 "size", size, "sent", bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000655 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000656 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000657 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000658 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000659 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000660 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000661}
662
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000663int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000664 if (!video_)
665 return -1;
666 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000667}
668
669int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000670 if (!video_)
671 return -1;
672 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000673}
674
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000675void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000676 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000677 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000678 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
679 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000680 const int64_t now = clock_->TimeInMilliseconds();
681 uint32_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000682 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000683
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000684 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000685 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000686 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000687 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000688 return;
689 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000690
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000691 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
692 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000693 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000694 if (bytes_sent > 0) {
695 bytes_re_sent += bytes_sent;
696 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000697 // The packet has previously been resent.
698 // Try resending next packet in the list.
699 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000700 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000701 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000702 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
703 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000704 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000706 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000707 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000708 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000709 uint32_t target_bytes =
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000710 (static_cast<uint32_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000711 if (bytes_re_sent > target_bytes) {
712 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000713 }
714 }
715 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000716 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000717 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000718 UpdateNACKBitRate(bytes_re_sent, now);
719 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000720 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000721}
722
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000723bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
724 uint32_t num = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000725 int byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000726 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000727 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000728
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000729 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000730
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000731 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000732 return true;
733 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000734 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000735 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000736 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000737 break;
738 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000739 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000740 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000741 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000742 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000743 if (num == NACK_BYTECOUNT_SIZE) {
744 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000745 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000746 if (nack_byte_count_times_[num - 1] <= now) {
747 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000748 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000749 }
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000750 return (byte_count * 8) <
751 static_cast<int>(target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000752}
753
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000754void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
755 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000756 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000757
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000758 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000759 if (bytes > 0) {
760 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000761 // Add padding length.
762 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000763 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000764 if (nack_byte_count_times_[0] == 0) {
765 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000766 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000767 // Shift.
768 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
769 nack_byte_count_[i + 1] = nack_byte_count_[i];
770 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000771 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000772 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000773 nack_byte_count_[0] = bytes;
774 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000775 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000776 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000777}
778
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000779// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000780bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000781 int64_t capture_time_ms,
782 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000783 uint16_t length = IP_PACKET_SIZE;
784 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000785 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000786
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000787 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
788 0,
789 retransmission,
790 data_buffer,
791 &length,
792 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000793 // Packet cannot be found. Allow sending to continue.
794 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000795 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000796 if (!retransmission && capture_time_ms > 0) {
797 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
798 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000799 int rtx;
800 {
801 CriticalSectionScoped lock(send_critsect_);
802 rtx = rtx_;
803 }
804 return PrepareAndSendPacket(data_buffer,
805 length,
806 capture_time_ms,
807 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000808 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000809}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000810
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000811bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
812 uint16_t length,
813 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000814 bool send_over_rtx,
815 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000816 uint8_t *buffer_to_send_ptr = buffer;
817
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000818 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000819 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000820 rtp_parser.Parse(rtp_header);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000821 TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000822 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000823 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000824
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000825 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000826 if (send_over_rtx) {
827 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000828 buffer_to_send_ptr = data_buffer_rtx;
829 }
830
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000831 int64_t now_ms = clock_->TimeInMilliseconds();
832 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000833 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
834 diff_ms);
835 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000836 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000837 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
838 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000839 return ret;
840}
841
842void RTPSender::UpdateRtpStats(const uint8_t* buffer,
843 uint32_t size,
844 const RTPHeader& header,
845 bool is_rtx,
846 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000847 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000848 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000849 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000850
851 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000852 if (is_rtx) {
853 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000854 } 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;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000895 int rtx;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000896 {
897 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000898 if (!sending_media_) {
899 return 0;
900 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000901 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_ :
902 payload_type_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000903 timestamp = timestamp_;
904 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000905 if (last_timestamp_time_ms_ > 0) {
906 timestamp +=
907 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
908 capture_time_ms +=
909 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
910 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000911 rtx = rtx_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000912 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000913 int bytes_sent = 0;
914 if ((rtx & kRtxRedundantPayloads) != 0)
915 bytes_sent = SendRedundantPayloads(payload_type, bytes);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000916 bytes -= bytes_sent;
917 if (bytes > 0) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000918 int padding_sent = SendPadData(payload_type,
919 timestamp,
920 capture_time_ms,
921 bytes,
922 kDontStore,
923 true,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000924 rtx == kRtxOff);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000925 bytes_sent += padding_sent;
926 }
927 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000928}
929
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000930// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000931int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000932 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000933 int64_t capture_time_ms, StorageType storage,
934 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000935 RtpUtility::RtpHeaderParser rtp_parser(buffer,
936 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000937 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000938 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000939
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000940 int64_t now_ms = clock_->TimeInMilliseconds();
941
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000942 // |capture_time_ms| <= 0 is considered invalid.
943 // TODO(holmer): This should be changed all over Video Engine so that negative
944 // time is consider invalid, while 0 is considered a valid time.
945 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000946 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000947 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000948 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000949
950 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
951 rtp_header, now_ms);
952
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000953 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000954 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
955 max_payload_length_, capture_time_ms,
956 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000957 return -1;
958 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000959
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000960 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000961 int64_t clock_delta_ms = clock_->TimeInMilliseconds() -
962 TickTime::MillisecondTimestamp();
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000963 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000964 rtp_header.sequenceNumber,
965 capture_time_ms + clock_delta_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000966 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000967 // We can't send the packet right now.
968 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000969 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000970 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000971 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000972 if (capture_time_ms > 0) {
973 UpdateDelayStatistics(capture_time_ms, now_ms);
974 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000975 uint32_t length = payload_length + rtp_header_length;
976 if (!SendPacketToNetwork(buffer, length))
977 return -1;
978 UpdateRtpStats(buffer, length, rtp_header, false, false);
979 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000980}
981
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000982void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
983 CriticalSectionScoped cs(statistics_crit_.get());
984 send_delays_[now_ms] = now_ms - capture_time_ms;
985 send_delays_.erase(send_delays_.begin(),
986 send_delays_.lower_bound(now_ms - kSendSideDelayWindowMs));
987}
988
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000989void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000990 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000991 bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000992 nack_bitrate_.Process();
993 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000994 return;
995 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000996 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000997}
998
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000999uint16_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001000 CriticalSectionScoped lock(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001001 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001002 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001003 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001004 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001005 rtp_header_length += RtpHeaderExtensionTotalLength();
1006 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001007}
1008
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001009uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001010 CriticalSectionScoped cs(send_critsect_);
1011 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001012}
1013
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001014void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001015 uint32_t ssrc;
1016 uint32_t ssrc_rtx;
1017 {
1018 CriticalSectionScoped ssrc_lock(send_critsect_);
1019 ssrc = ssrc_;
1020 ssrc_rtx = ssrc_rtx_;
1021 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001022 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001023 rtp_stats_ = StreamDataCounters();
1024 rtx_rtp_stats_ = StreamDataCounters();
1025 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001026 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
1027 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001028 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001029}
1030
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001031uint32_t RTPSender::Packets() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001032 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001033 return rtp_stats_.packets + rtx_rtp_stats_.packets;
niklase@google.com470e71d2011-07-07 08:21:25 +00001034}
1035
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001036// Number of sent RTP bytes.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001037uint32_t RTPSender::Bytes() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001038 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001039 return rtp_stats_.bytes + rtx_rtp_stats_.bytes;
niklase@google.com470e71d2011-07-07 08:21:25 +00001040}
1041
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001042int RTPSender::CreateRTPHeader(
1043 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
1044 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
1045 uint8_t num_csrcs) const {
1046 header[0] = 0x80; // version 2.
1047 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001048 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001049 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001050 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001051 RtpUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1052 RtpUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1053 RtpUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001054 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001055
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001056 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001057 if (num_csrcs > 0) {
1058 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001059 // error
1060 assert(false);
1061 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001062 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001063 uint8_t *ptr = &header[rtp_header_length];
1064 for (int i = 0; i < num_csrcs; ++i) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001065 RtpUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001066 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001067 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001068 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001069
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001070 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001071 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001072 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001073
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001074 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1075 if (len > 0) {
1076 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001077 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001078 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001079 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001080}
1081
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001082int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
1083 const int8_t payload_type,
1084 const bool marker_bit,
1085 const uint32_t capture_timestamp,
1086 int64_t capture_time_ms,
1087 const bool timestamp_provided,
1088 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001089 assert(payload_type >= 0);
1090 CriticalSectionScoped cs(send_critsect_);
1091
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001092 if (timestamp_provided) {
1093 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001094 } else {
1095 // Make a unique time stamp.
1096 // We can't inc by the actual time, since then we increase the risk of back
1097 // timing.
1098 timestamp_++;
1099 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001100 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001101 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001102 capture_time_ms_ = capture_time_ms;
1103 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001104 int csrcs_length = 0;
1105 if (include_csrcs_)
1106 csrcs_length = num_csrcs_;
1107 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1108 timestamp_, sequence_number, csrcs_, csrcs_length);
1109}
1110
1111uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001112 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001113 return 0;
1114 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001115 // RTP header extension, RFC 3550.
1116 // 0 1 2 3
1117 // 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
1118 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1119 // | defined by profile | length |
1120 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1121 // | header extension |
1122 // | .... |
1123 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001124 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001125 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001126
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001127 // Add extension ID (0xBEDE).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001128 RtpUtility::AssignUWord16ToBuffer(data_buffer, kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001129
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001130 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001131 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001132
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001133 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001134 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001135 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001136 switch (type) {
1137 case kRtpExtensionTransmissionTimeOffset:
1138 block_length = BuildTransmissionTimeOffsetExtension(
1139 data_buffer + kHeaderLength + total_block_length);
1140 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001141 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001142 block_length = BuildAudioLevelExtension(
1143 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001144 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001145 case kRtpExtensionAbsoluteSendTime:
1146 block_length = BuildAbsoluteSendTimeExtension(
1147 data_buffer + kHeaderLength + total_block_length);
1148 break;
1149 default:
1150 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001151 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001152 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001153 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001154 }
1155 if (total_block_length == 0) {
1156 // No extension added.
1157 return 0;
1158 }
1159 // Set header length (in number of Word32, header excluded).
1160 assert(total_block_length % 4 == 0);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001161 RtpUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
1162 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001163 // Total added length.
1164 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001165}
1166
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001167uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1168 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001169 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1170 //
1171 // The transmission time is signaled to the receiver in-band using the
1172 // general mechanism for RTP header extensions [RFC5285]. The payload
1173 // of this extension (the transmitted value) is a 24-bit signed integer.
1174 // When added to the RTP timestamp of the packet, it represents the
1175 // "effective" RTP transmission time of the packet, on the RTP
1176 // timescale.
1177 //
1178 // The form of the transmission offset extension block:
1179 //
1180 // 0 1 2 3
1181 // 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
1182 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1183 // | ID | len=2 | transmission offset |
1184 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001185
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001186 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001187 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001188 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1189 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001190 // Not registered.
1191 return 0;
1192 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001193 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001194 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001195 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001196 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos,
1197 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001198 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001199 assert(pos == kTransmissionTimeOffsetLength);
1200 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001201}
1202
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001203uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1204 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1205 //
1206 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1207 //
1208 // The form of the audio level extension block:
1209 //
1210 // 0 1 2 3
1211 // 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
1212 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1213 // | ID | len=0 |V| level | 0x00 | 0x00 |
1214 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1215 //
1216 // Note that we always include 2 pad bytes, which will result in legal and
1217 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1218 // are implemented. Right now the pad bytes would anyway be required at end
1219 // of the extension block, so it makes no difference.
1220
1221 // Get id defined by user.
1222 uint8_t id;
1223 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1224 // Not registered.
1225 return 0;
1226 }
1227 size_t pos = 0;
1228 const uint8_t len = 0;
1229 data_buffer[pos++] = (id << 4) + len;
1230 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1231 data_buffer[pos++] = 0; // Padding.
1232 data_buffer[pos++] = 0; // Padding.
1233 // kAudioLevelLength is including pad bytes.
1234 assert(pos == kAudioLevelLength);
1235 return kAudioLevelLength;
1236}
1237
1238uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001239 // Absolute send time in RTP streams.
1240 //
1241 // The absolute send time is signaled to the receiver in-band using the
1242 // general mechanism for RTP header extensions [RFC5285]. The payload
1243 // of this extension (the transmitted value) is a 24-bit unsigned integer
1244 // containing the sender's current time in seconds as a fixed point number
1245 // with 18 bits fractional part.
1246 //
1247 // The form of the absolute send time extension block:
1248 //
1249 // 0 1 2 3
1250 // 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
1251 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1252 // | ID | len=2 | absolute send time |
1253 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1254
1255 // Get id defined by user.
1256 uint8_t id;
1257 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1258 &id) != 0) {
1259 // Not registered.
1260 return 0;
1261 }
1262 size_t pos = 0;
1263 const uint8_t len = 2;
1264 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001265 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos, absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001266 pos += 3;
1267 assert(pos == kAbsoluteSendTimeLength);
1268 return kAbsoluteSendTimeLength;
1269}
1270
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001271void RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001272 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001273 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001274 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001275 // Get id.
1276 uint8_t id = 0;
1277 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1278 &id) != 0) {
1279 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001280 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001281 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001282 // Get length until start of header extension block.
1283 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001284 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001285 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001286 if (extension_block_pos < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001287 LOG(LS_WARNING)
1288 << "Failed to update transmission time offset, not registered.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001289 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001290 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001291 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001292 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001293 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001294 block_pos + kTransmissionTimeOffsetLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001295 LOG(LS_WARNING)
1296 << "Failed to update transmission time offset, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001297 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001298 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001299 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001300 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1301 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001302 LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1303 "extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001304 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001305 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001306 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001307 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001308 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001309 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001310 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001311 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001312 // Update transmission offset field (converting to a 90 kHz timestamp).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001313 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1314 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001315}
1316
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001317bool RTPSender::UpdateAudioLevel(uint8_t *rtp_packet,
1318 const uint16_t rtp_packet_length,
1319 const RTPHeader &rtp_header,
1320 const bool is_voiced,
1321 const uint8_t dBov) const {
1322 CriticalSectionScoped cs(send_critsect_);
1323
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001324 // Get id.
1325 uint8_t id = 0;
1326 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1327 // Not registered.
1328 return false;
1329 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001330 // Get length until start of header extension block.
1331 int extension_block_pos =
1332 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1333 kRtpExtensionAudioLevel);
1334 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001335 // The feature is not enabled.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001336 return false;
1337 }
1338 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1339 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1340 rtp_header.headerLength < block_pos + kAudioLevelLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001341 LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001342 return false;
1343 }
1344 // Verify that header contains extension.
1345 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1346 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001347 LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001348 return false;
1349 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001350 // Verify first byte in block.
1351 const uint8_t first_block_byte = (id << 4) + 0;
1352 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001353 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001354 return false;
1355 }
1356 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1357 return true;
1358}
1359
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001360void RTPSender::UpdateAbsoluteSendTime(
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001361 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001362 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001363 CriticalSectionScoped cs(send_critsect_);
1364
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001365 // Get id.
1366 uint8_t id = 0;
1367 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1368 &id) != 0) {
1369 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001370 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001371 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001372 // Get length until start of header extension block.
1373 int extension_block_pos =
1374 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1375 kRtpExtensionAbsoluteSendTime);
1376 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001377 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001378 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001379 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001380 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001381 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001382 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001383 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001384 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001385 }
1386 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001387 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1388 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001389 LOG(LS_WARNING)
1390 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001391 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001392 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001393 // Verify first byte in block.
1394 const uint8_t first_block_byte = (id << 4) + 2;
1395 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001396 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001397 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001398 }
1399 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1400 // fractional part).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001401 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1402 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001403}
1404
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001405void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001406 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001407 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001408 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001409
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001410 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001411 SetStartTimestamp(RTPtime, false);
1412 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001413 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001414 if (!ssrc_forced_) {
1415 // Generate a new SSRC.
1416 ssrc_db_.ReturnSSRC(ssrc_);
1417 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001418 }
1419 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001420 if (!sequence_number_forced_ && !ssrc_forced_) {
1421 // Generate a new sequence number.
1422 sequence_number_ =
1423 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001424 }
1425 }
1426}
1427
1428void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001429 CriticalSectionScoped cs(send_critsect_);
1430 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001431}
1432
1433bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001434 CriticalSectionScoped cs(send_critsect_);
1435 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001436}
1437
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001438uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001439 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001440 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001441}
1442
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001443void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001444 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001445 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001446 start_timestamp_forced_ = true;
1447 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001448 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001449 if (!start_timestamp_forced_) {
1450 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001451 }
1452 }
1453}
1454
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001455uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001456 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001457 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001458}
1459
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001460uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001461 // If configured via API, return 0.
1462 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001463
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001464 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001465 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001466 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001467 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1468 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001469}
1470
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001471void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001472 // This is configured via the API.
1473 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001474
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001475 if (ssrc_ == ssrc && ssrc_forced_) {
1476 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001477 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001478 ssrc_forced_ = true;
1479 ssrc_db_.ReturnSSRC(ssrc_);
1480 ssrc_db_.RegisterSSRC(ssrc);
1481 ssrc_ = ssrc;
1482 if (!sequence_number_forced_) {
1483 sequence_number_ =
1484 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001485 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001486}
1487
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001488uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001489 CriticalSectionScoped cs(send_critsect_);
1490 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001491}
1492
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001493void RTPSender::SetCSRCStatus(const bool include) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001494 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001495 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001496}
1497
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001498void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1499 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001500 assert(arr_length <= kRtpCsrcSize);
1501 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001502
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001503 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001504 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001505 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001506 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001507}
1508
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001509int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001510 assert(arr_of_csrc);
1511 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001512 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1513 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001514 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001515 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001516}
1517
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001518void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001519 CriticalSectionScoped cs(send_critsect_);
1520 sequence_number_forced_ = true;
1521 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001522}
1523
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001524uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001525 CriticalSectionScoped cs(send_critsect_);
1526 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001527}
1528
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001529// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001530int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1531 const uint16_t time_ms,
1532 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001533 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001534 return -1;
1535 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001536 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001537}
1538
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001539bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001540 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001541 return false;
1542 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001543 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001544}
1545
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001546int32_t RTPSender::SetAudioPacketSize(
1547 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001548 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001549 return -1;
1550 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001551 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001552}
1553
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001554int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001555 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001556}
1557
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001558int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001559 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001560 return -1;
1561 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001562 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001563}
1564
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001565int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001566 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001567 return -1;
1568 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001569 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001570}
1571
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001572// Video
1573VideoCodecInformation *RTPSender::CodecInformationVideo() {
1574 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001575 return NULL;
1576 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001577 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001578}
1579
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001580RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001581 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001582 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001583}
1584
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001585uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001586 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001587 return 0;
1588 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001589 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001590}
1591
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001592int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001593 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001594 return -1;
1595 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001596 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001597}
1598
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001599int32_t RTPSender::SetGenericFECStatus(
1600 const bool enable, const uint8_t payload_type_red,
1601 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001602 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001603 return -1;
1604 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001605 return video_->SetGenericFECStatus(enable, payload_type_red,
1606 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001607}
1608
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001609int32_t RTPSender::GenericFECStatus(
1610 bool *enable, uint8_t *payload_type_red,
1611 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001612 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001613 return -1;
1614 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001615 return video_->GenericFECStatus(
1616 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001617}
1618
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001619int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001620 const FecProtectionParams *delta_params,
1621 const FecProtectionParams *key_params) {
1622 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001623 return -1;
1624 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001625 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001626}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001627
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001628void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1629 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001630 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001631 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001632 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001633 RtpUtility::RtpHeaderParser rtp_parser(
1634 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001635
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001636 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001637 rtp_parser.Parse(rtp_header);
1638
1639 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001640 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001641
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001642 // Replace payload type, if a specific type is set for RTX.
1643 if (payload_type_rtx_ != -1) {
1644 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001645 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001646 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1647 }
1648
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001649 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001650 uint8_t *ptr = data_buffer_rtx + 2;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001651 RtpUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001652
1653 // Replace SSRC.
1654 ptr += 6;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001655 RtpUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001656
1657 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001658 ptr = data_buffer_rtx + rtp_header.headerLength;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001659 RtpUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001660 ptr += 2;
1661
1662 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001663 memcpy(ptr, buffer + rtp_header.headerLength,
1664 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001665 *length += 2;
1666}
1667
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001668void RTPSender::RegisterRtpStatisticsCallback(
1669 StreamDataCountersCallback* callback) {
1670 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001671 rtp_stats_callback_ = callback;
1672}
1673
1674StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1675 CriticalSectionScoped cs(statistics_crit_.get());
1676 return rtp_stats_callback_;
1677}
1678
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001679uint32_t RTPSender::BitrateSent() const { return bitrate_sent_.BitrateLast(); }
1680
1681void RTPSender::BitrateUpdated(const BitrateStatistics& stats) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001682 uint32_t ssrc;
1683 {
1684 CriticalSectionScoped ssrc_lock(send_critsect_);
1685 ssrc = ssrc_;
1686 }
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001687 if (bitrate_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001688 bitrate_callback_->Notify(stats, ssrc);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001689 }
1690}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001691
1692void RTPSender::SetRtpState(const RtpState& rtp_state) {
1693 SetStartTimestamp(rtp_state.start_timestamp, true);
1694 CriticalSectionScoped lock(send_critsect_);
1695 sequence_number_ = rtp_state.sequence_number;
1696 sequence_number_forced_ = true;
1697 timestamp_ = rtp_state.timestamp;
1698 capture_time_ms_ = rtp_state.capture_time_ms;
1699 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
1700}
1701
1702RtpState RTPSender::GetRtpState() const {
1703 CriticalSectionScoped lock(send_critsect_);
1704
1705 RtpState state;
1706 state.sequence_number = sequence_number_;
1707 state.start_timestamp = start_timestamp_;
1708 state.timestamp = timestamp_;
1709 state.capture_time_ms = capture_time_ms_;
1710 state.last_timestamp_time_ms = last_timestamp_time_ms_;
1711
1712 return state;
1713}
1714
1715void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
1716 CriticalSectionScoped lock(send_critsect_);
1717 sequence_number_rtx_ = rtp_state.sequence_number;
1718}
1719
1720RtpState RTPSender::GetRtxRtpState() const {
1721 CriticalSectionScoped lock(send_critsect_);
1722
1723 RtpState state;
1724 state.sequence_number = sequence_number_rtx_;
1725 state.start_timestamp = start_timestamp_;
1726
1727 return state;
1728}
1729
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001730} // namespace webrtc