blob: 0438b9f7d60e22e7c9751447c8c393862ef9418d [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),
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +000053 // TODO(holmer): Remove this conversion when we remove the use of
54 // TickTime.
55 clock_delta_ms_(clock_->TimeInMilliseconds() -
56 TickTime::MillisecondTimestamp()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000057 bitrate_sent_(clock, this),
58 id_(id),
59 audio_configured_(audio),
60 audio_(NULL),
61 video_(NULL),
62 paced_sender_(paced_sender),
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +000063 last_capture_time_ms_sent_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000064 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000065 transport_(transport),
66 sending_media_(true), // Default to sending media.
67 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000068 packet_over_head_(28),
69 payload_type_(-1),
70 payload_type_map_(),
71 rtp_header_extension_map_(),
72 transmission_time_offset_(0),
73 absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000074 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000075 nack_byte_count_times_(),
76 nack_byte_count_(),
77 nack_bitrate_(clock, NULL),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000078 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000079 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +000080 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000081 rtp_stats_callback_(NULL),
andresp@webrtc.orgd11bec42014-07-08 14:32:58 +000082 bitrate_callback_(bitrate_callback),
andresp@webrtc.org8f151212014-07-10 09:39:23 +000083 frame_count_observer_(frame_count_observer),
stefan@webrtc.org168f23f2014-07-11 13:44:02 +000084 send_side_delay_observer_(send_side_delay_observer),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +000085 // RTP variables
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +000086 start_timestamp_forced_(false),
87 start_timestamp_(0),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000088 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
89 remote_ssrc_(0),
90 sequence_number_forced_(false),
91 ssrc_forced_(false),
92 timestamp_(0),
93 capture_time_ms_(0),
94 last_timestamp_time_ms_(0),
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +000095 media_has_been_sent_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000096 last_packet_marker_bit_(false),
97 num_csrcs_(0),
98 csrcs_(),
99 include_csrcs_(true),
100 rtx_(kRtxOff),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000101 payload_type_rtx_(-1),
102 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000103 target_bitrate_(0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000104 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
105 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000106 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000107 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000108 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000109 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000110 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
111 // Random start, 16 bits. Can't be 0.
112 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
113 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000115 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000116 audio_ = new RTPSenderAudio(id, clock_, this);
117 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000118 } else {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000119 video_ = new RTPSenderVideo(clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000120 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000121}
122
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000123RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000124 if (remote_ssrc_ != 0) {
125 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000126 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000127 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000128
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000129 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000130 delete send_critsect_;
131 while (!payload_type_map_.empty()) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000132 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000133 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000134 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000135 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000136 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000137 delete audio_;
138 delete video_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000139}
niklase@google.com470e71d2011-07-07 08:21:25 +0000140
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000141void RTPSender::SetTargetBitrate(uint32_t bitrate) {
142 CriticalSectionScoped cs(target_bitrate_critsect_.get());
143 target_bitrate_ = bitrate;
144}
145
146uint32_t RTPSender::GetTargetBitrate() {
147 CriticalSectionScoped cs(target_bitrate_critsect_.get());
148 return target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000149}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000150
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000151uint16_t RTPSender::ActualSendBitrateKbit() const {
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000152 return (uint16_t)(bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000153}
154
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000155uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000156 if (video_) {
157 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000158 }
159 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000160}
161
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000162uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000163 if (video_) {
164 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000165 }
166 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000167}
168
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000169uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000170 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000171}
172
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000173bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
174 int* max_send_delay_ms) const {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000175 CriticalSectionScoped lock(statistics_crit_.get());
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000176 SendDelayMap::const_iterator it = send_delays_.upper_bound(
177 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000178 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000179 return false;
180 int num_delays = 0;
181 for (; it != send_delays_.end(); ++it) {
182 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
183 *avg_send_delay_ms += it->second;
184 ++num_delays;
185 }
186 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
187 return true;
188}
189
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000190int32_t RTPSender::SetTransmissionTimeOffset(
191 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000192 if (transmission_time_offset > (0x800000 - 1) ||
193 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000194 return -1;
195 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000196 CriticalSectionScoped cs(send_critsect_);
197 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000198 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000199}
200
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000201int32_t RTPSender::SetAbsoluteSendTime(
202 const uint32_t absolute_send_time) {
203 if (absolute_send_time > 0xffffff) { // UWord24.
204 return -1;
205 }
206 CriticalSectionScoped cs(send_critsect_);
207 absolute_send_time_ = absolute_send_time;
208 return 0;
209}
210
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000211int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
212 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000213 CriticalSectionScoped cs(send_critsect_);
214 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000215}
216
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000217int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000218 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000219 CriticalSectionScoped cs(send_critsect_);
220 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000221}
222
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000223uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000224 CriticalSectionScoped cs(send_critsect_);
225 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000226}
227
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000228int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000229 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000230 const int8_t payload_number, const uint32_t frequency,
231 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000232 assert(payload_name);
233 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000234
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000235 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000236 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000237
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000238 if (payload_type_map_.end() != it) {
239 // We already use this payload type.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000240 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000241 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000242
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000243 // Check if it's the same as we already have.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000244 if (RtpUtility::StringCompare(
245 payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000246 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000247 payload->typeSpecific.Audio.frequency == frequency &&
248 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000249 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000250 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000251 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000252 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000253 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000254 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000255 return 0;
256 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000257 }
258 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000259 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000260 int32_t ret_val = -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000261 RtpUtility::Payload* payload = NULL;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000262 if (audio_configured_) {
263 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
264 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000265 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000266 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
267 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000268 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000269 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000270 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000271 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000272 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000273}
274
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000275int32_t RTPSender::DeRegisterSendPayload(
276 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000277 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000278
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000279 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000280 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000281
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000282 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000283 return -1;
284 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000285 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000286 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000287 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000288 return 0;
289}
niklase@google.com470e71d2011-07-07 08:21:25 +0000290
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000291void RTPSender::SetSendPayloadType(int8_t payload_type) {
292 CriticalSectionScoped cs(send_critsect_);
293 payload_type_ = payload_type;
294}
295
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000296int8_t RTPSender::SendPayloadType() const {
297 CriticalSectionScoped cs(send_critsect_);
298 return payload_type_;
299}
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000301int RTPSender::SendPayloadFrequency() const {
302 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
303}
niklase@google.com470e71d2011-07-07 08:21:25 +0000304
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000305int32_t RTPSender::SetMaxPayloadLength(
306 const uint16_t max_payload_length,
307 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000308 // Sanity check.
309 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000310 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000311 return -1;
312 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000313 CriticalSectionScoped cs(send_critsect_);
314 max_payload_length_ = max_payload_length;
315 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000316 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000317}
318
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000319uint16_t RTPSender::MaxDataPayloadLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000320 int rtx;
321 {
322 CriticalSectionScoped rtx_lock(send_critsect_);
323 rtx = rtx_;
324 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000325 if (audio_configured_) {
326 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000327 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000328 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
329 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000330 - ((rtx) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000331 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000332}
333
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000334uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000335 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000336}
337
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000338uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000339
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000340void RTPSender::SetRTXStatus(int mode) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000341 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000342 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000343}
344
345void RTPSender::SetRtxSsrc(uint32_t ssrc) {
346 CriticalSectionScoped cs(send_critsect_);
347 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000348}
349
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000350uint32_t RTPSender::RtxSsrc() const {
351 CriticalSectionScoped cs(send_critsect_);
352 return ssrc_rtx_;
353}
354
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000355void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000356 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000357 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000358 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000359 *ssrc = ssrc_rtx_;
360 *payload_type = payload_type_rtx_;
361}
362
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000363void RTPSender::SetRtxPayloadType(int payload_type) {
364 CriticalSectionScoped cs(send_critsect_);
365 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000366}
367
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000368int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
369 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000370 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000371
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000372 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000373 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000374 return -1;
375 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000376 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000377 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000378 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000379 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000380 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000381 // And it's a match...
382 return 0;
383 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000384 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000385 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000386 if (payload_type_ == payload_type) {
387 if (!audio_configured_) {
388 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000389 }
390 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000391 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000392 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000393 payload_type_map_.find(payload_type);
394 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000395 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000396 return -1;
397 }
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000398 SetSendPayloadType(payload_type);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000399 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000400 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000401 if (!payload->audio && !audio_configured_) {
402 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
403 *video_type = payload->typeSpecific.Video.videoCodecType;
404 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000405 }
406 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000407}
408
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000409int32_t RTPSender::SendOutgoingData(
410 const FrameType frame_type, const int8_t payload_type,
411 const uint32_t capture_timestamp, int64_t capture_time_ms,
412 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000413 const RTPFragmentationHeader *fragmentation,
414 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000415 uint32_t ssrc;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000416 {
417 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000418 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000419 ssrc = ssrc_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000420 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000421 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000422 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000423 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000424 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000425 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000426 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000427 return -1;
428 }
429
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000430 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000431 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000432 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
433 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000434 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000435 frame_type == kFrameEmpty);
436
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000437 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
438 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000439 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000440 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
441 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000442 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000443
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000444 if (frame_type == kFrameEmpty)
445 return 0;
446
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000447 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
448 capture_timestamp, capture_time_ms,
449 payload_data, payload_size,
450 fragmentation, codec_info,
451 rtp_type_hdr);
452
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000453 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000454
455 CriticalSectionScoped cs(statistics_crit_.get());
456 uint32_t frame_count = ++frame_counts_[frame_type];
457 if (frame_count_observer_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000458 frame_count_observer_->FrameCountUpdated(frame_type, frame_count, ssrc);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000459 }
460
461 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000462}
463
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000464int RTPSender::TrySendRedundantPayloads(int bytes_to_send) {
465 {
466 CriticalSectionScoped cs(send_critsect_);
467 if ((rtx_ & kRtxRedundantPayloads) == 0)
468 return 0;
469 }
470
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000471 uint8_t buffer[IP_PACKET_SIZE];
472 int bytes_left = bytes_to_send;
473 while (bytes_left > 0) {
474 uint16_t length = bytes_left;
475 int64_t capture_time_ms;
476 if (!packet_history_.GetBestFittingPacket(buffer, &length,
477 &capture_time_ms)) {
478 break;
479 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000480 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000481 return -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000482 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000483 RTPHeader rtp_header;
484 rtp_parser.Parse(rtp_header);
485 bytes_left -= length - rtp_header.headerLength;
486 }
487 return bytes_to_send - bytes_left;
488}
489
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000490int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
491 int32_t bytes) {
492 int padding_bytes_in_packet = kMaxPaddingLength;
493 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000494 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000495 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000496 packet[0] |= 0x20; // Set padding bit.
497 int32_t *data =
498 reinterpret_cast<int32_t *>(&(packet[header_length]));
499
500 // Fill data buffer with random data.
501 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
502 data[j] = rand(); // NOLINT
503 }
504 // Set number of padding bytes in the last byte of the packet.
505 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
506 return padding_bytes_in_packet;
507}
508
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000509int RTPSender::TrySendPadData(int bytes) {
510 int64_t capture_time_ms;
511 uint32_t timestamp;
512 {
513 CriticalSectionScoped cs(send_critsect_);
514 timestamp = timestamp_;
515 capture_time_ms = capture_time_ms_;
516 if (last_timestamp_time_ms_ > 0) {
517 timestamp +=
518 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
519 capture_time_ms +=
520 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
521 }
522 }
523 return SendPadData(timestamp, capture_time_ms, bytes);
524}
525
526int RTPSender::SendPadData(uint32_t timestamp,
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000527 int64_t capture_time_ms,
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000528 int32_t bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000529 int padding_bytes_in_packet = 0;
530 int bytes_sent = 0;
531 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000532 // Always send full padding packets.
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000533 if (bytes < kMaxPaddingLength)
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000534 bytes = kMaxPaddingLength;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000535
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000536 uint32_t ssrc;
537 uint16_t sequence_number;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000538 int payload_type;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000539 bool over_rtx;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000540 {
541 CriticalSectionScoped cs(send_critsect_);
542 // Only send padding packets following the last packet of a frame,
543 // indicated by the marker bit.
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000544 if (rtx_ == kRtxOff) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000545 // Without RTX we can't send padding in the middle of frames.
546 if (!last_packet_marker_bit_)
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000547 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000548 ssrc = ssrc_;
549 sequence_number = sequence_number_;
550 ++sequence_number_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000551 payload_type = payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000552 over_rtx = false;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000553 } else {
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000554 // Without abs-send-time a media packet must be sent before padding so
555 // that the timestamps used for estimation are correct.
556 if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
557 kRtpExtensionAbsoluteSendTime))
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000558 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000559 ssrc = ssrc_rtx_;
560 sequence_number = sequence_number_rtx_;
561 ++sequence_number_rtx_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000562 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_
563 : payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000564 over_rtx = true;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000565 }
566 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000567
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000568 uint8_t padding_packet[IP_PACKET_SIZE];
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000569 int header_length = CreateRTPHeader(padding_packet,
570 payload_type,
571 ssrc,
572 false,
573 timestamp,
574 sequence_number,
575 NULL,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000576 0);
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000577 padding_bytes_in_packet =
578 BuildPaddingPacket(padding_packet, header_length, bytes);
579 int length = padding_bytes_in_packet + header_length;
580 int64_t now_ms = clock_->TimeInMilliseconds();
581
582 RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
583 RTPHeader rtp_header;
584 rtp_parser.Parse(rtp_header);
585
586 if (capture_time_ms > 0) {
587 UpdateTransmissionTimeOffset(
588 padding_packet, length, rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000589 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000590
591 UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
592 if (!SendPacketToNetwork(padding_packet, length))
593 break;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000594 bytes_sent += padding_bytes_in_packet;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000595 UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000596 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000597
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000598 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000599}
600
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000601void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000602 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000603 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000604}
605
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000606bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000607 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000608}
niklase@google.com470e71d2011-07-07 08:21:25 +0000609
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000610int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
611 uint16_t length = IP_PACKET_SIZE;
612 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000613 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000614 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
615 data_buffer, &length,
616 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000617 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000618 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000619 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000620
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000621 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000622 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000623 RTPHeader header;
624 if (!rtp_parser.Parse(header)) {
625 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000626 return -1;
627 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000628 // Convert from TickTime to Clock since capture_time_ms is based on
629 // TickTime.
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000630 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_;
631 if (!paced_sender_->SendPacket(
632 PacedSender::kHighPriority, header.ssrc, header.sequenceNumber,
633 corrected_capture_tims_ms, length - header.headerLength, true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000634 // We can't send the packet right now.
635 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000636 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000637 }
638 }
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000639 int rtx = kRtxOff;
640 {
641 CriticalSectionScoped lock(send_critsect_);
642 rtx = rtx_;
643 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000644 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000645 (rtx & kRtxRetransmitted) > 0, true) ?
stefan@webrtc.org16395222014-03-19 19:34:07 +0000646 length : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000647}
648
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000649bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
650 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000651 if (transport_) {
652 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000653 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000654 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
655 "size", size, "sent", bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000656 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000657 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000658 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000659 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000660 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000661 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000662}
663
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000664int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000665 if (!video_)
666 return -1;
667 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000668}
669
670int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000671 if (!video_)
672 return -1;
673 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000674}
675
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000676void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000677 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000678 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000679 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
680 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000681 const int64_t now = clock_->TimeInMilliseconds();
682 uint32_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000683 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000684
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000685 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000686 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000687 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000688 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000689 return;
690 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000691
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000692 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
693 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000694 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000695 if (bytes_sent > 0) {
696 bytes_re_sent += bytes_sent;
697 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000698 // The packet has previously been resent.
699 // Try resending next packet in the list.
700 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000701 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000702 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000703 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
704 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000705 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000706 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000707 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000708 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000709 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000710 uint32_t target_bytes =
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000711 (static_cast<uint32_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000712 if (bytes_re_sent > target_bytes) {
713 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000714 }
715 }
716 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000717 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000718 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000719 UpdateNACKBitRate(bytes_re_sent, now);
720 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000721 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000722}
723
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000724bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
725 uint32_t num = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000726 int byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000727 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000728 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000729
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000730 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000731
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000732 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000733 return true;
734 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000735 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000736 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000737 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000738 break;
739 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000740 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000741 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000742 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000743 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000744 if (num == NACK_BYTECOUNT_SIZE) {
745 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000746 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000747 if (nack_byte_count_times_[num - 1] <= now) {
748 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000749 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000750 }
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000751 return (byte_count * 8) <
752 static_cast<int>(target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000753}
754
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000755void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
756 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000757 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000758
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000759 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000760 if (bytes > 0) {
761 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000762 // Add padding length.
763 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000764 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000765 if (nack_byte_count_times_[0] == 0) {
766 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000767 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000768 // Shift.
769 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
770 nack_byte_count_[i + 1] = nack_byte_count_[i];
771 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000772 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000773 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000774 nack_byte_count_[0] = bytes;
775 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000776 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000777 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000778}
779
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000780// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000781bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000782 int64_t capture_time_ms,
783 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000784 uint16_t length = IP_PACKET_SIZE;
785 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000786 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000787
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000788 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
789 0,
790 retransmission,
791 data_buffer,
792 &length,
793 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000794 // Packet cannot be found. Allow sending to continue.
795 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000796 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000797 if (!retransmission && capture_time_ms > 0) {
798 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
799 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000800 int rtx;
801 {
802 CriticalSectionScoped lock(send_critsect_);
803 rtx = rtx_;
804 }
805 return PrepareAndSendPacket(data_buffer,
806 length,
807 capture_time_ms,
808 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000809 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000810}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000811
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000812bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
813 uint16_t length,
814 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000815 bool send_over_rtx,
816 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000817 uint8_t *buffer_to_send_ptr = buffer;
818
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000819 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000820 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000821 rtp_parser.Parse(rtp_header);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000822 if (!is_retransmit && rtp_header.markerBit) {
823 TRACE_EVENT_ASYNC_END0("webrtc_rtp", "PacedSend", capture_time_ms);
824 }
825
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000826 TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000827 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000828 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000829
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000830 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000831 if (send_over_rtx) {
832 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000833 buffer_to_send_ptr = data_buffer_rtx;
834 }
835
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000836 int64_t now_ms = clock_->TimeInMilliseconds();
837 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000838 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
839 diff_ms);
840 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000841 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000842 if (ret) {
843 CriticalSectionScoped lock(send_critsect_);
844 media_has_been_sent_ = true;
845 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000846 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
847 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000848 return ret;
849}
850
851void RTPSender::UpdateRtpStats(const uint8_t* buffer,
852 uint32_t size,
853 const RTPHeader& header,
854 bool is_rtx,
855 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000856 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000857 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000858 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000859
860 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000861 if (is_rtx) {
862 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000863 } else {
864 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000865 }
866
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000867 bitrate_sent_.Update(size);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000868 ++counters->packets;
869 if (IsFecPacket(buffer, header)) {
870 ++counters->fec_packets;
871 }
872
873 if (is_retransmit) {
874 ++counters->retransmitted_packets;
875 } else {
876 counters->bytes += size - (header.headerLength + header.paddingLength);
877 counters->header_bytes += header.headerLength;
878 counters->padding_bytes += header.paddingLength;
879 }
880
881 if (rtp_stats_callback_) {
882 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
883 }
884}
885
886bool RTPSender::IsFecPacket(const uint8_t* buffer,
887 const RTPHeader& header) const {
888 if (!video_) {
889 return false;
890 }
891 bool fec_enabled;
892 uint8_t pt_red;
893 uint8_t pt_fec;
894 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
895 return fec_enabled &&
896 header.payloadType == pt_red &&
897 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000898}
899
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000900int RTPSender::TimeToSendPadding(int bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000901 {
902 CriticalSectionScoped cs(send_critsect_);
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000903 if (!sending_media_) return 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000904 }
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000905 int available_bytes = bytes;
906 if (available_bytes > 0)
907 available_bytes -= TrySendRedundantPayloads(available_bytes);
908 if (available_bytes > 0)
909 available_bytes -= TrySendPadData(available_bytes);
910 return bytes - available_bytes;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000911}
912
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000913// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000914int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000915 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000916 int64_t capture_time_ms, StorageType storage,
917 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000918 RtpUtility::RtpHeaderParser rtp_parser(buffer,
919 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000920 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000921 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000922
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000923 int64_t now_ms = clock_->TimeInMilliseconds();
924
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000925 // |capture_time_ms| <= 0 is considered invalid.
926 // TODO(holmer): This should be changed all over Video Engine so that negative
927 // time is consider invalid, while 0 is considered a valid time.
928 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000929 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000930 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000931 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000932
933 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
934 rtp_header, now_ms);
935
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000936 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000937 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
938 max_payload_length_, capture_time_ms,
939 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000940 return -1;
941 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000942
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000943 if (paced_sender_ && storage != kDontStore) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000944 // Correct offset between implementations of millisecond time stamps in
945 // TickTime and Clock.
946 int64_t corrected_time_ms = capture_time_ms + clock_delta_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000947 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000948 rtp_header.sequenceNumber, corrected_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000949 payload_length, false)) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000950 if (last_capture_time_ms_sent_ == 0 ||
951 corrected_time_ms > last_capture_time_ms_sent_) {
952 last_capture_time_ms_sent_ = corrected_time_ms;
953 TRACE_EVENT_ASYNC_BEGIN1("webrtc_rtp", "PacedSend", corrected_time_ms,
954 "capture_time_ms", corrected_time_ms);
955 }
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000956 // We can't send the packet right now.
957 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000958 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000959 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000960 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000961 if (capture_time_ms > 0) {
962 UpdateDelayStatistics(capture_time_ms, now_ms);
963 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000964 uint32_t length = payload_length + rtp_header_length;
965 if (!SendPacketToNetwork(buffer, length))
966 return -1;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000967 {
968 CriticalSectionScoped lock(send_critsect_);
969 media_has_been_sent_ = true;
970 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000971 UpdateRtpStats(buffer, length, rtp_header, false, false);
972 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000973}
974
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000975void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000976 uint32_t ssrc;
977 int avg_delay_ms = 0;
978 int max_delay_ms = 0;
979 {
980 CriticalSectionScoped lock(send_critsect_);
981 ssrc = ssrc_;
982 }
983 {
984 CriticalSectionScoped cs(statistics_crit_.get());
985 // TODO(holmer): Compute this iteratively instead.
986 send_delays_[now_ms] = now_ms - capture_time_ms;
987 send_delays_.erase(send_delays_.begin(),
988 send_delays_.lower_bound(now_ms -
989 kSendSideDelayWindowMs));
990 }
991 if (send_side_delay_observer_ &&
992 GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
993 send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
994 max_delay_ms, ssrc);
995 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000996}
997
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000998void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000999 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001000 bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001001 nack_bitrate_.Process();
1002 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001003 return;
1004 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001005 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +00001006}
1007
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001008uint16_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001009 CriticalSectionScoped lock(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001010 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001011 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001012 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001013 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001014 rtp_header_length += RtpHeaderExtensionTotalLength();
1015 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001016}
1017
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001018uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001019 CriticalSectionScoped cs(send_critsect_);
1020 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001021}
1022
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001023void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001024 uint32_t ssrc;
1025 uint32_t ssrc_rtx;
1026 {
1027 CriticalSectionScoped ssrc_lock(send_critsect_);
1028 ssrc = ssrc_;
1029 ssrc_rtx = ssrc_rtx_;
1030 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001031 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001032 rtp_stats_ = StreamDataCounters();
1033 rtx_rtp_stats_ = StreamDataCounters();
1034 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001035 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
1036 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001037 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001038}
1039
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001040void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1041 StreamDataCounters* rtx_stats) const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001042 CriticalSectionScoped lock(statistics_crit_.get());
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001043 *rtp_stats = rtp_stats_;
1044 *rtx_stats = rtx_rtp_stats_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001045}
1046
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001047int RTPSender::CreateRTPHeader(
1048 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
1049 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
1050 uint8_t num_csrcs) const {
1051 header[0] = 0x80; // version 2.
1052 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001053 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001054 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001055 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001056 RtpUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1057 RtpUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1058 RtpUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001059 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001060
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001061 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001062 if (num_csrcs > 0) {
1063 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001064 // error
1065 assert(false);
1066 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001067 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001068 uint8_t *ptr = &header[rtp_header_length];
1069 for (int i = 0; i < num_csrcs; ++i) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001070 RtpUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001071 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001072 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001073 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001074
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001075 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001076 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001077 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001078
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001079 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1080 if (len > 0) {
1081 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001082 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001083 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001084 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001085}
1086
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001087int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
1088 const int8_t payload_type,
1089 const bool marker_bit,
1090 const uint32_t capture_timestamp,
1091 int64_t capture_time_ms,
1092 const bool timestamp_provided,
1093 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001094 assert(payload_type >= 0);
1095 CriticalSectionScoped cs(send_critsect_);
1096
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001097 if (timestamp_provided) {
1098 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001099 } else {
1100 // Make a unique time stamp.
1101 // We can't inc by the actual time, since then we increase the risk of back
1102 // timing.
1103 timestamp_++;
1104 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001105 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001106 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001107 capture_time_ms_ = capture_time_ms;
1108 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001109 int csrcs_length = 0;
1110 if (include_csrcs_)
1111 csrcs_length = num_csrcs_;
1112 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1113 timestamp_, sequence_number, csrcs_, csrcs_length);
1114}
1115
1116uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001117 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001118 return 0;
1119 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001120 // RTP header extension, RFC 3550.
1121 // 0 1 2 3
1122 // 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
1123 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1124 // | defined by profile | length |
1125 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1126 // | header extension |
1127 // | .... |
1128 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001129 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001130 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001131
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001132 // Add extension ID (0xBEDE).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001133 RtpUtility::AssignUWord16ToBuffer(data_buffer, kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001134
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001135 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001136 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001137
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001138 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001139 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001140 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001141 switch (type) {
1142 case kRtpExtensionTransmissionTimeOffset:
1143 block_length = BuildTransmissionTimeOffsetExtension(
1144 data_buffer + kHeaderLength + total_block_length);
1145 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001146 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001147 block_length = BuildAudioLevelExtension(
1148 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001149 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001150 case kRtpExtensionAbsoluteSendTime:
1151 block_length = BuildAbsoluteSendTimeExtension(
1152 data_buffer + kHeaderLength + total_block_length);
1153 break;
1154 default:
1155 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001156 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001157 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001158 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001159 }
1160 if (total_block_length == 0) {
1161 // No extension added.
1162 return 0;
1163 }
1164 // Set header length (in number of Word32, header excluded).
1165 assert(total_block_length % 4 == 0);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001166 RtpUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
1167 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001168 // Total added length.
1169 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001170}
1171
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001172uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1173 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001174 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1175 //
1176 // The transmission time is signaled to the receiver in-band using the
1177 // general mechanism for RTP header extensions [RFC5285]. The payload
1178 // of this extension (the transmitted value) is a 24-bit signed integer.
1179 // When added to the RTP timestamp of the packet, it represents the
1180 // "effective" RTP transmission time of the packet, on the RTP
1181 // timescale.
1182 //
1183 // The form of the transmission offset extension block:
1184 //
1185 // 0 1 2 3
1186 // 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
1187 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1188 // | ID | len=2 | transmission offset |
1189 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001190
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001191 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001192 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001193 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1194 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001195 // Not registered.
1196 return 0;
1197 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001198 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001199 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001200 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001201 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos,
1202 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001203 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001204 assert(pos == kTransmissionTimeOffsetLength);
1205 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001206}
1207
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001208uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1209 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1210 //
1211 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1212 //
1213 // The form of the audio level extension block:
1214 //
1215 // 0 1 2 3
1216 // 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
1217 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1218 // | ID | len=0 |V| level | 0x00 | 0x00 |
1219 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1220 //
1221 // Note that we always include 2 pad bytes, which will result in legal and
1222 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1223 // are implemented. Right now the pad bytes would anyway be required at end
1224 // of the extension block, so it makes no difference.
1225
1226 // Get id defined by user.
1227 uint8_t id;
1228 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1229 // Not registered.
1230 return 0;
1231 }
1232 size_t pos = 0;
1233 const uint8_t len = 0;
1234 data_buffer[pos++] = (id << 4) + len;
1235 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1236 data_buffer[pos++] = 0; // Padding.
1237 data_buffer[pos++] = 0; // Padding.
1238 // kAudioLevelLength is including pad bytes.
1239 assert(pos == kAudioLevelLength);
1240 return kAudioLevelLength;
1241}
1242
1243uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001244 // Absolute send time in RTP streams.
1245 //
1246 // The absolute send time is signaled to the receiver in-band using the
1247 // general mechanism for RTP header extensions [RFC5285]. The payload
1248 // of this extension (the transmitted value) is a 24-bit unsigned integer
1249 // containing the sender's current time in seconds as a fixed point number
1250 // with 18 bits fractional part.
1251 //
1252 // The form of the absolute send time extension block:
1253 //
1254 // 0 1 2 3
1255 // 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
1256 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1257 // | ID | len=2 | absolute send time |
1258 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1259
1260 // Get id defined by user.
1261 uint8_t id;
1262 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1263 &id) != 0) {
1264 // Not registered.
1265 return 0;
1266 }
1267 size_t pos = 0;
1268 const uint8_t len = 2;
1269 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001270 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos, absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001271 pos += 3;
1272 assert(pos == kAbsoluteSendTimeLength);
1273 return kAbsoluteSendTimeLength;
1274}
1275
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001276void RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001277 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001278 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001279 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001280 // Get id.
1281 uint8_t id = 0;
1282 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1283 &id) != 0) {
1284 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001285 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001286 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001287 // Get length until start of header extension block.
1288 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001289 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001290 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001291 if (extension_block_pos < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001292 LOG(LS_WARNING)
1293 << "Failed to update transmission time offset, not registered.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001294 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001295 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001296 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001297 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001298 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001299 block_pos + kTransmissionTimeOffsetLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001300 LOG(LS_WARNING)
1301 << "Failed to update transmission time offset, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001302 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001303 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001304 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001305 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1306 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001307 LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1308 "extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001309 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001310 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001311 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001312 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001313 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001314 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001315 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001316 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001317 // Update transmission offset field (converting to a 90 kHz timestamp).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001318 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1319 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001320}
1321
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001322bool RTPSender::UpdateAudioLevel(uint8_t *rtp_packet,
1323 const uint16_t rtp_packet_length,
1324 const RTPHeader &rtp_header,
1325 const bool is_voiced,
1326 const uint8_t dBov) const {
1327 CriticalSectionScoped cs(send_critsect_);
1328
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001329 // Get id.
1330 uint8_t id = 0;
1331 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1332 // Not registered.
1333 return false;
1334 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001335 // Get length until start of header extension block.
1336 int extension_block_pos =
1337 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1338 kRtpExtensionAudioLevel);
1339 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001340 // The feature is not enabled.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001341 return false;
1342 }
1343 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1344 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1345 rtp_header.headerLength < block_pos + kAudioLevelLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001346 LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001347 return false;
1348 }
1349 // Verify that header contains extension.
1350 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1351 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001352 LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001353 return false;
1354 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001355 // Verify first byte in block.
1356 const uint8_t first_block_byte = (id << 4) + 0;
1357 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001358 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001359 return false;
1360 }
1361 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1362 return true;
1363}
1364
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001365void RTPSender::UpdateAbsoluteSendTime(
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001366 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001367 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001368 CriticalSectionScoped cs(send_critsect_);
1369
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001370 // Get id.
1371 uint8_t id = 0;
1372 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1373 &id) != 0) {
1374 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001375 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001376 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001377 // Get length until start of header extension block.
1378 int extension_block_pos =
1379 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1380 kRtpExtensionAbsoluteSendTime);
1381 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001382 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001383 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001384 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001385 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001386 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001387 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001388 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001389 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001390 }
1391 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001392 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1393 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001394 LOG(LS_WARNING)
1395 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001396 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001397 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001398 // Verify first byte in block.
1399 const uint8_t first_block_byte = (id << 4) + 2;
1400 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001401 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001402 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001403 }
1404 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1405 // fractional part).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001406 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1407 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001408}
1409
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001410void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001411 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001412 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001413 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001414
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001415 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001416 SetStartTimestamp(RTPtime, false);
1417 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001418 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001419 if (!ssrc_forced_) {
1420 // Generate a new SSRC.
1421 ssrc_db_.ReturnSSRC(ssrc_);
1422 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001423 }
1424 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001425 if (!sequence_number_forced_ && !ssrc_forced_) {
1426 // Generate a new sequence number.
1427 sequence_number_ =
1428 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001429 }
1430 }
1431}
1432
1433void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001434 CriticalSectionScoped cs(send_critsect_);
1435 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001436}
1437
1438bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001439 CriticalSectionScoped cs(send_critsect_);
1440 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001441}
1442
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001443uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001444 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001445 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001446}
1447
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001448void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001449 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001450 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001451 start_timestamp_forced_ = true;
1452 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001453 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001454 if (!start_timestamp_forced_) {
1455 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001456 }
1457 }
1458}
1459
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001460uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001461 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001462 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001463}
1464
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001465uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001466 // If configured via API, return 0.
1467 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001468
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001469 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001470 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001471 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001472 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1473 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001474}
1475
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001476void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001477 // This is configured via the API.
1478 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001479
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001480 if (ssrc_ == ssrc && ssrc_forced_) {
1481 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001482 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001483 ssrc_forced_ = true;
1484 ssrc_db_.ReturnSSRC(ssrc_);
1485 ssrc_db_.RegisterSSRC(ssrc);
1486 ssrc_ = ssrc;
1487 if (!sequence_number_forced_) {
1488 sequence_number_ =
1489 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001490 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001491}
1492
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001493uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001494 CriticalSectionScoped cs(send_critsect_);
1495 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001496}
1497
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001498void RTPSender::SetCSRCStatus(const bool include) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001499 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001500 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001501}
1502
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001503void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1504 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001505 assert(arr_length <= kRtpCsrcSize);
1506 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001507
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001508 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001509 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001510 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001511 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001512}
1513
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001514int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001515 assert(arr_of_csrc);
1516 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001517 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1518 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001519 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001520 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001521}
1522
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001523void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001524 CriticalSectionScoped cs(send_critsect_);
1525 sequence_number_forced_ = true;
1526 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001527}
1528
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001529uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001530 CriticalSectionScoped cs(send_critsect_);
1531 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001532}
1533
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001534// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001535int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1536 const uint16_t time_ms,
1537 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001538 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001539 return -1;
1540 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001541 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001542}
1543
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001544bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001545 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001546 return false;
1547 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001548 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001549}
1550
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001551int32_t RTPSender::SetAudioPacketSize(
1552 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001553 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001554 return -1;
1555 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001556 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001557}
1558
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001559int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001560 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001561}
1562
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001563int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001564 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001565 return -1;
1566 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001567 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001568}
1569
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001570int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001571 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001572 return -1;
1573 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001574 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001575}
1576
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001577// Video
1578VideoCodecInformation *RTPSender::CodecInformationVideo() {
1579 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001580 return NULL;
1581 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001582 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001583}
1584
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001585RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001586 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001587 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001588}
1589
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001590uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001591 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001592 return 0;
1593 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001594 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001595}
1596
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001597int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001598 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001599 return -1;
1600 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001601 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001602}
1603
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001604int32_t RTPSender::SetGenericFECStatus(
1605 const bool enable, const uint8_t payload_type_red,
1606 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001607 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001608 return -1;
1609 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001610 return video_->SetGenericFECStatus(enable, payload_type_red,
1611 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001612}
1613
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001614int32_t RTPSender::GenericFECStatus(
1615 bool *enable, uint8_t *payload_type_red,
1616 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001617 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001618 return -1;
1619 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001620 return video_->GenericFECStatus(
1621 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001622}
1623
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001624int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001625 const FecProtectionParams *delta_params,
1626 const FecProtectionParams *key_params) {
1627 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001628 return -1;
1629 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001630 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001631}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001632
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001633void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1634 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001635 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001636 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001637 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001638 RtpUtility::RtpHeaderParser rtp_parser(
1639 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001640
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001641 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001642 rtp_parser.Parse(rtp_header);
1643
1644 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001645 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001646
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001647 // Replace payload type, if a specific type is set for RTX.
1648 if (payload_type_rtx_ != -1) {
1649 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001650 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001651 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1652 }
1653
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001654 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001655 uint8_t *ptr = data_buffer_rtx + 2;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001656 RtpUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001657
1658 // Replace SSRC.
1659 ptr += 6;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001660 RtpUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001661
1662 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001663 ptr = data_buffer_rtx + rtp_header.headerLength;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001664 RtpUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001665 ptr += 2;
1666
1667 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001668 memcpy(ptr, buffer + rtp_header.headerLength,
1669 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001670 *length += 2;
1671}
1672
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001673void RTPSender::RegisterRtpStatisticsCallback(
1674 StreamDataCountersCallback* callback) {
1675 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001676 rtp_stats_callback_ = callback;
1677}
1678
1679StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1680 CriticalSectionScoped cs(statistics_crit_.get());
1681 return rtp_stats_callback_;
1682}
1683
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001684uint32_t RTPSender::BitrateSent() const { return bitrate_sent_.BitrateLast(); }
1685
1686void RTPSender::BitrateUpdated(const BitrateStatistics& stats) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001687 uint32_t ssrc;
1688 {
1689 CriticalSectionScoped ssrc_lock(send_critsect_);
1690 ssrc = ssrc_;
1691 }
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001692 if (bitrate_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001693 bitrate_callback_->Notify(stats, ssrc);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001694 }
1695}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001696
1697void RTPSender::SetRtpState(const RtpState& rtp_state) {
1698 SetStartTimestamp(rtp_state.start_timestamp, true);
1699 CriticalSectionScoped lock(send_critsect_);
1700 sequence_number_ = rtp_state.sequence_number;
1701 sequence_number_forced_ = true;
1702 timestamp_ = rtp_state.timestamp;
1703 capture_time_ms_ = rtp_state.capture_time_ms;
1704 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001705 media_has_been_sent_ = rtp_state.media_has_been_sent;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001706}
1707
1708RtpState RTPSender::GetRtpState() const {
1709 CriticalSectionScoped lock(send_critsect_);
1710
1711 RtpState state;
1712 state.sequence_number = sequence_number_;
1713 state.start_timestamp = start_timestamp_;
1714 state.timestamp = timestamp_;
1715 state.capture_time_ms = capture_time_ms_;
1716 state.last_timestamp_time_ms = last_timestamp_time_ms_;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001717 state.media_has_been_sent = media_has_been_sent_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001718
1719 return state;
1720}
1721
1722void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
1723 CriticalSectionScoped lock(send_critsect_);
1724 sequence_number_rtx_ = rtp_state.sequence_number;
1725}
1726
1727RtpState RTPSender::GetRtxRtpState() const {
1728 CriticalSectionScoped lock(send_critsect_);
1729
1730 RtpState state;
1731 state.sequence_number = sequence_number_rtx_;
1732 state.start_timestamp = start_timestamp_;
1733
1734 return state;
1735}
1736
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001737} // namespace webrtc