blob: 0d465d936c7cb0f30315fe666ecef87d119df449 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000011#include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
12
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <stdlib.h> // srand
niklase@google.com470e71d2011-07-07 08:21:25 +000014
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000015#include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
16#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
17#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000018#include "webrtc/system_wrappers/interface/logging.h"
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000019#include "webrtc/system_wrappers/interface/tick_util.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000020#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
stefan@webrtc.orga8179622013-06-04 13:47:36 +000024// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
25const int kMaxPaddingLength = 224;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +000026const int kSendSideDelayWindowMs = 1000;
stefan@webrtc.orga8179622013-06-04 13:47:36 +000027
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000028namespace {
29
30const char* FrameTypeToString(const FrameType frame_type) {
31 switch (frame_type) {
32 case kFrameEmpty: return "empty";
33 case kAudioFrameSpeech: return "audio_speech";
34 case kAudioFrameCN: return "audio_cn";
35 case kVideoFrameKey: return "video_key";
36 case kVideoFrameDelta: return "video_delta";
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000037 }
38 return "";
39}
40
41} // namespace
42
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000043RTPSender::RTPSender(const int32_t id,
44 const bool audio,
45 Clock* clock,
46 Transport* transport,
47 RtpAudioFeedback* audio_feedback,
andresp@webrtc.orgd11bec42014-07-08 14:32:58 +000048 PacedSender* paced_sender,
andresp@webrtc.org8f151212014-07-10 09:39:23 +000049 BitrateStatisticsObserver* bitrate_callback,
stefan@webrtc.org168f23f2014-07-11 13:44:02 +000050 FrameCountObserver* frame_count_observer,
51 SendSideDelayObserver* send_side_delay_observer)
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000052 : clock_(clock),
53 bitrate_sent_(clock, this),
54 id_(id),
55 audio_configured_(audio),
56 audio_(NULL),
57 video_(NULL),
58 paced_sender_(paced_sender),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000059 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000060 transport_(transport),
61 sending_media_(true), // Default to sending media.
62 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000063 packet_over_head_(28),
64 payload_type_(-1),
65 payload_type_map_(),
66 rtp_header_extension_map_(),
67 transmission_time_offset_(0),
68 absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000069 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000070 nack_byte_count_times_(),
71 nack_byte_count_(),
72 nack_bitrate_(clock, NULL),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000073 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000074 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +000075 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000076 rtp_stats_callback_(NULL),
andresp@webrtc.orgd11bec42014-07-08 14:32:58 +000077 bitrate_callback_(bitrate_callback),
andresp@webrtc.org8f151212014-07-10 09:39:23 +000078 frame_count_observer_(frame_count_observer),
stefan@webrtc.org168f23f2014-07-11 13:44:02 +000079 send_side_delay_observer_(send_side_delay_observer),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +000080 // RTP variables
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +000081 start_timestamp_forced_(false),
82 start_timestamp_(0),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000083 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
84 remote_ssrc_(0),
85 sequence_number_forced_(false),
86 ssrc_forced_(false),
87 timestamp_(0),
88 capture_time_ms_(0),
89 last_timestamp_time_ms_(0),
90 last_packet_marker_bit_(false),
91 num_csrcs_(0),
92 csrcs_(),
93 include_csrcs_(true),
94 rtx_(kRtxOff),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +000095 payload_type_rtx_(-1),
96 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +000097 target_bitrate_(0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000098 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
99 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000100 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000101 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000102 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000103 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000104 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
105 // Random start, 16 bits. Can't be 0.
106 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
107 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000109 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000110 audio_ = new RTPSenderAudio(id, clock_, this);
111 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000112 } else {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000113 video_ = new RTPSenderVideo(clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000114 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000115}
116
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000117RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000118 if (remote_ssrc_ != 0) {
119 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000120 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000121 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000123 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000124 delete send_critsect_;
125 while (!payload_type_map_.empty()) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000126 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000127 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000128 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000129 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000130 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000131 delete audio_;
132 delete video_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000133}
niklase@google.com470e71d2011-07-07 08:21:25 +0000134
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000135void RTPSender::SetTargetBitrate(uint32_t bitrate) {
136 CriticalSectionScoped cs(target_bitrate_critsect_.get());
137 target_bitrate_ = bitrate;
138}
139
140uint32_t RTPSender::GetTargetBitrate() {
141 CriticalSectionScoped cs(target_bitrate_critsect_.get());
142 return target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000143}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000144
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000145uint16_t RTPSender::ActualSendBitrateKbit() const {
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000146 return (uint16_t)(bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000147}
148
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000149uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000150 if (video_) {
151 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000152 }
153 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000154}
155
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000156uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000157 if (video_) {
158 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000159 }
160 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000161}
162
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000163uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000164 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000165}
166
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000167bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
168 int* max_send_delay_ms) const {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000169 CriticalSectionScoped lock(statistics_crit_.get());
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000170 SendDelayMap::const_iterator it = send_delays_.upper_bound(
171 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000172 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000173 return false;
174 int num_delays = 0;
175 for (; it != send_delays_.end(); ++it) {
176 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
177 *avg_send_delay_ms += it->second;
178 ++num_delays;
179 }
180 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
181 return true;
182}
183
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000184int32_t RTPSender::SetTransmissionTimeOffset(
185 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000186 if (transmission_time_offset > (0x800000 - 1) ||
187 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000188 return -1;
189 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000190 CriticalSectionScoped cs(send_critsect_);
191 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000192 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000193}
194
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000195int32_t RTPSender::SetAbsoluteSendTime(
196 const uint32_t absolute_send_time) {
197 if (absolute_send_time > 0xffffff) { // UWord24.
198 return -1;
199 }
200 CriticalSectionScoped cs(send_critsect_);
201 absolute_send_time_ = absolute_send_time;
202 return 0;
203}
204
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000205int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
206 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000207 CriticalSectionScoped cs(send_critsect_);
208 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000209}
210
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000211int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000212 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000213 CriticalSectionScoped cs(send_critsect_);
214 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000215}
216
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000217uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000218 CriticalSectionScoped cs(send_critsect_);
219 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000220}
221
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000222int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000223 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000224 const int8_t payload_number, const uint32_t frequency,
225 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000226 assert(payload_name);
227 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000228
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000229 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000230 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000231
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000232 if (payload_type_map_.end() != it) {
233 // We already use this payload type.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000234 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000235 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000237 // Check if it's the same as we already have.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000238 if (RtpUtility::StringCompare(
239 payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000240 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000241 payload->typeSpecific.Audio.frequency == frequency &&
242 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000243 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000244 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000245 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000246 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000247 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000248 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000249 return 0;
250 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000251 }
252 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000253 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000254 int32_t ret_val = -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000255 RtpUtility::Payload* payload = NULL;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000256 if (audio_configured_) {
257 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
258 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000259 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000260 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
261 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000262 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000263 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000264 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000265 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000266 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000267}
268
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000269int32_t RTPSender::DeRegisterSendPayload(
270 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000271 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000272
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000273 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000274 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000275
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000276 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000277 return -1;
278 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000279 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000280 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000281 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000282 return 0;
283}
niklase@google.com470e71d2011-07-07 08:21:25 +0000284
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000285int8_t RTPSender::SendPayloadType() const {
286 CriticalSectionScoped cs(send_critsect_);
287 return payload_type_;
288}
niklase@google.com470e71d2011-07-07 08:21:25 +0000289
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000290int RTPSender::SendPayloadFrequency() const {
291 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
292}
niklase@google.com470e71d2011-07-07 08:21:25 +0000293
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000294int32_t RTPSender::SetMaxPayloadLength(
295 const uint16_t max_payload_length,
296 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000297 // Sanity check.
298 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000299 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000300 return -1;
301 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000302 CriticalSectionScoped cs(send_critsect_);
303 max_payload_length_ = max_payload_length;
304 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000305 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000306}
307
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000308uint16_t RTPSender::MaxDataPayloadLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000309 int rtx;
310 {
311 CriticalSectionScoped rtx_lock(send_critsect_);
312 rtx = rtx_;
313 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000314 if (audio_configured_) {
315 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000316 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000317 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
318 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000319 - ((rtx) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000320 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000321}
322
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000323uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000324 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000325}
326
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000327uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000328
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000329void RTPSender::SetRTXStatus(int mode) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000330 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000331 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000332}
333
334void RTPSender::SetRtxSsrc(uint32_t ssrc) {
335 CriticalSectionScoped cs(send_critsect_);
336 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000337}
338
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000339uint32_t RTPSender::RtxSsrc() const {
340 CriticalSectionScoped cs(send_critsect_);
341 return ssrc_rtx_;
342}
343
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000344void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000345 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000346 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000347 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000348 *ssrc = ssrc_rtx_;
349 *payload_type = payload_type_rtx_;
350}
351
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000352void RTPSender::SetRtxPayloadType(int payload_type) {
353 CriticalSectionScoped cs(send_critsect_);
354 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000355}
356
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000357int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
358 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000359 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000360
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000361 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000362 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000363 return -1;
364 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000365 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000366 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000367 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000368 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000369 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000370 // And it's a match...
371 return 0;
372 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000373 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000374 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000375 if (payload_type_ == payload_type) {
376 if (!audio_configured_) {
377 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000378 }
379 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000380 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000381 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000382 payload_type_map_.find(payload_type);
383 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000384 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000385 return -1;
386 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000387 payload_type_ = payload_type;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000388 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000389 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000390 if (!payload->audio && !audio_configured_) {
391 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
392 *video_type = payload->typeSpecific.Video.videoCodecType;
393 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000394 }
395 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000396}
397
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000398int32_t RTPSender::SendOutgoingData(
399 const FrameType frame_type, const int8_t payload_type,
400 const uint32_t capture_timestamp, int64_t capture_time_ms,
401 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000402 const RTPFragmentationHeader *fragmentation,
403 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000404 uint32_t ssrc;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000405 {
406 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000407 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000408 ssrc = ssrc_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000409 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000410 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000411 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000412 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000413 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000414 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000415 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000416 return -1;
417 }
418
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000419 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000420 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000421 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
422 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000423 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000424 frame_type == kFrameEmpty);
425
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000426 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
427 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000428 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000429 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
430 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000431 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000432
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000433 if (frame_type == kFrameEmpty)
434 return 0;
435
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000436 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
437 capture_timestamp, capture_time_ms,
438 payload_data, payload_size,
439 fragmentation, codec_info,
440 rtp_type_hdr);
441
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000442 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000443
444 CriticalSectionScoped cs(statistics_crit_.get());
445 uint32_t frame_count = ++frame_counts_[frame_type];
446 if (frame_count_observer_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000447 frame_count_observer_->FrameCountUpdated(frame_type, frame_count, ssrc);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000448 }
449
450 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000451}
452
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000453int RTPSender::SendRedundantPayloads(int payload_type, int bytes_to_send) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000454 uint8_t buffer[IP_PACKET_SIZE];
455 int bytes_left = bytes_to_send;
456 while (bytes_left > 0) {
457 uint16_t length = bytes_left;
458 int64_t capture_time_ms;
459 if (!packet_history_.GetBestFittingPacket(buffer, &length,
460 &capture_time_ms)) {
461 break;
462 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000463 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000464 return -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000465 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000466 RTPHeader rtp_header;
467 rtp_parser.Parse(rtp_header);
468 bytes_left -= length - rtp_header.headerLength;
469 }
470 return bytes_to_send - bytes_left;
471}
472
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000473int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
474 int32_t bytes) {
475 int padding_bytes_in_packet = kMaxPaddingLength;
476 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000477 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000478 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000479 packet[0] |= 0x20; // Set padding bit.
480 int32_t *data =
481 reinterpret_cast<int32_t *>(&(packet[header_length]));
482
483 // Fill data buffer with random data.
484 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
485 data[j] = rand(); // NOLINT
486 }
487 // Set number of padding bytes in the last byte of the packet.
488 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
489 return padding_bytes_in_packet;
490}
491
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000492int RTPSender::SendPadData(int payload_type,
493 uint32_t timestamp,
494 int64_t capture_time_ms,
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000495 int32_t bytes) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000496 // Drop this packet if we're not sending media packets.
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000497 if (!SendingMedia()) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000498 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000499 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000500 int padding_bytes_in_packet = 0;
501 int bytes_sent = 0;
502 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000503 // Always send full padding packets.
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000504 if (bytes < kMaxPaddingLength)
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000505 bytes = kMaxPaddingLength;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000506
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000507 uint32_t ssrc;
508 uint16_t sequence_number;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000509 bool over_rtx;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000510 {
511 CriticalSectionScoped cs(send_critsect_);
512 // Only send padding packets following the last packet of a frame,
513 // indicated by the marker bit.
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000514 if (rtx_ == kRtxOff) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000515 // Without RTX we can't send padding in the middle of frames.
516 if (!last_packet_marker_bit_)
517 return bytes_sent;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000518 ssrc = ssrc_;
519 sequence_number = sequence_number_;
520 ++sequence_number_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000521 over_rtx = false;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000522 } else {
523 ssrc = ssrc_rtx_;
524 sequence_number = sequence_number_rtx_;
525 ++sequence_number_rtx_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000526 over_rtx = true;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000527 }
528 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000529
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000530 uint8_t padding_packet[IP_PACKET_SIZE];
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000531 int header_length = CreateRTPHeader(padding_packet,
532 payload_type,
533 ssrc,
534 false,
535 timestamp,
536 sequence_number,
537 NULL,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000538 0);
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000539 padding_bytes_in_packet =
540 BuildPaddingPacket(padding_packet, header_length, bytes);
541 int length = padding_bytes_in_packet + header_length;
542 int64_t now_ms = clock_->TimeInMilliseconds();
543
544 RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
545 RTPHeader rtp_header;
546 rtp_parser.Parse(rtp_header);
547
548 if (capture_time_ms > 0) {
549 UpdateTransmissionTimeOffset(
550 padding_packet, length, rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000551 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000552
553 UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
554 if (!SendPacketToNetwork(padding_packet, length))
555 break;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000556 bytes_sent += padding_bytes_in_packet;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000557 UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000558 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000559
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000560 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000561}
562
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000563void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000564 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000565 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000566}
567
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000568bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000569 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000570}
niklase@google.com470e71d2011-07-07 08:21:25 +0000571
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000572int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
573 uint16_t length = IP_PACKET_SIZE;
574 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000575 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000576 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
577 data_buffer, &length,
578 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000579 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000580 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000581 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000582
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000583 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000584 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000585 RTPHeader header;
586 if (!rtp_parser.Parse(header)) {
587 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000588 return -1;
589 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000590 // Convert from TickTime to Clock since capture_time_ms is based on
591 // TickTime.
592 // TODO(holmer): Remove this conversion when we remove the use of TickTime.
593 int64_t clock_delta_ms = clock_->TimeInMilliseconds() -
594 TickTime::MillisecondTimestamp();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000595 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000596 header.ssrc,
597 header.sequenceNumber,
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000598 capture_time_ms + clock_delta_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000599 length - header.headerLength,
600 true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000601 // We can't send the packet right now.
602 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000603 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000604 }
605 }
606
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000607 CriticalSectionScoped lock(send_critsect_);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000608 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org16395222014-03-19 19:34:07 +0000609 (rtx_ & kRtxRetransmitted) > 0, true) ?
610 length : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000611}
612
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000613bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
614 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000615 if (transport_) {
616 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000617 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000618 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
619 "size", size, "sent", bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000620 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000621 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000622 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000623 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000624 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000625 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000626}
627
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000628int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000629 if (!video_)
630 return -1;
631 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000632}
633
634int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000635 if (!video_)
636 return -1;
637 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000638}
639
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000640void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000641 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000642 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000643 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
644 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000645 const int64_t now = clock_->TimeInMilliseconds();
646 uint32_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000647 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000648
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000649 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000650 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000651 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000652 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000653 return;
654 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000655
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000656 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
657 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000658 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000659 if (bytes_sent > 0) {
660 bytes_re_sent += bytes_sent;
661 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000662 // The packet has previously been resent.
663 // Try resending next packet in the list.
664 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000665 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000666 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000667 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
668 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000669 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000670 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000671 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000672 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000673 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000674 uint32_t target_bytes =
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000675 (static_cast<uint32_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000676 if (bytes_re_sent > target_bytes) {
677 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000678 }
679 }
680 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000681 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000682 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000683 UpdateNACKBitRate(bytes_re_sent, now);
684 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000685 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000686}
687
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000688bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
689 uint32_t num = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000690 int byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000691 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000692 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000693
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000694 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000695
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000696 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000697 return true;
698 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000699 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000700 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000701 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000702 break;
703 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000704 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000706 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000707 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000708 if (num == NACK_BYTECOUNT_SIZE) {
709 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000710 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000711 if (nack_byte_count_times_[num - 1] <= now) {
712 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000713 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000714 }
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000715 return (byte_count * 8) <
716 static_cast<int>(target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000717}
718
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000719void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
720 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000721 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000722
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000723 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000724 if (bytes > 0) {
725 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000726 // Add padding length.
727 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000728 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000729 if (nack_byte_count_times_[0] == 0) {
730 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000731 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000732 // Shift.
733 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
734 nack_byte_count_[i + 1] = nack_byte_count_[i];
735 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000736 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000737 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000738 nack_byte_count_[0] = bytes;
739 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000740 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000741 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000742}
743
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000744// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000745bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000746 int64_t capture_time_ms,
747 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000748 uint16_t length = IP_PACKET_SIZE;
749 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000750 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000751
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000752 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
753 0,
754 retransmission,
755 data_buffer,
756 &length,
757 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000758 // Packet cannot be found. Allow sending to continue.
759 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000760 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000761 if (!retransmission && capture_time_ms > 0) {
762 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
763 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000764 int rtx;
765 {
766 CriticalSectionScoped lock(send_critsect_);
767 rtx = rtx_;
768 }
769 return PrepareAndSendPacket(data_buffer,
770 length,
771 capture_time_ms,
772 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000773 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000774}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000775
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000776bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
777 uint16_t length,
778 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000779 bool send_over_rtx,
780 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000781 uint8_t *buffer_to_send_ptr = buffer;
782
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000783 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000784 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000785 rtp_parser.Parse(rtp_header);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000786 TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000787 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000788 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000789
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000790 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000791 if (send_over_rtx) {
792 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000793 buffer_to_send_ptr = data_buffer_rtx;
794 }
795
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000796 int64_t now_ms = clock_->TimeInMilliseconds();
797 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000798 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
799 diff_ms);
800 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000801 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000802 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
803 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000804 return ret;
805}
806
807void RTPSender::UpdateRtpStats(const uint8_t* buffer,
808 uint32_t size,
809 const RTPHeader& header,
810 bool is_rtx,
811 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000812 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000813 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000814 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000815
816 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000817 if (is_rtx) {
818 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000819 } else {
820 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000821 }
822
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000823 bitrate_sent_.Update(size);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000824 ++counters->packets;
825 if (IsFecPacket(buffer, header)) {
826 ++counters->fec_packets;
827 }
828
829 if (is_retransmit) {
830 ++counters->retransmitted_packets;
831 } else {
832 counters->bytes += size - (header.headerLength + header.paddingLength);
833 counters->header_bytes += header.headerLength;
834 counters->padding_bytes += header.paddingLength;
835 }
836
837 if (rtp_stats_callback_) {
838 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
839 }
840}
841
842bool RTPSender::IsFecPacket(const uint8_t* buffer,
843 const RTPHeader& header) const {
844 if (!video_) {
845 return false;
846 }
847 bool fec_enabled;
848 uint8_t pt_red;
849 uint8_t pt_fec;
850 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
851 return fec_enabled &&
852 header.payloadType == pt_red &&
853 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000854}
855
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000856int RTPSender::TimeToSendPadding(int bytes) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000857 assert(bytes > 0);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000858 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000859 int64_t capture_time_ms;
860 uint32_t timestamp;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000861 int rtx;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000862 {
863 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000864 if (!sending_media_) {
865 return 0;
866 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000867 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_ :
868 payload_type_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000869 timestamp = timestamp_;
870 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000871 if (last_timestamp_time_ms_ > 0) {
872 timestamp +=
873 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
874 capture_time_ms +=
875 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
876 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000877 rtx = rtx_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000878 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000879 int bytes_sent = 0;
880 if ((rtx & kRtxRedundantPayloads) != 0)
881 bytes_sent = SendRedundantPayloads(payload_type, bytes);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000882 bytes -= bytes_sent;
883 if (bytes > 0) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000884 int padding_sent =
885 SendPadData(payload_type, timestamp, capture_time_ms, bytes);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000886 bytes_sent += padding_sent;
887 }
888 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000889}
890
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000891// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000892int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000893 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000894 int64_t capture_time_ms, StorageType storage,
895 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000896 RtpUtility::RtpHeaderParser rtp_parser(buffer,
897 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000898 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000899 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000900
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000901 int64_t now_ms = clock_->TimeInMilliseconds();
902
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000903 // |capture_time_ms| <= 0 is considered invalid.
904 // TODO(holmer): This should be changed all over Video Engine so that negative
905 // time is consider invalid, while 0 is considered a valid time.
906 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000907 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000908 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000909 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000910
911 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
912 rtp_header, now_ms);
913
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000914 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000915 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
916 max_payload_length_, capture_time_ms,
917 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000918 return -1;
919 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000920
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000921 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000922 int64_t clock_delta_ms = clock_->TimeInMilliseconds() -
923 TickTime::MillisecondTimestamp();
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000924 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000925 rtp_header.sequenceNumber,
926 capture_time_ms + clock_delta_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000927 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000928 // We can't send the packet right now.
929 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000930 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000931 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000932 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000933 if (capture_time_ms > 0) {
934 UpdateDelayStatistics(capture_time_ms, now_ms);
935 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000936 uint32_t length = payload_length + rtp_header_length;
937 if (!SendPacketToNetwork(buffer, length))
938 return -1;
939 UpdateRtpStats(buffer, length, rtp_header, false, false);
940 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000941}
942
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000943void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000944 uint32_t ssrc;
945 int avg_delay_ms = 0;
946 int max_delay_ms = 0;
947 {
948 CriticalSectionScoped lock(send_critsect_);
949 ssrc = ssrc_;
950 }
951 {
952 CriticalSectionScoped cs(statistics_crit_.get());
953 // TODO(holmer): Compute this iteratively instead.
954 send_delays_[now_ms] = now_ms - capture_time_ms;
955 send_delays_.erase(send_delays_.begin(),
956 send_delays_.lower_bound(now_ms -
957 kSendSideDelayWindowMs));
958 }
959 if (send_side_delay_observer_ &&
960 GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
961 send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
962 max_delay_ms, ssrc);
963 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000964}
965
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000966void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000967 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000968 bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000969 nack_bitrate_.Process();
970 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000971 return;
972 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000973 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000974}
975
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000976uint16_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000977 CriticalSectionScoped lock(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000978 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000979 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000980 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000981 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000982 rtp_header_length += RtpHeaderExtensionTotalLength();
983 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000984}
985
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000986uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000987 CriticalSectionScoped cs(send_critsect_);
988 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000989}
990
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000991void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000992 uint32_t ssrc;
993 uint32_t ssrc_rtx;
994 {
995 CriticalSectionScoped ssrc_lock(send_critsect_);
996 ssrc = ssrc_;
997 ssrc_rtx = ssrc_rtx_;
998 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000999 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001000 rtp_stats_ = StreamDataCounters();
1001 rtx_rtp_stats_ = StreamDataCounters();
1002 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001003 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
1004 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001005 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001006}
1007
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001008void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1009 StreamDataCounters* rtx_stats) const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001010 CriticalSectionScoped lock(statistics_crit_.get());
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001011 *rtp_stats = rtp_stats_;
1012 *rtx_stats = rtx_rtp_stats_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001013}
1014
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001015int RTPSender::CreateRTPHeader(
1016 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
1017 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
1018 uint8_t num_csrcs) const {
1019 header[0] = 0x80; // version 2.
1020 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001021 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001022 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001023 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001024 RtpUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1025 RtpUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1026 RtpUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001027 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001028
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001029 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001030 if (num_csrcs > 0) {
1031 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001032 // error
1033 assert(false);
1034 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001035 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001036 uint8_t *ptr = &header[rtp_header_length];
1037 for (int i = 0; i < num_csrcs; ++i) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001038 RtpUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001039 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001040 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001041 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001042
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001043 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001044 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001045 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001046
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001047 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1048 if (len > 0) {
1049 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001050 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001051 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001052 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001053}
1054
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001055int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
1056 const int8_t payload_type,
1057 const bool marker_bit,
1058 const uint32_t capture_timestamp,
1059 int64_t capture_time_ms,
1060 const bool timestamp_provided,
1061 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001062 assert(payload_type >= 0);
1063 CriticalSectionScoped cs(send_critsect_);
1064
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001065 if (timestamp_provided) {
1066 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001067 } else {
1068 // Make a unique time stamp.
1069 // We can't inc by the actual time, since then we increase the risk of back
1070 // timing.
1071 timestamp_++;
1072 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001073 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001074 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001075 capture_time_ms_ = capture_time_ms;
1076 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001077 int csrcs_length = 0;
1078 if (include_csrcs_)
1079 csrcs_length = num_csrcs_;
1080 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1081 timestamp_, sequence_number, csrcs_, csrcs_length);
1082}
1083
1084uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001085 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001086 return 0;
1087 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001088 // RTP header extension, RFC 3550.
1089 // 0 1 2 3
1090 // 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
1091 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1092 // | defined by profile | length |
1093 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1094 // | header extension |
1095 // | .... |
1096 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001097 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001098 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001099
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001100 // Add extension ID (0xBEDE).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001101 RtpUtility::AssignUWord16ToBuffer(data_buffer, kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001102
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001103 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001104 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001105
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001106 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001107 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001108 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001109 switch (type) {
1110 case kRtpExtensionTransmissionTimeOffset:
1111 block_length = BuildTransmissionTimeOffsetExtension(
1112 data_buffer + kHeaderLength + total_block_length);
1113 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001114 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001115 block_length = BuildAudioLevelExtension(
1116 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001117 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001118 case kRtpExtensionAbsoluteSendTime:
1119 block_length = BuildAbsoluteSendTimeExtension(
1120 data_buffer + kHeaderLength + total_block_length);
1121 break;
1122 default:
1123 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001124 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001125 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001126 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001127 }
1128 if (total_block_length == 0) {
1129 // No extension added.
1130 return 0;
1131 }
1132 // Set header length (in number of Word32, header excluded).
1133 assert(total_block_length % 4 == 0);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001134 RtpUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
1135 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001136 // Total added length.
1137 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001138}
1139
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001140uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1141 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001142 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1143 //
1144 // The transmission time is signaled to the receiver in-band using the
1145 // general mechanism for RTP header extensions [RFC5285]. The payload
1146 // of this extension (the transmitted value) is a 24-bit signed integer.
1147 // When added to the RTP timestamp of the packet, it represents the
1148 // "effective" RTP transmission time of the packet, on the RTP
1149 // timescale.
1150 //
1151 // The form of the transmission offset extension block:
1152 //
1153 // 0 1 2 3
1154 // 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
1155 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1156 // | ID | len=2 | transmission offset |
1157 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001158
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001159 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001160 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001161 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1162 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001163 // Not registered.
1164 return 0;
1165 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001166 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001167 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001168 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001169 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos,
1170 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001171 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001172 assert(pos == kTransmissionTimeOffsetLength);
1173 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001174}
1175
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001176uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1177 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1178 //
1179 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1180 //
1181 // The form of the audio level extension block:
1182 //
1183 // 0 1 2 3
1184 // 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
1185 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1186 // | ID | len=0 |V| level | 0x00 | 0x00 |
1187 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1188 //
1189 // Note that we always include 2 pad bytes, which will result in legal and
1190 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1191 // are implemented. Right now the pad bytes would anyway be required at end
1192 // of the extension block, so it makes no difference.
1193
1194 // Get id defined by user.
1195 uint8_t id;
1196 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1197 // Not registered.
1198 return 0;
1199 }
1200 size_t pos = 0;
1201 const uint8_t len = 0;
1202 data_buffer[pos++] = (id << 4) + len;
1203 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1204 data_buffer[pos++] = 0; // Padding.
1205 data_buffer[pos++] = 0; // Padding.
1206 // kAudioLevelLength is including pad bytes.
1207 assert(pos == kAudioLevelLength);
1208 return kAudioLevelLength;
1209}
1210
1211uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001212 // Absolute send time in RTP streams.
1213 //
1214 // The absolute send time is signaled to the receiver in-band using the
1215 // general mechanism for RTP header extensions [RFC5285]. The payload
1216 // of this extension (the transmitted value) is a 24-bit unsigned integer
1217 // containing the sender's current time in seconds as a fixed point number
1218 // with 18 bits fractional part.
1219 //
1220 // The form of the absolute send time extension block:
1221 //
1222 // 0 1 2 3
1223 // 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
1224 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1225 // | ID | len=2 | absolute send time |
1226 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1227
1228 // Get id defined by user.
1229 uint8_t id;
1230 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1231 &id) != 0) {
1232 // Not registered.
1233 return 0;
1234 }
1235 size_t pos = 0;
1236 const uint8_t len = 2;
1237 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001238 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos, absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001239 pos += 3;
1240 assert(pos == kAbsoluteSendTimeLength);
1241 return kAbsoluteSendTimeLength;
1242}
1243
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001244void RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001245 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001246 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001247 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001248 // Get id.
1249 uint8_t id = 0;
1250 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1251 &id) != 0) {
1252 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001253 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001254 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001255 // Get length until start of header extension block.
1256 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001257 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001258 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001259 if (extension_block_pos < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001260 LOG(LS_WARNING)
1261 << "Failed to update transmission time offset, not registered.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001262 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001263 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001264 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001265 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001266 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001267 block_pos + kTransmissionTimeOffsetLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001268 LOG(LS_WARNING)
1269 << "Failed to update transmission time offset, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001270 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001271 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001272 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001273 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1274 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001275 LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1276 "extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001277 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001278 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001279 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001280 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001281 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001282 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001283 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001284 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001285 // Update transmission offset field (converting to a 90 kHz timestamp).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001286 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1287 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001288}
1289
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001290bool RTPSender::UpdateAudioLevel(uint8_t *rtp_packet,
1291 const uint16_t rtp_packet_length,
1292 const RTPHeader &rtp_header,
1293 const bool is_voiced,
1294 const uint8_t dBov) const {
1295 CriticalSectionScoped cs(send_critsect_);
1296
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001297 // Get id.
1298 uint8_t id = 0;
1299 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1300 // Not registered.
1301 return false;
1302 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001303 // Get length until start of header extension block.
1304 int extension_block_pos =
1305 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1306 kRtpExtensionAudioLevel);
1307 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001308 // The feature is not enabled.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001309 return false;
1310 }
1311 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1312 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1313 rtp_header.headerLength < block_pos + kAudioLevelLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001314 LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001315 return false;
1316 }
1317 // Verify that header contains extension.
1318 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1319 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001320 LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001321 return false;
1322 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001323 // Verify first byte in block.
1324 const uint8_t first_block_byte = (id << 4) + 0;
1325 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001326 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001327 return false;
1328 }
1329 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1330 return true;
1331}
1332
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001333void RTPSender::UpdateAbsoluteSendTime(
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001334 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001335 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001336 CriticalSectionScoped cs(send_critsect_);
1337
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001338 // Get id.
1339 uint8_t id = 0;
1340 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1341 &id) != 0) {
1342 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001343 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001344 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001345 // Get length until start of header extension block.
1346 int extension_block_pos =
1347 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1348 kRtpExtensionAbsoluteSendTime);
1349 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001350 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001351 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001352 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001353 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001354 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001355 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001356 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001357 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001358 }
1359 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001360 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1361 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001362 LOG(LS_WARNING)
1363 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001364 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001365 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001366 // Verify first byte in block.
1367 const uint8_t first_block_byte = (id << 4) + 2;
1368 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001369 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001370 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001371 }
1372 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1373 // fractional part).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001374 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1375 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001376}
1377
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001378void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001379 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001380 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001381 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001382
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001383 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001384 SetStartTimestamp(RTPtime, false);
1385 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001386 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001387 if (!ssrc_forced_) {
1388 // Generate a new SSRC.
1389 ssrc_db_.ReturnSSRC(ssrc_);
1390 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001391 }
1392 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001393 if (!sequence_number_forced_ && !ssrc_forced_) {
1394 // Generate a new sequence number.
1395 sequence_number_ =
1396 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001397 }
1398 }
1399}
1400
1401void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001402 CriticalSectionScoped cs(send_critsect_);
1403 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001404}
1405
1406bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001407 CriticalSectionScoped cs(send_critsect_);
1408 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001409}
1410
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001411uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001412 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001413 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001414}
1415
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001416void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001417 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001418 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001419 start_timestamp_forced_ = true;
1420 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001421 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001422 if (!start_timestamp_forced_) {
1423 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001424 }
1425 }
1426}
1427
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001428uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001429 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001430 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001431}
1432
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001433uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001434 // If configured via API, return 0.
1435 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001436
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001437 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001438 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001439 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001440 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1441 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001442}
1443
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001444void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001445 // This is configured via the API.
1446 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001447
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001448 if (ssrc_ == ssrc && ssrc_forced_) {
1449 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001450 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001451 ssrc_forced_ = true;
1452 ssrc_db_.ReturnSSRC(ssrc_);
1453 ssrc_db_.RegisterSSRC(ssrc);
1454 ssrc_ = ssrc;
1455 if (!sequence_number_forced_) {
1456 sequence_number_ =
1457 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001458 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001459}
1460
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001461uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001462 CriticalSectionScoped cs(send_critsect_);
1463 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001464}
1465
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001466void RTPSender::SetCSRCStatus(const bool include) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001467 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001468 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001469}
1470
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001471void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1472 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001473 assert(arr_length <= kRtpCsrcSize);
1474 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001475
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001476 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001477 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001478 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001479 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001480}
1481
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001482int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001483 assert(arr_of_csrc);
1484 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001485 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1486 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001487 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001488 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001489}
1490
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001491void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001492 CriticalSectionScoped cs(send_critsect_);
1493 sequence_number_forced_ = true;
1494 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001495}
1496
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001497uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001498 CriticalSectionScoped cs(send_critsect_);
1499 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001500}
1501
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001502// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001503int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1504 const uint16_t time_ms,
1505 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001506 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001507 return -1;
1508 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001509 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001510}
1511
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001512bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001513 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001514 return false;
1515 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001516 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001517}
1518
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001519int32_t RTPSender::SetAudioPacketSize(
1520 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001521 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001522 return -1;
1523 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001524 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001525}
1526
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001527int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001528 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001529}
1530
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001531int32_t RTPSender::SetRED(const int8_t payload_type) {
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_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001536}
1537
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001538int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001539 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001540 return -1;
1541 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001542 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001543}
1544
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001545// Video
1546VideoCodecInformation *RTPSender::CodecInformationVideo() {
1547 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001548 return NULL;
1549 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001550 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001551}
1552
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001553RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001554 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001555 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001556}
1557
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001558uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001559 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001560 return 0;
1561 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001562 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001563}
1564
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001565int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001566 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001567 return -1;
1568 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001569 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001570}
1571
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001572int32_t RTPSender::SetGenericFECStatus(
1573 const bool enable, const uint8_t payload_type_red,
1574 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001575 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001576 return -1;
1577 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001578 return video_->SetGenericFECStatus(enable, payload_type_red,
1579 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001580}
1581
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001582int32_t RTPSender::GenericFECStatus(
1583 bool *enable, uint8_t *payload_type_red,
1584 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001585 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001586 return -1;
1587 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001588 return video_->GenericFECStatus(
1589 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001590}
1591
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001592int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001593 const FecProtectionParams *delta_params,
1594 const FecProtectionParams *key_params) {
1595 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001596 return -1;
1597 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001598 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001599}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001600
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001601void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1602 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001603 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001604 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001605 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001606 RtpUtility::RtpHeaderParser rtp_parser(
1607 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001608
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001609 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001610 rtp_parser.Parse(rtp_header);
1611
1612 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001613 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001614
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001615 // Replace payload type, if a specific type is set for RTX.
1616 if (payload_type_rtx_ != -1) {
1617 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001618 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001619 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1620 }
1621
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001622 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001623 uint8_t *ptr = data_buffer_rtx + 2;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001624 RtpUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001625
1626 // Replace SSRC.
1627 ptr += 6;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001628 RtpUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001629
1630 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001631 ptr = data_buffer_rtx + rtp_header.headerLength;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001632 RtpUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001633 ptr += 2;
1634
1635 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001636 memcpy(ptr, buffer + rtp_header.headerLength,
1637 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001638 *length += 2;
1639}
1640
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001641void RTPSender::RegisterRtpStatisticsCallback(
1642 StreamDataCountersCallback* callback) {
1643 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001644 rtp_stats_callback_ = callback;
1645}
1646
1647StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1648 CriticalSectionScoped cs(statistics_crit_.get());
1649 return rtp_stats_callback_;
1650}
1651
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001652uint32_t RTPSender::BitrateSent() const { return bitrate_sent_.BitrateLast(); }
1653
1654void RTPSender::BitrateUpdated(const BitrateStatistics& stats) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001655 uint32_t ssrc;
1656 {
1657 CriticalSectionScoped ssrc_lock(send_critsect_);
1658 ssrc = ssrc_;
1659 }
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001660 if (bitrate_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001661 bitrate_callback_->Notify(stats, ssrc);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001662 }
1663}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001664
1665void RTPSender::SetRtpState(const RtpState& rtp_state) {
1666 SetStartTimestamp(rtp_state.start_timestamp, true);
1667 CriticalSectionScoped lock(send_critsect_);
1668 sequence_number_ = rtp_state.sequence_number;
1669 sequence_number_forced_ = true;
1670 timestamp_ = rtp_state.timestamp;
1671 capture_time_ms_ = rtp_state.capture_time_ms;
1672 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
1673}
1674
1675RtpState RTPSender::GetRtpState() const {
1676 CriticalSectionScoped lock(send_critsect_);
1677
1678 RtpState state;
1679 state.sequence_number = sequence_number_;
1680 state.start_timestamp = start_timestamp_;
1681 state.timestamp = timestamp_;
1682 state.capture_time_ms = capture_time_ms_;
1683 state.last_timestamp_time_ms = last_timestamp_time_ms_;
1684
1685 return state;
1686}
1687
1688void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
1689 CriticalSectionScoped lock(send_critsect_);
1690 sequence_number_rtx_ = rtp_state.sequence_number;
1691}
1692
1693RtpState RTPSender::GetRtxRtpState() const {
1694 CriticalSectionScoped lock(send_critsect_);
1695
1696 RtpState state;
1697 state.sequence_number = sequence_number_rtx_;
1698 state.start_timestamp = start_timestamp_;
1699
1700 return state;
1701}
1702
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001703} // namespace webrtc