blob: ae814a75c6d38ad40937041febbfce88be78d042 [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),
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000112 audio_(audio ? new RTPSenderAudio(id, clock, this, audio_feedback)
113 : nullptr),
114 video_(audio ? nullptr : new RTPSenderVideo(clock, this)),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000115 paced_sender_(paced_sender),
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000116 last_capture_time_ms_sent_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000117 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000118 transport_(transport),
119 sending_media_(true), // Default to sending media.
120 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000121 packet_over_head_(28),
122 payload_type_(-1),
123 payload_type_map_(),
124 rtp_header_extension_map_(),
125 transmission_time_offset_(0),
126 absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000127 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000128 nack_byte_count_times_(),
129 nack_byte_count_(),
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000130 nack_bitrate_(clock, bitrates_->retransmit_bitrate_observer()),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000131 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000132 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000133 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000134 rtp_stats_callback_(NULL),
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000135 frame_count_observer_(frame_count_observer),
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000136 send_side_delay_observer_(send_side_delay_observer),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000137 // RTP variables
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000138 start_timestamp_forced_(false),
139 start_timestamp_(0),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000140 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
141 remote_ssrc_(0),
142 sequence_number_forced_(false),
143 ssrc_forced_(false),
144 timestamp_(0),
145 capture_time_ms_(0),
146 last_timestamp_time_ms_(0),
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000147 media_has_been_sent_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000148 last_packet_marker_bit_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000149 csrcs_(),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000150 rtx_(kRtxOff),
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000151 payload_type_rtx_(-1),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000152 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000153 target_bitrate_(0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000154 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
155 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000156 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000157 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000158 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000159 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000160 bitrates_->set_ssrc(ssrc_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000161 // Random start, 16 bits. Can't be 0.
162 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
163 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000164}
165
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000166RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000167 if (remote_ssrc_ != 0) {
168 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000169 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000170 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000172 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000173 while (!payload_type_map_.empty()) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000174 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000175 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000176 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000177 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000178 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000179}
niklase@google.com470e71d2011-07-07 08:21:25 +0000180
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000181void RTPSender::SetTargetBitrate(uint32_t bitrate) {
182 CriticalSectionScoped cs(target_bitrate_critsect_.get());
183 target_bitrate_ = bitrate;
184}
185
186uint32_t RTPSender::GetTargetBitrate() {
187 CriticalSectionScoped cs(target_bitrate_critsect_.get());
188 return target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000189}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000190
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000191uint16_t RTPSender::ActualSendBitrateKbit() const {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000192 return (uint16_t)(total_bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000193}
194
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000195uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000196 if (video_) {
197 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000198 }
199 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000200}
201
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000202uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000203 if (video_) {
204 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000205 }
206 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000207}
208
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000209uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000210 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000211}
212
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000213bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
214 int* max_send_delay_ms) const {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000215 CriticalSectionScoped lock(statistics_crit_.get());
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000216 SendDelayMap::const_iterator it = send_delays_.upper_bound(
217 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000218 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000219 return false;
220 int num_delays = 0;
221 for (; it != send_delays_.end(); ++it) {
222 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
223 *avg_send_delay_ms += it->second;
224 ++num_delays;
225 }
226 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
227 return true;
228}
229
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000230int32_t RTPSender::SetTransmissionTimeOffset(int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000231 if (transmission_time_offset > (0x800000 - 1) ||
232 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000233 return -1;
234 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000235 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000236 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000237 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000238}
239
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000240int32_t RTPSender::SetAbsoluteSendTime(uint32_t absolute_send_time) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000241 if (absolute_send_time > 0xffffff) { // UWord24.
242 return -1;
243 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000244 CriticalSectionScoped cs(send_critsect_.get());
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000245 absolute_send_time_ = absolute_send_time;
246 return 0;
247}
248
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000249int32_t RTPSender::RegisterRtpHeaderExtension(RTPExtensionType type,
250 uint8_t id) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000251 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000252 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000253}
254
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000255int32_t RTPSender::DeregisterRtpHeaderExtension(RTPExtensionType type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000256 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000257 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000258}
259
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000260size_t RTPSender::RtpHeaderExtensionTotalLength() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000261 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000262 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000263}
264
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000265int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000266 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000267 int8_t payload_number,
268 uint32_t frequency,
269 uint8_t channels,
270 uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000271 assert(payload_name);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000272 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000273
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000274 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000275 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000276
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000277 if (payload_type_map_.end() != it) {
278 // We already use this payload type.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000279 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000280 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000281
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000282 // Check if it's the same as we already have.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000283 if (RtpUtility::StringCompare(
284 payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000285 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000286 payload->typeSpecific.Audio.frequency == frequency &&
287 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000288 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000289 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000290 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000291 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000292 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000293 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000294 return 0;
295 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000296 }
297 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000298 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000299 int32_t ret_val = -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000300 RtpUtility::Payload* payload = NULL;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000301 if (audio_configured_) {
302 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
303 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000304 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000305 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
306 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000307 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000308 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000309 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000310 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000311 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000312}
313
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000314int32_t RTPSender::DeRegisterSendPayload(int8_t payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000315 CriticalSectionScoped lock(send_critsect_.get());
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000316
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000317 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000318 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000319
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000320 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000321 return -1;
322 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000323 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000324 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000325 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000326 return 0;
327}
niklase@google.com470e71d2011-07-07 08:21:25 +0000328
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000329void RTPSender::SetSendPayloadType(int8_t payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000330 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000331 payload_type_ = payload_type;
332}
333
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000334int8_t RTPSender::SendPayloadType() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000335 CriticalSectionScoped cs(send_critsect_.get());
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000336 return payload_type_;
337}
niklase@google.com470e71d2011-07-07 08:21:25 +0000338
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000339int RTPSender::SendPayloadFrequency() const {
340 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
341}
niklase@google.com470e71d2011-07-07 08:21:25 +0000342
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000343int32_t RTPSender::SetMaxPayloadLength(size_t max_payload_length,
344 uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000345 // Sanity check.
346 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000347 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000348 return -1;
349 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000350 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000351 max_payload_length_ = max_payload_length;
352 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000353 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000354}
355
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000356size_t RTPSender::MaxDataPayloadLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000357 int rtx;
358 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000359 CriticalSectionScoped rtx_lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000360 rtx = rtx_;
361 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000362 if (audio_configured_) {
363 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000364 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000365 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
366 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000367 - ((rtx) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000368 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000369}
370
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000371size_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000372 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000373}
374
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000375uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000376
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000377void RTPSender::SetRtxStatus(int mode) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000378 CriticalSectionScoped cs(send_critsect_.get());
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000379 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000380}
381
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000382int RTPSender::RtxStatus() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000383 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000384 return rtx_;
385}
386
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000387void RTPSender::SetRtxSsrc(uint32_t ssrc) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000388 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000389 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000390}
391
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000392uint32_t RTPSender::RtxSsrc() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000393 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000394 return ssrc_rtx_;
395}
396
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000397void RTPSender::SetRtxPayloadType(int payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000398 CriticalSectionScoped cs(send_critsect_.get());
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000399 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000400}
401
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000402int32_t RTPSender::CheckPayloadType(int8_t payload_type,
403 RtpVideoCodecTypes* video_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000404 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000406 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000407 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000408 return -1;
409 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000410 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000411 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000412 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000413 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000414 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000415 // And it's a match...
416 return 0;
417 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000418 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000419 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000420 if (payload_type_ == payload_type) {
421 if (!audio_configured_) {
422 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000423 }
424 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000425 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000426 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000427 payload_type_map_.find(payload_type);
428 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000429 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000430 return -1;
431 }
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000432 SetSendPayloadType(payload_type);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000433 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000434 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000435 if (!payload->audio && !audio_configured_) {
436 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
437 *video_type = payload->typeSpecific.Video.videoCodecType;
438 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000439 }
440 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000441}
442
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000443int32_t RTPSender::SendOutgoingData(FrameType frame_type,
444 int8_t payload_type,
445 uint32_t capture_timestamp,
446 int64_t capture_time_ms,
447 const uint8_t* payload_data,
448 size_t payload_size,
449 const RTPFragmentationHeader* fragmentation,
450 VideoCodecInformation* codec_info,
451 const RTPVideoTypeHeader* rtp_type_hdr) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000452 uint32_t ssrc;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000453 {
454 // Drop this packet if we're not sending media packets.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000455 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000456 ssrc = ssrc_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000457 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000458 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000459 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000460 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000461 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000462 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000463 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000464 return -1;
465 }
466
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000467 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000468 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000469 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
470 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000471 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000472 frame_type == kFrameEmpty);
473
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000474 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
475 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000476 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000477 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
478 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000479 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000480
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000481 if (frame_type == kFrameEmpty)
482 return 0;
483
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000484 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
485 capture_timestamp, capture_time_ms,
486 payload_data, payload_size,
487 fragmentation, codec_info,
488 rtp_type_hdr);
489
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000490 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000491
492 CriticalSectionScoped cs(statistics_crit_.get());
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000493 // Note: This is currently only counting for video.
494 if (frame_type == kVideoFrameKey) {
495 ++frame_counts_.key_frames;
496 } else if (frame_type == kVideoFrameDelta) {
497 ++frame_counts_.delta_frames;
498 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000499 if (frame_count_observer_) {
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000500 frame_count_observer_->FrameCountUpdated(frame_counts_, ssrc);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000501 }
502
503 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000504}
505
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000506size_t RTPSender::TrySendRedundantPayloads(size_t bytes_to_send) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000507 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000508 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000509 if ((rtx_ & kRtxRedundantPayloads) == 0)
510 return 0;
511 }
512
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000513 uint8_t buffer[IP_PACKET_SIZE];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000514 int bytes_left = static_cast<int>(bytes_to_send);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000515 while (bytes_left > 0) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000516 size_t length = bytes_left;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000517 int64_t capture_time_ms;
518 if (!packet_history_.GetBestFittingPacket(buffer, &length,
519 &capture_time_ms)) {
520 break;
521 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000522 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000523 break;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000524 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000525 RTPHeader rtp_header;
526 rtp_parser.Parse(rtp_header);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000527 bytes_left -= static_cast<int>(length - rtp_header.headerLength);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000528 }
529 return bytes_to_send - bytes_left;
530}
531
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000532size_t RTPSender::BuildPaddingPacket(uint8_t* packet, size_t header_length) {
533 size_t padding_bytes_in_packet = kMaxPaddingLength;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000534 packet[0] |= 0x20; // Set padding bit.
535 int32_t *data =
536 reinterpret_cast<int32_t *>(&(packet[header_length]));
537
538 // Fill data buffer with random data.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000539 for (size_t j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000540 data[j] = rand(); // NOLINT
541 }
542 // Set number of padding bytes in the last byte of the packet.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000543 packet[header_length + padding_bytes_in_packet - 1] =
544 static_cast<uint8_t>(padding_bytes_in_packet);
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000545 return padding_bytes_in_packet;
546}
547
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000548size_t RTPSender::TrySendPadData(size_t bytes) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000549 int64_t capture_time_ms;
550 uint32_t timestamp;
551 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000552 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000553 timestamp = timestamp_;
554 capture_time_ms = capture_time_ms_;
555 if (last_timestamp_time_ms_ > 0) {
556 timestamp +=
557 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
558 capture_time_ms +=
559 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
560 }
561 }
562 return SendPadData(timestamp, capture_time_ms, bytes);
563}
564
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000565size_t RTPSender::SendPadData(uint32_t timestamp,
566 int64_t capture_time_ms,
567 size_t bytes) {
568 size_t padding_bytes_in_packet = 0;
569 size_t bytes_sent = 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000570 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000571 // Always send full padding packets.
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000572 if (bytes < kMaxPaddingLength)
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000573 bytes = kMaxPaddingLength;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000574
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000575 uint32_t ssrc;
576 uint16_t sequence_number;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000577 int payload_type;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000578 bool over_rtx;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000579 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000580 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000581 // Only send padding packets following the last packet of a frame,
582 // indicated by the marker bit.
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000583 if (rtx_ == kRtxOff) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000584 // Without RTX we can't send padding in the middle of frames.
585 if (!last_packet_marker_bit_)
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000586 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000587 ssrc = ssrc_;
588 sequence_number = sequence_number_;
589 ++sequence_number_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000590 payload_type = payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000591 over_rtx = false;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000592 } else {
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000593 // Without abs-send-time a media packet must be sent before padding so
594 // that the timestamps used for estimation are correct.
595 if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
596 kRtpExtensionAbsoluteSendTime))
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000597 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000598 ssrc = ssrc_rtx_;
599 sequence_number = sequence_number_rtx_;
600 ++sequence_number_rtx_;
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000601 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_
602 : payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000603 over_rtx = true;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000604 }
605 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000606
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000607 uint8_t padding_packet[IP_PACKET_SIZE];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000608 size_t header_length =
609 CreateRtpHeader(padding_packet, payload_type, ssrc, false, timestamp,
610 sequence_number, std::vector<uint32_t>());
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000611 assert(header_length != static_cast<size_t>(-1));
612 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length);
613 assert(padding_bytes_in_packet <= bytes);
614 size_t length = padding_bytes_in_packet + header_length;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000615 int64_t now_ms = clock_->TimeInMilliseconds();
616
617 RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
618 RTPHeader rtp_header;
619 rtp_parser.Parse(rtp_header);
620
621 if (capture_time_ms > 0) {
622 UpdateTransmissionTimeOffset(
623 padding_packet, length, rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000624 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000625
626 UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
627 if (!SendPacketToNetwork(padding_packet, length))
628 break;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000629 bytes_sent += padding_bytes_in_packet;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000630 UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000631 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000632
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000633 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000634}
635
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000636void RTPSender::SetStorePacketsStatus(bool enable, uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000637 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000638}
639
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000640bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000641 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000642}
niklase@google.com470e71d2011-07-07 08:21:25 +0000643
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000644int32_t RTPSender::ReSendPacket(uint16_t packet_id, int64_t min_resend_time) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000645 size_t length = IP_PACKET_SIZE;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000646 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000647 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000648 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
649 data_buffer, &length,
650 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000651 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000652 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000653 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000654
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000655 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000656 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000657 RTPHeader header;
658 if (!rtp_parser.Parse(header)) {
659 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000660 return -1;
661 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000662 // Convert from TickTime to Clock since capture_time_ms is based on
663 // TickTime.
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000664 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_;
665 if (!paced_sender_->SendPacket(
666 PacedSender::kHighPriority, header.ssrc, header.sequenceNumber,
667 corrected_capture_tims_ms, length - header.headerLength, true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000668 // We can't send the packet right now.
669 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000670 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000671 }
672 }
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000673 int rtx = kRtxOff;
674 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000675 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000676 rtx = rtx_;
677 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000678 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000679 (rtx & kRtxRetransmitted) > 0, true) ?
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000680 static_cast<int32_t>(length) : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000681}
682
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000683bool RTPSender::SendPacketToNetwork(const uint8_t *packet, size_t size) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000684 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000685 if (transport_) {
686 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000687 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000688 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
689 "size", size, "sent", bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000690 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000691 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000692 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000693 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000694 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000695 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000696}
697
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000698int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000699 if (!video_)
700 return -1;
701 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000702}
703
704int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000705 if (!video_)
706 return -1;
707 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000708}
709
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000710void RTPSender::OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000711 int64_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000712 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
713 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000714 const int64_t now = clock_->TimeInMilliseconds();
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000715 uint32_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000716 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000717
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000718 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000719 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000720 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000721 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000722 return;
723 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000724
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000725 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
726 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000727 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000728 if (bytes_sent > 0) {
729 bytes_re_sent += bytes_sent;
730 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000731 // The packet has previously been resent.
732 // Try resending next packet in the list.
733 continue;
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000734 } else {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000735 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000736 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
737 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000738 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000739 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000740 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000741 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000742 // kbits/s * ms = bits => bits/8 = bytes
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000743 size_t target_bytes =
744 (static_cast<size_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000745 if (bytes_re_sent > target_bytes) {
746 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000747 }
748 }
749 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000750 if (bytes_re_sent > 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000751 UpdateNACKBitRate(bytes_re_sent, now);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000752 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000753}
754
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000755bool RTPSender::ProcessNACKBitRate(uint32_t now) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000756 uint32_t num = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000757 size_t byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000758 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000759 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000760
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000761 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000762
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000763 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000764 return true;
765 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000766 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000767 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000768 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000769 break;
770 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000771 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000772 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000773 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000774 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000775 if (num == NACK_BYTECOUNT_SIZE) {
776 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000777 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000778 if (nack_byte_count_times_[num - 1] <= now) {
779 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000780 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000781 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000782 return (byte_count * 8) < (target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000783}
784
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000785void RTPSender::UpdateNACKBitRate(uint32_t bytes, int64_t now) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000786 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000787 if (bytes == 0)
788 return;
789 nack_bitrate_.Update(bytes);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000790 // Save bitrate statistics.
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000791 // Shift all but first time.
792 for (int i = NACK_BYTECOUNT_SIZE - 2; i >= 0; i--) {
793 nack_byte_count_[i + 1] = nack_byte_count_[i];
794 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000795 }
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000796 nack_byte_count_[0] = bytes;
797 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000798}
799
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000800// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000801bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000802 int64_t capture_time_ms,
803 bool retransmission) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000804 size_t length = IP_PACKET_SIZE;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000805 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000806 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000807
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000808 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
809 0,
810 retransmission,
811 data_buffer,
812 &length,
813 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000814 // Packet cannot be found. Allow sending to continue.
815 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000816 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000817 if (!retransmission && capture_time_ms > 0) {
818 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
819 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000820 int rtx;
821 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000822 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000823 rtx = rtx_;
824 }
825 return PrepareAndSendPacket(data_buffer,
826 length,
827 capture_time_ms,
828 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000829 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000830}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000831
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000832bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000833 size_t length,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000834 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000835 bool send_over_rtx,
836 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000837 uint8_t *buffer_to_send_ptr = buffer;
838
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000839 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000840 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000841 rtp_parser.Parse(rtp_header);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000842 if (!is_retransmit && rtp_header.markerBit) {
843 TRACE_EVENT_ASYNC_END0("webrtc_rtp", "PacedSend", capture_time_ms);
844 }
845
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000846 TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000847 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000848 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000849
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000850 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000851 if (send_over_rtx) {
852 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000853 buffer_to_send_ptr = data_buffer_rtx;
854 }
855
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000856 int64_t now_ms = clock_->TimeInMilliseconds();
857 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000858 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
859 diff_ms);
860 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000861 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000862 if (ret) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000863 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000864 media_has_been_sent_ = true;
865 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000866 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
867 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000868 return ret;
869}
870
871void RTPSender::UpdateRtpStats(const uint8_t* buffer,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000872 size_t packet_length,
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000873 const RTPHeader& header,
874 bool is_rtx,
875 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000876 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000877 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000878 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000879
880 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000881 if (is_rtx) {
882 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000883 } else {
884 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000885 }
886
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000887 total_bitrate_sent_.Update(packet_length);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000888
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000889 if (counters->first_packet_time_ms == -1) {
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000890 counters->first_packet_time_ms = clock_->TimeInMilliseconds();
891 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000892 if (IsFecPacket(buffer, header)) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000893 counters->fec.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000894 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000895 if (is_retransmit) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000896 counters->retransmitted.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000897 }
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000898 counters->transmitted.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000899
900 if (rtp_stats_callback_) {
901 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
902 }
903}
904
905bool RTPSender::IsFecPacket(const uint8_t* buffer,
906 const RTPHeader& header) const {
907 if (!video_) {
908 return false;
909 }
910 bool fec_enabled;
911 uint8_t pt_red;
912 uint8_t pt_fec;
913 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
914 return fec_enabled &&
915 header.payloadType == pt_red &&
916 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000917}
918
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000919size_t RTPSender::TimeToSendPadding(size_t bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000920 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000921 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000922 if (!sending_media_) return 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000923 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000924 if (bytes == 0)
925 return 0;
926 size_t bytes_sent = TrySendRedundantPayloads(bytes);
927 if (bytes_sent < bytes)
928 bytes_sent += TrySendPadData(bytes - bytes_sent);
929 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000930}
931
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000932// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000933int32_t RTPSender::SendToNetwork(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000934 uint8_t *buffer, size_t payload_length, size_t rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000935 int64_t capture_time_ms, StorageType storage,
936 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000937 RtpUtility::RtpHeaderParser rtp_parser(buffer,
938 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000939 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000940 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000941
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000942 int64_t now_ms = clock_->TimeInMilliseconds();
943
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000944 // |capture_time_ms| <= 0 is considered invalid.
945 // TODO(holmer): This should be changed all over Video Engine so that negative
946 // time is consider invalid, while 0 is considered a valid time.
947 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000948 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000949 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000950 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000951
952 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
953 rtp_header, now_ms);
954
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000955 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000956 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
957 max_payload_length_, capture_time_ms,
958 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000959 return -1;
960 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000961
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000962 if (paced_sender_ && storage != kDontStore) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000963 // Correct offset between implementations of millisecond time stamps in
964 // TickTime and Clock.
965 int64_t corrected_time_ms = capture_time_ms + clock_delta_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000966 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000967 rtp_header.sequenceNumber, corrected_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000968 payload_length, false)) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000969 if (last_capture_time_ms_sent_ == 0 ||
970 corrected_time_ms > last_capture_time_ms_sent_) {
971 last_capture_time_ms_sent_ = corrected_time_ms;
972 TRACE_EVENT_ASYNC_BEGIN1("webrtc_rtp", "PacedSend", corrected_time_ms,
973 "capture_time_ms", corrected_time_ms);
974 }
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000975 // We can't send the packet right now.
976 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000977 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000978 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000979 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000980 if (capture_time_ms > 0) {
981 UpdateDelayStatistics(capture_time_ms, now_ms);
982 }
sprang@webrtc.org43c88392015-01-29 09:09:17 +0000983
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000984 size_t length = payload_length + rtp_header_length;
sprang@webrtc.orgc957ffc2015-02-02 13:08:02 +0000985 bool sent = SendPacketToNetwork(buffer, length);
986
987 if (storage != kDontStore) {
988 // Mark the packet as sent in the history even if send failed. Dropping a
989 // packet here should be treated as any other packet drop so we should be
990 // ready for a retransmission.
991 packet_history_.SetSent(rtp_header.sequenceNumber);
992 }
993 if (!sent)
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000994 return -1;
sprang@webrtc.orgc957ffc2015-02-02 13:08:02 +0000995
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000996 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000997 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000998 media_has_been_sent_ = true;
999 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001000 UpdateRtpStats(buffer, length, rtp_header, false, false);
1001 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +00001002}
1003
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001004void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001005 uint32_t ssrc;
1006 int avg_delay_ms = 0;
1007 int max_delay_ms = 0;
1008 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001009 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001010 ssrc = ssrc_;
1011 }
1012 {
1013 CriticalSectionScoped cs(statistics_crit_.get());
1014 // TODO(holmer): Compute this iteratively instead.
1015 send_delays_[now_ms] = now_ms - capture_time_ms;
1016 send_delays_.erase(send_delays_.begin(),
1017 send_delays_.lower_bound(now_ms -
1018 kSendSideDelayWindowMs));
1019 }
1020 if (send_side_delay_observer_ &&
1021 GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
1022 send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
1023 max_delay_ms, ssrc);
1024 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001025}
1026
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001027void RTPSender::ProcessBitrate() {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001028 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001029 total_bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001030 nack_bitrate_.Process();
1031 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001032 return;
1033 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001034 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +00001035}
1036
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001037size_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001038 CriticalSectionScoped lock(send_critsect_.get());
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001039 size_t rtp_header_length = 12;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001040 rtp_header_length += sizeof(uint32_t) * csrcs_.size();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001041 rtp_header_length += RtpHeaderExtensionTotalLength();
1042 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001043}
1044
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001045uint16_t RTPSender::IncrementSequenceNumber() {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001046 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001047 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001048}
1049
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001050void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001051 uint32_t ssrc;
1052 uint32_t ssrc_rtx;
1053 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001054 CriticalSectionScoped ssrc_lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001055 ssrc = ssrc_;
1056 ssrc_rtx = ssrc_rtx_;
1057 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001058 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001059 rtp_stats_ = StreamDataCounters();
1060 rtx_rtp_stats_ = StreamDataCounters();
1061 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001062 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
1063 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001064 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001065}
1066
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001067void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1068 StreamDataCounters* rtx_stats) const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001069 CriticalSectionScoped lock(statistics_crit_.get());
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001070 *rtp_stats = rtp_stats_;
1071 *rtx_stats = rtx_rtp_stats_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001072}
1073
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001074size_t RTPSender::CreateRtpHeader(uint8_t* header,
1075 int8_t payload_type,
1076 uint32_t ssrc,
1077 bool marker_bit,
1078 uint32_t timestamp,
1079 uint16_t sequence_number,
1080 const std::vector<uint32_t>& csrcs) const {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001081 header[0] = 0x80; // version 2.
1082 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001083 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001084 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001085 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001086 RtpUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1087 RtpUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1088 RtpUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001089 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001090
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001091 if (csrcs.size() > 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001092 uint8_t *ptr = &header[rtp_header_length];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001093 for (size_t i = 0; i < csrcs.size(); ++i) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001094 RtpUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001095 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001096 }
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001097 header[0] = (header[0] & 0xf0) | csrcs.size();
niklase@google.com470e71d2011-07-07 08:21:25 +00001098
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001099 // Update length of header.
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001100 rtp_header_length += sizeof(uint32_t) * csrcs.size();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001101 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001102
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001103 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1104 if (len > 0) {
1105 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001106 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001107 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001108 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001109}
1110
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001111int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001112 int8_t payload_type,
1113 bool marker_bit,
1114 uint32_t capture_timestamp,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001115 int64_t capture_time_ms,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001116 bool timestamp_provided,
1117 bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001118 assert(payload_type >= 0);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001119 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001120
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001121 if (timestamp_provided) {
1122 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001123 } else {
1124 // Make a unique time stamp.
1125 // We can't inc by the actual time, since then we increase the risk of back
1126 // timing.
1127 timestamp_++;
1128 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001129 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001130 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001131 capture_time_ms_ = capture_time_ms;
1132 last_packet_marker_bit_ = marker_bit;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001133 return CreateRtpHeader(data_buffer, payload_type, ssrc_, marker_bit,
1134 timestamp_, sequence_number, csrcs_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001135}
1136
1137uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001138 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001139 return 0;
1140 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001141 // RTP header extension, RFC 3550.
1142 // 0 1 2 3
1143 // 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
1144 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1145 // | defined by profile | length |
1146 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1147 // | header extension |
1148 // | .... |
1149 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001150 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001151 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001152
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001153 // Add extension ID (0xBEDE).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001154 RtpUtility::AssignUWord16ToBuffer(data_buffer, kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001155
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001156 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001157 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001158
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001159 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001160 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001161 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001162 switch (type) {
1163 case kRtpExtensionTransmissionTimeOffset:
1164 block_length = BuildTransmissionTimeOffsetExtension(
1165 data_buffer + kHeaderLength + total_block_length);
1166 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001167 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001168 block_length = BuildAudioLevelExtension(
1169 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001170 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001171 case kRtpExtensionAbsoluteSendTime:
1172 block_length = BuildAbsoluteSendTimeExtension(
1173 data_buffer + kHeaderLength + total_block_length);
1174 break;
1175 default:
1176 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001177 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001178 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001179 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001180 }
1181 if (total_block_length == 0) {
1182 // No extension added.
1183 return 0;
1184 }
1185 // Set header length (in number of Word32, header excluded).
1186 assert(total_block_length % 4 == 0);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001187 RtpUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
1188 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001189 // Total added length.
1190 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001191}
1192
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001193uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1194 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001195 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1196 //
1197 // The transmission time is signaled to the receiver in-band using the
1198 // general mechanism for RTP header extensions [RFC5285]. The payload
1199 // of this extension (the transmitted value) is a 24-bit signed integer.
1200 // When added to the RTP timestamp of the packet, it represents the
1201 // "effective" RTP transmission time of the packet, on the RTP
1202 // timescale.
1203 //
1204 // The form of the transmission offset extension block:
1205 //
1206 // 0 1 2 3
1207 // 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
1208 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1209 // | ID | len=2 | transmission offset |
1210 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001211
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001212 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001213 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001214 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1215 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001216 // Not registered.
1217 return 0;
1218 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001219 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001220 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001221 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001222 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos,
1223 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001224 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001225 assert(pos == kTransmissionTimeOffsetLength);
1226 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001227}
1228
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001229uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1230 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1231 //
1232 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1233 //
1234 // The form of the audio level extension block:
1235 //
1236 // 0 1 2 3
1237 // 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
1238 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1239 // | ID | len=0 |V| level | 0x00 | 0x00 |
1240 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1241 //
1242 // Note that we always include 2 pad bytes, which will result in legal and
1243 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1244 // are implemented. Right now the pad bytes would anyway be required at end
1245 // of the extension block, so it makes no difference.
1246
1247 // Get id defined by user.
1248 uint8_t id;
1249 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1250 // Not registered.
1251 return 0;
1252 }
1253 size_t pos = 0;
1254 const uint8_t len = 0;
1255 data_buffer[pos++] = (id << 4) + len;
1256 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1257 data_buffer[pos++] = 0; // Padding.
1258 data_buffer[pos++] = 0; // Padding.
1259 // kAudioLevelLength is including pad bytes.
1260 assert(pos == kAudioLevelLength);
1261 return kAudioLevelLength;
1262}
1263
1264uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001265 // Absolute send time in RTP streams.
1266 //
1267 // The absolute send time is signaled to the receiver in-band using the
1268 // general mechanism for RTP header extensions [RFC5285]. The payload
1269 // of this extension (the transmitted value) is a 24-bit unsigned integer
1270 // containing the sender's current time in seconds as a fixed point number
1271 // with 18 bits fractional part.
1272 //
1273 // The form of the absolute send time extension block:
1274 //
1275 // 0 1 2 3
1276 // 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
1277 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1278 // | ID | len=2 | absolute send time |
1279 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1280
1281 // Get id defined by user.
1282 uint8_t id;
1283 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1284 &id) != 0) {
1285 // Not registered.
1286 return 0;
1287 }
1288 size_t pos = 0;
1289 const uint8_t len = 2;
1290 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001291 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos, absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001292 pos += 3;
1293 assert(pos == kAbsoluteSendTimeLength);
1294 return kAbsoluteSendTimeLength;
1295}
1296
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001297void RTPSender::UpdateTransmissionTimeOffset(uint8_t* rtp_packet,
1298 size_t rtp_packet_length,
1299 const RTPHeader& rtp_header,
1300 int64_t time_diff_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001301 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001302 // Get id.
1303 uint8_t id = 0;
1304 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1305 &id) != 0) {
1306 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001307 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001308 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001309 // Get length until start of header extension block.
1310 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001311 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001312 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001313 if (extension_block_pos < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001314 LOG(LS_WARNING)
1315 << "Failed to update transmission time offset, not registered.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001316 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001317 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001318 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001319 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001320 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001321 block_pos + kTransmissionTimeOffsetLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001322 LOG(LS_WARNING)
1323 << "Failed to update transmission time offset, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001324 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001325 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001326 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001327 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1328 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001329 LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1330 "extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001331 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001332 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001333 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001334 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001335 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001336 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001337 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001338 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001339 // Update transmission offset field (converting to a 90 kHz timestamp).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001340 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1341 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001342}
1343
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001344bool RTPSender::UpdateAudioLevel(uint8_t* rtp_packet,
1345 size_t rtp_packet_length,
1346 const RTPHeader& rtp_header,
1347 bool is_voiced,
1348 uint8_t dBov) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001349 CriticalSectionScoped cs(send_critsect_.get());
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001350
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001351 // Get id.
1352 uint8_t id = 0;
1353 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1354 // Not registered.
1355 return false;
1356 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001357 // Get length until start of header extension block.
1358 int extension_block_pos =
1359 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1360 kRtpExtensionAudioLevel);
1361 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001362 // The feature is not enabled.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001363 return false;
1364 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001365 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001366 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1367 rtp_header.headerLength < block_pos + kAudioLevelLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001368 LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001369 return false;
1370 }
1371 // Verify that header contains extension.
1372 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1373 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001374 LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001375 return false;
1376 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001377 // Verify first byte in block.
1378 const uint8_t first_block_byte = (id << 4) + 0;
1379 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001380 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001381 return false;
1382 }
1383 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1384 return true;
1385}
1386
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001387void RTPSender::UpdateAbsoluteSendTime(uint8_t* rtp_packet,
1388 size_t rtp_packet_length,
1389 const RTPHeader& rtp_header,
1390 int64_t now_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001391 CriticalSectionScoped cs(send_critsect_.get());
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001392
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001393 // Get id.
1394 uint8_t id = 0;
1395 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1396 &id) != 0) {
1397 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001398 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001399 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001400 // Get length until start of header extension block.
1401 int extension_block_pos =
1402 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1403 kRtpExtensionAbsoluteSendTime);
1404 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001405 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001406 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001407 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001408 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001409 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001410 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001411 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001412 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001413 }
1414 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001415 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1416 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001417 LOG(LS_WARNING)
1418 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001419 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001420 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001421 // Verify first byte in block.
1422 const uint8_t first_block_byte = (id << 4) + 2;
1423 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001424 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001425 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001426 }
1427 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1428 // fractional part).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001429 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1430 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001431}
1432
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001433void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001434 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001435 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001436 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001437
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001438 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001439 SetStartTimestamp(RTPtime, false);
1440 } else {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001441 CriticalSectionScoped lock(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001442 if (!ssrc_forced_) {
1443 // Generate a new SSRC.
1444 ssrc_db_.ReturnSSRC(ssrc_);
1445 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001446 bitrates_->set_ssrc(ssrc_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001447 }
1448 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001449 if (!sequence_number_forced_ && !ssrc_forced_) {
1450 // Generate a new sequence number.
1451 sequence_number_ =
1452 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001453 }
1454 }
1455}
1456
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001457void RTPSender::SetSendingMediaStatus(bool enabled) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001458 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001459 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001460}
1461
1462bool RTPSender::SendingMedia() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001463 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001464 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001465}
1466
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001467uint32_t RTPSender::Timestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001468 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001469 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001470}
1471
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001472void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001473 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001474 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001475 start_timestamp_forced_ = true;
1476 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001477 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001478 if (!start_timestamp_forced_) {
1479 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001480 }
1481 }
1482}
1483
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001484uint32_t RTPSender::StartTimestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001485 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001486 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001487}
1488
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001489uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001490 // If configured via API, return 0.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001491 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001492
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001493 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001494 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001495 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001496 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001497 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001498 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001499}
1500
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001501void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001502 // This is configured via the API.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001503 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +00001504
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001505 if (ssrc_ == ssrc && ssrc_forced_) {
1506 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001507 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001508 ssrc_forced_ = true;
1509 ssrc_db_.ReturnSSRC(ssrc_);
1510 ssrc_db_.RegisterSSRC(ssrc);
1511 ssrc_ = ssrc;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001512 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001513 if (!sequence_number_forced_) {
1514 sequence_number_ =
1515 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001516 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001517}
1518
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001519uint32_t RTPSender::SSRC() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001520 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001521 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001522}
1523
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001524void RTPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
1525 assert(csrcs.size() <= kRtpCsrcSize);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001526 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001527 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001528}
1529
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001530void RTPSender::SetSequenceNumber(uint16_t seq) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001531 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001532 sequence_number_forced_ = true;
1533 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001534}
1535
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001536uint16_t RTPSender::SequenceNumber() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001537 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001538 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001539}
1540
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001541// Audio.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001542int32_t RTPSender::SendTelephoneEvent(uint8_t key,
1543 uint16_t time_ms,
1544 uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001545 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001546 return -1;
1547 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001548 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001549}
1550
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001551int32_t RTPSender::SetAudioPacketSize(uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001552 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001553 return -1;
1554 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001555 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001556}
1557
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001558int32_t RTPSender::SetAudioLevel(uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001559 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001560}
1561
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001562int32_t RTPSender::SetRED(int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001563 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001564 return -1;
1565 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001566 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001567}
1568
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001569int32_t RTPSender::RED(int8_t *payload_type) const {
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_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001574}
1575
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001576// Video
1577VideoCodecInformation *RTPSender::CodecInformationVideo() {
1578 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001579 return NULL;
1580 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001581 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001582}
1583
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001584RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001585 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001586 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001587}
1588
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001589uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001590 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001591 return 0;
1592 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001593 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001594}
1595
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001596int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001597 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001598 return -1;
1599 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001600 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001601}
1602
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001603int32_t RTPSender::SetGenericFECStatus(bool enable,
1604 uint8_t payload_type_red,
1605 uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001606 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001607 return -1;
1608 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001609 return video_->SetGenericFECStatus(enable, payload_type_red,
1610 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001611}
1612
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001613int32_t RTPSender::GenericFECStatus(
1614 bool *enable, uint8_t *payload_type_red,
1615 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001616 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001617 return -1;
1618 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001619 return video_->GenericFECStatus(
1620 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001621}
1622
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001623int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001624 const FecProtectionParams *delta_params,
1625 const FecProtectionParams *key_params) {
1626 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001627 return -1;
1628 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001629 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001630}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001631
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001632void RTPSender::BuildRtxPacket(uint8_t* buffer, size_t* length,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001633 uint8_t* buffer_rtx) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001634 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001635 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001636 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001637 RtpUtility::RtpHeaderParser rtp_parser(
1638 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001639
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001640 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001641 rtp_parser.Parse(rtp_header);
1642
1643 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001644 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001645
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00001646 // Replace payload type, if a specific type is set for RTX.
1647 if (payload_type_rtx_ != -1) {
1648 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001649 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001650 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1651 }
1652
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001653 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001654 uint8_t *ptr = data_buffer_rtx + 2;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001655 RtpUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001656
1657 // Replace SSRC.
1658 ptr += 6;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001659 RtpUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001660
1661 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001662 ptr = data_buffer_rtx + rtp_header.headerLength;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001663 RtpUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001664 ptr += 2;
1665
1666 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001667 memcpy(ptr, buffer + rtp_header.headerLength,
1668 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001669 *length += 2;
1670}
1671
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001672void RTPSender::RegisterRtpStatisticsCallback(
1673 StreamDataCountersCallback* callback) {
1674 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001675 rtp_stats_callback_ = callback;
1676}
1677
1678StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1679 CriticalSectionScoped cs(statistics_crit_.get());
1680 return rtp_stats_callback_;
1681}
1682
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001683uint32_t RTPSender::BitrateSent() const {
1684 return total_bitrate_sent_.BitrateLast();
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001685}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001686
1687void RTPSender::SetRtpState(const RtpState& rtp_state) {
1688 SetStartTimestamp(rtp_state.start_timestamp, true);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001689 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001690 sequence_number_ = rtp_state.sequence_number;
1691 sequence_number_forced_ = true;
1692 timestamp_ = rtp_state.timestamp;
1693 capture_time_ms_ = rtp_state.capture_time_ms;
1694 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001695 media_has_been_sent_ = rtp_state.media_has_been_sent;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001696}
1697
1698RtpState RTPSender::GetRtpState() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001699 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001700
1701 RtpState state;
1702 state.sequence_number = sequence_number_;
1703 state.start_timestamp = start_timestamp_;
1704 state.timestamp = timestamp_;
1705 state.capture_time_ms = capture_time_ms_;
1706 state.last_timestamp_time_ms = last_timestamp_time_ms_;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001707 state.media_has_been_sent = media_has_been_sent_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001708
1709 return state;
1710}
1711
1712void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001713 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001714 sequence_number_rtx_ = rtp_state.sequence_number;
1715}
1716
1717RtpState RTPSender::GetRtxRtpState() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001718 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001719
1720 RtpState state;
1721 state.sequence_number = sequence_number_rtx_;
1722 state.start_timestamp = start_timestamp_;
1723
1724 return state;
1725}
1726
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001727} // namespace webrtc