blob: 5f5a71ae54f63e1b05addeef0854a14c019b0768 [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 }
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000688 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
689 "RTPSender::SendPacketToNetwork", "size", size, "sent",
690 bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000691 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000692 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000693 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000694 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000695 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000696 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000697}
698
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000699int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000700 if (!video_)
701 return -1;
702 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000703}
704
705int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000706 if (!video_)
707 return -1;
708 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000709}
710
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000711void RTPSender::OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000712 int64_t avg_rtt) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000713 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
714 "RTPSender::OnReceivedNACK", "num_seqnum",
715 nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000716 const int64_t now = clock_->TimeInMilliseconds();
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000717 uint32_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000718 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000719
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000720 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000721 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000722 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000723 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000724 return;
725 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000726
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000727 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
728 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000729 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000730 if (bytes_sent > 0) {
731 bytes_re_sent += bytes_sent;
732 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000733 // The packet has previously been resent.
734 // Try resending next packet in the list.
735 continue;
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000736 } else {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000737 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000738 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
739 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000740 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000741 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000742 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000743 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000744 // kbits/s * ms = bits => bits/8 = bytes
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000745 size_t target_bytes =
746 (static_cast<size_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000747 if (bytes_re_sent > target_bytes) {
748 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000749 }
750 }
751 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000752 if (bytes_re_sent > 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000753 UpdateNACKBitRate(bytes_re_sent, now);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000754 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000755}
756
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000757bool RTPSender::ProcessNACKBitRate(uint32_t now) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000758 uint32_t num = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000759 size_t byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000760 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000761 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000762
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000763 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000764
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000765 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000766 return true;
767 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000768 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000769 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000770 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000771 break;
772 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000773 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000774 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000775 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000776 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000777 if (num == NACK_BYTECOUNT_SIZE) {
778 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000779 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000780 if (nack_byte_count_times_[num - 1] <= now) {
781 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000782 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000783 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000784 return (byte_count * 8) < (target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000785}
786
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000787void RTPSender::UpdateNACKBitRate(uint32_t bytes, int64_t now) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000788 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000789 if (bytes == 0)
790 return;
791 nack_bitrate_.Update(bytes);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000792 // Save bitrate statistics.
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000793 // Shift all but first time.
794 for (int i = NACK_BYTECOUNT_SIZE - 2; i >= 0; i--) {
795 nack_byte_count_[i + 1] = nack_byte_count_[i];
796 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000797 }
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000798 nack_byte_count_[0] = bytes;
799 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000800}
801
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000802// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000803bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000804 int64_t capture_time_ms,
805 bool retransmission) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000806 size_t length = IP_PACKET_SIZE;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000807 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000808 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000809
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000810 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
811 0,
812 retransmission,
813 data_buffer,
814 &length,
815 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000816 // Packet cannot be found. Allow sending to continue.
817 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000818 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000819 if (!retransmission && capture_time_ms > 0) {
820 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
821 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000822 int rtx;
823 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000824 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000825 rtx = rtx_;
826 }
827 return PrepareAndSendPacket(data_buffer,
828 length,
829 capture_time_ms,
830 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000831 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000832}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000833
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000834bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000835 size_t length,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000836 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000837 bool send_over_rtx,
838 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000839 uint8_t *buffer_to_send_ptr = buffer;
840
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000841 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000842 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000843 rtp_parser.Parse(rtp_header);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000844 if (!is_retransmit && rtp_header.markerBit) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000845 TRACE_EVENT_ASYNC_END0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PacedSend",
846 capture_time_ms);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000847 }
848
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000849 TRACE_EVENT_INSTANT2(
850 TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PrepareAndSendPacket",
851 "timestamp", rtp_header.timestamp, "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000852
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000853 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000854 if (send_over_rtx) {
855 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000856 buffer_to_send_ptr = data_buffer_rtx;
857 }
858
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000859 int64_t now_ms = clock_->TimeInMilliseconds();
860 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000861 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
862 diff_ms);
863 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000864 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000865 if (ret) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000866 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000867 media_has_been_sent_ = true;
868 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000869 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
870 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000871 return ret;
872}
873
874void RTPSender::UpdateRtpStats(const uint8_t* buffer,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000875 size_t packet_length,
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000876 const RTPHeader& header,
877 bool is_rtx,
878 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000879 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000880 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000881 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000882
883 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000884 if (is_rtx) {
885 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000886 } else {
887 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000888 }
889
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000890 total_bitrate_sent_.Update(packet_length);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000891
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000892 if (counters->first_packet_time_ms == -1) {
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000893 counters->first_packet_time_ms = clock_->TimeInMilliseconds();
894 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000895 if (IsFecPacket(buffer, header)) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000896 counters->fec.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000897 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000898 if (is_retransmit) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000899 counters->retransmitted.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000900 }
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000901 counters->transmitted.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000902
903 if (rtp_stats_callback_) {
904 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
905 }
906}
907
908bool RTPSender::IsFecPacket(const uint8_t* buffer,
909 const RTPHeader& header) const {
910 if (!video_) {
911 return false;
912 }
913 bool fec_enabled;
914 uint8_t pt_red;
915 uint8_t pt_fec;
916 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
917 return fec_enabled &&
918 header.payloadType == pt_red &&
919 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000920}
921
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000922size_t RTPSender::TimeToSendPadding(size_t bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000923 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000924 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000925 if (!sending_media_) return 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000926 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000927 if (bytes == 0)
928 return 0;
929 size_t bytes_sent = TrySendRedundantPayloads(bytes);
930 if (bytes_sent < bytes)
931 bytes_sent += TrySendPadData(bytes - bytes_sent);
932 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000933}
934
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000935// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000936int32_t RTPSender::SendToNetwork(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000937 uint8_t *buffer, size_t payload_length, size_t rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000938 int64_t capture_time_ms, StorageType storage,
939 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000940 RtpUtility::RtpHeaderParser rtp_parser(buffer,
941 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000942 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000943 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000944
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000945 int64_t now_ms = clock_->TimeInMilliseconds();
946
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000947 // |capture_time_ms| <= 0 is considered invalid.
948 // TODO(holmer): This should be changed all over Video Engine so that negative
949 // time is consider invalid, while 0 is considered a valid time.
950 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000951 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000952 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000953 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000954
955 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
956 rtp_header, now_ms);
957
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000958 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000959 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
960 max_payload_length_, capture_time_ms,
961 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000962 return -1;
963 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000964
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000965 if (paced_sender_ && storage != kDontStore) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000966 // Correct offset between implementations of millisecond time stamps in
967 // TickTime and Clock.
968 int64_t corrected_time_ms = capture_time_ms + clock_delta_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000969 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000970 rtp_header.sequenceNumber, corrected_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000971 payload_length, false)) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000972 if (last_capture_time_ms_sent_ == 0 ||
973 corrected_time_ms > last_capture_time_ms_sent_) {
974 last_capture_time_ms_sent_ = corrected_time_ms;
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000975 TRACE_EVENT_ASYNC_BEGIN1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
976 "PacedSend", corrected_time_ms,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000977 "capture_time_ms", corrected_time_ms);
978 }
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000979 // We can't send the packet right now.
980 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000981 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000982 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000983 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000984 if (capture_time_ms > 0) {
985 UpdateDelayStatistics(capture_time_ms, now_ms);
986 }
sprang@webrtc.org43c88392015-01-29 09:09:17 +0000987
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000988 size_t length = payload_length + rtp_header_length;
sprang@webrtc.orgc957ffc2015-02-02 13:08:02 +0000989 bool sent = SendPacketToNetwork(buffer, length);
990
991 if (storage != kDontStore) {
992 // Mark the packet as sent in the history even if send failed. Dropping a
993 // packet here should be treated as any other packet drop so we should be
994 // ready for a retransmission.
995 packet_history_.SetSent(rtp_header.sequenceNumber);
996 }
997 if (!sent)
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000998 return -1;
sprang@webrtc.orgc957ffc2015-02-02 13:08:02 +0000999
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001000 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001001 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001002 media_has_been_sent_ = true;
1003 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001004 UpdateRtpStats(buffer, length, rtp_header, false, false);
1005 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +00001006}
1007
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001008void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001009 uint32_t ssrc;
1010 int avg_delay_ms = 0;
1011 int max_delay_ms = 0;
1012 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001013 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001014 ssrc = ssrc_;
1015 }
1016 {
1017 CriticalSectionScoped cs(statistics_crit_.get());
1018 // TODO(holmer): Compute this iteratively instead.
1019 send_delays_[now_ms] = now_ms - capture_time_ms;
1020 send_delays_.erase(send_delays_.begin(),
1021 send_delays_.lower_bound(now_ms -
1022 kSendSideDelayWindowMs));
1023 }
1024 if (send_side_delay_observer_ &&
1025 GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
1026 send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
1027 max_delay_ms, ssrc);
1028 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001029}
1030
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001031void RTPSender::ProcessBitrate() {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001032 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001033 total_bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001034 nack_bitrate_.Process();
1035 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001036 return;
1037 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001038 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +00001039}
1040
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001041size_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001042 CriticalSectionScoped lock(send_critsect_.get());
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001043 size_t rtp_header_length = 12;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001044 rtp_header_length += sizeof(uint32_t) * csrcs_.size();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001045 rtp_header_length += RtpHeaderExtensionTotalLength();
1046 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001047}
1048
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001049uint16_t RTPSender::IncrementSequenceNumber() {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001050 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001051 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001052}
1053
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001054void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001055 uint32_t ssrc;
1056 uint32_t ssrc_rtx;
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001057 bool report_rtx;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001058 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001059 CriticalSectionScoped ssrc_lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001060 ssrc = ssrc_;
1061 ssrc_rtx = ssrc_rtx_;
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001062 report_rtx = rtx_ != kRtxOff;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001063 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001064 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001065 rtp_stats_ = StreamDataCounters();
1066 rtx_rtp_stats_ = StreamDataCounters();
1067 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001068 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001069 if (report_rtx)
1070 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001071 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001072}
1073
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001074void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1075 StreamDataCounters* rtx_stats) const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001076 CriticalSectionScoped lock(statistics_crit_.get());
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001077 *rtp_stats = rtp_stats_;
1078 *rtx_stats = rtx_rtp_stats_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001079}
1080
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001081size_t RTPSender::CreateRtpHeader(uint8_t* header,
1082 int8_t payload_type,
1083 uint32_t ssrc,
1084 bool marker_bit,
1085 uint32_t timestamp,
1086 uint16_t sequence_number,
1087 const std::vector<uint32_t>& csrcs) const {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001088 header[0] = 0x80; // version 2.
1089 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001090 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001091 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001092 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001093 RtpUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1094 RtpUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1095 RtpUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001096 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001097
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001098 if (csrcs.size() > 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001099 uint8_t *ptr = &header[rtp_header_length];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001100 for (size_t i = 0; i < csrcs.size(); ++i) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001101 RtpUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001102 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001103 }
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001104 header[0] = (header[0] & 0xf0) | csrcs.size();
niklase@google.com470e71d2011-07-07 08:21:25 +00001105
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001106 // Update length of header.
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001107 rtp_header_length += sizeof(uint32_t) * csrcs.size();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001108 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001109
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001110 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1111 if (len > 0) {
1112 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001113 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001114 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001115 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001116}
1117
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001118int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001119 int8_t payload_type,
1120 bool marker_bit,
1121 uint32_t capture_timestamp,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001122 int64_t capture_time_ms,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001123 bool timestamp_provided,
1124 bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001125 assert(payload_type >= 0);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001126 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001127
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001128 if (timestamp_provided) {
1129 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001130 } else {
1131 // Make a unique time stamp.
1132 // We can't inc by the actual time, since then we increase the risk of back
1133 // timing.
1134 timestamp_++;
1135 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001136 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001137 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001138 capture_time_ms_ = capture_time_ms;
1139 last_packet_marker_bit_ = marker_bit;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001140 return CreateRtpHeader(data_buffer, payload_type, ssrc_, marker_bit,
1141 timestamp_, sequence_number, csrcs_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001142}
1143
1144uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001145 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001146 return 0;
1147 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001148 // RTP header extension, RFC 3550.
1149 // 0 1 2 3
1150 // 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
1151 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1152 // | defined by profile | length |
1153 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1154 // | header extension |
1155 // | .... |
1156 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001157 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001158 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001159
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001160 // Add extension ID (0xBEDE).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001161 RtpUtility::AssignUWord16ToBuffer(data_buffer, kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001162
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001163 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001164 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001165
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001166 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001167 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001168 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001169 switch (type) {
1170 case kRtpExtensionTransmissionTimeOffset:
1171 block_length = BuildTransmissionTimeOffsetExtension(
1172 data_buffer + kHeaderLength + total_block_length);
1173 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001174 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001175 block_length = BuildAudioLevelExtension(
1176 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001177 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001178 case kRtpExtensionAbsoluteSendTime:
1179 block_length = BuildAbsoluteSendTimeExtension(
1180 data_buffer + kHeaderLength + total_block_length);
1181 break;
1182 default:
1183 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001184 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001185 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001186 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001187 }
1188 if (total_block_length == 0) {
1189 // No extension added.
1190 return 0;
1191 }
1192 // Set header length (in number of Word32, header excluded).
1193 assert(total_block_length % 4 == 0);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001194 RtpUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
1195 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001196 // Total added length.
1197 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001198}
1199
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001200uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1201 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001202 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1203 //
1204 // The transmission time is signaled to the receiver in-band using the
1205 // general mechanism for RTP header extensions [RFC5285]. The payload
1206 // of this extension (the transmitted value) is a 24-bit signed integer.
1207 // When added to the RTP timestamp of the packet, it represents the
1208 // "effective" RTP transmission time of the packet, on the RTP
1209 // timescale.
1210 //
1211 // The form of the transmission offset extension block:
1212 //
1213 // 0 1 2 3
1214 // 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
1215 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1216 // | ID | len=2 | transmission offset |
1217 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001218
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001219 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001220 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001221 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1222 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001223 // Not registered.
1224 return 0;
1225 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001226 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001227 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001228 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001229 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos,
1230 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001231 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001232 assert(pos == kTransmissionTimeOffsetLength);
1233 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001234}
1235
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001236uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1237 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1238 //
1239 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1240 //
1241 // The form of the audio level extension block:
1242 //
1243 // 0 1 2 3
1244 // 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
1245 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1246 // | ID | len=0 |V| level | 0x00 | 0x00 |
1247 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1248 //
1249 // Note that we always include 2 pad bytes, which will result in legal and
1250 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1251 // are implemented. Right now the pad bytes would anyway be required at end
1252 // of the extension block, so it makes no difference.
1253
1254 // Get id defined by user.
1255 uint8_t id;
1256 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1257 // Not registered.
1258 return 0;
1259 }
1260 size_t pos = 0;
1261 const uint8_t len = 0;
1262 data_buffer[pos++] = (id << 4) + len;
1263 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1264 data_buffer[pos++] = 0; // Padding.
1265 data_buffer[pos++] = 0; // Padding.
1266 // kAudioLevelLength is including pad bytes.
1267 assert(pos == kAudioLevelLength);
1268 return kAudioLevelLength;
1269}
1270
1271uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001272 // Absolute send time in RTP streams.
1273 //
1274 // The absolute send time is signaled to the receiver in-band using the
1275 // general mechanism for RTP header extensions [RFC5285]. The payload
1276 // of this extension (the transmitted value) is a 24-bit unsigned integer
1277 // containing the sender's current time in seconds as a fixed point number
1278 // with 18 bits fractional part.
1279 //
1280 // The form of the absolute send time extension block:
1281 //
1282 // 0 1 2 3
1283 // 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
1284 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1285 // | ID | len=2 | absolute send time |
1286 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1287
1288 // Get id defined by user.
1289 uint8_t id;
1290 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1291 &id) != 0) {
1292 // Not registered.
1293 return 0;
1294 }
1295 size_t pos = 0;
1296 const uint8_t len = 2;
1297 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001298 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos, absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001299 pos += 3;
1300 assert(pos == kAbsoluteSendTimeLength);
1301 return kAbsoluteSendTimeLength;
1302}
1303
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001304void RTPSender::UpdateTransmissionTimeOffset(uint8_t* rtp_packet,
1305 size_t rtp_packet_length,
1306 const RTPHeader& rtp_header,
1307 int64_t time_diff_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001308 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001309 // Get id.
1310 uint8_t id = 0;
1311 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1312 &id) != 0) {
1313 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001314 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001315 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001316 // Get length until start of header extension block.
1317 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001318 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001319 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001320 if (extension_block_pos < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001321 LOG(LS_WARNING)
1322 << "Failed to update transmission time offset, not registered.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001323 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001324 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001325 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001326 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001327 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001328 block_pos + kTransmissionTimeOffsetLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001329 LOG(LS_WARNING)
1330 << "Failed to update transmission time offset, invalid length.";
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 that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001334 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1335 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001336 LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1337 "extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001338 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001339 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001340 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001341 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001342 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001343 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001344 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001345 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001346 // Update transmission offset field (converting to a 90 kHz timestamp).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001347 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1348 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001349}
1350
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001351bool RTPSender::UpdateAudioLevel(uint8_t* rtp_packet,
1352 size_t rtp_packet_length,
1353 const RTPHeader& rtp_header,
1354 bool is_voiced,
1355 uint8_t dBov) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001356 CriticalSectionScoped cs(send_critsect_.get());
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001357
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001358 // Get id.
1359 uint8_t id = 0;
1360 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1361 // Not registered.
1362 return false;
1363 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001364 // Get length until start of header extension block.
1365 int extension_block_pos =
1366 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1367 kRtpExtensionAudioLevel);
1368 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001369 // The feature is not enabled.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001370 return false;
1371 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001372 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001373 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1374 rtp_header.headerLength < block_pos + kAudioLevelLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001375 LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001376 return false;
1377 }
1378 // Verify that header contains extension.
1379 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1380 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001381 LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001382 return false;
1383 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001384 // Verify first byte in block.
1385 const uint8_t first_block_byte = (id << 4) + 0;
1386 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001387 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001388 return false;
1389 }
1390 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1391 return true;
1392}
1393
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001394void RTPSender::UpdateAbsoluteSendTime(uint8_t* rtp_packet,
1395 size_t rtp_packet_length,
1396 const RTPHeader& rtp_header,
1397 int64_t now_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001398 CriticalSectionScoped cs(send_critsect_.get());
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001399
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001400 // Get id.
1401 uint8_t id = 0;
1402 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1403 &id) != 0) {
1404 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001405 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001406 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001407 // Get length until start of header extension block.
1408 int extension_block_pos =
1409 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1410 kRtpExtensionAbsoluteSendTime);
1411 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001412 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001413 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001414 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001415 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001416 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001417 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001418 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001419 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001420 }
1421 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001422 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1423 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001424 LOG(LS_WARNING)
1425 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001426 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001427 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001428 // Verify first byte in block.
1429 const uint8_t first_block_byte = (id << 4) + 2;
1430 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001431 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001432 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001433 }
1434 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1435 // fractional part).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001436 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1437 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001438}
1439
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001440void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001441 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001442 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001443 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001444
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001445 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001446 SetStartTimestamp(RTPtime, false);
1447 } else {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001448 CriticalSectionScoped lock(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001449 if (!ssrc_forced_) {
1450 // Generate a new SSRC.
1451 ssrc_db_.ReturnSSRC(ssrc_);
1452 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001453 bitrates_->set_ssrc(ssrc_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001454 }
1455 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001456 if (!sequence_number_forced_ && !ssrc_forced_) {
1457 // Generate a new sequence number.
1458 sequence_number_ =
1459 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001460 }
1461 }
1462}
1463
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001464void RTPSender::SetSendingMediaStatus(bool enabled) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001465 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001466 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001467}
1468
1469bool RTPSender::SendingMedia() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001470 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001471 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001472}
1473
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001474uint32_t RTPSender::Timestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001475 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001476 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001477}
1478
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001479void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001480 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001481 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001482 start_timestamp_forced_ = true;
1483 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001484 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001485 if (!start_timestamp_forced_) {
1486 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001487 }
1488 }
1489}
1490
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001491uint32_t RTPSender::StartTimestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001492 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001493 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001494}
1495
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001496uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001497 // If configured via API, return 0.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001498 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001499
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001500 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001501 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001502 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001503 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001504 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001505 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001506}
1507
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001508void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001509 // This is configured via the API.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001510 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +00001511
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001512 if (ssrc_ == ssrc && ssrc_forced_) {
1513 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001514 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001515 ssrc_forced_ = true;
1516 ssrc_db_.ReturnSSRC(ssrc_);
1517 ssrc_db_.RegisterSSRC(ssrc);
1518 ssrc_ = ssrc;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001519 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001520 if (!sequence_number_forced_) {
1521 sequence_number_ =
1522 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001523 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001524}
1525
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001526uint32_t RTPSender::SSRC() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001527 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001528 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001529}
1530
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001531void RTPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
1532 assert(csrcs.size() <= kRtpCsrcSize);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001533 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001534 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001535}
1536
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001537void RTPSender::SetSequenceNumber(uint16_t seq) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001538 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001539 sequence_number_forced_ = true;
1540 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001541}
1542
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001543uint16_t RTPSender::SequenceNumber() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001544 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001545 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001546}
1547
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001548// Audio.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001549int32_t RTPSender::SendTelephoneEvent(uint8_t key,
1550 uint16_t time_ms,
1551 uint8_t level) {
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_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001556}
1557
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001558int32_t RTPSender::SetAudioPacketSize(uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001559 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001560 return -1;
1561 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001562 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001563}
1564
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001565int32_t RTPSender::SetAudioLevel(uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001566 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001567}
1568
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001569int32_t RTPSender::SetRED(int8_t payload_type) {
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_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001574}
1575
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001576int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001577 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001578 return -1;
1579 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001580 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001581}
1582
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001583// Video
1584VideoCodecInformation *RTPSender::CodecInformationVideo() {
1585 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001586 return NULL;
1587 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001588 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001589}
1590
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001591RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001592 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001593 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001594}
1595
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001596uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001597 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001598 return 0;
1599 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001600 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001601}
1602
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001603int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001604 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001605 return -1;
1606 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001607 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001608}
1609
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001610int32_t RTPSender::SetGenericFECStatus(bool enable,
1611 uint8_t payload_type_red,
1612 uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001613 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001614 return -1;
1615 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001616 return video_->SetGenericFECStatus(enable, payload_type_red,
1617 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001618}
1619
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00001620int32_t RTPSender::GenericFECStatus(bool* enable,
1621 uint8_t* payload_type_red,
1622 uint8_t* payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001623 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001624 return -1;
1625 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001626 return video_->GenericFECStatus(
1627 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001628}
1629
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001630int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001631 const FecProtectionParams *delta_params,
1632 const FecProtectionParams *key_params) {
1633 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001634 return -1;
1635 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001636 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001637}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001638
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001639void RTPSender::BuildRtxPacket(uint8_t* buffer, size_t* length,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001640 uint8_t* buffer_rtx) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001641 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001642 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001643 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001644 RtpUtility::RtpHeaderParser rtp_parser(
1645 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001646
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001647 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001648 rtp_parser.Parse(rtp_header);
1649
1650 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001651 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001652
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00001653 // Replace payload type, if a specific type is set for RTX.
1654 if (payload_type_rtx_ != -1) {
1655 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001656 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001657 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1658 }
1659
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001660 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001661 uint8_t *ptr = data_buffer_rtx + 2;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001662 RtpUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001663
1664 // Replace SSRC.
1665 ptr += 6;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001666 RtpUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001667
1668 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001669 ptr = data_buffer_rtx + rtp_header.headerLength;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001670 RtpUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001671 ptr += 2;
1672
1673 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001674 memcpy(ptr, buffer + rtp_header.headerLength,
1675 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001676 *length += 2;
1677}
1678
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001679void RTPSender::RegisterRtpStatisticsCallback(
1680 StreamDataCountersCallback* callback) {
1681 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001682 rtp_stats_callback_ = callback;
1683}
1684
1685StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1686 CriticalSectionScoped cs(statistics_crit_.get());
1687 return rtp_stats_callback_;
1688}
1689
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001690uint32_t RTPSender::BitrateSent() const {
1691 return total_bitrate_sent_.BitrateLast();
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001692}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001693
1694void RTPSender::SetRtpState(const RtpState& rtp_state) {
1695 SetStartTimestamp(rtp_state.start_timestamp, true);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001696 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001697 sequence_number_ = rtp_state.sequence_number;
1698 sequence_number_forced_ = true;
1699 timestamp_ = rtp_state.timestamp;
1700 capture_time_ms_ = rtp_state.capture_time_ms;
1701 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001702 media_has_been_sent_ = rtp_state.media_has_been_sent;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001703}
1704
1705RtpState RTPSender::GetRtpState() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001706 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001707
1708 RtpState state;
1709 state.sequence_number = sequence_number_;
1710 state.start_timestamp = start_timestamp_;
1711 state.timestamp = timestamp_;
1712 state.capture_time_ms = capture_time_ms_;
1713 state.last_timestamp_time_ms = last_timestamp_time_ms_;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001714 state.media_has_been_sent = media_has_been_sent_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001715
1716 return state;
1717}
1718
1719void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001720 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001721 sequence_number_rtx_ = rtp_state.sequence_number;
1722}
1723
1724RtpState RTPSender::GetRtxRtpState() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001725 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001726
1727 RtpState state;
1728 state.sequence_number = sequence_number_rtx_;
1729 state.start_timestamp = start_timestamp_;
1730
1731 return state;
1732}
1733
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001734} // namespace webrtc