blob: 30804f9ce087acd001022e93bd45045fdd5a9da2 [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
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +000015#include "webrtc/modules/rtp_rtcp/interface/rtp_cvo.h"
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000016#include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
17#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
18#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000019#include "webrtc/system_wrappers/interface/logging.h"
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000020#include "webrtc/system_wrappers/interface/tick_util.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000021#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
stefan@webrtc.orga8179622013-06-04 13:47:36 +000025// 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 +000026const size_t kMaxPaddingLength = 224;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +000027const int kSendSideDelayWindowMs = 1000;
stefan@webrtc.orga8179622013-06-04 13:47:36 +000028
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000029namespace {
30
guoweis@webrtc.org45362892015-03-04 22:55:15 +000031const size_t kRtpHeaderLength = 12;
32
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000033const char* FrameTypeToString(FrameType frame_type) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000034 switch (frame_type) {
35 case kFrameEmpty: return "empty";
36 case kAudioFrameSpeech: return "audio_speech";
37 case kAudioFrameCN: return "audio_cn";
38 case kVideoFrameKey: return "video_key";
39 case kVideoFrameDelta: return "video_delta";
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000040 }
41 return "";
42}
43
44} // namespace
45
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +000046class BitrateAggregator {
47 public:
48 explicit BitrateAggregator(BitrateStatisticsObserver* bitrate_callback)
49 : callback_(bitrate_callback),
50 total_bitrate_observer_(*this),
51 retransmit_bitrate_observer_(*this),
52 ssrc_(0) {}
53
54 void OnStatsUpdated() const {
55 if (callback_)
56 callback_->Notify(total_bitrate_observer_.statistics(),
57 retransmit_bitrate_observer_.statistics(),
58 ssrc_);
59 }
60
61 Bitrate::Observer* total_bitrate_observer() {
62 return &total_bitrate_observer_;
63 }
64 Bitrate::Observer* retransmit_bitrate_observer() {
65 return &retransmit_bitrate_observer_;
66 }
67
68 void set_ssrc(uint32_t ssrc) { ssrc_ = ssrc; }
69
70 private:
71 // We assume that these observers are called on the same thread, which is
72 // true for RtpSender as they are called on the Process thread.
73 class BitrateObserver : public Bitrate::Observer {
74 public:
75 explicit BitrateObserver(const BitrateAggregator& aggregator)
76 : aggregator_(aggregator) {}
77
78 // Implements Bitrate::Observer.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000079 void BitrateUpdated(const BitrateStatistics& stats) override {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +000080 statistics_ = stats;
81 aggregator_.OnStatsUpdated();
82 }
83
84 BitrateStatistics statistics() const { return statistics_; }
85
86 private:
87 BitrateStatistics statistics_;
88 const BitrateAggregator& aggregator_;
89 };
90
91 BitrateStatisticsObserver* const callback_;
92 BitrateObserver total_bitrate_observer_;
93 BitrateObserver retransmit_bitrate_observer_;
94 uint32_t ssrc_;
95};
96
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000097RTPSender::RTPSender(int32_t id,
98 bool audio,
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000099 Clock* clock,
100 Transport* transport,
101 RtpAudioFeedback* audio_feedback,
andresp@webrtc.orgd11bec42014-07-08 14:32:58 +0000102 PacedSender* paced_sender,
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000103 BitrateStatisticsObserver* bitrate_callback,
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000104 FrameCountObserver* frame_count_observer,
105 SendSideDelayObserver* send_side_delay_observer)
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000106 : clock_(clock),
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000107 // TODO(holmer): Remove this conversion when we remove the use of
108 // TickTime.
109 clock_delta_ms_(clock_->TimeInMilliseconds() -
110 TickTime::MillisecondTimestamp()),
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000111 bitrates_(new BitrateAggregator(bitrate_callback)),
112 total_bitrate_sent_(clock, bitrates_->total_bitrate_observer()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000113 id_(id),
114 audio_configured_(audio),
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000115 audio_(audio ? new RTPSenderAudio(id, clock, this, audio_feedback)
116 : nullptr),
117 video_(audio ? nullptr : new RTPSenderVideo(clock, this)),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000118 paced_sender_(paced_sender),
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000119 last_capture_time_ms_sent_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000120 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000121 transport_(transport),
122 sending_media_(true), // Default to sending media.
123 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000124 packet_over_head_(28),
125 payload_type_(-1),
126 payload_type_map_(),
127 rtp_header_extension_map_(),
128 transmission_time_offset_(0),
129 absolute_send_time_(0),
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000130 rotation_(kVideoRotation_0),
sprang@webrtc.org30933902015-03-17 14:33:12 +0000131 transport_sequence_number_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000132 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000133 nack_byte_count_times_(),
134 nack_byte_count_(),
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000135 nack_bitrate_(clock, bitrates_->retransmit_bitrate_observer()),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000136 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000137 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000138 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000139 rtp_stats_callback_(NULL),
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000140 frame_count_observer_(frame_count_observer),
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000141 send_side_delay_observer_(send_side_delay_observer),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000142 // RTP variables
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000143 start_timestamp_forced_(false),
144 start_timestamp_(0),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000145 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
146 remote_ssrc_(0),
147 sequence_number_forced_(false),
148 ssrc_forced_(false),
149 timestamp_(0),
150 capture_time_ms_(0),
151 last_timestamp_time_ms_(0),
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000152 media_has_been_sent_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000153 last_packet_marker_bit_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000154 csrcs_(),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000155 rtx_(kRtxOff),
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000156 payload_type_rtx_(-1),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000157 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000158 target_bitrate_(0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000159 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
160 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000161 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000162 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000163 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000164 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000165 bitrates_->set_ssrc(ssrc_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000166 // Random start, 16 bits. Can't be 0.
167 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
168 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000169}
170
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000171RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000172 if (remote_ssrc_ != 0) {
173 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000174 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000175 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000177 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000178 while (!payload_type_map_.empty()) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000179 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000180 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000181 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000182 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000183 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000184}
niklase@google.com470e71d2011-07-07 08:21:25 +0000185
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000186void RTPSender::SetTargetBitrate(uint32_t bitrate) {
187 CriticalSectionScoped cs(target_bitrate_critsect_.get());
188 target_bitrate_ = bitrate;
189}
190
191uint32_t RTPSender::GetTargetBitrate() {
192 CriticalSectionScoped cs(target_bitrate_critsect_.get());
193 return target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000194}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000195
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000196uint16_t RTPSender::ActualSendBitrateKbit() const {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000197 return (uint16_t)(total_bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000198}
199
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000200uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000201 if (video_) {
202 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000203 }
204 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000205}
206
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000207uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000208 if (video_) {
209 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000210 }
211 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000212}
213
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000214uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000215 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000216}
217
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000218bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
219 int* max_send_delay_ms) const {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000220 CriticalSectionScoped lock(statistics_crit_.get());
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000221 SendDelayMap::const_iterator it = send_delays_.upper_bound(
222 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000223 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000224 return false;
225 int num_delays = 0;
226 for (; it != send_delays_.end(); ++it) {
227 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
228 *avg_send_delay_ms += it->second;
229 ++num_delays;
230 }
231 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
232 return true;
233}
234
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000235int32_t RTPSender::SetTransmissionTimeOffset(int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000236 if (transmission_time_offset > (0x800000 - 1) ||
237 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000238 return -1;
239 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000240 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000241 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000242 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000243}
244
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000245int32_t RTPSender::SetAbsoluteSendTime(uint32_t absolute_send_time) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000246 if (absolute_send_time > 0xffffff) { // UWord24.
247 return -1;
248 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000249 CriticalSectionScoped cs(send_critsect_.get());
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000250 absolute_send_time_ = absolute_send_time;
251 return 0;
252}
253
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000254void RTPSender::SetVideoRotation(VideoRotation rotation) {
255 CriticalSectionScoped cs(send_critsect_.get());
256 rotation_ = rotation;
257}
258
sprang@webrtc.org30933902015-03-17 14:33:12 +0000259int32_t RTPSender::SetTransportSequenceNumber(uint16_t sequence_number) {
260 CriticalSectionScoped cs(send_critsect_.get());
261 transport_sequence_number_ = sequence_number;
262 return 0;
263}
264
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000265int32_t RTPSender::RegisterRtpHeaderExtension(RTPExtensionType type,
266 uint8_t id) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000267 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000268 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000269}
270
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000271bool RTPSender::IsRtpHeaderExtensionRegistered(RTPExtensionType type) {
272 CriticalSectionScoped cs(send_critsect_.get());
273 return rtp_header_extension_map_.IsRegistered(type);
274}
275
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000276int32_t RTPSender::DeregisterRtpHeaderExtension(RTPExtensionType type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000277 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000278 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000279}
280
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000281size_t RTPSender::RtpHeaderExtensionTotalLength() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000282 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000283 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000284}
285
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000286int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000287 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000288 int8_t payload_number,
289 uint32_t frequency,
290 uint8_t channels,
291 uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000292 assert(payload_name);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000293 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000294
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000295 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000296 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000297
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000298 if (payload_type_map_.end() != it) {
299 // We already use this payload type.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000300 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000301 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000302
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000303 // Check if it's the same as we already have.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000304 if (RtpUtility::StringCompare(
305 payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000306 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000307 payload->typeSpecific.Audio.frequency == frequency &&
308 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000309 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000310 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000311 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000312 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000313 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000314 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000315 return 0;
316 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000317 }
318 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000319 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000320 int32_t ret_val = -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000321 RtpUtility::Payload* payload = NULL;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000322 if (audio_configured_) {
323 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
324 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000325 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000326 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
327 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000328 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000329 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000330 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000331 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000332 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000333}
334
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000335int32_t RTPSender::DeRegisterSendPayload(int8_t payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000336 CriticalSectionScoped lock(send_critsect_.get());
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000337
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000338 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000339 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000340
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000341 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000342 return -1;
343 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000344 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000345 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000346 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000347 return 0;
348}
niklase@google.com470e71d2011-07-07 08:21:25 +0000349
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000350void RTPSender::SetSendPayloadType(int8_t payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000351 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000352 payload_type_ = payload_type;
353}
354
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000355int8_t RTPSender::SendPayloadType() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000356 CriticalSectionScoped cs(send_critsect_.get());
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000357 return payload_type_;
358}
niklase@google.com470e71d2011-07-07 08:21:25 +0000359
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000360int RTPSender::SendPayloadFrequency() const {
361 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
362}
niklase@google.com470e71d2011-07-07 08:21:25 +0000363
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000364int32_t RTPSender::SetMaxPayloadLength(size_t max_payload_length,
365 uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000366 // Sanity check.
367 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000368 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000369 return -1;
370 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000371 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000372 max_payload_length_ = max_payload_length;
373 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000374 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000375}
376
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000377size_t RTPSender::MaxDataPayloadLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000378 int rtx;
379 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000380 CriticalSectionScoped rtx_lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000381 rtx = rtx_;
382 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000383 if (audio_configured_) {
384 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000385 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000386 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
387 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000388 - ((rtx) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000389 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000390}
391
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000392size_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000393 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000394}
395
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000396uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000397
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000398void RTPSender::SetRtxStatus(int mode) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000399 CriticalSectionScoped cs(send_critsect_.get());
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000400 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000401}
402
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000403int RTPSender::RtxStatus() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000404 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000405 return rtx_;
406}
407
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000408void RTPSender::SetRtxSsrc(uint32_t ssrc) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000409 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000410 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000411}
412
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000413uint32_t RTPSender::RtxSsrc() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000414 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000415 return ssrc_rtx_;
416}
417
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000418void RTPSender::SetRtxPayloadType(int payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000419 CriticalSectionScoped cs(send_critsect_.get());
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000420 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000421}
422
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000423int32_t RTPSender::CheckPayloadType(int8_t payload_type,
424 RtpVideoCodecTypes* video_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000425 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000426
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000427 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000428 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000429 return -1;
430 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000431 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000432 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000433 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000434 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000435 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000436 // And it's a match...
437 return 0;
438 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000439 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000440 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000441 if (payload_type_ == payload_type) {
442 if (!audio_configured_) {
443 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000444 }
445 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000446 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000447 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000448 payload_type_map_.find(payload_type);
449 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000450 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000451 return -1;
452 }
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000453 SetSendPayloadType(payload_type);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000454 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000455 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000456 if (!payload->audio && !audio_configured_) {
457 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
458 *video_type = payload->typeSpecific.Video.videoCodecType;
459 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000460 }
461 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000462}
463
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000464int32_t RTPSender::SendOutgoingData(FrameType frame_type,
465 int8_t payload_type,
466 uint32_t capture_timestamp,
467 int64_t capture_time_ms,
468 const uint8_t* payload_data,
469 size_t payload_size,
470 const RTPFragmentationHeader* fragmentation,
471 VideoCodecInformation* codec_info,
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000472 const RTPVideoHeader* rtp_hdr) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000473 uint32_t ssrc;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000474 {
475 // Drop this packet if we're not sending media packets.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000476 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000477 ssrc = ssrc_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000478 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000479 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000480 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000481 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000482 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000483 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000484 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000485 return -1;
486 }
487
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000488 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000489 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000490 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
491 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000492 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000493 frame_type == kFrameEmpty);
494
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000495 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
496 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000497 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000498 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
499 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000500 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000501
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000502 if (frame_type == kFrameEmpty)
503 return 0;
504
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000505 ret_val =
506 video_->SendVideo(video_type, frame_type, payload_type,
507 capture_timestamp, capture_time_ms, payload_data,
508 payload_size, fragmentation, codec_info, rtp_hdr);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000509 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000510
511 CriticalSectionScoped cs(statistics_crit_.get());
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000512 // Note: This is currently only counting for video.
513 if (frame_type == kVideoFrameKey) {
514 ++frame_counts_.key_frames;
515 } else if (frame_type == kVideoFrameDelta) {
516 ++frame_counts_.delta_frames;
517 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000518 if (frame_count_observer_) {
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000519 frame_count_observer_->FrameCountUpdated(frame_counts_, ssrc);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000520 }
521
522 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000523}
524
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000525size_t RTPSender::TrySendRedundantPayloads(size_t bytes_to_send) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000526 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000527 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000528 if ((rtx_ & kRtxRedundantPayloads) == 0)
529 return 0;
530 }
531
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000532 uint8_t buffer[IP_PACKET_SIZE];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000533 int bytes_left = static_cast<int>(bytes_to_send);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000534 while (bytes_left > 0) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000535 size_t length = bytes_left;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000536 int64_t capture_time_ms;
537 if (!packet_history_.GetBestFittingPacket(buffer, &length,
538 &capture_time_ms)) {
539 break;
540 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000541 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000542 break;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000543 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000544 RTPHeader rtp_header;
545 rtp_parser.Parse(rtp_header);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000546 bytes_left -= static_cast<int>(length - rtp_header.headerLength);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000547 }
548 return bytes_to_send - bytes_left;
549}
550
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000551size_t RTPSender::BuildPaddingPacket(uint8_t* packet, size_t header_length) {
552 size_t padding_bytes_in_packet = kMaxPaddingLength;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000553 packet[0] |= 0x20; // Set padding bit.
554 int32_t *data =
555 reinterpret_cast<int32_t *>(&(packet[header_length]));
556
557 // Fill data buffer with random data.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000558 for (size_t j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000559 data[j] = rand(); // NOLINT
560 }
561 // Set number of padding bytes in the last byte of the packet.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000562 packet[header_length + padding_bytes_in_packet - 1] =
563 static_cast<uint8_t>(padding_bytes_in_packet);
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000564 return padding_bytes_in_packet;
565}
566
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000567size_t RTPSender::TrySendPadData(size_t bytes) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000568 int64_t capture_time_ms;
569 uint32_t timestamp;
570 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000571 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000572 timestamp = timestamp_;
573 capture_time_ms = capture_time_ms_;
574 if (last_timestamp_time_ms_ > 0) {
575 timestamp +=
576 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
577 capture_time_ms +=
578 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
579 }
580 }
581 return SendPadData(timestamp, capture_time_ms, bytes);
582}
583
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000584size_t RTPSender::SendPadData(uint32_t timestamp,
585 int64_t capture_time_ms,
586 size_t bytes) {
587 size_t padding_bytes_in_packet = 0;
588 size_t bytes_sent = 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000589 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000590 // Always send full padding packets.
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000591 if (bytes < kMaxPaddingLength)
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000592 bytes = kMaxPaddingLength;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000593
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000594 uint32_t ssrc;
595 uint16_t sequence_number;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000596 int payload_type;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000597 bool over_rtx;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000598 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000599 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000600 // Only send padding packets following the last packet of a frame,
601 // indicated by the marker bit.
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000602 if (rtx_ == kRtxOff) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000603 // Without RTX we can't send padding in the middle of frames.
604 if (!last_packet_marker_bit_)
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000605 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000606 ssrc = ssrc_;
607 sequence_number = sequence_number_;
608 ++sequence_number_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000609 payload_type = payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000610 over_rtx = false;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000611 } else {
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000612 // Without abs-send-time a media packet must be sent before padding so
613 // that the timestamps used for estimation are correct.
614 if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
615 kRtpExtensionAbsoluteSendTime))
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000616 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000617 ssrc = ssrc_rtx_;
618 sequence_number = sequence_number_rtx_;
619 ++sequence_number_rtx_;
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000620 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_
621 : payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000622 over_rtx = true;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000623 }
624 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000625
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000626 uint8_t padding_packet[IP_PACKET_SIZE];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000627 size_t header_length =
628 CreateRtpHeader(padding_packet, payload_type, ssrc, false, timestamp,
629 sequence_number, std::vector<uint32_t>());
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000630 assert(header_length != static_cast<size_t>(-1));
631 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length);
632 assert(padding_bytes_in_packet <= bytes);
633 size_t length = padding_bytes_in_packet + header_length;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000634 int64_t now_ms = clock_->TimeInMilliseconds();
635
636 RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
637 RTPHeader rtp_header;
638 rtp_parser.Parse(rtp_header);
639
640 if (capture_time_ms > 0) {
641 UpdateTransmissionTimeOffset(
642 padding_packet, length, rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000643 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000644
645 UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
646 if (!SendPacketToNetwork(padding_packet, length))
647 break;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000648 bytes_sent += padding_bytes_in_packet;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000649 UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000650 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000651
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000652 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000653}
654
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000655void RTPSender::SetStorePacketsStatus(bool enable, uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000656 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000657}
658
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000659bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000660 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000661}
niklase@google.com470e71d2011-07-07 08:21:25 +0000662
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000663int32_t RTPSender::ReSendPacket(uint16_t packet_id, int64_t min_resend_time) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000664 size_t length = IP_PACKET_SIZE;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000665 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000666 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000667 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
668 data_buffer, &length,
669 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000670 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000671 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000672 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000673
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000674 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000675 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000676 RTPHeader header;
677 if (!rtp_parser.Parse(header)) {
678 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000679 return -1;
680 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000681 // Convert from TickTime to Clock since capture_time_ms is based on
682 // TickTime.
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000683 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_;
684 if (!paced_sender_->SendPacket(
685 PacedSender::kHighPriority, header.ssrc, header.sequenceNumber,
686 corrected_capture_tims_ms, length - header.headerLength, true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000687 // We can't send the packet right now.
688 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000689 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000690 }
691 }
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000692 int rtx = kRtxOff;
693 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000694 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000695 rtx = rtx_;
696 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000697 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000698 (rtx & kRtxRetransmitted) > 0, true) ?
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000699 static_cast<int32_t>(length) : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000700}
701
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000702bool RTPSender::SendPacketToNetwork(const uint8_t *packet, size_t size) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000703 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000704 if (transport_) {
705 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000706 }
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000707 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
708 "RTPSender::SendPacketToNetwork", "size", size, "sent",
709 bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000710 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000711 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000712 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000713 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000714 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000715 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000716}
717
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000718int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000719 if (!video_)
720 return -1;
721 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000722}
723
724int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000725 if (!video_)
726 return -1;
727 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000728}
729
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000730void RTPSender::OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000731 int64_t avg_rtt) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000732 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
733 "RTPSender::OnReceivedNACK", "num_seqnum",
734 nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000735 const int64_t now = clock_->TimeInMilliseconds();
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000736 uint32_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000737 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000738
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000739 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000740 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000741 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000742 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000743 return;
744 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000745
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000746 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
747 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000748 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000749 if (bytes_sent > 0) {
750 bytes_re_sent += bytes_sent;
751 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000752 // The packet has previously been resent.
753 // Try resending next packet in the list.
754 continue;
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000755 } else {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000756 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000757 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
758 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000759 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000760 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000761 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000762 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000763 // kbits/s * ms = bits => bits/8 = bytes
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000764 size_t target_bytes =
765 (static_cast<size_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000766 if (bytes_re_sent > target_bytes) {
767 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000768 }
769 }
770 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000771 if (bytes_re_sent > 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000772 UpdateNACKBitRate(bytes_re_sent, now);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000773 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000774}
775
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000776bool RTPSender::ProcessNACKBitRate(uint32_t now) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000777 uint32_t num = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000778 size_t byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000779 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000780 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000781
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000782 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000783
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000784 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000785 return true;
786 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000787 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000788 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000789 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000790 break;
791 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000792 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000793 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000794 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000795 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000796 if (num == NACK_BYTECOUNT_SIZE) {
797 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000798 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000799 if (nack_byte_count_times_[num - 1] <= now) {
800 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000801 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000802 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000803 return (byte_count * 8) < (target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000804}
805
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000806void RTPSender::UpdateNACKBitRate(uint32_t bytes, int64_t now) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000807 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000808 if (bytes == 0)
809 return;
810 nack_bitrate_.Update(bytes);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000811 // Save bitrate statistics.
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000812 // Shift all but first time.
813 for (int i = NACK_BYTECOUNT_SIZE - 2; i >= 0; i--) {
814 nack_byte_count_[i + 1] = nack_byte_count_[i];
815 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000816 }
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000817 nack_byte_count_[0] = bytes;
818 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000819}
820
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000821// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000822bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000823 int64_t capture_time_ms,
824 bool retransmission) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000825 size_t length = IP_PACKET_SIZE;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000826 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000827 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000828
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000829 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
830 0,
831 retransmission,
832 data_buffer,
833 &length,
834 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000835 // Packet cannot be found. Allow sending to continue.
836 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000837 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000838 if (!retransmission && capture_time_ms > 0) {
839 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
840 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000841 int rtx;
842 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000843 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000844 rtx = rtx_;
845 }
846 return PrepareAndSendPacket(data_buffer,
847 length,
848 capture_time_ms,
849 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000850 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000851}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000852
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000853bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000854 size_t length,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000855 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000856 bool send_over_rtx,
857 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000858 uint8_t *buffer_to_send_ptr = buffer;
859
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000860 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000861 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000862 rtp_parser.Parse(rtp_header);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000863 if (!is_retransmit && rtp_header.markerBit) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000864 TRACE_EVENT_ASYNC_END0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PacedSend",
865 capture_time_ms);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000866 }
867
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000868 TRACE_EVENT_INSTANT2(
869 TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PrepareAndSendPacket",
870 "timestamp", rtp_header.timestamp, "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000871
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000872 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000873 if (send_over_rtx) {
874 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000875 buffer_to_send_ptr = data_buffer_rtx;
876 }
877
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000878 int64_t now_ms = clock_->TimeInMilliseconds();
879 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000880 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
881 diff_ms);
882 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000883 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000884 if (ret) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000885 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000886 media_has_been_sent_ = true;
887 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000888 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
889 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000890 return ret;
891}
892
893void RTPSender::UpdateRtpStats(const uint8_t* buffer,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000894 size_t packet_length,
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000895 const RTPHeader& header,
896 bool is_rtx,
897 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000898 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000899 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000900 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000901
902 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000903 if (is_rtx) {
904 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000905 } else {
906 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000907 }
908
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000909 total_bitrate_sent_.Update(packet_length);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000910
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000911 if (counters->first_packet_time_ms == -1) {
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000912 counters->first_packet_time_ms = clock_->TimeInMilliseconds();
913 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000914 if (IsFecPacket(buffer, header)) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000915 counters->fec.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000916 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000917 if (is_retransmit) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000918 counters->retransmitted.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000919 }
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000920 counters->transmitted.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000921
922 if (rtp_stats_callback_) {
923 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
924 }
925}
926
927bool RTPSender::IsFecPacket(const uint8_t* buffer,
928 const RTPHeader& header) const {
929 if (!video_) {
930 return false;
931 }
932 bool fec_enabled;
933 uint8_t pt_red;
934 uint8_t pt_fec;
935 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
936 return fec_enabled &&
937 header.payloadType == pt_red &&
938 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000939}
940
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000941size_t RTPSender::TimeToSendPadding(size_t bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000942 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000943 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000944 if (!sending_media_) return 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000945 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000946 if (bytes == 0)
947 return 0;
948 size_t bytes_sent = TrySendRedundantPayloads(bytes);
949 if (bytes_sent < bytes)
950 bytes_sent += TrySendPadData(bytes - bytes_sent);
951 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000952}
953
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000954// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000955int32_t RTPSender::SendToNetwork(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000956 uint8_t *buffer, size_t payload_length, size_t rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000957 int64_t capture_time_ms, StorageType storage,
958 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000959 RtpUtility::RtpHeaderParser rtp_parser(buffer,
960 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000961 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000962 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000963
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000964 int64_t now_ms = clock_->TimeInMilliseconds();
965
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000966 // |capture_time_ms| <= 0 is considered invalid.
967 // TODO(holmer): This should be changed all over Video Engine so that negative
968 // time is consider invalid, while 0 is considered a valid time.
969 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000970 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000971 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000972 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000973
974 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
975 rtp_header, now_ms);
976
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000977 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000978 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
979 max_payload_length_, capture_time_ms,
980 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000981 return -1;
982 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000983
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000984 if (paced_sender_ && storage != kDontStore) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000985 // Correct offset between implementations of millisecond time stamps in
986 // TickTime and Clock.
987 int64_t corrected_time_ms = capture_time_ms + clock_delta_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000988 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000989 rtp_header.sequenceNumber, corrected_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000990 payload_length, false)) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000991 if (last_capture_time_ms_sent_ == 0 ||
992 corrected_time_ms > last_capture_time_ms_sent_) {
993 last_capture_time_ms_sent_ = corrected_time_ms;
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000994 TRACE_EVENT_ASYNC_BEGIN1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
995 "PacedSend", corrected_time_ms,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000996 "capture_time_ms", corrected_time_ms);
997 }
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000998 // We can't send the packet right now.
999 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +00001000 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001001 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +00001002 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001003 if (capture_time_ms > 0) {
1004 UpdateDelayStatistics(capture_time_ms, now_ms);
1005 }
sprang@webrtc.org43c88392015-01-29 09:09:17 +00001006
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001007 size_t length = payload_length + rtp_header_length;
sprang@webrtc.orgc957ffc2015-02-02 13:08:02 +00001008 bool sent = SendPacketToNetwork(buffer, length);
1009
1010 if (storage != kDontStore) {
1011 // Mark the packet as sent in the history even if send failed. Dropping a
1012 // packet here should be treated as any other packet drop so we should be
1013 // ready for a retransmission.
1014 packet_history_.SetSent(rtp_header.sequenceNumber);
1015 }
1016 if (!sent)
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001017 return -1;
sprang@webrtc.orgc957ffc2015-02-02 13:08:02 +00001018
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001019 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001020 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001021 media_has_been_sent_ = true;
1022 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001023 UpdateRtpStats(buffer, length, rtp_header, false, false);
1024 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +00001025}
1026
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001027void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001028 uint32_t ssrc;
1029 int avg_delay_ms = 0;
1030 int max_delay_ms = 0;
1031 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001032 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001033 ssrc = ssrc_;
1034 }
1035 {
1036 CriticalSectionScoped cs(statistics_crit_.get());
1037 // TODO(holmer): Compute this iteratively instead.
1038 send_delays_[now_ms] = now_ms - capture_time_ms;
1039 send_delays_.erase(send_delays_.begin(),
1040 send_delays_.lower_bound(now_ms -
1041 kSendSideDelayWindowMs));
1042 }
1043 if (send_side_delay_observer_ &&
1044 GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
1045 send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
1046 max_delay_ms, ssrc);
1047 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001048}
1049
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001050void RTPSender::ProcessBitrate() {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001051 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001052 total_bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001053 nack_bitrate_.Process();
1054 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001055 return;
1056 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001057 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +00001058}
1059
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001060size_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001061 CriticalSectionScoped lock(send_critsect_.get());
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001062 size_t rtp_header_length = kRtpHeaderLength;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001063 rtp_header_length += sizeof(uint32_t) * csrcs_.size();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001064 rtp_header_length += RtpHeaderExtensionTotalLength();
1065 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001066}
1067
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001068uint16_t RTPSender::IncrementSequenceNumber() {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001069 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001070 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001071}
1072
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001073void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001074 uint32_t ssrc;
1075 uint32_t ssrc_rtx;
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001076 bool report_rtx;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001077 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001078 CriticalSectionScoped ssrc_lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001079 ssrc = ssrc_;
1080 ssrc_rtx = ssrc_rtx_;
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001081 report_rtx = rtx_ != kRtxOff;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001082 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001083 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001084 rtp_stats_ = StreamDataCounters();
1085 rtx_rtp_stats_ = StreamDataCounters();
1086 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001087 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001088 if (report_rtx)
1089 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001090 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001091}
1092
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001093void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1094 StreamDataCounters* rtx_stats) const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001095 CriticalSectionScoped lock(statistics_crit_.get());
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001096 *rtp_stats = rtp_stats_;
1097 *rtx_stats = rtx_rtp_stats_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001098}
1099
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001100size_t RTPSender::CreateRtpHeader(uint8_t* header,
1101 int8_t payload_type,
1102 uint32_t ssrc,
1103 bool marker_bit,
1104 uint32_t timestamp,
1105 uint16_t sequence_number,
1106 const std::vector<uint32_t>& csrcs) const {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001107 header[0] = 0x80; // version 2.
1108 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001109 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001110 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001111 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001112 RtpUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1113 RtpUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1114 RtpUtility::AssignUWord32ToBuffer(header + 8, ssrc);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001115 int32_t rtp_header_length = kRtpHeaderLength;
niklase@google.com470e71d2011-07-07 08:21:25 +00001116
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001117 if (csrcs.size() > 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001118 uint8_t *ptr = &header[rtp_header_length];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001119 for (size_t i = 0; i < csrcs.size(); ++i) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001120 RtpUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001121 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001122 }
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001123 header[0] = (header[0] & 0xf0) | csrcs.size();
niklase@google.com470e71d2011-07-07 08:21:25 +00001124
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001125 // Update length of header.
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001126 rtp_header_length += sizeof(uint32_t) * csrcs.size();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001127 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001128
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001129 uint16_t len =
1130 BuildRTPHeaderExtension(header + rtp_header_length, marker_bit);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001131 if (len > 0) {
1132 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001133 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001134 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001135 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001136}
1137
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001138int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001139 int8_t payload_type,
1140 bool marker_bit,
1141 uint32_t capture_timestamp,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001142 int64_t capture_time_ms,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001143 bool timestamp_provided,
1144 bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001145 assert(payload_type >= 0);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001146 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001147
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001148 if (timestamp_provided) {
1149 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001150 } else {
1151 // Make a unique time stamp.
1152 // We can't inc by the actual time, since then we increase the risk of back
1153 // timing.
1154 timestamp_++;
1155 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001156 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001157 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001158 capture_time_ms_ = capture_time_ms;
1159 last_packet_marker_bit_ = marker_bit;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001160 return CreateRtpHeader(data_buffer, payload_type, ssrc_, marker_bit,
1161 timestamp_, sequence_number, csrcs_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001162}
1163
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001164uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer,
1165 bool marker_bit) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001166 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001167 return 0;
1168 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001169 // RTP header extension, RFC 3550.
1170 // 0 1 2 3
1171 // 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
1172 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1173 // | defined by profile | length |
1174 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1175 // | header extension |
1176 // | .... |
1177 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001178 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001179 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001180
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001181 // Add extension ID (0xBEDE).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001182 RtpUtility::AssignUWord16ToBuffer(data_buffer, kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001183
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001184 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001185 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001186
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001187 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001188 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001189 uint8_t block_length = 0;
sprang@webrtc.org30933902015-03-17 14:33:12 +00001190 uint8_t* extension_data = &data_buffer[kHeaderLength + total_block_length];
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001191 switch (type) {
1192 case kRtpExtensionTransmissionTimeOffset:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001193 block_length = BuildTransmissionTimeOffsetExtension(extension_data);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001194 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001195 case kRtpExtensionAudioLevel:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001196 block_length = BuildAudioLevelExtension(extension_data);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001197 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001198 case kRtpExtensionAbsoluteSendTime:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001199 block_length = BuildAbsoluteSendTimeExtension(extension_data);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001200 break;
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001201 case kRtpExtensionVideoRotation:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001202 if (marker_bit)
1203 block_length = BuildVideoRotationExtension(extension_data);
1204 break;
1205 case kRtpExtensionTransportSequenceNumber:
1206 block_length = BuildTransportSequenceNumberExtension(extension_data);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001207 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001208 default:
1209 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001210 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001211 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001212 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001213 }
1214 if (total_block_length == 0) {
1215 // No extension added.
1216 return 0;
1217 }
sprang@webrtc.org30933902015-03-17 14:33:12 +00001218 // Add padding elements until we've filled a 32 bit block.
1219 size_t padding_bytes =
1220 RtpUtility::Word32Align(total_block_length) - total_block_length;
1221 if (padding_bytes > 0) {
1222 memset(&data_buffer[kHeaderLength + total_block_length], 0, padding_bytes);
1223 total_block_length += padding_bytes;
1224 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001225 // Set header length (in number of Word32, header excluded).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001226 RtpUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
1227 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001228 // Total added length.
1229 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001230}
1231
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001232uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1233 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001234 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1235 //
1236 // The transmission time is signaled to the receiver in-band using the
1237 // general mechanism for RTP header extensions [RFC5285]. The payload
1238 // of this extension (the transmitted value) is a 24-bit signed integer.
1239 // When added to the RTP timestamp of the packet, it represents the
1240 // "effective" RTP transmission time of the packet, on the RTP
1241 // timescale.
1242 //
1243 // The form of the transmission offset extension block:
1244 //
1245 // 0 1 2 3
1246 // 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
1247 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1248 // | ID | len=2 | transmission offset |
1249 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001250
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001251 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001252 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001253 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1254 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001255 // Not registered.
1256 return 0;
1257 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001258 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001259 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001260 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001261 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos,
1262 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001263 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001264 assert(pos == kTransmissionTimeOffsetLength);
1265 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001266}
1267
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001268uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1269 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1270 //
1271 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1272 //
1273 // The form of the audio level extension block:
1274 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001275 // 0 1
1276 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1277 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1278 // | ID | len=0 |V| level |
1279 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001280 //
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001281
1282 // Get id defined by user.
1283 uint8_t id;
1284 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1285 // Not registered.
1286 return 0;
1287 }
1288 size_t pos = 0;
1289 const uint8_t len = 0;
1290 data_buffer[pos++] = (id << 4) + len;
1291 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001292 assert(pos == kAudioLevelLength);
1293 return kAudioLevelLength;
1294}
1295
1296uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001297 // Absolute send time in RTP streams.
1298 //
1299 // The absolute send time is signaled to the receiver in-band using the
1300 // general mechanism for RTP header extensions [RFC5285]. The payload
1301 // of this extension (the transmitted value) is a 24-bit unsigned integer
1302 // containing the sender's current time in seconds as a fixed point number
1303 // with 18 bits fractional part.
1304 //
1305 // The form of the absolute send time extension block:
1306 //
1307 // 0 1 2 3
1308 // 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
1309 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1310 // | ID | len=2 | absolute send time |
1311 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1312
1313 // Get id defined by user.
1314 uint8_t id;
1315 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1316 &id) != 0) {
1317 // Not registered.
1318 return 0;
1319 }
1320 size_t pos = 0;
1321 const uint8_t len = 2;
1322 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001323 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos, absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001324 pos += 3;
1325 assert(pos == kAbsoluteSendTimeLength);
1326 return kAbsoluteSendTimeLength;
1327}
1328
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001329uint8_t RTPSender::BuildVideoRotationExtension(uint8_t* data_buffer) const {
1330 // Coordination of Video Orientation in RTP streams.
1331 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001332 // Coordination of Video Orientation consists in signaling of the current
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001333 // orientation of the image captured on the sender side to the receiver for
1334 // appropriate rendering and displaying.
1335 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001336 // 0 1
1337 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1338 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1339 // | ID | len=0 |0 0 0 0 C F R R|
1340 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001341 //
1342
1343 // Get id defined by user.
1344 uint8_t id;
1345 if (rtp_header_extension_map_.GetId(kRtpExtensionVideoRotation, &id) != 0) {
1346 // Not registered.
1347 return 0;
1348 }
1349 size_t pos = 0;
1350 const uint8_t len = 0;
1351 data_buffer[pos++] = (id << 4) + len;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +00001352 data_buffer[pos++] = ConvertVideoRotationToCVOByte(rotation_);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001353 assert(pos == kVideoRotationLength);
1354 return kVideoRotationLength;
1355}
1356
sprang@webrtc.org30933902015-03-17 14:33:12 +00001357uint8_t RTPSender::BuildTransportSequenceNumberExtension(
1358 uint8_t* data_buffer) const {
1359 // 0 1 2
1360 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
1361 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1362 // | ID | L=1 |transport wide sequence number |
1363 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1364
1365 // Get id defined by user.
1366 uint8_t id;
1367 if (rtp_header_extension_map_.GetId(kRtpExtensionTransportSequenceNumber,
1368 &id) != 0) {
1369 // Not registered.
1370 return 0;
1371 }
1372 size_t pos = 0;
1373 const uint8_t len = 1;
1374 data_buffer[pos++] = (id << 4) + len;
1375 RtpUtility::AssignUWord16ToBuffer(data_buffer + pos,
1376 transport_sequence_number_);
1377 pos += 2;
1378 assert(pos == kTransportSequenceNumberLength);
1379 return kTransportSequenceNumberLength;
1380}
1381
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001382bool RTPSender::FindHeaderExtensionPosition(RTPExtensionType type,
1383 const uint8_t* rtp_packet,
1384 size_t rtp_packet_length,
1385 const RTPHeader& rtp_header,
1386 size_t* position) const {
1387 // Get length until start of header extension block.
1388 int extension_block_pos =
1389 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(type);
1390 if (extension_block_pos < 0) {
1391 LOG(LS_WARNING) << "Failed to find extension position for " << type
1392 << " as it is not registered.";
1393 return false;
1394 }
1395
1396 HeaderExtension header_extension(type);
1397
1398 size_t block_pos =
1399 kRtpHeaderLength + rtp_header.numCSRCs + extension_block_pos;
1400 if (rtp_packet_length < block_pos + header_extension.length ||
1401 rtp_header.headerLength < block_pos + header_extension.length) {
1402 LOG(LS_WARNING) << "Failed to find extension position for " << type
1403 << " as the length is invalid.";
1404 return false;
1405 }
1406
1407 // Verify that header contains extension.
1408 if (!((rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs] == 0xBE) &&
1409 (rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs + 1] == 0xDE))) {
1410 LOG(LS_WARNING) << "Failed to find extension position for " << type
1411 << "as hdr extension not found.";
1412 return false;
1413 }
1414
1415 *position = block_pos;
1416 return true;
1417}
1418
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001419void RTPSender::UpdateTransmissionTimeOffset(uint8_t* rtp_packet,
1420 size_t rtp_packet_length,
1421 const RTPHeader& rtp_header,
1422 int64_t time_diff_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001423 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001424 // Get id.
1425 uint8_t id = 0;
1426 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1427 &id) != 0) {
1428 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001429 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001430 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001431
1432 size_t block_pos = 0;
1433 if (!FindHeaderExtensionPosition(kRtpExtensionTransmissionTimeOffset,
1434 rtp_packet, rtp_packet_length, rtp_header,
1435 &block_pos)) {
1436 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001437 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001438 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001439
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001440 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001441 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001442 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001443 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001444 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001445 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001446 // Update transmission offset field (converting to a 90 kHz timestamp).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001447 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1448 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001449}
1450
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001451bool RTPSender::UpdateAudioLevel(uint8_t* rtp_packet,
1452 size_t rtp_packet_length,
1453 const RTPHeader& rtp_header,
1454 bool is_voiced,
1455 uint8_t dBov) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001456 CriticalSectionScoped cs(send_critsect_.get());
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001457
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001458 // Get id.
1459 uint8_t id = 0;
1460 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1461 // Not registered.
1462 return false;
1463 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001464
1465 size_t block_pos = 0;
1466 if (!FindHeaderExtensionPosition(kRtpExtensionAudioLevel, rtp_packet,
1467 rtp_packet_length, rtp_header, &block_pos)) {
1468 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001469 return false;
1470 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001471
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001472 // Verify first byte in block.
1473 const uint8_t first_block_byte = (id << 4) + 0;
1474 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001475 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001476 return false;
1477 }
1478 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1479 return true;
1480}
1481
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001482bool RTPSender::UpdateVideoRotation(uint8_t* rtp_packet,
1483 size_t rtp_packet_length,
1484 const RTPHeader& rtp_header,
1485 VideoRotation rotation) const {
1486 CriticalSectionScoped cs(send_critsect_.get());
1487
1488 // Get id.
1489 uint8_t id = 0;
1490 if (rtp_header_extension_map_.GetId(kRtpExtensionVideoRotation, &id) != 0) {
1491 // Not registered.
1492 return false;
1493 }
1494
1495 size_t block_pos = 0;
1496 if (!FindHeaderExtensionPosition(kRtpExtensionVideoRotation, rtp_packet,
1497 rtp_packet_length, rtp_header, &block_pos)) {
1498 LOG(LS_WARNING) << "Failed to update video rotation (CVO).";
1499 return false;
1500 }
1501 // Get length until start of header extension block.
1502 int extension_block_pos =
1503 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1504 kRtpExtensionVideoRotation);
1505 if (extension_block_pos < 0) {
1506 // The feature is not enabled.
1507 return false;
1508 }
1509
1510 // Verify first byte in block.
1511 const uint8_t first_block_byte = (id << 4) + 0;
1512 if (rtp_packet[block_pos] != first_block_byte) {
1513 LOG(LS_WARNING) << "Failed to update CVO.";
1514 return false;
1515 }
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +00001516 rtp_packet[block_pos + 1] = ConvertVideoRotationToCVOByte(rotation);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001517 return true;
1518}
1519
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001520void RTPSender::UpdateAbsoluteSendTime(uint8_t* rtp_packet,
1521 size_t rtp_packet_length,
1522 const RTPHeader& rtp_header,
1523 int64_t now_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001524 CriticalSectionScoped cs(send_critsect_.get());
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001525
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001526 // Get id.
1527 uint8_t id = 0;
1528 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1529 &id) != 0) {
1530 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001531 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001532 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001533 // Get length until start of header extension block.
1534 int extension_block_pos =
1535 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1536 kRtpExtensionAbsoluteSendTime);
1537 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001538 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001539 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001540 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001541 size_t block_pos =
1542 kRtpHeaderLength + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001543 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001544 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001545 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001546 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001547 }
1548 // Verify that header contains extension.
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001549 if (!((rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs] == 0xBE) &&
1550 (rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001551 LOG(LS_WARNING)
1552 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001553 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001554 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001555 // Verify first byte in block.
1556 const uint8_t first_block_byte = (id << 4) + 2;
1557 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001558 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001559 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001560 }
1561 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1562 // fractional part).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001563 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1564 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001565}
1566
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001567void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001568 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001569 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001570 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001571
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001572 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001573 SetStartTimestamp(RTPtime, false);
1574 } else {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001575 CriticalSectionScoped lock(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001576 if (!ssrc_forced_) {
1577 // Generate a new SSRC.
1578 ssrc_db_.ReturnSSRC(ssrc_);
1579 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001580 bitrates_->set_ssrc(ssrc_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001581 }
1582 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001583 if (!sequence_number_forced_ && !ssrc_forced_) {
1584 // Generate a new sequence number.
1585 sequence_number_ =
1586 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001587 }
1588 }
1589}
1590
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001591void RTPSender::SetSendingMediaStatus(bool enabled) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001592 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001593 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001594}
1595
1596bool RTPSender::SendingMedia() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001597 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001598 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001599}
1600
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001601uint32_t RTPSender::Timestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001602 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001603 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001604}
1605
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001606void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001607 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001608 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001609 start_timestamp_forced_ = true;
1610 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001611 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001612 if (!start_timestamp_forced_) {
1613 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001614 }
1615 }
1616}
1617
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001618uint32_t RTPSender::StartTimestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001619 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001620 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001621}
1622
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001623uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001624 // If configured via API, return 0.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001625 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001626
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001627 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001628 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001629 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001630 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001631 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001632 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001633}
1634
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001635void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001636 // This is configured via the API.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001637 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +00001638
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001639 if (ssrc_ == ssrc && ssrc_forced_) {
1640 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001641 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001642 ssrc_forced_ = true;
1643 ssrc_db_.ReturnSSRC(ssrc_);
1644 ssrc_db_.RegisterSSRC(ssrc);
1645 ssrc_ = ssrc;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001646 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001647 if (!sequence_number_forced_) {
1648 sequence_number_ =
1649 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001650 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001651}
1652
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001653uint32_t RTPSender::SSRC() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001654 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001655 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001656}
1657
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001658void RTPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
1659 assert(csrcs.size() <= kRtpCsrcSize);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001660 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001661 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001662}
1663
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001664void RTPSender::SetSequenceNumber(uint16_t seq) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001665 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001666 sequence_number_forced_ = true;
1667 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001668}
1669
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001670uint16_t RTPSender::SequenceNumber() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001671 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001672 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001673}
1674
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001675// Audio.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001676int32_t RTPSender::SendTelephoneEvent(uint8_t key,
1677 uint16_t time_ms,
1678 uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001679 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001680 return -1;
1681 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001682 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001683}
1684
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001685int32_t RTPSender::SetAudioPacketSize(uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001686 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001687 return -1;
1688 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001689 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001690}
1691
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001692int32_t RTPSender::SetAudioLevel(uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001693 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001694}
1695
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001696int32_t RTPSender::SetRED(int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001697 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001698 return -1;
1699 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001700 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001701}
1702
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001703int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001704 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001705 return -1;
1706 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001707 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001708}
1709
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001710// Video
1711VideoCodecInformation *RTPSender::CodecInformationVideo() {
1712 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001713 return NULL;
1714 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001715 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001716}
1717
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001718RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001719 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001720 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001721}
1722
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001723uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001724 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001725 return 0;
1726 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001727 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001728}
1729
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001730int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001731 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001732 return -1;
1733 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001734 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001735}
1736
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001737int32_t RTPSender::SetGenericFECStatus(bool enable,
1738 uint8_t payload_type_red,
1739 uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001740 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001741 return -1;
1742 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001743 return video_->SetGenericFECStatus(enable, payload_type_red,
1744 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001745}
1746
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00001747int32_t RTPSender::GenericFECStatus(bool* enable,
1748 uint8_t* payload_type_red,
1749 uint8_t* payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001750 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001751 return -1;
1752 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001753 return video_->GenericFECStatus(
1754 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001755}
1756
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001757int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001758 const FecProtectionParams *delta_params,
1759 const FecProtectionParams *key_params) {
1760 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001761 return -1;
1762 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001763 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001764}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001765
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001766void RTPSender::BuildRtxPacket(uint8_t* buffer, size_t* length,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001767 uint8_t* buffer_rtx) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001768 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001769 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001770 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001771 RtpUtility::RtpHeaderParser rtp_parser(
1772 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001773
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001774 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001775 rtp_parser.Parse(rtp_header);
1776
1777 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001778 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001779
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00001780 // Replace payload type, if a specific type is set for RTX.
1781 if (payload_type_rtx_ != -1) {
1782 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001783 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001784 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1785 }
1786
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001787 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001788 uint8_t *ptr = data_buffer_rtx + 2;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001789 RtpUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001790
1791 // Replace SSRC.
1792 ptr += 6;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001793 RtpUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001794
1795 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001796 ptr = data_buffer_rtx + rtp_header.headerLength;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001797 RtpUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001798 ptr += 2;
1799
1800 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001801 memcpy(ptr, buffer + rtp_header.headerLength,
1802 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001803 *length += 2;
1804}
1805
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001806void RTPSender::RegisterRtpStatisticsCallback(
1807 StreamDataCountersCallback* callback) {
1808 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001809 rtp_stats_callback_ = callback;
1810}
1811
1812StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1813 CriticalSectionScoped cs(statistics_crit_.get());
1814 return rtp_stats_callback_;
1815}
1816
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001817uint32_t RTPSender::BitrateSent() const {
1818 return total_bitrate_sent_.BitrateLast();
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001819}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001820
1821void RTPSender::SetRtpState(const RtpState& rtp_state) {
1822 SetStartTimestamp(rtp_state.start_timestamp, true);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001823 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001824 sequence_number_ = rtp_state.sequence_number;
1825 sequence_number_forced_ = true;
1826 timestamp_ = rtp_state.timestamp;
1827 capture_time_ms_ = rtp_state.capture_time_ms;
1828 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001829 media_has_been_sent_ = rtp_state.media_has_been_sent;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001830}
1831
1832RtpState RTPSender::GetRtpState() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001833 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001834
1835 RtpState state;
1836 state.sequence_number = sequence_number_;
1837 state.start_timestamp = start_timestamp_;
1838 state.timestamp = timestamp_;
1839 state.capture_time_ms = capture_time_ms_;
1840 state.last_timestamp_time_ms = last_timestamp_time_ms_;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001841 state.media_has_been_sent = media_has_been_sent_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001842
1843 return state;
1844}
1845
1846void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001847 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001848 sequence_number_rtx_ = rtp_state.sequence_number;
1849}
1850
1851RtpState RTPSender::GetRtxRtpState() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001852 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001853
1854 RtpState state;
1855 state.sequence_number = sequence_number_rtx_;
1856 state.start_timestamp = start_timestamp_;
1857
1858 return state;
1859}
1860
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001861} // namespace webrtc