blob: d7cc0bc8452c648be8bea81e036e2309d7c4217d [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
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000030const char* FrameTypeToString(FrameType frame_type) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000031 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
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000094RTPSender::RTPSender(int32_t id,
95 bool audio,
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000096 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),
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000150 payload_type_rtx_(-1),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000151 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.orgd16e8392014-12-19 13:49:55 +0000239int32_t RTPSender::SetTransmissionTimeOffset(int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000240 if (transmission_time_offset > (0x800000 - 1) ||
241 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000242 return -1;
243 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000244 CriticalSectionScoped cs(send_critsect_);
245 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000246 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000247}
248
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000249int32_t RTPSender::SetAbsoluteSendTime(uint32_t absolute_send_time) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000250 if (absolute_send_time > 0xffffff) { // UWord24.
251 return -1;
252 }
253 CriticalSectionScoped cs(send_critsect_);
254 absolute_send_time_ = absolute_send_time;
255 return 0;
256}
257
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000258int32_t RTPSender::RegisterRtpHeaderExtension(RTPExtensionType type,
259 uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000260 CriticalSectionScoped cs(send_critsect_);
261 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000262}
263
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000264int32_t RTPSender::DeregisterRtpHeaderExtension(RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000265 CriticalSectionScoped cs(send_critsect_);
266 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000267}
268
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000269size_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000270 CriticalSectionScoped cs(send_critsect_);
271 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000272}
273
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000274int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000275 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000276 int8_t payload_number,
277 uint32_t frequency,
278 uint8_t channels,
279 uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000280 assert(payload_name);
281 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000282
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000283 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000284 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000285
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000286 if (payload_type_map_.end() != it) {
287 // We already use this payload type.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000288 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000289 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000290
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000291 // Check if it's the same as we already have.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000292 if (RtpUtility::StringCompare(
293 payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000294 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000295 payload->typeSpecific.Audio.frequency == frequency &&
296 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000297 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000298 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000299 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000300 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000301 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000302 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000303 return 0;
304 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000305 }
306 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000307 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000308 int32_t ret_val = -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000309 RtpUtility::Payload* payload = NULL;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000310 if (audio_configured_) {
311 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
312 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000313 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000314 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
315 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000316 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000317 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000318 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000319 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000320 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000321}
322
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000323int32_t RTPSender::DeRegisterSendPayload(int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000324 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000325
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000326 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000327 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000328
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000329 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000330 return -1;
331 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000332 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000333 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000334 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000335 return 0;
336}
niklase@google.com470e71d2011-07-07 08:21:25 +0000337
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000338void RTPSender::SetSendPayloadType(int8_t payload_type) {
339 CriticalSectionScoped cs(send_critsect_);
340 payload_type_ = payload_type;
341}
342
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000343int8_t RTPSender::SendPayloadType() const {
344 CriticalSectionScoped cs(send_critsect_);
345 return payload_type_;
346}
niklase@google.com470e71d2011-07-07 08:21:25 +0000347
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000348int RTPSender::SendPayloadFrequency() const {
349 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
350}
niklase@google.com470e71d2011-07-07 08:21:25 +0000351
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000352int32_t RTPSender::SetMaxPayloadLength(size_t max_payload_length,
353 uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000354 // Sanity check.
355 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000356 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000357 return -1;
358 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000359 CriticalSectionScoped cs(send_critsect_);
360 max_payload_length_ = max_payload_length;
361 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000362 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000363}
364
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000365size_t RTPSender::MaxDataPayloadLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000366 int rtx;
367 {
368 CriticalSectionScoped rtx_lock(send_critsect_);
369 rtx = rtx_;
370 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000371 if (audio_configured_) {
372 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000373 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000374 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
375 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000376 - ((rtx) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000377 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000378}
379
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000380size_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000381 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000382}
383
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000384uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000385
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000386void RTPSender::SetRtxStatus(int mode) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000387 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000388 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000389}
390
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000391int RTPSender::RtxStatus() const {
392 CriticalSectionScoped cs(send_critsect_);
393 return rtx_;
394}
395
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000396void RTPSender::SetRtxSsrc(uint32_t ssrc) {
397 CriticalSectionScoped cs(send_critsect_);
398 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000399}
400
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000401uint32_t RTPSender::RtxSsrc() const {
402 CriticalSectionScoped cs(send_critsect_);
403 return ssrc_rtx_;
404}
405
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000406void RTPSender::SetRtxPayloadType(int payload_type) {
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000407 CriticalSectionScoped cs(send_critsect_);
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000408 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000409}
410
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000411int32_t RTPSender::CheckPayloadType(int8_t payload_type,
412 RtpVideoCodecTypes* video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000413 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000415 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000416 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000417 return -1;
418 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000419 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000420 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000421 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000422 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000423 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000424 // And it's a match...
425 return 0;
426 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000427 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000428 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000429 if (payload_type_ == payload_type) {
430 if (!audio_configured_) {
431 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000432 }
433 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000434 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000435 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000436 payload_type_map_.find(payload_type);
437 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000438 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000439 return -1;
440 }
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000441 SetSendPayloadType(payload_type);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000442 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000443 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000444 if (!payload->audio && !audio_configured_) {
445 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
446 *video_type = payload->typeSpecific.Video.videoCodecType;
447 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000448 }
449 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000450}
451
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000452int32_t RTPSender::SendOutgoingData(FrameType frame_type,
453 int8_t payload_type,
454 uint32_t capture_timestamp,
455 int64_t capture_time_ms,
456 const uint8_t* payload_data,
457 size_t payload_size,
458 const RTPFragmentationHeader* fragmentation,
459 VideoCodecInformation* codec_info,
460 const RTPVideoTypeHeader* rtp_type_hdr) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000461 uint32_t ssrc;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000462 {
463 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000464 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000465 ssrc = ssrc_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000466 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000467 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000468 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000469 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000470 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000471 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000472 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000473 return -1;
474 }
475
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000476 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000477 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000478 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
479 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000480 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000481 frame_type == kFrameEmpty);
482
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000483 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
484 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000485 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000486 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
487 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000488 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000489
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000490 if (frame_type == kFrameEmpty)
491 return 0;
492
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000493 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
494 capture_timestamp, capture_time_ms,
495 payload_data, payload_size,
496 fragmentation, codec_info,
497 rtp_type_hdr);
498
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000499 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000500
501 CriticalSectionScoped cs(statistics_crit_.get());
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000502 // Note: This is currently only counting for video.
503 if (frame_type == kVideoFrameKey) {
504 ++frame_counts_.key_frames;
505 } else if (frame_type == kVideoFrameDelta) {
506 ++frame_counts_.delta_frames;
507 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000508 if (frame_count_observer_) {
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000509 frame_count_observer_->FrameCountUpdated(frame_counts_, ssrc);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000510 }
511
512 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000513}
514
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000515size_t RTPSender::TrySendRedundantPayloads(size_t bytes_to_send) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000516 {
517 CriticalSectionScoped cs(send_critsect_);
518 if ((rtx_ & kRtxRedundantPayloads) == 0)
519 return 0;
520 }
521
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000522 uint8_t buffer[IP_PACKET_SIZE];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000523 int bytes_left = static_cast<int>(bytes_to_send);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000524 while (bytes_left > 0) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000525 size_t length = bytes_left;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000526 int64_t capture_time_ms;
527 if (!packet_history_.GetBestFittingPacket(buffer, &length,
528 &capture_time_ms)) {
529 break;
530 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000531 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000532 break;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000533 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000534 RTPHeader rtp_header;
535 rtp_parser.Parse(rtp_header);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000536 bytes_left -= static_cast<int>(length - rtp_header.headerLength);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000537 }
538 return bytes_to_send - bytes_left;
539}
540
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000541size_t RTPSender::BuildPaddingPacket(uint8_t* packet, size_t header_length) {
542 size_t padding_bytes_in_packet = kMaxPaddingLength;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000543 packet[0] |= 0x20; // Set padding bit.
544 int32_t *data =
545 reinterpret_cast<int32_t *>(&(packet[header_length]));
546
547 // Fill data buffer with random data.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000548 for (size_t j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000549 data[j] = rand(); // NOLINT
550 }
551 // Set number of padding bytes in the last byte of the packet.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000552 packet[header_length + padding_bytes_in_packet - 1] =
553 static_cast<uint8_t>(padding_bytes_in_packet);
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000554 return padding_bytes_in_packet;
555}
556
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000557size_t RTPSender::TrySendPadData(size_t bytes) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000558 int64_t capture_time_ms;
559 uint32_t timestamp;
560 {
561 CriticalSectionScoped cs(send_critsect_);
562 timestamp = timestamp_;
563 capture_time_ms = capture_time_ms_;
564 if (last_timestamp_time_ms_ > 0) {
565 timestamp +=
566 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
567 capture_time_ms +=
568 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
569 }
570 }
571 return SendPadData(timestamp, capture_time_ms, bytes);
572}
573
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000574size_t RTPSender::SendPadData(uint32_t timestamp,
575 int64_t capture_time_ms,
576 size_t bytes) {
577 size_t padding_bytes_in_packet = 0;
578 size_t bytes_sent = 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000579 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000580 // Always send full padding packets.
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000581 if (bytes < kMaxPaddingLength)
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000582 bytes = kMaxPaddingLength;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000583
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000584 uint32_t ssrc;
585 uint16_t sequence_number;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000586 int payload_type;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000587 bool over_rtx;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000588 {
589 CriticalSectionScoped cs(send_critsect_);
590 // Only send padding packets following the last packet of a frame,
591 // indicated by the marker bit.
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000592 if (rtx_ == kRtxOff) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000593 // Without RTX we can't send padding in the middle of frames.
594 if (!last_packet_marker_bit_)
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000595 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000596 ssrc = ssrc_;
597 sequence_number = sequence_number_;
598 ++sequence_number_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000599 payload_type = payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000600 over_rtx = false;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000601 } else {
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000602 // Without abs-send-time a media packet must be sent before padding so
603 // that the timestamps used for estimation are correct.
604 if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
605 kRtpExtensionAbsoluteSendTime))
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000606 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000607 ssrc = ssrc_rtx_;
608 sequence_number = sequence_number_rtx_;
609 ++sequence_number_rtx_;
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000610 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_
611 : payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000612 over_rtx = true;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000613 }
614 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000615
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000616 uint8_t padding_packet[IP_PACKET_SIZE];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000617 size_t header_length =
618 CreateRtpHeader(padding_packet, payload_type, ssrc, false, timestamp,
619 sequence_number, std::vector<uint32_t>());
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000620 assert(header_length != static_cast<size_t>(-1));
621 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length);
622 assert(padding_bytes_in_packet <= bytes);
623 size_t length = padding_bytes_in_packet + header_length;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000624 int64_t now_ms = clock_->TimeInMilliseconds();
625
626 RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
627 RTPHeader rtp_header;
628 rtp_parser.Parse(rtp_header);
629
630 if (capture_time_ms > 0) {
631 UpdateTransmissionTimeOffset(
632 padding_packet, length, rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000633 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000634
635 UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
636 if (!SendPacketToNetwork(padding_packet, length))
637 break;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000638 bytes_sent += padding_bytes_in_packet;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000639 UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000640 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000641
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000642 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000643}
644
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000645void RTPSender::SetStorePacketsStatus(bool enable, uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000646 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000647}
648
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000649bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000650 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000651}
niklase@google.com470e71d2011-07-07 08:21:25 +0000652
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000653int32_t RTPSender::ReSendPacket(uint16_t packet_id, int64_t min_resend_time) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000654 size_t length = IP_PACKET_SIZE;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000655 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000656 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000657 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
658 data_buffer, &length,
659 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000660 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000661 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000662 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000663
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000664 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000665 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000666 RTPHeader header;
667 if (!rtp_parser.Parse(header)) {
668 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000669 return -1;
670 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000671 // Convert from TickTime to Clock since capture_time_ms is based on
672 // TickTime.
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000673 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_;
674 if (!paced_sender_->SendPacket(
675 PacedSender::kHighPriority, header.ssrc, header.sequenceNumber,
676 corrected_capture_tims_ms, length - header.headerLength, true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000677 // We can't send the packet right now.
678 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000679 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000680 }
681 }
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000682 int rtx = kRtxOff;
683 {
684 CriticalSectionScoped lock(send_critsect_);
685 rtx = rtx_;
686 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000687 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000688 (rtx & kRtxRetransmitted) > 0, true) ?
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000689 static_cast<int32_t>(length) : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000690}
691
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000692bool RTPSender::SendPacketToNetwork(const uint8_t *packet, size_t size) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000693 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000694 if (transport_) {
695 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000696 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000697 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
698 "size", size, "sent", bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000699 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000700 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000701 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000702 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000703 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000704 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000705}
706
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000707int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000708 if (!video_)
709 return -1;
710 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000711}
712
713int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000714 if (!video_)
715 return -1;
716 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000717}
718
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000719void RTPSender::OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000720 int64_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();
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000724 uint32_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;
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000743 } else {
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) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000760 UpdateNACKBitRate(bytes_re_sent, now);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000761 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000762}
763
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000764bool RTPSender::ProcessNACKBitRate(uint32_t now) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000765 uint32_t num = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000766 size_t byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000767 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000768 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000769
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000770 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000771
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000772 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000773 return true;
774 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000775 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000776 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000777 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000778 break;
779 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000780 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000781 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000782 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000783 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000784 if (num == NACK_BYTECOUNT_SIZE) {
785 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000786 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000787 if (nack_byte_count_times_[num - 1] <= now) {
788 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000789 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000790 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000791 return (byte_count * 8) < (target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000792}
793
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000794void RTPSender::UpdateNACKBitRate(uint32_t bytes, int64_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000795 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000796 if (bytes == 0)
797 return;
798 nack_bitrate_.Update(bytes);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000799 // Save bitrate statistics.
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000800 // Shift all but first time.
801 for (int i = NACK_BYTECOUNT_SIZE - 2; i >= 0; i--) {
802 nack_byte_count_[i + 1] = nack_byte_count_[i];
803 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000804 }
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000805 nack_byte_count_[0] = bytes;
806 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000807}
808
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000809// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000810bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000811 int64_t capture_time_ms,
812 bool retransmission) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000813 size_t length = IP_PACKET_SIZE;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000814 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000815 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000816
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000817 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
818 0,
819 retransmission,
820 data_buffer,
821 &length,
822 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000823 // Packet cannot be found. Allow sending to continue.
824 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000825 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000826 if (!retransmission && capture_time_ms > 0) {
827 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
828 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000829 int rtx;
830 {
831 CriticalSectionScoped lock(send_critsect_);
832 rtx = rtx_;
833 }
834 return PrepareAndSendPacket(data_buffer,
835 length,
836 capture_time_ms,
837 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000838 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000839}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000840
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000841bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000842 size_t length,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000843 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000844 bool send_over_rtx,
845 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000846 uint8_t *buffer_to_send_ptr = buffer;
847
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000848 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000849 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000850 rtp_parser.Parse(rtp_header);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000851 if (!is_retransmit && rtp_header.markerBit) {
852 TRACE_EVENT_ASYNC_END0("webrtc_rtp", "PacedSend", capture_time_ms);
853 }
854
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000855 TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000856 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000857 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000858
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000859 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000860 if (send_over_rtx) {
861 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000862 buffer_to_send_ptr = data_buffer_rtx;
863 }
864
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000865 int64_t now_ms = clock_->TimeInMilliseconds();
866 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000867 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
868 diff_ms);
869 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000870 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000871 if (ret) {
872 CriticalSectionScoped lock(send_critsect_);
873 media_has_been_sent_ = true;
874 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000875 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
876 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000877 return ret;
878}
879
880void RTPSender::UpdateRtpStats(const uint8_t* buffer,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000881 size_t packet_length,
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000882 const RTPHeader& header,
883 bool is_rtx,
884 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000885 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000886 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000887 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000888
889 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000890 if (is_rtx) {
891 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000892 } else {
893 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000894 }
895
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000896 total_bitrate_sent_.Update(packet_length);
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000897 ++counters->transmitted.packets;
898 if (counters->first_packet_time_ms == -1) {
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000899 counters->first_packet_time_ms = clock_->TimeInMilliseconds();
900 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000901 if (IsFecPacket(buffer, header)) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000902 ++counters->fec.packets;
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000903 counters->fec.payload_bytes +=
904 packet_length - (header.headerLength + header.paddingLength);
905 counters->fec.header_bytes += header.headerLength;
906 counters->fec.padding_bytes += header.paddingLength;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000907 }
908
909 if (is_retransmit) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000910 ++counters->retransmitted.packets;
911 counters->retransmitted.payload_bytes +=
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000912 packet_length - (header.headerLength + header.paddingLength);
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000913 counters->retransmitted.header_bytes += header.headerLength;
914 counters->retransmitted.padding_bytes += header.paddingLength;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000915 }
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000916 counters->transmitted.payload_bytes +=
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000917 packet_length - (header.headerLength + header.paddingLength);
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000918 counters->transmitted.header_bytes += header.headerLength;
919 counters->transmitted.padding_bytes += header.paddingLength;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000920
921 if (rtp_stats_callback_) {
922 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
923 }
924}
925
926bool RTPSender::IsFecPacket(const uint8_t* buffer,
927 const RTPHeader& header) const {
928 if (!video_) {
929 return false;
930 }
931 bool fec_enabled;
932 uint8_t pt_red;
933 uint8_t pt_fec;
934 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
935 return fec_enabled &&
936 header.payloadType == pt_red &&
937 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000938}
939
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000940size_t RTPSender::TimeToSendPadding(size_t bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000941 {
942 CriticalSectionScoped cs(send_critsect_);
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000943 if (!sending_media_) return 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000944 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000945 if (bytes == 0)
946 return 0;
947 size_t bytes_sent = TrySendRedundantPayloads(bytes);
948 if (bytes_sent < bytes)
949 bytes_sent += TrySendPadData(bytes - bytes_sent);
950 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000951}
952
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000953// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000954int32_t RTPSender::SendToNetwork(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000955 uint8_t *buffer, size_t payload_length, size_t rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000956 int64_t capture_time_ms, StorageType storage,
957 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000958 RtpUtility::RtpHeaderParser rtp_parser(buffer,
959 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000960 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000961 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000962
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000963 int64_t now_ms = clock_->TimeInMilliseconds();
964
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000965 // |capture_time_ms| <= 0 is considered invalid.
966 // TODO(holmer): This should be changed all over Video Engine so that negative
967 // time is consider invalid, while 0 is considered a valid time.
968 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000969 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000970 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000971 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000972
973 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
974 rtp_header, now_ms);
975
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000976 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000977 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
978 max_payload_length_, capture_time_ms,
979 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000980 return -1;
981 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000982
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000983 if (paced_sender_ && storage != kDontStore) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000984 // Correct offset between implementations of millisecond time stamps in
985 // TickTime and Clock.
986 int64_t corrected_time_ms = capture_time_ms + clock_delta_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000987 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000988 rtp_header.sequenceNumber, corrected_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000989 payload_length, false)) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000990 if (last_capture_time_ms_sent_ == 0 ||
991 corrected_time_ms > last_capture_time_ms_sent_) {
992 last_capture_time_ms_sent_ = corrected_time_ms;
993 TRACE_EVENT_ASYNC_BEGIN1("webrtc_rtp", "PacedSend", corrected_time_ms,
994 "capture_time_ms", corrected_time_ms);
995 }
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000996 // We can't send the packet right now.
997 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000998 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000999 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +00001000 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001001 if (capture_time_ms > 0) {
1002 UpdateDelayStatistics(capture_time_ms, now_ms);
1003 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001004 size_t length = payload_length + rtp_header_length;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001005 if (!SendPacketToNetwork(buffer, length))
1006 return -1;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001007 {
1008 CriticalSectionScoped lock(send_critsect_);
1009 media_has_been_sent_ = true;
1010 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001011 UpdateRtpStats(buffer, length, rtp_header, false, false);
1012 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +00001013}
1014
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001015void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001016 uint32_t ssrc;
1017 int avg_delay_ms = 0;
1018 int max_delay_ms = 0;
1019 {
1020 CriticalSectionScoped lock(send_critsect_);
1021 ssrc = ssrc_;
1022 }
1023 {
1024 CriticalSectionScoped cs(statistics_crit_.get());
1025 // TODO(holmer): Compute this iteratively instead.
1026 send_delays_[now_ms] = now_ms - capture_time_ms;
1027 send_delays_.erase(send_delays_.begin(),
1028 send_delays_.lower_bound(now_ms -
1029 kSendSideDelayWindowMs));
1030 }
1031 if (send_side_delay_observer_ &&
1032 GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
1033 send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
1034 max_delay_ms, ssrc);
1035 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001036}
1037
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001038void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001039 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001040 total_bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001041 nack_bitrate_.Process();
1042 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001043 return;
1044 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001045 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +00001046}
1047
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001048size_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001049 CriticalSectionScoped lock(send_critsect_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001050 size_t rtp_header_length = 12;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001051 rtp_header_length += sizeof(uint32_t) * csrcs_.size();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001052 rtp_header_length += RtpHeaderExtensionTotalLength();
1053 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001054}
1055
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001056uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001057 CriticalSectionScoped cs(send_critsect_);
1058 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001059}
1060
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001061void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001062 uint32_t ssrc;
1063 uint32_t ssrc_rtx;
1064 {
1065 CriticalSectionScoped ssrc_lock(send_critsect_);
1066 ssrc = ssrc_;
1067 ssrc_rtx = ssrc_rtx_;
1068 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001069 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001070 rtp_stats_ = StreamDataCounters();
1071 rtx_rtp_stats_ = StreamDataCounters();
1072 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001073 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
1074 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001075 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001076}
1077
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001078void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1079 StreamDataCounters* rtx_stats) const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001080 CriticalSectionScoped lock(statistics_crit_.get());
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001081 *rtp_stats = rtp_stats_;
1082 *rtx_stats = rtx_rtp_stats_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001083}
1084
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001085size_t RTPSender::CreateRtpHeader(uint8_t* header,
1086 int8_t payload_type,
1087 uint32_t ssrc,
1088 bool marker_bit,
1089 uint32_t timestamp,
1090 uint16_t sequence_number,
1091 const std::vector<uint32_t>& csrcs) const {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001092 header[0] = 0x80; // version 2.
1093 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001094 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001095 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001096 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001097 RtpUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1098 RtpUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1099 RtpUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001100 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001101
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001102 if (csrcs.size() > 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001103 uint8_t *ptr = &header[rtp_header_length];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001104 for (size_t i = 0; i < csrcs.size(); ++i) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001105 RtpUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001106 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001107 }
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001108 header[0] = (header[0] & 0xf0) | csrcs.size();
niklase@google.com470e71d2011-07-07 08:21:25 +00001109
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001110 // Update length of header.
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001111 rtp_header_length += sizeof(uint32_t) * csrcs.size();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001112 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001113
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001114 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1115 if (len > 0) {
1116 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001117 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001118 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001119 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001120}
1121
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001122int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001123 int8_t payload_type,
1124 bool marker_bit,
1125 uint32_t capture_timestamp,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001126 int64_t capture_time_ms,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001127 bool timestamp_provided,
1128 bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001129 assert(payload_type >= 0);
1130 CriticalSectionScoped cs(send_critsect_);
1131
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001132 if (timestamp_provided) {
1133 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001134 } else {
1135 // Make a unique time stamp.
1136 // We can't inc by the actual time, since then we increase the risk of back
1137 // timing.
1138 timestamp_++;
1139 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001140 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001141 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001142 capture_time_ms_ = capture_time_ms;
1143 last_packet_marker_bit_ = marker_bit;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001144 return CreateRtpHeader(data_buffer, payload_type, ssrc_, marker_bit,
1145 timestamp_, sequence_number, csrcs_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001146}
1147
1148uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001149 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001150 return 0;
1151 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001152 // RTP header extension, RFC 3550.
1153 // 0 1 2 3
1154 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1155 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1156 // | defined by profile | length |
1157 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1158 // | header extension |
1159 // | .... |
1160 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001161 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001162 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001163
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001164 // Add extension ID (0xBEDE).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001165 RtpUtility::AssignUWord16ToBuffer(data_buffer, kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001166
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001167 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001168 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001169
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001170 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001171 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001172 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001173 switch (type) {
1174 case kRtpExtensionTransmissionTimeOffset:
1175 block_length = BuildTransmissionTimeOffsetExtension(
1176 data_buffer + kHeaderLength + total_block_length);
1177 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001178 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001179 block_length = BuildAudioLevelExtension(
1180 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001181 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001182 case kRtpExtensionAbsoluteSendTime:
1183 block_length = BuildAbsoluteSendTimeExtension(
1184 data_buffer + kHeaderLength + total_block_length);
1185 break;
1186 default:
1187 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001188 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001189 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001190 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001191 }
1192 if (total_block_length == 0) {
1193 // No extension added.
1194 return 0;
1195 }
1196 // Set header length (in number of Word32, header excluded).
1197 assert(total_block_length % 4 == 0);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001198 RtpUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
1199 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001200 // Total added length.
1201 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001202}
1203
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001204uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1205 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001206 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1207 //
1208 // The transmission time is signaled to the receiver in-band using the
1209 // general mechanism for RTP header extensions [RFC5285]. The payload
1210 // of this extension (the transmitted value) is a 24-bit signed integer.
1211 // When added to the RTP timestamp of the packet, it represents the
1212 // "effective" RTP transmission time of the packet, on the RTP
1213 // timescale.
1214 //
1215 // The form of the transmission offset extension block:
1216 //
1217 // 0 1 2 3
1218 // 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
1219 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1220 // | ID | len=2 | transmission offset |
1221 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001222
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001223 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001224 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001225 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1226 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001227 // Not registered.
1228 return 0;
1229 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001230 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001231 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001232 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001233 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos,
1234 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001235 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001236 assert(pos == kTransmissionTimeOffsetLength);
1237 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001238}
1239
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001240uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1241 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1242 //
1243 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1244 //
1245 // The form of the audio level extension block:
1246 //
1247 // 0 1 2 3
1248 // 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
1249 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1250 // | ID | len=0 |V| level | 0x00 | 0x00 |
1251 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1252 //
1253 // Note that we always include 2 pad bytes, which will result in legal and
1254 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1255 // are implemented. Right now the pad bytes would anyway be required at end
1256 // of the extension block, so it makes no difference.
1257
1258 // Get id defined by user.
1259 uint8_t id;
1260 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1261 // Not registered.
1262 return 0;
1263 }
1264 size_t pos = 0;
1265 const uint8_t len = 0;
1266 data_buffer[pos++] = (id << 4) + len;
1267 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1268 data_buffer[pos++] = 0; // Padding.
1269 data_buffer[pos++] = 0; // Padding.
1270 // kAudioLevelLength is including pad bytes.
1271 assert(pos == kAudioLevelLength);
1272 return kAudioLevelLength;
1273}
1274
1275uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001276 // Absolute send time in RTP streams.
1277 //
1278 // The absolute send time is signaled to the receiver in-band using the
1279 // general mechanism for RTP header extensions [RFC5285]. The payload
1280 // of this extension (the transmitted value) is a 24-bit unsigned integer
1281 // containing the sender's current time in seconds as a fixed point number
1282 // with 18 bits fractional part.
1283 //
1284 // The form of the absolute send time extension block:
1285 //
1286 // 0 1 2 3
1287 // 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
1288 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1289 // | ID | len=2 | absolute send time |
1290 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1291
1292 // Get id defined by user.
1293 uint8_t id;
1294 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1295 &id) != 0) {
1296 // Not registered.
1297 return 0;
1298 }
1299 size_t pos = 0;
1300 const uint8_t len = 2;
1301 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001302 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos, absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001303 pos += 3;
1304 assert(pos == kAbsoluteSendTimeLength);
1305 return kAbsoluteSendTimeLength;
1306}
1307
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001308void RTPSender::UpdateTransmissionTimeOffset(uint8_t* rtp_packet,
1309 size_t rtp_packet_length,
1310 const RTPHeader& rtp_header,
1311 int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001312 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001313 // Get id.
1314 uint8_t id = 0;
1315 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1316 &id) != 0) {
1317 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001318 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001319 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001320 // Get length until start of header extension block.
1321 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001322 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001323 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001324 if (extension_block_pos < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001325 LOG(LS_WARNING)
1326 << "Failed to update transmission time offset, not registered.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001327 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001328 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001329 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001330 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001331 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001332 block_pos + kTransmissionTimeOffsetLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001333 LOG(LS_WARNING)
1334 << "Failed to update transmission time offset, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001335 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001336 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001337 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001338 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1339 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001340 LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1341 "extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001342 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001343 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001344 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001345 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001346 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001347 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001348 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001349 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001350 // Update transmission offset field (converting to a 90 kHz timestamp).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001351 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1352 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001353}
1354
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001355bool RTPSender::UpdateAudioLevel(uint8_t* rtp_packet,
1356 size_t rtp_packet_length,
1357 const RTPHeader& rtp_header,
1358 bool is_voiced,
1359 uint8_t dBov) const {
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001360 CriticalSectionScoped cs(send_critsect_);
1361
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001362 // Get id.
1363 uint8_t id = 0;
1364 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1365 // Not registered.
1366 return false;
1367 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001368 // Get length until start of header extension block.
1369 int extension_block_pos =
1370 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1371 kRtpExtensionAudioLevel);
1372 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001373 // The feature is not enabled.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001374 return false;
1375 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001376 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001377 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1378 rtp_header.headerLength < block_pos + kAudioLevelLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001379 LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001380 return false;
1381 }
1382 // Verify that header contains extension.
1383 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1384 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001385 LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001386 return false;
1387 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001388 // Verify first byte in block.
1389 const uint8_t first_block_byte = (id << 4) + 0;
1390 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001391 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001392 return false;
1393 }
1394 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1395 return true;
1396}
1397
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001398void RTPSender::UpdateAbsoluteSendTime(uint8_t* rtp_packet,
1399 size_t rtp_packet_length,
1400 const RTPHeader& rtp_header,
1401 int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001402 CriticalSectionScoped cs(send_critsect_);
1403
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001404 // Get id.
1405 uint8_t id = 0;
1406 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1407 &id) != 0) {
1408 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001409 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001410 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001411 // Get length until start of header extension block.
1412 int extension_block_pos =
1413 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1414 kRtpExtensionAbsoluteSendTime);
1415 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001416 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001417 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001418 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001419 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001420 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001421 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001422 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001423 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001424 }
1425 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001426 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1427 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001428 LOG(LS_WARNING)
1429 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001430 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001431 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001432 // Verify first byte in block.
1433 const uint8_t first_block_byte = (id << 4) + 2;
1434 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001435 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001436 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001437 }
1438 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1439 // fractional part).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001440 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1441 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001442}
1443
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001444void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001445 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001446 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001447 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001448
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001449 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001450 SetStartTimestamp(RTPtime, false);
1451 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001452 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001453 if (!ssrc_forced_) {
1454 // Generate a new SSRC.
1455 ssrc_db_.ReturnSSRC(ssrc_);
1456 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001457 bitrates_->set_ssrc(ssrc_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001458 }
1459 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001460 if (!sequence_number_forced_ && !ssrc_forced_) {
1461 // Generate a new sequence number.
1462 sequence_number_ =
1463 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001464 }
1465 }
1466}
1467
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001468void RTPSender::SetSendingMediaStatus(bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001469 CriticalSectionScoped cs(send_critsect_);
1470 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001471}
1472
1473bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001474 CriticalSectionScoped cs(send_critsect_);
1475 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001476}
1477
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001478uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001479 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001480 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001481}
1482
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001483void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001484 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001485 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001486 start_timestamp_forced_ = true;
1487 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001488 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001489 if (!start_timestamp_forced_) {
1490 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001491 }
1492 }
1493}
1494
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001495uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001496 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001497 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001498}
1499
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001500uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001501 // If configured via API, return 0.
1502 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001503
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001504 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001505 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001506 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001507 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001508 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001509 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001510}
1511
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001512void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001513 // This is configured via the API.
1514 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001515
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001516 if (ssrc_ == ssrc && ssrc_forced_) {
1517 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001518 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001519 ssrc_forced_ = true;
1520 ssrc_db_.ReturnSSRC(ssrc_);
1521 ssrc_db_.RegisterSSRC(ssrc);
1522 ssrc_ = ssrc;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001523 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001524 if (!sequence_number_forced_) {
1525 sequence_number_ =
1526 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001527 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001528}
1529
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001530uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001531 CriticalSectionScoped cs(send_critsect_);
1532 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001533}
1534
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001535void RTPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
1536 assert(csrcs.size() <= kRtpCsrcSize);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001537 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001538 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001539}
1540
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001541void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001542 CriticalSectionScoped cs(send_critsect_);
1543 sequence_number_forced_ = true;
1544 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001545}
1546
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001547uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001548 CriticalSectionScoped cs(send_critsect_);
1549 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001550}
1551
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001552// Audio.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001553int32_t RTPSender::SendTelephoneEvent(uint8_t key,
1554 uint16_t time_ms,
1555 uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001556 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001557 return -1;
1558 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001559 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001560}
1561
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001562bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001563 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001564 return false;
1565 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001566 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001567}
1568
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001569int32_t RTPSender::SetAudioPacketSize(uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001570 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001571 return -1;
1572 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001573 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001574}
1575
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001576int32_t RTPSender::SetAudioLevel(uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001577 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001578}
1579
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001580int32_t RTPSender::SetRED(int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001581 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001582 return -1;
1583 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001584 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001585}
1586
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001587int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001588 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001589 return -1;
1590 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001591 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001592}
1593
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001594// Video
1595VideoCodecInformation *RTPSender::CodecInformationVideo() {
1596 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001597 return NULL;
1598 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001599 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001600}
1601
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001602RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001603 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001604 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001605}
1606
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001607uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001608 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001609 return 0;
1610 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001611 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001612}
1613
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001614int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001615 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001616 return -1;
1617 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001618 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001619}
1620
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001621int32_t RTPSender::SetGenericFECStatus(bool enable,
1622 uint8_t payload_type_red,
1623 uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001624 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001625 return -1;
1626 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001627 return video_->SetGenericFECStatus(enable, payload_type_red,
1628 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001629}
1630
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001631int32_t RTPSender::GenericFECStatus(
1632 bool *enable, uint8_t *payload_type_red,
1633 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001634 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001635 return -1;
1636 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001637 return video_->GenericFECStatus(
1638 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001639}
1640
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001641int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001642 const FecProtectionParams *delta_params,
1643 const FecProtectionParams *key_params) {
1644 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001645 return -1;
1646 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001647 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001648}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001649
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001650void RTPSender::BuildRtxPacket(uint8_t* buffer, size_t* length,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001651 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001652 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001653 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001654 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001655 RtpUtility::RtpHeaderParser rtp_parser(
1656 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001657
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001658 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001659 rtp_parser.Parse(rtp_header);
1660
1661 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001662 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001663
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00001664 // Replace payload type, if a specific type is set for RTX.
1665 if (payload_type_rtx_ != -1) {
1666 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001667 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001668 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1669 }
1670
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001671 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001672 uint8_t *ptr = data_buffer_rtx + 2;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001673 RtpUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001674
1675 // Replace SSRC.
1676 ptr += 6;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001677 RtpUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001678
1679 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001680 ptr = data_buffer_rtx + rtp_header.headerLength;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001681 RtpUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001682 ptr += 2;
1683
1684 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001685 memcpy(ptr, buffer + rtp_header.headerLength,
1686 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001687 *length += 2;
1688}
1689
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001690void RTPSender::RegisterRtpStatisticsCallback(
1691 StreamDataCountersCallback* callback) {
1692 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001693 rtp_stats_callback_ = callback;
1694}
1695
1696StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1697 CriticalSectionScoped cs(statistics_crit_.get());
1698 return rtp_stats_callback_;
1699}
1700
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001701uint32_t RTPSender::BitrateSent() const {
1702 return total_bitrate_sent_.BitrateLast();
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001703}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001704
1705void RTPSender::SetRtpState(const RtpState& rtp_state) {
1706 SetStartTimestamp(rtp_state.start_timestamp, true);
1707 CriticalSectionScoped lock(send_critsect_);
1708 sequence_number_ = rtp_state.sequence_number;
1709 sequence_number_forced_ = true;
1710 timestamp_ = rtp_state.timestamp;
1711 capture_time_ms_ = rtp_state.capture_time_ms;
1712 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001713 media_has_been_sent_ = rtp_state.media_has_been_sent;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001714}
1715
1716RtpState RTPSender::GetRtpState() const {
1717 CriticalSectionScoped lock(send_critsect_);
1718
1719 RtpState state;
1720 state.sequence_number = sequence_number_;
1721 state.start_timestamp = start_timestamp_;
1722 state.timestamp = timestamp_;
1723 state.capture_time_ms = capture_time_ms_;
1724 state.last_timestamp_time_ms = last_timestamp_time_ms_;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001725 state.media_has_been_sent = media_has_been_sent_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001726
1727 return state;
1728}
1729
1730void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
1731 CriticalSectionScoped lock(send_critsect_);
1732 sequence_number_rtx_ = rtp_state.sequence_number;
1733}
1734
1735RtpState RTPSender::GetRtxRtpState() const {
1736 CriticalSectionScoped lock(send_critsect_);
1737
1738 RtpState state;
1739 state.sequence_number = sequence_number_rtx_;
1740 state.start_timestamp = start_timestamp_;
1741
1742 return state;
1743}
1744
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001745} // namespace webrtc