blob: 7c4307f01a9d8ca9e19dcea846e42c37f497a2be [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.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000025const size_t 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
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +000043class BitrateAggregator {
44 public:
45 explicit BitrateAggregator(BitrateStatisticsObserver* bitrate_callback)
46 : callback_(bitrate_callback),
47 total_bitrate_observer_(*this),
48 retransmit_bitrate_observer_(*this),
49 ssrc_(0) {}
50
51 void OnStatsUpdated() const {
52 if (callback_)
53 callback_->Notify(total_bitrate_observer_.statistics(),
54 retransmit_bitrate_observer_.statistics(),
55 ssrc_);
56 }
57
58 Bitrate::Observer* total_bitrate_observer() {
59 return &total_bitrate_observer_;
60 }
61 Bitrate::Observer* retransmit_bitrate_observer() {
62 return &retransmit_bitrate_observer_;
63 }
64
65 void set_ssrc(uint32_t ssrc) { ssrc_ = ssrc; }
66
67 private:
68 // We assume that these observers are called on the same thread, which is
69 // true for RtpSender as they are called on the Process thread.
70 class BitrateObserver : public Bitrate::Observer {
71 public:
72 explicit BitrateObserver(const BitrateAggregator& aggregator)
73 : aggregator_(aggregator) {}
74
75 // Implements Bitrate::Observer.
76 virtual void BitrateUpdated(const BitrateStatistics& stats) OVERRIDE {
77 statistics_ = stats;
78 aggregator_.OnStatsUpdated();
79 }
80
81 BitrateStatistics statistics() const { return statistics_; }
82
83 private:
84 BitrateStatistics statistics_;
85 const BitrateAggregator& aggregator_;
86 };
87
88 BitrateStatisticsObserver* const callback_;
89 BitrateObserver total_bitrate_observer_;
90 BitrateObserver retransmit_bitrate_observer_;
91 uint32_t ssrc_;
92};
93
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000094RTPSender::RTPSender(const int32_t id,
95 const bool audio,
96 Clock* clock,
97 Transport* transport,
98 RtpAudioFeedback* audio_feedback,
andresp@webrtc.orgd11bec42014-07-08 14:32:58 +000099 PacedSender* paced_sender,
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000100 BitrateStatisticsObserver* bitrate_callback,
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000101 FrameCountObserver* frame_count_observer,
102 SendSideDelayObserver* send_side_delay_observer)
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000103 : clock_(clock),
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000104 // TODO(holmer): Remove this conversion when we remove the use of
105 // TickTime.
106 clock_delta_ms_(clock_->TimeInMilliseconds() -
107 TickTime::MillisecondTimestamp()),
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000108 bitrates_(new BitrateAggregator(bitrate_callback)),
109 total_bitrate_sent_(clock, bitrates_->total_bitrate_observer()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000110 id_(id),
111 audio_configured_(audio),
112 audio_(NULL),
113 video_(NULL),
114 paced_sender_(paced_sender),
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000115 last_capture_time_ms_sent_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000116 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000117 transport_(transport),
118 sending_media_(true), // Default to sending media.
119 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000120 packet_over_head_(28),
121 payload_type_(-1),
122 payload_type_map_(),
123 rtp_header_extension_map_(),
124 transmission_time_offset_(0),
125 absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000126 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000127 nack_byte_count_times_(),
128 nack_byte_count_(),
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000129 nack_bitrate_(clock, bitrates_->retransmit_bitrate_observer()),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000130 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000131 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000132 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000133 rtp_stats_callback_(NULL),
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000134 frame_count_observer_(frame_count_observer),
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000135 send_side_delay_observer_(send_side_delay_observer),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000136 // RTP variables
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000137 start_timestamp_forced_(false),
138 start_timestamp_(0),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000139 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
140 remote_ssrc_(0),
141 sequence_number_forced_(false),
142 ssrc_forced_(false),
143 timestamp_(0),
144 capture_time_ms_(0),
145 last_timestamp_time_ms_(0),
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000146 media_has_been_sent_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000147 last_packet_marker_bit_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000148 csrcs_(),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000149 rtx_(kRtxOff),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000150 payload_type_rtx_(-1),
151 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000152 target_bitrate_(0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000153 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
154 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000155 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000156 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000157 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000158 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000159 bitrates_->set_ssrc(ssrc_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000160 // Random start, 16 bits. Can't be 0.
161 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
162 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000163
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000164 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000165 audio_ = new RTPSenderAudio(id, clock_, this);
166 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000167 } else {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000168 video_ = new RTPSenderVideo(clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000169 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000170}
171
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000172RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000173 if (remote_ssrc_ != 0) {
174 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000175 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000176 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000178 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000179 delete send_critsect_;
180 while (!payload_type_map_.empty()) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000181 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000182 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000183 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000184 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000185 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000186 delete audio_;
187 delete video_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000188}
niklase@google.com470e71d2011-07-07 08:21:25 +0000189
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000190void RTPSender::SetTargetBitrate(uint32_t bitrate) {
191 CriticalSectionScoped cs(target_bitrate_critsect_.get());
192 target_bitrate_ = bitrate;
193}
194
195uint32_t RTPSender::GetTargetBitrate() {
196 CriticalSectionScoped cs(target_bitrate_critsect_.get());
197 return target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000198}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000199
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000200uint16_t RTPSender::ActualSendBitrateKbit() const {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000201 return (uint16_t)(total_bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202}
203
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000204uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000205 if (video_) {
206 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000207 }
208 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000209}
210
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000211uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000212 if (video_) {
213 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000214 }
215 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000216}
217
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000218uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000219 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000220}
221
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000222bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
223 int* max_send_delay_ms) const {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000224 CriticalSectionScoped lock(statistics_crit_.get());
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000225 SendDelayMap::const_iterator it = send_delays_.upper_bound(
226 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000227 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000228 return false;
229 int num_delays = 0;
230 for (; it != send_delays_.end(); ++it) {
231 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
232 *avg_send_delay_ms += it->second;
233 ++num_delays;
234 }
235 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
236 return true;
237}
238
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000239int32_t RTPSender::SetTransmissionTimeOffset(
240 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000241 if (transmission_time_offset > (0x800000 - 1) ||
242 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000243 return -1;
244 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000245 CriticalSectionScoped cs(send_critsect_);
246 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000247 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000248}
249
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000250int32_t RTPSender::SetAbsoluteSendTime(
251 const uint32_t absolute_send_time) {
252 if (absolute_send_time > 0xffffff) { // UWord24.
253 return -1;
254 }
255 CriticalSectionScoped cs(send_critsect_);
256 absolute_send_time_ = absolute_send_time;
257 return 0;
258}
259
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000260int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
261 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000262 CriticalSectionScoped cs(send_critsect_);
263 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000264}
265
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000266int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000267 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000268 CriticalSectionScoped cs(send_critsect_);
269 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000270}
271
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000272size_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000273 CriticalSectionScoped cs(send_critsect_);
274 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000275}
276
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000277int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000278 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000279 const int8_t payload_number, const uint32_t frequency,
280 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000281 assert(payload_name);
282 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000283
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000284 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000285 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000286
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000287 if (payload_type_map_.end() != it) {
288 // We already use this payload type.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000289 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000290 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000291
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000292 // Check if it's the same as we already have.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000293 if (RtpUtility::StringCompare(
294 payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000295 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000296 payload->typeSpecific.Audio.frequency == frequency &&
297 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000298 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000299 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000300 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000301 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000302 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000303 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000304 return 0;
305 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000306 }
307 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000308 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000309 int32_t ret_val = -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000310 RtpUtility::Payload* payload = NULL;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000311 if (audio_configured_) {
312 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
313 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000314 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000315 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
316 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000317 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000318 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000319 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000320 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000321 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000322}
323
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000324int32_t RTPSender::DeRegisterSendPayload(
325 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000326 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000327
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000328 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000329 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000330
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000331 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000332 return -1;
333 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000334 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000335 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000336 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000337 return 0;
338}
niklase@google.com470e71d2011-07-07 08:21:25 +0000339
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000340void RTPSender::SetSendPayloadType(int8_t payload_type) {
341 CriticalSectionScoped cs(send_critsect_);
342 payload_type_ = payload_type;
343}
344
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000345int8_t RTPSender::SendPayloadType() const {
346 CriticalSectionScoped cs(send_critsect_);
347 return payload_type_;
348}
niklase@google.com470e71d2011-07-07 08:21:25 +0000349
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000350int RTPSender::SendPayloadFrequency() const {
351 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
352}
niklase@google.com470e71d2011-07-07 08:21:25 +0000353
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000354int32_t RTPSender::SetMaxPayloadLength(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000355 const size_t max_payload_length,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000356 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000357 // Sanity check.
358 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000359 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000360 return -1;
361 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000362 CriticalSectionScoped cs(send_critsect_);
363 max_payload_length_ = max_payload_length;
364 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000365 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000366}
367
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000368size_t RTPSender::MaxDataPayloadLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000369 int rtx;
370 {
371 CriticalSectionScoped rtx_lock(send_critsect_);
372 rtx = rtx_;
373 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000374 if (audio_configured_) {
375 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000376 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000377 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
378 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000379 - ((rtx) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000380 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000381}
382
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000383size_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000384 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000385}
386
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000387uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000388
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000389void RTPSender::SetRTXStatus(int mode) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000390 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000391 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000392}
393
394void RTPSender::SetRtxSsrc(uint32_t ssrc) {
395 CriticalSectionScoped cs(send_critsect_);
396 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000397}
398
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000399uint32_t RTPSender::RtxSsrc() const {
400 CriticalSectionScoped cs(send_critsect_);
401 return ssrc_rtx_;
402}
403
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000404void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000405 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000406 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000407 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000408 *ssrc = ssrc_rtx_;
409 *payload_type = payload_type_rtx_;
410}
411
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000412void RTPSender::SetRtxPayloadType(int payload_type) {
413 CriticalSectionScoped cs(send_critsect_);
414 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000415}
416
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000417int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
418 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000419 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000420
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000421 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000422 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000423 return -1;
424 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000425 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000426 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000427 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000428 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000429 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000430 // And it's a match...
431 return 0;
432 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000433 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000434 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000435 if (payload_type_ == payload_type) {
436 if (!audio_configured_) {
437 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000438 }
439 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000440 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000441 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000442 payload_type_map_.find(payload_type);
443 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000444 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000445 return -1;
446 }
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000447 SetSendPayloadType(payload_type);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000448 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000449 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000450 if (!payload->audio && !audio_configured_) {
451 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
452 *video_type = payload->typeSpecific.Video.videoCodecType;
453 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000454 }
455 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000456}
457
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000458int32_t RTPSender::SendOutgoingData(
459 const FrameType frame_type, const int8_t payload_type,
460 const uint32_t capture_timestamp, int64_t capture_time_ms,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000461 const uint8_t *payload_data, const size_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000462 const RTPFragmentationHeader *fragmentation,
463 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000464 uint32_t ssrc;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000465 {
466 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000467 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000468 ssrc = ssrc_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000469 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000470 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000471 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000472 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000473 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000474 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000475 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000476 return -1;
477 }
478
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000479 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000480 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000481 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
482 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000483 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000484 frame_type == kFrameEmpty);
485
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000486 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
487 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000488 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000489 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
490 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000491 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000492
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000493 if (frame_type == kFrameEmpty)
494 return 0;
495
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000496 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
497 capture_timestamp, capture_time_ms,
498 payload_data, payload_size,
499 fragmentation, codec_info,
500 rtp_type_hdr);
501
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000502 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000503
504 CriticalSectionScoped cs(statistics_crit_.get());
505 uint32_t frame_count = ++frame_counts_[frame_type];
506 if (frame_count_observer_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000507 frame_count_observer_->FrameCountUpdated(frame_type, frame_count, ssrc);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000508 }
509
510 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000511}
512
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000513size_t RTPSender::TrySendRedundantPayloads(size_t bytes_to_send) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000514 {
515 CriticalSectionScoped cs(send_critsect_);
516 if ((rtx_ & kRtxRedundantPayloads) == 0)
517 return 0;
518 }
519
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000520 uint8_t buffer[IP_PACKET_SIZE];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000521 int bytes_left = static_cast<int>(bytes_to_send);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000522 while (bytes_left > 0) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000523 size_t length = bytes_left;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000524 int64_t capture_time_ms;
525 if (!packet_history_.GetBestFittingPacket(buffer, &length,
526 &capture_time_ms)) {
527 break;
528 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000529 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000530 break;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000531 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000532 RTPHeader rtp_header;
533 rtp_parser.Parse(rtp_header);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000534 bytes_left -= static_cast<int>(length - rtp_header.headerLength);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000535 }
536 return bytes_to_send - bytes_left;
537}
538
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000539size_t RTPSender::BuildPaddingPacket(uint8_t* packet, size_t header_length) {
540 size_t padding_bytes_in_packet = kMaxPaddingLength;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000541 packet[0] |= 0x20; // Set padding bit.
542 int32_t *data =
543 reinterpret_cast<int32_t *>(&(packet[header_length]));
544
545 // Fill data buffer with random data.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000546 for (size_t j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000547 data[j] = rand(); // NOLINT
548 }
549 // Set number of padding bytes in the last byte of the packet.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000550 packet[header_length + padding_bytes_in_packet - 1] =
551 static_cast<uint8_t>(padding_bytes_in_packet);
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000552 return padding_bytes_in_packet;
553}
554
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000555size_t RTPSender::TrySendPadData(size_t bytes) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000556 int64_t capture_time_ms;
557 uint32_t timestamp;
558 {
559 CriticalSectionScoped cs(send_critsect_);
560 timestamp = timestamp_;
561 capture_time_ms = capture_time_ms_;
562 if (last_timestamp_time_ms_ > 0) {
563 timestamp +=
564 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
565 capture_time_ms +=
566 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
567 }
568 }
569 return SendPadData(timestamp, capture_time_ms, bytes);
570}
571
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000572size_t RTPSender::SendPadData(uint32_t timestamp,
573 int64_t capture_time_ms,
574 size_t bytes) {
575 size_t padding_bytes_in_packet = 0;
576 size_t bytes_sent = 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000577 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000578 // Always send full padding packets.
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000579 if (bytes < kMaxPaddingLength)
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000580 bytes = kMaxPaddingLength;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000581
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000582 uint32_t ssrc;
583 uint16_t sequence_number;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000584 int payload_type;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000585 bool over_rtx;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000586 {
587 CriticalSectionScoped cs(send_critsect_);
588 // Only send padding packets following the last packet of a frame,
589 // indicated by the marker bit.
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000590 if (rtx_ == kRtxOff) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000591 // Without RTX we can't send padding in the middle of frames.
592 if (!last_packet_marker_bit_)
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000593 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000594 ssrc = ssrc_;
595 sequence_number = sequence_number_;
596 ++sequence_number_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000597 payload_type = payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000598 over_rtx = false;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000599 } else {
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000600 // Without abs-send-time a media packet must be sent before padding so
601 // that the timestamps used for estimation are correct.
602 if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
603 kRtpExtensionAbsoluteSendTime))
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000604 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000605 ssrc = ssrc_rtx_;
606 sequence_number = sequence_number_rtx_;
607 ++sequence_number_rtx_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000608 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_
609 : payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000610 over_rtx = true;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000611 }
612 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000613
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000614 uint8_t padding_packet[IP_PACKET_SIZE];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000615 size_t header_length =
616 CreateRtpHeader(padding_packet, payload_type, ssrc, false, timestamp,
617 sequence_number, std::vector<uint32_t>());
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000618 assert(header_length != static_cast<size_t>(-1));
619 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length);
620 assert(padding_bytes_in_packet <= bytes);
621 size_t length = padding_bytes_in_packet + header_length;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000622 int64_t now_ms = clock_->TimeInMilliseconds();
623
624 RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
625 RTPHeader rtp_header;
626 rtp_parser.Parse(rtp_header);
627
628 if (capture_time_ms > 0) {
629 UpdateTransmissionTimeOffset(
630 padding_packet, length, rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000631 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000632
633 UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
634 if (!SendPacketToNetwork(padding_packet, length))
635 break;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000636 bytes_sent += padding_bytes_in_packet;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000637 UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000638 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000639
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000640 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000641}
642
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000643void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000644 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000645 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000646}
647
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000648bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000649 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000650}
niklase@google.com470e71d2011-07-07 08:21:25 +0000651
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000652int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000653 size_t length = IP_PACKET_SIZE;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000654 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000655 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000656 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
657 data_buffer, &length,
658 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000659 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000660 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000661 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000662
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000663 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000664 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000665 RTPHeader header;
666 if (!rtp_parser.Parse(header)) {
667 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000668 return -1;
669 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000670 // Convert from TickTime to Clock since capture_time_ms is based on
671 // TickTime.
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000672 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_;
673 if (!paced_sender_->SendPacket(
674 PacedSender::kHighPriority, header.ssrc, header.sequenceNumber,
675 corrected_capture_tims_ms, length - header.headerLength, true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000676 // We can't send the packet right now.
677 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000678 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000679 }
680 }
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000681 int rtx = kRtxOff;
682 {
683 CriticalSectionScoped lock(send_critsect_);
684 rtx = rtx_;
685 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000686 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000687 (rtx & kRtxRetransmitted) > 0, true) ?
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000688 static_cast<int32_t>(length) : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000689}
690
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000691bool RTPSender::SendPacketToNetwork(const uint8_t *packet, size_t size) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000692 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000693 if (transport_) {
694 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000695 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000696 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
697 "size", size, "sent", bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000698 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000699 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000700 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000701 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000702 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000703 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000704}
705
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000706int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000707 if (!video_)
708 return -1;
709 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000710}
711
712int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000713 if (!video_)
714 return -1;
715 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000716}
717
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000718void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000719 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000720 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000721 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
722 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000723 const int64_t now = clock_->TimeInMilliseconds();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000724 size_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000725 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000726
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000727 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000728 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000729 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000730 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000731 return;
732 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000733
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000734 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
735 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000736 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000737 if (bytes_sent > 0) {
738 bytes_re_sent += bytes_sent;
739 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000740 // The packet has previously been resent.
741 // Try resending next packet in the list.
742 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000743 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000744 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000745 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
746 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000747 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000748 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000749 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000750 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000751 // kbits/s * ms = bits => bits/8 = bytes
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000752 size_t target_bytes =
753 (static_cast<size_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000754 if (bytes_re_sent > target_bytes) {
755 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000756 }
757 }
758 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000759 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000760 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000761 UpdateNACKBitRate(bytes_re_sent, now);
762 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000763 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000764}
765
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000766bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
767 uint32_t num = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000768 size_t byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000769 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000770 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000771
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000772 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000773
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000774 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000775 return true;
776 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000777 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000778 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000779 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000780 break;
781 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000782 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000783 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000784 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000785 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000786 if (num == NACK_BYTECOUNT_SIZE) {
787 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000788 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000789 if (nack_byte_count_times_[num - 1] <= now) {
790 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000791 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000792 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000793 return (byte_count * 8) < (target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000794}
795
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000796void RTPSender::UpdateNACKBitRate(const size_t bytes,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000797 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000798 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000799
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000800 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000801 if (bytes > 0) {
802 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000803 // Add padding length.
804 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000805 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000806 if (nack_byte_count_times_[0] == 0) {
807 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000808 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000809 // Shift.
810 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
811 nack_byte_count_[i + 1] = nack_byte_count_[i];
812 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000813 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000814 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000815 nack_byte_count_[0] = bytes;
816 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000817 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000818 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000819}
820
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000821// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000822bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000823 int64_t capture_time_ms,
824 bool retransmission) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000825 size_t length = IP_PACKET_SIZE;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000826 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000827 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000828
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000829 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
830 0,
831 retransmission,
832 data_buffer,
833 &length,
834 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000835 // Packet cannot be found. Allow sending to continue.
836 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000837 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000838 if (!retransmission && capture_time_ms > 0) {
839 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
840 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000841 int rtx;
842 {
843 CriticalSectionScoped lock(send_critsect_);
844 rtx = rtx_;
845 }
846 return PrepareAndSendPacket(data_buffer,
847 length,
848 capture_time_ms,
849 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000850 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000851}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000852
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000853bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000854 size_t length,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000855 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000856 bool send_over_rtx,
857 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000858 uint8_t *buffer_to_send_ptr = buffer;
859
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000860 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000861 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000862 rtp_parser.Parse(rtp_header);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000863 if (!is_retransmit && rtp_header.markerBit) {
864 TRACE_EVENT_ASYNC_END0("webrtc_rtp", "PacedSend", capture_time_ms);
865 }
866
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000867 TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000868 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000869 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000870
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000871 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000872 if (send_over_rtx) {
873 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000874 buffer_to_send_ptr = data_buffer_rtx;
875 }
876
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000877 int64_t now_ms = clock_->TimeInMilliseconds();
878 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000879 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
880 diff_ms);
881 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000882 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000883 if (ret) {
884 CriticalSectionScoped lock(send_critsect_);
885 media_has_been_sent_ = true;
886 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000887 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
888 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000889 return ret;
890}
891
892void RTPSender::UpdateRtpStats(const uint8_t* buffer,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000893 size_t packet_length,
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000894 const RTPHeader& header,
895 bool is_rtx,
896 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000897 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000898 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000899 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000900
901 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000902 if (is_rtx) {
903 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000904 } else {
905 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000906 }
907
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000908 total_bitrate_sent_.Update(packet_length);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000909 ++counters->packets;
910 if (IsFecPacket(buffer, header)) {
911 ++counters->fec_packets;
912 }
913
914 if (is_retransmit) {
915 ++counters->retransmitted_packets;
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000916 counters->retransmitted_bytes +=
917 packet_length - (header.headerLength + header.paddingLength);
918 counters->retransmitted_header_bytes += header.headerLength;
919 counters->retransmitted_padding_bytes += header.paddingLength;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000920 }
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000921 counters->bytes +=
922 packet_length - (header.headerLength + header.paddingLength);
923 counters->header_bytes += header.headerLength;
924 counters->padding_bytes += header.paddingLength;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000925
926 if (rtp_stats_callback_) {
927 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
928 }
929}
930
931bool RTPSender::IsFecPacket(const uint8_t* buffer,
932 const RTPHeader& header) const {
933 if (!video_) {
934 return false;
935 }
936 bool fec_enabled;
937 uint8_t pt_red;
938 uint8_t pt_fec;
939 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
940 return fec_enabled &&
941 header.payloadType == pt_red &&
942 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000943}
944
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000945size_t RTPSender::TimeToSendPadding(size_t bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000946 {
947 CriticalSectionScoped cs(send_critsect_);
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000948 if (!sending_media_) return 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000949 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000950 if (bytes == 0)
951 return 0;
952 size_t bytes_sent = TrySendRedundantPayloads(bytes);
953 if (bytes_sent < bytes)
954 bytes_sent += TrySendPadData(bytes - bytes_sent);
955 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000956}
957
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000958// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000959int32_t RTPSender::SendToNetwork(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000960 uint8_t *buffer, size_t payload_length, size_t rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000961 int64_t capture_time_ms, StorageType storage,
962 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000963 RtpUtility::RtpHeaderParser rtp_parser(buffer,
964 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000965 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000966 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000967
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000968 int64_t now_ms = clock_->TimeInMilliseconds();
969
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000970 // |capture_time_ms| <= 0 is considered invalid.
971 // TODO(holmer): This should be changed all over Video Engine so that negative
972 // time is consider invalid, while 0 is considered a valid time.
973 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000974 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000975 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000976 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000977
978 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
979 rtp_header, now_ms);
980
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000981 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000982 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
983 max_payload_length_, capture_time_ms,
984 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000985 return -1;
986 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000987
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000988 if (paced_sender_ && storage != kDontStore) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000989 // Correct offset between implementations of millisecond time stamps in
990 // TickTime and Clock.
991 int64_t corrected_time_ms = capture_time_ms + clock_delta_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000992 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000993 rtp_header.sequenceNumber, corrected_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000994 payload_length, false)) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000995 if (last_capture_time_ms_sent_ == 0 ||
996 corrected_time_ms > last_capture_time_ms_sent_) {
997 last_capture_time_ms_sent_ = corrected_time_ms;
998 TRACE_EVENT_ASYNC_BEGIN1("webrtc_rtp", "PacedSend", corrected_time_ms,
999 "capture_time_ms", corrected_time_ms);
1000 }
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +00001001 // We can't send the packet right now.
1002 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +00001003 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001004 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +00001005 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001006 if (capture_time_ms > 0) {
1007 UpdateDelayStatistics(capture_time_ms, now_ms);
1008 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001009 size_t length = payload_length + rtp_header_length;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001010 if (!SendPacketToNetwork(buffer, length))
1011 return -1;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001012 {
1013 CriticalSectionScoped lock(send_critsect_);
1014 media_has_been_sent_ = true;
1015 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001016 UpdateRtpStats(buffer, length, rtp_header, false, false);
1017 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +00001018}
1019
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001020void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001021 uint32_t ssrc;
1022 int avg_delay_ms = 0;
1023 int max_delay_ms = 0;
1024 {
1025 CriticalSectionScoped lock(send_critsect_);
1026 ssrc = ssrc_;
1027 }
1028 {
1029 CriticalSectionScoped cs(statistics_crit_.get());
1030 // TODO(holmer): Compute this iteratively instead.
1031 send_delays_[now_ms] = now_ms - capture_time_ms;
1032 send_delays_.erase(send_delays_.begin(),
1033 send_delays_.lower_bound(now_ms -
1034 kSendSideDelayWindowMs));
1035 }
1036 if (send_side_delay_observer_ &&
1037 GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
1038 send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
1039 max_delay_ms, ssrc);
1040 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001041}
1042
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001043void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001044 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001045 total_bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001046 nack_bitrate_.Process();
1047 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001048 return;
1049 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001050 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +00001051}
1052
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001053size_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001054 CriticalSectionScoped lock(send_critsect_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001055 size_t rtp_header_length = 12;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001056 rtp_header_length += sizeof(uint32_t) * csrcs_.size();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001057 rtp_header_length += RtpHeaderExtensionTotalLength();
1058 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001059}
1060
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001061uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001062 CriticalSectionScoped cs(send_critsect_);
1063 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001064}
1065
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001066void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001067 uint32_t ssrc;
1068 uint32_t ssrc_rtx;
1069 {
1070 CriticalSectionScoped ssrc_lock(send_critsect_);
1071 ssrc = ssrc_;
1072 ssrc_rtx = ssrc_rtx_;
1073 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001074 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001075 rtp_stats_ = StreamDataCounters();
1076 rtx_rtp_stats_ = StreamDataCounters();
1077 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001078 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
1079 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001080 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001081}
1082
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001083void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1084 StreamDataCounters* rtx_stats) const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001085 CriticalSectionScoped lock(statistics_crit_.get());
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001086 *rtp_stats = rtp_stats_;
1087 *rtx_stats = rtx_rtp_stats_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001088}
1089
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001090size_t RTPSender::CreateRtpHeader(uint8_t* header,
1091 int8_t payload_type,
1092 uint32_t ssrc,
1093 bool marker_bit,
1094 uint32_t timestamp,
1095 uint16_t sequence_number,
1096 const std::vector<uint32_t>& csrcs) const {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001097 header[0] = 0x80; // version 2.
1098 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001099 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001100 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001101 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001102 RtpUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1103 RtpUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1104 RtpUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001105 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001106
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001107 if (csrcs.size() > 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001108 uint8_t *ptr = &header[rtp_header_length];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001109 for (size_t i = 0; i < csrcs.size(); ++i) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001110 RtpUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001111 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001112 }
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001113 header[0] = (header[0] & 0xf0) | csrcs.size();
niklase@google.com470e71d2011-07-07 08:21:25 +00001114
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001115 // Update length of header.
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001116 rtp_header_length += sizeof(uint32_t) * csrcs.size();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001117 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001118
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001119 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1120 if (len > 0) {
1121 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001122 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001123 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001124 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001125}
1126
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001127int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
1128 const int8_t payload_type,
1129 const bool marker_bit,
1130 const uint32_t capture_timestamp,
1131 int64_t capture_time_ms,
1132 const bool timestamp_provided,
1133 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001134 assert(payload_type >= 0);
1135 CriticalSectionScoped cs(send_critsect_);
1136
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001137 if (timestamp_provided) {
1138 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001139 } else {
1140 // Make a unique time stamp.
1141 // We can't inc by the actual time, since then we increase the risk of back
1142 // timing.
1143 timestamp_++;
1144 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001145 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001146 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001147 capture_time_ms_ = capture_time_ms;
1148 last_packet_marker_bit_ = marker_bit;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001149 return CreateRtpHeader(data_buffer, payload_type, ssrc_, marker_bit,
1150 timestamp_, sequence_number, csrcs_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001151}
1152
1153uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001154 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001155 return 0;
1156 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001157 // RTP header extension, RFC 3550.
1158 // 0 1 2 3
1159 // 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
1160 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1161 // | defined by profile | length |
1162 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1163 // | header extension |
1164 // | .... |
1165 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001166 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001167 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001168
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001169 // Add extension ID (0xBEDE).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001170 RtpUtility::AssignUWord16ToBuffer(data_buffer, kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001171
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001172 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001173 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001174
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001175 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001176 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001177 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001178 switch (type) {
1179 case kRtpExtensionTransmissionTimeOffset:
1180 block_length = BuildTransmissionTimeOffsetExtension(
1181 data_buffer + kHeaderLength + total_block_length);
1182 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001183 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001184 block_length = BuildAudioLevelExtension(
1185 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001186 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001187 case kRtpExtensionAbsoluteSendTime:
1188 block_length = BuildAbsoluteSendTimeExtension(
1189 data_buffer + kHeaderLength + total_block_length);
1190 break;
1191 default:
1192 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001193 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001194 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001195 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001196 }
1197 if (total_block_length == 0) {
1198 // No extension added.
1199 return 0;
1200 }
1201 // Set header length (in number of Word32, header excluded).
1202 assert(total_block_length % 4 == 0);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001203 RtpUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
1204 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001205 // Total added length.
1206 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001207}
1208
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001209uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1210 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001211 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1212 //
1213 // The transmission time is signaled to the receiver in-band using the
1214 // general mechanism for RTP header extensions [RFC5285]. The payload
1215 // of this extension (the transmitted value) is a 24-bit signed integer.
1216 // When added to the RTP timestamp of the packet, it represents the
1217 // "effective" RTP transmission time of the packet, on the RTP
1218 // timescale.
1219 //
1220 // The form of the transmission offset extension block:
1221 //
1222 // 0 1 2 3
1223 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1224 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1225 // | ID | len=2 | transmission offset |
1226 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001227
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001228 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001229 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001230 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1231 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001232 // Not registered.
1233 return 0;
1234 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001235 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001236 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001237 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001238 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos,
1239 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001240 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001241 assert(pos == kTransmissionTimeOffsetLength);
1242 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001243}
1244
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001245uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1246 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1247 //
1248 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1249 //
1250 // The form of the audio level extension block:
1251 //
1252 // 0 1 2 3
1253 // 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
1254 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1255 // | ID | len=0 |V| level | 0x00 | 0x00 |
1256 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1257 //
1258 // Note that we always include 2 pad bytes, which will result in legal and
1259 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1260 // are implemented. Right now the pad bytes would anyway be required at end
1261 // of the extension block, so it makes no difference.
1262
1263 // Get id defined by user.
1264 uint8_t id;
1265 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1266 // Not registered.
1267 return 0;
1268 }
1269 size_t pos = 0;
1270 const uint8_t len = 0;
1271 data_buffer[pos++] = (id << 4) + len;
1272 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1273 data_buffer[pos++] = 0; // Padding.
1274 data_buffer[pos++] = 0; // Padding.
1275 // kAudioLevelLength is including pad bytes.
1276 assert(pos == kAudioLevelLength);
1277 return kAudioLevelLength;
1278}
1279
1280uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001281 // Absolute send time in RTP streams.
1282 //
1283 // The absolute send time is signaled to the receiver in-band using the
1284 // general mechanism for RTP header extensions [RFC5285]. The payload
1285 // of this extension (the transmitted value) is a 24-bit unsigned integer
1286 // containing the sender's current time in seconds as a fixed point number
1287 // with 18 bits fractional part.
1288 //
1289 // The form of the absolute send time extension block:
1290 //
1291 // 0 1 2 3
1292 // 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
1293 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1294 // | ID | len=2 | absolute send time |
1295 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1296
1297 // Get id defined by user.
1298 uint8_t id;
1299 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1300 &id) != 0) {
1301 // Not registered.
1302 return 0;
1303 }
1304 size_t pos = 0;
1305 const uint8_t len = 2;
1306 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001307 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos, absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001308 pos += 3;
1309 assert(pos == kAbsoluteSendTimeLength);
1310 return kAbsoluteSendTimeLength;
1311}
1312
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001313void RTPSender::UpdateTransmissionTimeOffset(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001314 uint8_t *rtp_packet, const size_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001315 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001316 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001317 // Get id.
1318 uint8_t id = 0;
1319 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1320 &id) != 0) {
1321 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001322 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001323 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001324 // Get length until start of header extension block.
1325 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001326 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001327 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001328 if (extension_block_pos < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001329 LOG(LS_WARNING)
1330 << "Failed to update transmission time offset, not registered.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001331 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001332 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001333 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001334 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001335 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001336 block_pos + kTransmissionTimeOffsetLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001337 LOG(LS_WARNING)
1338 << "Failed to update transmission time offset, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001339 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001340 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001341 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001342 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1343 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001344 LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1345 "extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001346 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001347 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001348 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001349 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001350 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001351 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001352 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001353 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001354 // Update transmission offset field (converting to a 90 kHz timestamp).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001355 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1356 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001357}
1358
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001359bool RTPSender::UpdateAudioLevel(uint8_t *rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001360 const size_t rtp_packet_length,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001361 const RTPHeader &rtp_header,
1362 const bool is_voiced,
1363 const uint8_t dBov) const {
1364 CriticalSectionScoped cs(send_critsect_);
1365
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001366 // Get id.
1367 uint8_t id = 0;
1368 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1369 // Not registered.
1370 return false;
1371 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001372 // Get length until start of header extension block.
1373 int extension_block_pos =
1374 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1375 kRtpExtensionAudioLevel);
1376 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001377 // The feature is not enabled.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001378 return false;
1379 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001380 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001381 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1382 rtp_header.headerLength < block_pos + kAudioLevelLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001383 LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001384 return false;
1385 }
1386 // Verify that header contains extension.
1387 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1388 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001389 LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001390 return false;
1391 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001392 // Verify first byte in block.
1393 const uint8_t first_block_byte = (id << 4) + 0;
1394 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001395 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001396 return false;
1397 }
1398 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1399 return true;
1400}
1401
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001402void RTPSender::UpdateAbsoluteSendTime(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001403 uint8_t *rtp_packet, const size_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001404 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001405 CriticalSectionScoped cs(send_critsect_);
1406
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001407 // Get id.
1408 uint8_t id = 0;
1409 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1410 &id) != 0) {
1411 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001412 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001413 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001414 // Get length until start of header extension block.
1415 int extension_block_pos =
1416 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1417 kRtpExtensionAbsoluteSendTime);
1418 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001419 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001420 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001421 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001422 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001423 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001424 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001425 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001426 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001427 }
1428 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001429 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1430 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001431 LOG(LS_WARNING)
1432 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001433 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001434 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001435 // Verify first byte in block.
1436 const uint8_t first_block_byte = (id << 4) + 2;
1437 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001438 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001439 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001440 }
1441 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1442 // fractional part).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001443 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1444 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001445}
1446
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001447void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001448 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001449 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001450 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001451
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001452 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001453 SetStartTimestamp(RTPtime, false);
1454 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001455 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001456 if (!ssrc_forced_) {
1457 // Generate a new SSRC.
1458 ssrc_db_.ReturnSSRC(ssrc_);
1459 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001460 bitrates_->set_ssrc(ssrc_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001461 }
1462 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001463 if (!sequence_number_forced_ && !ssrc_forced_) {
1464 // Generate a new sequence number.
1465 sequence_number_ =
1466 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001467 }
1468 }
1469}
1470
1471void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001472 CriticalSectionScoped cs(send_critsect_);
1473 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001474}
1475
1476bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001477 CriticalSectionScoped cs(send_critsect_);
1478 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001479}
1480
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001481uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001482 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001483 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001484}
1485
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001486void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001487 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001488 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001489 start_timestamp_forced_ = true;
1490 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001491 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001492 if (!start_timestamp_forced_) {
1493 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001494 }
1495 }
1496}
1497
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001498uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001499 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001500 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001501}
1502
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001503uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001504 // If configured via API, return 0.
1505 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001506
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001507 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001508 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001509 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001510 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001511 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001512 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001513}
1514
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001515void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001516 // This is configured via the API.
1517 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001518
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001519 if (ssrc_ == ssrc && ssrc_forced_) {
1520 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001521 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001522 ssrc_forced_ = true;
1523 ssrc_db_.ReturnSSRC(ssrc_);
1524 ssrc_db_.RegisterSSRC(ssrc);
1525 ssrc_ = ssrc;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001526 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001527 if (!sequence_number_forced_) {
1528 sequence_number_ =
1529 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001530 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001531}
1532
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001533uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001534 CriticalSectionScoped cs(send_critsect_);
1535 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001536}
1537
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001538void RTPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
1539 assert(csrcs.size() <= kRtpCsrcSize);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001540 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001541 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001542}
1543
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001544void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001545 CriticalSectionScoped cs(send_critsect_);
1546 sequence_number_forced_ = true;
1547 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001548}
1549
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001550uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001551 CriticalSectionScoped cs(send_critsect_);
1552 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001553}
1554
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001555// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001556int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1557 const uint16_t time_ms,
1558 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001559 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001560 return -1;
1561 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001562 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001563}
1564
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001565bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001566 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001567 return false;
1568 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001569 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001570}
1571
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001572int32_t RTPSender::SetAudioPacketSize(
1573 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001574 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001575 return -1;
1576 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001577 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001578}
1579
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001580int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001581 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001582}
1583
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001584int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001585 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001586 return -1;
1587 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001588 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001589}
1590
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001591int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001592 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001593 return -1;
1594 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001595 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001596}
1597
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001598// Video
1599VideoCodecInformation *RTPSender::CodecInformationVideo() {
1600 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001601 return NULL;
1602 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001603 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001604}
1605
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001606RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001607 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001608 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001609}
1610
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001611uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001612 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001613 return 0;
1614 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001615 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001616}
1617
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001618int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001619 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001620 return -1;
1621 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001622 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001623}
1624
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001625int32_t RTPSender::SetGenericFECStatus(
1626 const bool enable, const uint8_t payload_type_red,
1627 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001628 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001629 return -1;
1630 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001631 return video_->SetGenericFECStatus(enable, payload_type_red,
1632 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001633}
1634
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001635int32_t RTPSender::GenericFECStatus(
1636 bool *enable, uint8_t *payload_type_red,
1637 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001638 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001639 return -1;
1640 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001641 return video_->GenericFECStatus(
1642 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001643}
1644
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001645int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001646 const FecProtectionParams *delta_params,
1647 const FecProtectionParams *key_params) {
1648 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001649 return -1;
1650 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001651 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001652}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001653
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001654void RTPSender::BuildRtxPacket(uint8_t* buffer, size_t* length,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001655 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001656 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001657 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001658 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001659 RtpUtility::RtpHeaderParser rtp_parser(
1660 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001661
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001662 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001663 rtp_parser.Parse(rtp_header);
1664
1665 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001666 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001667
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001668 // Replace payload type, if a specific type is set for RTX.
1669 if (payload_type_rtx_ != -1) {
1670 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001671 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001672 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1673 }
1674
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001675 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001676 uint8_t *ptr = data_buffer_rtx + 2;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001677 RtpUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001678
1679 // Replace SSRC.
1680 ptr += 6;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001681 RtpUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001682
1683 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001684 ptr = data_buffer_rtx + rtp_header.headerLength;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001685 RtpUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001686 ptr += 2;
1687
1688 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001689 memcpy(ptr, buffer + rtp_header.headerLength,
1690 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001691 *length += 2;
1692}
1693
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001694void RTPSender::RegisterRtpStatisticsCallback(
1695 StreamDataCountersCallback* callback) {
1696 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001697 rtp_stats_callback_ = callback;
1698}
1699
1700StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1701 CriticalSectionScoped cs(statistics_crit_.get());
1702 return rtp_stats_callback_;
1703}
1704
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001705uint32_t RTPSender::BitrateSent() const {
1706 return total_bitrate_sent_.BitrateLast();
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001707}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001708
1709void RTPSender::SetRtpState(const RtpState& rtp_state) {
1710 SetStartTimestamp(rtp_state.start_timestamp, true);
1711 CriticalSectionScoped lock(send_critsect_);
1712 sequence_number_ = rtp_state.sequence_number;
1713 sequence_number_forced_ = true;
1714 timestamp_ = rtp_state.timestamp;
1715 capture_time_ms_ = rtp_state.capture_time_ms;
1716 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001717 media_has_been_sent_ = rtp_state.media_has_been_sent;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001718}
1719
1720RtpState RTPSender::GetRtpState() const {
1721 CriticalSectionScoped lock(send_critsect_);
1722
1723 RtpState state;
1724 state.sequence_number = sequence_number_;
1725 state.start_timestamp = start_timestamp_;
1726 state.timestamp = timestamp_;
1727 state.capture_time_ms = capture_time_ms_;
1728 state.last_timestamp_time_ms = last_timestamp_time_ms_;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001729 state.media_has_been_sent = media_has_been_sent_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001730
1731 return state;
1732}
1733
1734void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
1735 CriticalSectionScoped lock(send_critsect_);
1736 sequence_number_rtx_ = rtp_state.sequence_number;
1737}
1738
1739RtpState RTPSender::GetRtxRtpState() const {
1740 CriticalSectionScoped lock(send_critsect_);
1741
1742 RtpState state;
1743 state.sequence_number = sequence_number_rtx_;
1744 state.start_timestamp = start_timestamp_;
1745
1746 return state;
1747}
1748
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001749} // namespace webrtc