blob: 858fc42a2428b9a2994374f868cc197d1c593045 [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,
49 BitrateStatisticsObserver* bitrate_callback)
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000050 : clock_(clock),
51 bitrate_sent_(clock, this),
52 id_(id),
53 audio_configured_(audio),
54 audio_(NULL),
55 video_(NULL),
56 paced_sender_(paced_sender),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000057 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000058 transport_(transport),
59 sending_media_(true), // Default to sending media.
60 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000061 packet_over_head_(28),
62 payload_type_(-1),
63 payload_type_map_(),
64 rtp_header_extension_map_(),
65 transmission_time_offset_(0),
66 absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000067 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000068 nack_byte_count_times_(),
69 nack_byte_count_(),
70 nack_bitrate_(clock, NULL),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000071 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000072 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +000073 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000074 frame_count_observer_(NULL),
75 rtp_stats_callback_(NULL),
andresp@webrtc.orgd11bec42014-07-08 14:32:58 +000076 bitrate_callback_(bitrate_callback),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +000077 // RTP variables
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +000078 start_timestamp_forced_(false),
79 start_timestamp_(0),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000080 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
81 remote_ssrc_(0),
82 sequence_number_forced_(false),
83 ssrc_forced_(false),
84 timestamp_(0),
85 capture_time_ms_(0),
86 last_timestamp_time_ms_(0),
87 last_packet_marker_bit_(false),
88 num_csrcs_(0),
89 csrcs_(),
90 include_csrcs_(true),
91 rtx_(kRtxOff),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +000092 payload_type_rtx_(-1),
93 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +000094 target_bitrate_(0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000095 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
96 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +000097 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000098 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000099 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000100 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000101 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
102 // Random start, 16 bits. Can't be 0.
103 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
104 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000106 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000107 audio_ = new RTPSenderAudio(id, clock_, this);
108 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000109 } else {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000110 video_ = new RTPSenderVideo(clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000111 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000112}
113
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000114RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000115 if (remote_ssrc_ != 0) {
116 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000117 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000118 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000119
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000120 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000121 delete send_critsect_;
122 while (!payload_type_map_.empty()) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000123 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000124 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000125 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000126 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000127 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000128 delete audio_;
129 delete video_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000130}
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000132void RTPSender::SetTargetBitrate(uint32_t bitrate) {
133 CriticalSectionScoped cs(target_bitrate_critsect_.get());
134 target_bitrate_ = bitrate;
135}
136
137uint32_t RTPSender::GetTargetBitrate() {
138 CriticalSectionScoped cs(target_bitrate_critsect_.get());
139 return target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000140}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000141
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000142uint16_t RTPSender::ActualSendBitrateKbit() const {
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000143 return (uint16_t)(bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000144}
145
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000146uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000147 if (video_) {
148 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000149 }
150 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000151}
152
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000153uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000154 if (video_) {
155 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000156 }
157 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000158}
159
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000160uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000161 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000162}
163
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000164bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
165 int* max_send_delay_ms) const {
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000166 if (!SendingMedia())
167 return false;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000168 CriticalSectionScoped cs(statistics_crit_.get());
169 SendDelayMap::const_iterator it = send_delays_.upper_bound(
170 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000171 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000172 return false;
173 int num_delays = 0;
174 for (; it != send_delays_.end(); ++it) {
175 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
176 *avg_send_delay_ms += it->second;
177 ++num_delays;
178 }
179 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
180 return true;
181}
182
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000183int32_t RTPSender::SetTransmissionTimeOffset(
184 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000185 if (transmission_time_offset > (0x800000 - 1) ||
186 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000187 return -1;
188 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000189 CriticalSectionScoped cs(send_critsect_);
190 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000191 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000192}
193
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000194int32_t RTPSender::SetAbsoluteSendTime(
195 const uint32_t absolute_send_time) {
196 if (absolute_send_time > 0xffffff) { // UWord24.
197 return -1;
198 }
199 CriticalSectionScoped cs(send_critsect_);
200 absolute_send_time_ = absolute_send_time;
201 return 0;
202}
203
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000204int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
205 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000206 CriticalSectionScoped cs(send_critsect_);
207 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000208}
209
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000210int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000211 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000212 CriticalSectionScoped cs(send_critsect_);
213 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000214}
215
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000216uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000217 CriticalSectionScoped cs(send_critsect_);
218 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000219}
220
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000221int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000222 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000223 const int8_t payload_number, const uint32_t frequency,
224 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000225 assert(payload_name);
226 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000228 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000229 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000230
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000231 if (payload_type_map_.end() != it) {
232 // We already use this payload type.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000233 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000234 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000235
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000236 // Check if it's the same as we already have.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000237 if (RtpUtility::StringCompare(
238 payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000239 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000240 payload->typeSpecific.Audio.frequency == frequency &&
241 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000242 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000243 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000244 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000245 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000246 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000247 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000248 return 0;
249 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000250 }
251 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000252 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000253 int32_t ret_val = -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000254 RtpUtility::Payload* payload = NULL;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000255 if (audio_configured_) {
256 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
257 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000258 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000259 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
260 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000261 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000262 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000263 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000264 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000265 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000266}
267
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000268int32_t RTPSender::DeRegisterSendPayload(
269 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000270 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000271
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000272 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000273 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000274
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000275 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000276 return -1;
277 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000278 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000279 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000280 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000281 return 0;
282}
niklase@google.com470e71d2011-07-07 08:21:25 +0000283
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000284int8_t RTPSender::SendPayloadType() const {
285 CriticalSectionScoped cs(send_critsect_);
286 return payload_type_;
287}
niklase@google.com470e71d2011-07-07 08:21:25 +0000288
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000289int RTPSender::SendPayloadFrequency() const {
290 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
291}
niklase@google.com470e71d2011-07-07 08:21:25 +0000292
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000293int32_t RTPSender::SetMaxPayloadLength(
294 const uint16_t max_payload_length,
295 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000296 // Sanity check.
297 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000298 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000299 return -1;
300 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000301 CriticalSectionScoped cs(send_critsect_);
302 max_payload_length_ = max_payload_length;
303 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000304 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000305}
306
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000307uint16_t RTPSender::MaxDataPayloadLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000308 int rtx;
309 {
310 CriticalSectionScoped rtx_lock(send_critsect_);
311 rtx = rtx_;
312 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000313 if (audio_configured_) {
314 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000315 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000316 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
317 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000318 - ((rtx) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000319 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000320}
321
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000322uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000323 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000324}
325
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000326uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000327
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000328void RTPSender::SetRTXStatus(int mode) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000329 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000330 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000331}
332
333void RTPSender::SetRtxSsrc(uint32_t ssrc) {
334 CriticalSectionScoped cs(send_critsect_);
335 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000336}
337
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000338uint32_t RTPSender::RtxSsrc() const {
339 CriticalSectionScoped cs(send_critsect_);
340 return ssrc_rtx_;
341}
342
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000343void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000344 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000345 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000346 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000347 *ssrc = ssrc_rtx_;
348 *payload_type = payload_type_rtx_;
349}
350
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000351void RTPSender::SetRtxPayloadType(int payload_type) {
352 CriticalSectionScoped cs(send_critsect_);
353 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000354}
355
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000356int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
357 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000358 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000359
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000360 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000361 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000362 return -1;
363 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000364 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000365 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000366 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000367 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000368 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000369 // And it's a match...
370 return 0;
371 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000372 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000373 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000374 if (payload_type_ == payload_type) {
375 if (!audio_configured_) {
376 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000377 }
378 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000379 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000380 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000381 payload_type_map_.find(payload_type);
382 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000383 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000384 return -1;
385 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000386 payload_type_ = payload_type;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000387 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000388 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000389 if (!payload->audio && !audio_configured_) {
390 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
391 *video_type = payload->typeSpecific.Video.videoCodecType;
392 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000393 }
394 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000395}
396
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000397int32_t RTPSender::SendOutgoingData(
398 const FrameType frame_type, const int8_t payload_type,
399 const uint32_t capture_timestamp, int64_t capture_time_ms,
400 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000401 const RTPFragmentationHeader *fragmentation,
402 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000403 uint32_t ssrc;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000404 {
405 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000406 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000407 ssrc = ssrc_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000408 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000409 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000410 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000411 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000412 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000413 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000414 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000415 return -1;
416 }
417
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000418 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000419 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000420 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
421 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000422 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000423 frame_type == kFrameEmpty);
424
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000425 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
426 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000427 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000428 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
429 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000430 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000431
432 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000433 if (paced_sender_->Enabled()) {
434 // Padding is driven by the pacer and not by the encoder.
435 return 0;
436 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000437 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000438 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000439 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000440 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
441 capture_timestamp, capture_time_ms,
442 payload_data, payload_size,
443 fragmentation, codec_info,
444 rtp_type_hdr);
445
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000446 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000447
448 CriticalSectionScoped cs(statistics_crit_.get());
449 uint32_t frame_count = ++frame_counts_[frame_type];
450 if (frame_count_observer_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000451 frame_count_observer_->FrameCountUpdated(frame_type, frame_count, ssrc);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000452 }
453
454 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000455}
456
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000457int RTPSender::SendRedundantPayloads(int payload_type, int bytes_to_send) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000458 uint8_t buffer[IP_PACKET_SIZE];
459 int bytes_left = bytes_to_send;
460 while (bytes_left > 0) {
461 uint16_t length = bytes_left;
462 int64_t capture_time_ms;
463 if (!packet_history_.GetBestFittingPacket(buffer, &length,
464 &capture_time_ms)) {
465 break;
466 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000467 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000468 return -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000469 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000470 RTPHeader rtp_header;
471 rtp_parser.Parse(rtp_header);
472 bytes_left -= length - rtp_header.headerLength;
473 }
474 return bytes_to_send - bytes_left;
475}
476
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000477bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000478 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000479 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000480 // Current bitrate since last estimate(1 second) averaged with the
481 // estimate since then, to get the most up to date bitrate.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000482 uint32_t current_bitrate = bitrate_sent_.BitrateNow();
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000483 uint32_t target_bitrate = GetTargetBitrate();
484 int bitrate_diff = target_bitrate - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000485 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000486 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000487 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000488 int bytes = 0;
489 if (current_bitrate == 0) {
490 // Start up phase. Send one 33.3 ms batch to start with.
491 bytes = (bitrate_diff / 8) / 30;
492 } else {
493 bytes = (bitrate_diff / 8);
494 // Cap at 200 ms of target send data.
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000495 int bytes_cap = target_bitrate / 1000 * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000496 if (bytes > bytes_cap) {
497 bytes = bytes_cap;
498 }
499 }
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000500 uint32_t timestamp;
501 {
502 CriticalSectionScoped cs(send_critsect_);
503 // Add the random RTP timestamp offset and store the capture time for
504 // later calculation of the send time offset.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000505 timestamp = start_timestamp_ + capture_timestamp;
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000506 timestamp_ = timestamp;
507 capture_time_ms_ = capture_time_ms;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000508 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000509 }
510 int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
511 bytes, kDontRetransmit, false, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000512 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
513 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000514}
515
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000516int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
517 int32_t bytes) {
518 int padding_bytes_in_packet = kMaxPaddingLength;
519 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000520 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000521 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000522 packet[0] |= 0x20; // Set padding bit.
523 int32_t *data =
524 reinterpret_cast<int32_t *>(&(packet[header_length]));
525
526 // Fill data buffer with random data.
527 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
528 data[j] = rand(); // NOLINT
529 }
530 // Set number of padding bytes in the last byte of the packet.
531 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
532 return padding_bytes_in_packet;
533}
534
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000535int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
536 int64_t capture_time_ms, int32_t bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000537 StorageType store, bool force_full_size_packets,
538 bool only_pad_after_markerbit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000539 // Drop this packet if we're not sending media packets.
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000540 if (!SendingMedia()) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000541 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000542 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000543 int padding_bytes_in_packet = 0;
544 int bytes_sent = 0;
545 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000546 // Always send full padding packets.
547 if (force_full_size_packets && bytes < kMaxPaddingLength)
548 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000549 if (bytes < kMaxPaddingLength) {
550 if (force_full_size_packets) {
551 bytes = kMaxPaddingLength;
552 } else {
553 // Round to the nearest multiple of 32.
554 bytes = (bytes + 16) & 0xffe0;
555 }
556 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000557 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000558 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000559 break;
560 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000561 uint32_t ssrc;
562 uint16_t sequence_number;
563 {
564 CriticalSectionScoped cs(send_critsect_);
565 // Only send padding packets following the last packet of a frame,
566 // indicated by the marker bit.
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000567 if (only_pad_after_markerbit && !last_packet_marker_bit_)
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000568 return bytes_sent;
569 if (rtx_ == kRtxOff) {
570 ssrc = ssrc_;
571 sequence_number = sequence_number_;
572 ++sequence_number_;
573 } else {
574 ssrc = ssrc_rtx_;
575 sequence_number = sequence_number_rtx_;
576 ++sequence_number_rtx_;
577 }
578 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000579
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000580 uint8_t padding_packet[IP_PACKET_SIZE];
581 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
582 false, timestamp, sequence_number, NULL,
583 0);
584 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
585 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000586 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
587 header_length, capture_time_ms, store,
588 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000589 // Error sending the packet.
590 break;
591 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000592 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000593 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000594 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000595}
596
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000597void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000598 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000599 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000600}
601
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000602bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000603 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000604}
niklase@google.com470e71d2011-07-07 08:21:25 +0000605
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000606int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
607 uint16_t length = IP_PACKET_SIZE;
608 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000609 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000610 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
611 data_buffer, &length,
612 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000613 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000614 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000615 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000616
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000617 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000618 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000619 RTPHeader header;
620 if (!rtp_parser.Parse(header)) {
621 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000622 return -1;
623 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000624 // Convert from TickTime to Clock since capture_time_ms is based on
625 // TickTime.
626 // TODO(holmer): Remove this conversion when we remove the use of TickTime.
627 int64_t clock_delta_ms = clock_->TimeInMilliseconds() -
628 TickTime::MillisecondTimestamp();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000629 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000630 header.ssrc,
631 header.sequenceNumber,
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000632 capture_time_ms + clock_delta_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000633 length - header.headerLength,
634 true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000635 // We can't send the packet right now.
636 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000637 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000638 }
639 }
640
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000641 CriticalSectionScoped lock(send_critsect_);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000642 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org16395222014-03-19 19:34:07 +0000643 (rtx_ & kRtxRetransmitted) > 0, true) ?
644 length : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000645}
646
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000647bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
648 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000649 if (transport_) {
650 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000651 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000652 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
653 "size", size, "sent", bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000654 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000655 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000656 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000657 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000658 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000659 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000660}
661
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000662int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000663 if (!video_)
664 return -1;
665 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000666}
667
668int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000669 if (!video_)
670 return -1;
671 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000672}
673
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000674void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000675 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000676 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000677 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
678 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000679 const int64_t now = clock_->TimeInMilliseconds();
680 uint32_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000681 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000682
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000683 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000684 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000685 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000686 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000687 return;
688 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000689
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000690 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
691 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000692 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000693 if (bytes_sent > 0) {
694 bytes_re_sent += bytes_sent;
695 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000696 // The packet has previously been resent.
697 // Try resending next packet in the list.
698 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000699 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000700 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000701 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
702 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000703 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000704 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000705 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000706 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000707 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000708 uint32_t target_bytes =
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000709 (static_cast<uint32_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000710 if (bytes_re_sent > target_bytes) {
711 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000712 }
713 }
714 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000715 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000716 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000717 UpdateNACKBitRate(bytes_re_sent, now);
718 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000719 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000720}
721
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000722bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
723 uint32_t num = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000724 int byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000725 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000726 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000727
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000728 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000729
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000730 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000731 return true;
732 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000733 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000734 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000735 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000736 break;
737 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000738 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000739 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000740 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000741 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000742 if (num == NACK_BYTECOUNT_SIZE) {
743 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000744 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000745 if (nack_byte_count_times_[num - 1] <= now) {
746 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000747 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000748 }
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000749 return (byte_count * 8) <
750 static_cast<int>(target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000751}
752
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000753void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
754 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000755 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000756
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000757 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000758 if (bytes > 0) {
759 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000760 // Add padding length.
761 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000762 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000763 if (nack_byte_count_times_[0] == 0) {
764 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000765 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000766 // Shift.
767 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
768 nack_byte_count_[i + 1] = nack_byte_count_[i];
769 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000770 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000771 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000772 nack_byte_count_[0] = bytes;
773 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000774 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000775 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000776}
777
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000778// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000779bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000780 int64_t capture_time_ms,
781 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000782 uint16_t length = IP_PACKET_SIZE;
783 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000784 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000785
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000786 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
787 0,
788 retransmission,
789 data_buffer,
790 &length,
791 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000792 // Packet cannot be found. Allow sending to continue.
793 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000794 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000795 if (!retransmission && capture_time_ms > 0) {
796 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
797 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000798 int rtx;
799 {
800 CriticalSectionScoped lock(send_critsect_);
801 rtx = rtx_;
802 }
803 return PrepareAndSendPacket(data_buffer,
804 length,
805 capture_time_ms,
806 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000807 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000808}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000809
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000810bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
811 uint16_t length,
812 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000813 bool send_over_rtx,
814 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000815 uint8_t *buffer_to_send_ptr = buffer;
816
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000817 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000818 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000819 rtp_parser.Parse(rtp_header);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000820 TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000821 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000822 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000823
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000824 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000825 if (send_over_rtx) {
826 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000827 buffer_to_send_ptr = data_buffer_rtx;
828 }
829
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000830 int64_t now_ms = clock_->TimeInMilliseconds();
831 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000832 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
833 diff_ms);
834 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000835 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000836 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
837 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000838 return ret;
839}
840
841void RTPSender::UpdateRtpStats(const uint8_t* buffer,
842 uint32_t size,
843 const RTPHeader& header,
844 bool is_rtx,
845 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000846 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000847 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000848 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000849
850 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000851 if (is_rtx) {
852 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000853 } else {
854 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000855 }
856
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000857 bitrate_sent_.Update(size);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000858 ++counters->packets;
859 if (IsFecPacket(buffer, header)) {
860 ++counters->fec_packets;
861 }
862
863 if (is_retransmit) {
864 ++counters->retransmitted_packets;
865 } else {
866 counters->bytes += size - (header.headerLength + header.paddingLength);
867 counters->header_bytes += header.headerLength;
868 counters->padding_bytes += header.paddingLength;
869 }
870
871 if (rtp_stats_callback_) {
872 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
873 }
874}
875
876bool RTPSender::IsFecPacket(const uint8_t* buffer,
877 const RTPHeader& header) const {
878 if (!video_) {
879 return false;
880 }
881 bool fec_enabled;
882 uint8_t pt_red;
883 uint8_t pt_fec;
884 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
885 return fec_enabled &&
886 header.payloadType == pt_red &&
887 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000888}
889
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000890int RTPSender::TimeToSendPadding(int bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000891 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000892 int64_t capture_time_ms;
893 uint32_t timestamp;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000894 int rtx;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000895 {
896 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000897 if (!sending_media_) {
898 return 0;
899 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000900 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_ :
901 payload_type_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000902 timestamp = timestamp_;
903 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000904 if (last_timestamp_time_ms_ > 0) {
905 timestamp +=
906 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
907 capture_time_ms +=
908 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
909 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000910 rtx = rtx_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000911 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000912 int bytes_sent = 0;
913 if ((rtx & kRtxRedundantPayloads) != 0)
914 bytes_sent = SendRedundantPayloads(payload_type, bytes);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000915 bytes -= bytes_sent;
916 if (bytes > 0) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000917 int padding_sent = SendPadData(payload_type,
918 timestamp,
919 capture_time_ms,
920 bytes,
921 kDontStore,
922 true,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000923 rtx == kRtxOff);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000924 bytes_sent += padding_sent;
925 }
926 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000927}
928
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000929// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000930int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000931 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000932 int64_t capture_time_ms, StorageType storage,
933 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000934 RtpUtility::RtpHeaderParser rtp_parser(buffer,
935 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000936 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000937 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000938
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000939 int64_t now_ms = clock_->TimeInMilliseconds();
940
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000941 // |capture_time_ms| <= 0 is considered invalid.
942 // TODO(holmer): This should be changed all over Video Engine so that negative
943 // time is consider invalid, while 0 is considered a valid time.
944 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000945 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000946 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000947 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000948
949 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
950 rtp_header, now_ms);
951
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000952 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000953 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
954 max_payload_length_, capture_time_ms,
955 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000956 return -1;
957 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000958
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000959 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000960 int64_t clock_delta_ms = clock_->TimeInMilliseconds() -
961 TickTime::MillisecondTimestamp();
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000962 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000963 rtp_header.sequenceNumber,
964 capture_time_ms + clock_delta_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000965 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000966 // We can't send the packet right now.
967 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000968 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000969 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000970 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000971 if (capture_time_ms > 0) {
972 UpdateDelayStatistics(capture_time_ms, now_ms);
973 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000974 uint32_t length = payload_length + rtp_header_length;
975 if (!SendPacketToNetwork(buffer, length))
976 return -1;
977 UpdateRtpStats(buffer, length, rtp_header, false, false);
978 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000979}
980
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000981void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
982 CriticalSectionScoped cs(statistics_crit_.get());
983 send_delays_[now_ms] = now_ms - capture_time_ms;
984 send_delays_.erase(send_delays_.begin(),
985 send_delays_.lower_bound(now_ms - kSendSideDelayWindowMs));
986}
987
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000988void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000989 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000990 bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000991 nack_bitrate_.Process();
992 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000993 return;
994 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000995 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000996}
997
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000998uint16_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000999 CriticalSectionScoped lock(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001000 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001001 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001002 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001003 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001004 rtp_header_length += RtpHeaderExtensionTotalLength();
1005 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001006}
1007
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001008uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001009 CriticalSectionScoped cs(send_critsect_);
1010 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001011}
1012
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001013void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001014 uint32_t ssrc;
1015 uint32_t ssrc_rtx;
1016 {
1017 CriticalSectionScoped ssrc_lock(send_critsect_);
1018 ssrc = ssrc_;
1019 ssrc_rtx = ssrc_rtx_;
1020 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001021 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001022 rtp_stats_ = StreamDataCounters();
1023 rtx_rtp_stats_ = StreamDataCounters();
1024 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001025 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
1026 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001027 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001028}
1029
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001030uint32_t RTPSender::Packets() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001031 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001032 return rtp_stats_.packets + rtx_rtp_stats_.packets;
niklase@google.com470e71d2011-07-07 08:21:25 +00001033}
1034
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001035// Number of sent RTP bytes.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001036uint32_t RTPSender::Bytes() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001037 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001038 return rtp_stats_.bytes + rtx_rtp_stats_.bytes;
niklase@google.com470e71d2011-07-07 08:21:25 +00001039}
1040
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001041int RTPSender::CreateRTPHeader(
1042 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
1043 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
1044 uint8_t num_csrcs) const {
1045 header[0] = 0x80; // version 2.
1046 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001047 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001048 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001049 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001050 RtpUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1051 RtpUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1052 RtpUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001053 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001054
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001055 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001056 if (num_csrcs > 0) {
1057 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001058 // error
1059 assert(false);
1060 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001061 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001062 uint8_t *ptr = &header[rtp_header_length];
1063 for (int i = 0; i < num_csrcs; ++i) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001064 RtpUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001065 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001066 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001067 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001068
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001069 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001070 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001071 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001072
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001073 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1074 if (len > 0) {
1075 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001076 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001077 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001078 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001079}
1080
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001081int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
1082 const int8_t payload_type,
1083 const bool marker_bit,
1084 const uint32_t capture_timestamp,
1085 int64_t capture_time_ms,
1086 const bool timestamp_provided,
1087 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001088 assert(payload_type >= 0);
1089 CriticalSectionScoped cs(send_critsect_);
1090
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001091 if (timestamp_provided) {
1092 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001093 } else {
1094 // Make a unique time stamp.
1095 // We can't inc by the actual time, since then we increase the risk of back
1096 // timing.
1097 timestamp_++;
1098 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001099 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001100 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001101 capture_time_ms_ = capture_time_ms;
1102 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001103 int csrcs_length = 0;
1104 if (include_csrcs_)
1105 csrcs_length = num_csrcs_;
1106 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1107 timestamp_, sequence_number, csrcs_, csrcs_length);
1108}
1109
1110uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001111 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001112 return 0;
1113 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001114 // RTP header extension, RFC 3550.
1115 // 0 1 2 3
1116 // 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
1117 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1118 // | defined by profile | length |
1119 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1120 // | header extension |
1121 // | .... |
1122 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001123 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001124 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001125
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001126 // Add extension ID (0xBEDE).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001127 RtpUtility::AssignUWord16ToBuffer(data_buffer, kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001128
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001129 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001130 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001131
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001132 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001133 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001134 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001135 switch (type) {
1136 case kRtpExtensionTransmissionTimeOffset:
1137 block_length = BuildTransmissionTimeOffsetExtension(
1138 data_buffer + kHeaderLength + total_block_length);
1139 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001140 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001141 block_length = BuildAudioLevelExtension(
1142 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001143 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001144 case kRtpExtensionAbsoluteSendTime:
1145 block_length = BuildAbsoluteSendTimeExtension(
1146 data_buffer + kHeaderLength + total_block_length);
1147 break;
1148 default:
1149 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001150 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001151 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001152 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001153 }
1154 if (total_block_length == 0) {
1155 // No extension added.
1156 return 0;
1157 }
1158 // Set header length (in number of Word32, header excluded).
1159 assert(total_block_length % 4 == 0);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001160 RtpUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
1161 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001162 // Total added length.
1163 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001164}
1165
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001166uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1167 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001168 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1169 //
1170 // The transmission time is signaled to the receiver in-band using the
1171 // general mechanism for RTP header extensions [RFC5285]. The payload
1172 // of this extension (the transmitted value) is a 24-bit signed integer.
1173 // When added to the RTP timestamp of the packet, it represents the
1174 // "effective" RTP transmission time of the packet, on the RTP
1175 // timescale.
1176 //
1177 // The form of the transmission offset extension block:
1178 //
1179 // 0 1 2 3
1180 // 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
1181 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1182 // | ID | len=2 | transmission offset |
1183 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001184
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001185 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001186 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001187 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1188 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001189 // Not registered.
1190 return 0;
1191 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001192 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001193 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001194 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001195 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos,
1196 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001197 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001198 assert(pos == kTransmissionTimeOffsetLength);
1199 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001200}
1201
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001202uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1203 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1204 //
1205 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1206 //
1207 // The form of the audio level extension block:
1208 //
1209 // 0 1 2 3
1210 // 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
1211 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1212 // | ID | len=0 |V| level | 0x00 | 0x00 |
1213 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1214 //
1215 // Note that we always include 2 pad bytes, which will result in legal and
1216 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1217 // are implemented. Right now the pad bytes would anyway be required at end
1218 // of the extension block, so it makes no difference.
1219
1220 // Get id defined by user.
1221 uint8_t id;
1222 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1223 // Not registered.
1224 return 0;
1225 }
1226 size_t pos = 0;
1227 const uint8_t len = 0;
1228 data_buffer[pos++] = (id << 4) + len;
1229 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1230 data_buffer[pos++] = 0; // Padding.
1231 data_buffer[pos++] = 0; // Padding.
1232 // kAudioLevelLength is including pad bytes.
1233 assert(pos == kAudioLevelLength);
1234 return kAudioLevelLength;
1235}
1236
1237uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001238 // Absolute send time in RTP streams.
1239 //
1240 // The absolute send time is signaled to the receiver in-band using the
1241 // general mechanism for RTP header extensions [RFC5285]. The payload
1242 // of this extension (the transmitted value) is a 24-bit unsigned integer
1243 // containing the sender's current time in seconds as a fixed point number
1244 // with 18 bits fractional part.
1245 //
1246 // The form of the absolute send time extension block:
1247 //
1248 // 0 1 2 3
1249 // 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
1250 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1251 // | ID | len=2 | absolute send time |
1252 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1253
1254 // Get id defined by user.
1255 uint8_t id;
1256 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1257 &id) != 0) {
1258 // Not registered.
1259 return 0;
1260 }
1261 size_t pos = 0;
1262 const uint8_t len = 2;
1263 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001264 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos, absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001265 pos += 3;
1266 assert(pos == kAbsoluteSendTimeLength);
1267 return kAbsoluteSendTimeLength;
1268}
1269
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001270void RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001271 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001272 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001273 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001274 // Get id.
1275 uint8_t id = 0;
1276 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1277 &id) != 0) {
1278 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001279 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001280 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001281 // Get length until start of header extension block.
1282 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001283 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001284 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001285 if (extension_block_pos < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001286 LOG(LS_WARNING)
1287 << "Failed to update transmission time offset, not registered.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001288 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001289 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001290 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001291 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001292 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001293 block_pos + kTransmissionTimeOffsetLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001294 LOG(LS_WARNING)
1295 << "Failed to update transmission time offset, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001296 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001297 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001298 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001299 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1300 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001301 LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1302 "extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001303 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001304 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001305 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001306 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001307 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001308 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001309 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001310 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001311 // Update transmission offset field (converting to a 90 kHz timestamp).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001312 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1313 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001314}
1315
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001316bool RTPSender::UpdateAudioLevel(uint8_t *rtp_packet,
1317 const uint16_t rtp_packet_length,
1318 const RTPHeader &rtp_header,
1319 const bool is_voiced,
1320 const uint8_t dBov) const {
1321 CriticalSectionScoped cs(send_critsect_);
1322
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001323 // Get id.
1324 uint8_t id = 0;
1325 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1326 // Not registered.
1327 return false;
1328 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001329 // Get length until start of header extension block.
1330 int extension_block_pos =
1331 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1332 kRtpExtensionAudioLevel);
1333 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001334 // The feature is not enabled.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001335 return false;
1336 }
1337 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1338 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1339 rtp_header.headerLength < block_pos + kAudioLevelLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001340 LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001341 return false;
1342 }
1343 // Verify that header contains extension.
1344 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1345 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001346 LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001347 return false;
1348 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001349 // Verify first byte in block.
1350 const uint8_t first_block_byte = (id << 4) + 0;
1351 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001352 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001353 return false;
1354 }
1355 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1356 return true;
1357}
1358
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001359void RTPSender::UpdateAbsoluteSendTime(
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001360 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001361 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001362 CriticalSectionScoped cs(send_critsect_);
1363
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001364 // Get id.
1365 uint8_t id = 0;
1366 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1367 &id) != 0) {
1368 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001369 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001370 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001371 // Get length until start of header extension block.
1372 int extension_block_pos =
1373 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1374 kRtpExtensionAbsoluteSendTime);
1375 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001376 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001377 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001378 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001379 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001380 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001381 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001382 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001383 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001384 }
1385 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001386 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1387 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001388 LOG(LS_WARNING)
1389 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001390 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001391 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001392 // Verify first byte in block.
1393 const uint8_t first_block_byte = (id << 4) + 2;
1394 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001395 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001396 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001397 }
1398 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1399 // fractional part).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001400 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1401 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001402}
1403
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001404void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001405 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001406 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001407 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001408
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001409 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001410 SetStartTimestamp(RTPtime, false);
1411 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001412 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001413 if (!ssrc_forced_) {
1414 // Generate a new SSRC.
1415 ssrc_db_.ReturnSSRC(ssrc_);
1416 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001417 }
1418 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001419 if (!sequence_number_forced_ && !ssrc_forced_) {
1420 // Generate a new sequence number.
1421 sequence_number_ =
1422 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001423 }
1424 }
1425}
1426
1427void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001428 CriticalSectionScoped cs(send_critsect_);
1429 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001430}
1431
1432bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001433 CriticalSectionScoped cs(send_critsect_);
1434 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001435}
1436
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001437uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001438 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001439 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001440}
1441
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001442void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001443 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001444 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001445 start_timestamp_forced_ = true;
1446 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001447 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001448 if (!start_timestamp_forced_) {
1449 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001450 }
1451 }
1452}
1453
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001454uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001455 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001456 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001457}
1458
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001459uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001460 // If configured via API, return 0.
1461 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001462
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001463 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001464 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001465 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001466 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1467 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001468}
1469
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001470void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001471 // This is configured via the API.
1472 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001473
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001474 if (ssrc_ == ssrc && ssrc_forced_) {
1475 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001476 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001477 ssrc_forced_ = true;
1478 ssrc_db_.ReturnSSRC(ssrc_);
1479 ssrc_db_.RegisterSSRC(ssrc);
1480 ssrc_ = ssrc;
1481 if (!sequence_number_forced_) {
1482 sequence_number_ =
1483 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001484 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001485}
1486
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001487uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001488 CriticalSectionScoped cs(send_critsect_);
1489 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001490}
1491
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001492void RTPSender::SetCSRCStatus(const bool include) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001493 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001494 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001495}
1496
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001497void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1498 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001499 assert(arr_length <= kRtpCsrcSize);
1500 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001501
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001502 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001503 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001504 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001505 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001506}
1507
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001508int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001509 assert(arr_of_csrc);
1510 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001511 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1512 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001513 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001514 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001515}
1516
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001517void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001518 CriticalSectionScoped cs(send_critsect_);
1519 sequence_number_forced_ = true;
1520 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001521}
1522
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001523uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001524 CriticalSectionScoped cs(send_critsect_);
1525 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001526}
1527
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001528// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001529int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1530 const uint16_t time_ms,
1531 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001532 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001533 return -1;
1534 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001535 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001536}
1537
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001538bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001539 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001540 return false;
1541 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001542 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001543}
1544
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001545int32_t RTPSender::SetAudioPacketSize(
1546 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001547 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001548 return -1;
1549 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001550 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001551}
1552
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001553int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001554 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001555}
1556
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001557int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001558 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001559 return -1;
1560 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001561 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001562}
1563
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001564int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001565 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001566 return -1;
1567 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001568 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001569}
1570
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001571// Video
1572VideoCodecInformation *RTPSender::CodecInformationVideo() {
1573 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001574 return NULL;
1575 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001576 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001577}
1578
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001579RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001580 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001581 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001582}
1583
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001584uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001585 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001586 return 0;
1587 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001588 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001589}
1590
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001591int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001592 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001593 return -1;
1594 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001595 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001596}
1597
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001598int32_t RTPSender::SetGenericFECStatus(
1599 const bool enable, const uint8_t payload_type_red,
1600 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001601 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001602 return -1;
1603 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001604 return video_->SetGenericFECStatus(enable, payload_type_red,
1605 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001606}
1607
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001608int32_t RTPSender::GenericFECStatus(
1609 bool *enable, uint8_t *payload_type_red,
1610 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001611 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001612 return -1;
1613 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001614 return video_->GenericFECStatus(
1615 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001616}
1617
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001618int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001619 const FecProtectionParams *delta_params,
1620 const FecProtectionParams *key_params) {
1621 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001622 return -1;
1623 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001624 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001625}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001626
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001627void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1628 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001629 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001630 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001631 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001632 RtpUtility::RtpHeaderParser rtp_parser(
1633 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001634
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001635 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001636 rtp_parser.Parse(rtp_header);
1637
1638 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001639 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001640
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001641 // Replace payload type, if a specific type is set for RTX.
1642 if (payload_type_rtx_ != -1) {
1643 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001644 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001645 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1646 }
1647
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001648 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001649 uint8_t *ptr = data_buffer_rtx + 2;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001650 RtpUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001651
1652 // Replace SSRC.
1653 ptr += 6;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001654 RtpUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001655
1656 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001657 ptr = data_buffer_rtx + rtp_header.headerLength;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001658 RtpUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001659 ptr += 2;
1660
1661 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001662 memcpy(ptr, buffer + rtp_header.headerLength,
1663 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001664 *length += 2;
1665}
1666
sprang@webrtc.org71f055f2013-12-04 15:09:27 +00001667void RTPSender::RegisterFrameCountObserver(FrameCountObserver* observer) {
1668 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.org71f055f2013-12-04 15:09:27 +00001669 frame_count_observer_ = observer;
1670}
1671
1672FrameCountObserver* RTPSender::GetFrameCountObserver() const {
1673 CriticalSectionScoped cs(statistics_crit_.get());
1674 return frame_count_observer_;
1675}
1676
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001677void RTPSender::RegisterRtpStatisticsCallback(
1678 StreamDataCountersCallback* callback) {
1679 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001680 rtp_stats_callback_ = callback;
1681}
1682
1683StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1684 CriticalSectionScoped cs(statistics_crit_.get());
1685 return rtp_stats_callback_;
1686}
1687
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001688uint32_t RTPSender::BitrateSent() const { return bitrate_sent_.BitrateLast(); }
1689
1690void RTPSender::BitrateUpdated(const BitrateStatistics& stats) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001691 uint32_t ssrc;
1692 {
1693 CriticalSectionScoped ssrc_lock(send_critsect_);
1694 ssrc = ssrc_;
1695 }
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001696 if (bitrate_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001697 bitrate_callback_->Notify(stats, ssrc);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001698 }
1699}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001700
1701void RTPSender::SetRtpState(const RtpState& rtp_state) {
1702 SetStartTimestamp(rtp_state.start_timestamp, true);
1703 CriticalSectionScoped lock(send_critsect_);
1704 sequence_number_ = rtp_state.sequence_number;
1705 sequence_number_forced_ = true;
1706 timestamp_ = rtp_state.timestamp;
1707 capture_time_ms_ = rtp_state.capture_time_ms;
1708 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
1709}
1710
1711RtpState RTPSender::GetRtpState() const {
1712 CriticalSectionScoped lock(send_critsect_);
1713
1714 RtpState state;
1715 state.sequence_number = sequence_number_;
1716 state.start_timestamp = start_timestamp_;
1717 state.timestamp = timestamp_;
1718 state.capture_time_ms = capture_time_ms_;
1719 state.last_timestamp_time_ms = last_timestamp_time_ms_;
1720
1721 return state;
1722}
1723
1724void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
1725 CriticalSectionScoped lock(send_critsect_);
1726 sequence_number_rtx_ = rtp_state.sequence_number;
1727}
1728
1729RtpState RTPSender::GetRtxRtpState() const {
1730 CriticalSectionScoped lock(send_critsect_);
1731
1732 RtpState state;
1733 state.sequence_number = sequence_number_rtx_;
1734 state.start_timestamp = start_timestamp_;
1735
1736 return state;
1737}
1738
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001739} // namespace webrtc