blob: 81de5a3ba3a4ca21d931aaaaa1c07ea52147ddc9 [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"
sprang@webrtc.org779c3d12015-03-17 16:42:49 +000016#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000017#include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
18#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
19#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000020#include "webrtc/system_wrappers/interface/logging.h"
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000021#include "webrtc/system_wrappers/interface/tick_util.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000022#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000025
stefan@webrtc.orga8179622013-06-04 13:47:36 +000026// 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 +000027const size_t kMaxPaddingLength = 224;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +000028const int kSendSideDelayWindowMs = 1000;
stefan@webrtc.orga8179622013-06-04 13:47:36 +000029
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000030namespace {
31
guoweis@webrtc.org45362892015-03-04 22:55:15 +000032const size_t kRtpHeaderLength = 12;
33
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000034const char* FrameTypeToString(FrameType frame_type) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000035 switch (frame_type) {
36 case kFrameEmpty: return "empty";
37 case kAudioFrameSpeech: return "audio_speech";
38 case kAudioFrameCN: return "audio_cn";
39 case kVideoFrameKey: return "video_key";
40 case kVideoFrameDelta: return "video_delta";
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000041 }
42 return "";
43}
44
45} // namespace
46
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +000047class BitrateAggregator {
48 public:
49 explicit BitrateAggregator(BitrateStatisticsObserver* bitrate_callback)
50 : callback_(bitrate_callback),
51 total_bitrate_observer_(*this),
52 retransmit_bitrate_observer_(*this),
53 ssrc_(0) {}
54
55 void OnStatsUpdated() const {
56 if (callback_)
57 callback_->Notify(total_bitrate_observer_.statistics(),
58 retransmit_bitrate_observer_.statistics(),
59 ssrc_);
60 }
61
62 Bitrate::Observer* total_bitrate_observer() {
63 return &total_bitrate_observer_;
64 }
65 Bitrate::Observer* retransmit_bitrate_observer() {
66 return &retransmit_bitrate_observer_;
67 }
68
69 void set_ssrc(uint32_t ssrc) { ssrc_ = ssrc; }
70
71 private:
72 // We assume that these observers are called on the same thread, which is
73 // true for RtpSender as they are called on the Process thread.
74 class BitrateObserver : public Bitrate::Observer {
75 public:
76 explicit BitrateObserver(const BitrateAggregator& aggregator)
77 : aggregator_(aggregator) {}
78
79 // Implements Bitrate::Observer.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000080 void BitrateUpdated(const BitrateStatistics& stats) override {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +000081 statistics_ = stats;
82 aggregator_.OnStatsUpdated();
83 }
84
85 BitrateStatistics statistics() const { return statistics_; }
86
87 private:
88 BitrateStatistics statistics_;
89 const BitrateAggregator& aggregator_;
90 };
91
92 BitrateStatisticsObserver* const callback_;
93 BitrateObserver total_bitrate_observer_;
94 BitrateObserver retransmit_bitrate_observer_;
95 uint32_t ssrc_;
96};
97
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000098RTPSender::RTPSender(int32_t id,
99 bool audio,
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000100 Clock* clock,
101 Transport* transport,
102 RtpAudioFeedback* audio_feedback,
andresp@webrtc.orgd11bec42014-07-08 14:32:58 +0000103 PacedSender* paced_sender,
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000104 BitrateStatisticsObserver* bitrate_callback,
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000105 FrameCountObserver* frame_count_observer,
106 SendSideDelayObserver* send_side_delay_observer)
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000107 : clock_(clock),
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000108 // TODO(holmer): Remove this conversion when we remove the use of
109 // TickTime.
110 clock_delta_ms_(clock_->TimeInMilliseconds() -
111 TickTime::MillisecondTimestamp()),
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000112 bitrates_(new BitrateAggregator(bitrate_callback)),
113 total_bitrate_sent_(clock, bitrates_->total_bitrate_observer()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000114 id_(id),
115 audio_configured_(audio),
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000116 audio_(audio ? new RTPSenderAudio(id, clock, this, audio_feedback)
117 : nullptr),
118 video_(audio ? nullptr : new RTPSenderVideo(clock, this)),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000119 paced_sender_(paced_sender),
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000120 last_capture_time_ms_sent_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000121 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000122 transport_(transport),
123 sending_media_(true), // Default to sending media.
124 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000125 packet_over_head_(28),
126 payload_type_(-1),
127 payload_type_map_(),
128 rtp_header_extension_map_(),
129 transmission_time_offset_(0),
130 absolute_send_time_(0),
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000131 rotation_(kVideoRotation_0),
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700132 cvo_mode_(kCVONone),
sprang@webrtc.org30933902015-03-17 14:33:12 +0000133 transport_sequence_number_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000134 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000135 nack_byte_count_times_(),
136 nack_byte_count_(),
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000137 nack_bitrate_(clock, bitrates_->retransmit_bitrate_observer()),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000138 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000139 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000140 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000141 rtp_stats_callback_(NULL),
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000142 frame_count_observer_(frame_count_observer),
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000143 send_side_delay_observer_(send_side_delay_observer),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000144 // RTP variables
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000145 start_timestamp_forced_(false),
146 start_timestamp_(0),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000147 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
148 remote_ssrc_(0),
149 sequence_number_forced_(false),
150 ssrc_forced_(false),
151 timestamp_(0),
152 capture_time_ms_(0),
153 last_timestamp_time_ms_(0),
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000154 media_has_been_sent_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000155 last_packet_marker_bit_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000156 csrcs_(),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000157 rtx_(kRtxOff),
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000158 payload_type_rtx_(-1),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000159 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000160 target_bitrate_(0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000161 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
162 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000163 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000164 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000165 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000166 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000167 bitrates_->set_ssrc(ssrc_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000168 // Random start, 16 bits. Can't be 0.
169 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
170 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000171}
172
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000173RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000174 if (remote_ssrc_ != 0) {
175 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000176 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000177 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000178
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000179 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000180 while (!payload_type_map_.empty()) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000181 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000182 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000183 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000184 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000185 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000186}
niklase@google.com470e71d2011-07-07 08:21:25 +0000187
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000188void RTPSender::SetTargetBitrate(uint32_t bitrate) {
189 CriticalSectionScoped cs(target_bitrate_critsect_.get());
190 target_bitrate_ = bitrate;
191}
192
193uint32_t RTPSender::GetTargetBitrate() {
194 CriticalSectionScoped cs(target_bitrate_critsect_.get());
195 return target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000197
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000198uint16_t RTPSender::ActualSendBitrateKbit() const {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000199 return (uint16_t)(total_bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000200}
201
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000202uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000203 if (video_) {
204 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000205 }
206 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000207}
208
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000209uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000210 if (video_) {
211 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000212 }
213 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000214}
215
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000216uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000217 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000218}
219
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000220bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
221 int* max_send_delay_ms) const {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000222 CriticalSectionScoped lock(statistics_crit_.get());
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000223 SendDelayMap::const_iterator it = send_delays_.upper_bound(
224 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000225 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000226 return false;
227 int num_delays = 0;
228 for (; it != send_delays_.end(); ++it) {
229 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
230 *avg_send_delay_ms += it->second;
231 ++num_delays;
232 }
233 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
234 return true;
235}
236
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000237int32_t RTPSender::SetTransmissionTimeOffset(int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000238 if (transmission_time_offset > (0x800000 - 1) ||
239 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000240 return -1;
241 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000242 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000243 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000244 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000245}
246
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000247int32_t RTPSender::SetAbsoluteSendTime(uint32_t absolute_send_time) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000248 if (absolute_send_time > 0xffffff) { // UWord24.
249 return -1;
250 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000251 CriticalSectionScoped cs(send_critsect_.get());
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000252 absolute_send_time_ = absolute_send_time;
253 return 0;
254}
255
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000256void RTPSender::SetVideoRotation(VideoRotation rotation) {
257 CriticalSectionScoped cs(send_critsect_.get());
258 rotation_ = rotation;
259}
260
sprang@webrtc.org30933902015-03-17 14:33:12 +0000261int32_t RTPSender::SetTransportSequenceNumber(uint16_t sequence_number) {
262 CriticalSectionScoped cs(send_critsect_.get());
263 transport_sequence_number_ = sequence_number;
264 return 0;
265}
266
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000267int32_t RTPSender::RegisterRtpHeaderExtension(RTPExtensionType type,
268 uint8_t id) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000269 CriticalSectionScoped cs(send_critsect_.get());
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700270 if (type == kRtpExtensionVideoRotation) {
271 cvo_mode_ = kCVOInactive;
272 return rtp_header_extension_map_.RegisterInactive(type, id);
273 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000274 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000275}
276
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000277bool RTPSender::IsRtpHeaderExtensionRegistered(RTPExtensionType type) {
278 CriticalSectionScoped cs(send_critsect_.get());
279 return rtp_header_extension_map_.IsRegistered(type);
280}
281
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000282int32_t RTPSender::DeregisterRtpHeaderExtension(RTPExtensionType type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000283 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000284 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000285}
286
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000287size_t RTPSender::RtpHeaderExtensionTotalLength() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000288 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000289 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000290}
291
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000292int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000293 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000294 int8_t payload_number,
295 uint32_t frequency,
296 uint8_t channels,
297 uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000298 assert(payload_name);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000299 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000301 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000302 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000303
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000304 if (payload_type_map_.end() != it) {
305 // We already use this payload type.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000306 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000307 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000308
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000309 // Check if it's the same as we already have.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000310 if (RtpUtility::StringCompare(
311 payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000312 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000313 payload->typeSpecific.Audio.frequency == frequency &&
314 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000315 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000316 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000317 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000318 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000319 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000320 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000321 return 0;
322 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000323 }
324 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000325 }
mflodman0828a0c2015-03-31 15:29:23 +0200326 int32_t ret_val = -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000327 RtpUtility::Payload* payload = NULL;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000328 if (audio_configured_) {
329 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
330 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000331 } else {
mflodman0828a0c2015-03-31 15:29:23 +0200332 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
333 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000334 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000335 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000336 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000337 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000338 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000339}
340
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000341int32_t RTPSender::DeRegisterSendPayload(int8_t payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000342 CriticalSectionScoped lock(send_critsect_.get());
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000343
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000344 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000345 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000346
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000347 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000348 return -1;
349 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000350 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000351 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000352 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000353 return 0;
354}
niklase@google.com470e71d2011-07-07 08:21:25 +0000355
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000356void RTPSender::SetSendPayloadType(int8_t payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000357 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000358 payload_type_ = payload_type;
359}
360
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000361int8_t RTPSender::SendPayloadType() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000362 CriticalSectionScoped cs(send_critsect_.get());
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000363 return payload_type_;
364}
niklase@google.com470e71d2011-07-07 08:21:25 +0000365
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000366int RTPSender::SendPayloadFrequency() const {
367 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
368}
niklase@google.com470e71d2011-07-07 08:21:25 +0000369
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000370int32_t RTPSender::SetMaxPayloadLength(size_t max_payload_length,
371 uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000372 // Sanity check.
373 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000374 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000375 return -1;
376 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000377 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000378 max_payload_length_ = max_payload_length;
379 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000380 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000381}
382
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000383size_t RTPSender::MaxDataPayloadLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000384 int rtx;
385 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000386 CriticalSectionScoped rtx_lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000387 rtx = rtx_;
388 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000389 if (audio_configured_) {
390 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000391 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000392 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
393 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000394 - ((rtx) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000395 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000396}
397
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000398size_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000399 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000400}
401
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000402uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000403
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000404void RTPSender::SetRtxStatus(int mode) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000405 CriticalSectionScoped cs(send_critsect_.get());
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000406 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000407}
408
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000409int RTPSender::RtxStatus() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000410 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000411 return rtx_;
412}
413
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000414void RTPSender::SetRtxSsrc(uint32_t ssrc) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000415 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000416 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000417}
418
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000419uint32_t RTPSender::RtxSsrc() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000420 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000421 return ssrc_rtx_;
422}
423
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000424void RTPSender::SetRtxPayloadType(int payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000425 CriticalSectionScoped cs(send_critsect_.get());
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000426 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000427}
428
Ã…sa Persson6ae25722015-04-13 17:48:08 +0200429int RTPSender::RtxPayloadType() const {
430 CriticalSectionScoped cs(send_critsect_.get());
431 return payload_type_rtx_;
432}
433
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000434int32_t RTPSender::CheckPayloadType(int8_t payload_type,
435 RtpVideoCodecTypes* video_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000436 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000437
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000438 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000439 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000440 return -1;
441 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000442 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000443 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000444 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000445 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000446 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000447 // And it's a match...
448 return 0;
449 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000450 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000451 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000452 if (payload_type_ == payload_type) {
453 if (!audio_configured_) {
454 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000455 }
456 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000457 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000458 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000459 payload_type_map_.find(payload_type);
460 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000461 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000462 return -1;
463 }
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000464 SetSendPayloadType(payload_type);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000465 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000466 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000467 if (!payload->audio && !audio_configured_) {
468 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
469 *video_type = payload->typeSpecific.Video.videoCodecType;
470 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000471 }
472 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000473}
474
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700475RTPSenderInterface::CVOMode RTPSender::ActivateCVORtpHeaderExtension() {
476 if (cvo_mode_ == kCVOInactive) {
477 CriticalSectionScoped cs(send_critsect_.get());
478 if (rtp_header_extension_map_.SetActive(kRtpExtensionVideoRotation, true)) {
479 cvo_mode_ = kCVOActivated;
480 }
481 }
482 return cvo_mode_;
483}
484
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000485int32_t RTPSender::SendOutgoingData(FrameType frame_type,
486 int8_t payload_type,
487 uint32_t capture_timestamp,
488 int64_t capture_time_ms,
489 const uint8_t* payload_data,
490 size_t payload_size,
491 const RTPFragmentationHeader* fragmentation,
mflodman0828a0c2015-03-31 15:29:23 +0200492 VideoCodecInformation* codec_info,
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000493 const RTPVideoHeader* rtp_hdr) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000494 uint32_t ssrc;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000495 {
496 // Drop this packet if we're not sending media packets.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000497 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000498 ssrc = ssrc_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000499 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000500 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000501 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000502 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000503 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000504 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000505 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000506 return -1;
507 }
508
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000509 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000510 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000511 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
512 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000513 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000514 frame_type == kFrameEmpty);
515
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000516 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
517 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000518 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000519 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
520 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000521 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000522
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000523 if (frame_type == kFrameEmpty)
524 return 0;
525
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000526 ret_val =
527 video_->SendVideo(video_type, frame_type, payload_type,
528 capture_timestamp, capture_time_ms, payload_data,
mflodman0828a0c2015-03-31 15:29:23 +0200529 payload_size, fragmentation, codec_info, rtp_hdr);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000530 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000531
532 CriticalSectionScoped cs(statistics_crit_.get());
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000533 // Note: This is currently only counting for video.
534 if (frame_type == kVideoFrameKey) {
535 ++frame_counts_.key_frames;
536 } else if (frame_type == kVideoFrameDelta) {
537 ++frame_counts_.delta_frames;
538 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000539 if (frame_count_observer_) {
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000540 frame_count_observer_->FrameCountUpdated(frame_counts_, ssrc);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000541 }
542
543 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000544}
545
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000546size_t RTPSender::TrySendRedundantPayloads(size_t bytes_to_send) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000547 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000548 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000549 if ((rtx_ & kRtxRedundantPayloads) == 0)
550 return 0;
551 }
552
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000553 uint8_t buffer[IP_PACKET_SIZE];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000554 int bytes_left = static_cast<int>(bytes_to_send);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000555 while (bytes_left > 0) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000556 size_t length = bytes_left;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000557 int64_t capture_time_ms;
558 if (!packet_history_.GetBestFittingPacket(buffer, &length,
559 &capture_time_ms)) {
560 break;
561 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000562 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000563 break;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000564 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000565 RTPHeader rtp_header;
566 rtp_parser.Parse(rtp_header);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000567 bytes_left -= static_cast<int>(length - rtp_header.headerLength);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000568 }
569 return bytes_to_send - bytes_left;
570}
571
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000572size_t RTPSender::BuildPaddingPacket(uint8_t* packet, size_t header_length) {
573 size_t padding_bytes_in_packet = kMaxPaddingLength;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000574 packet[0] |= 0x20; // Set padding bit.
575 int32_t *data =
576 reinterpret_cast<int32_t *>(&(packet[header_length]));
577
578 // Fill data buffer with random data.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000579 for (size_t j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000580 data[j] = rand(); // NOLINT
581 }
582 // Set number of padding bytes in the last byte of the packet.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000583 packet[header_length + padding_bytes_in_packet - 1] =
584 static_cast<uint8_t>(padding_bytes_in_packet);
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000585 return padding_bytes_in_packet;
586}
587
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000588size_t RTPSender::TrySendPadData(size_t bytes) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000589 int64_t capture_time_ms;
590 uint32_t timestamp;
591 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000592 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000593 timestamp = timestamp_;
594 capture_time_ms = capture_time_ms_;
595 if (last_timestamp_time_ms_ > 0) {
596 timestamp +=
597 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
598 capture_time_ms +=
599 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
600 }
601 }
602 return SendPadData(timestamp, capture_time_ms, bytes);
603}
604
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000605size_t RTPSender::SendPadData(uint32_t timestamp,
606 int64_t capture_time_ms,
607 size_t bytes) {
608 size_t padding_bytes_in_packet = 0;
609 size_t bytes_sent = 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000610 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000611 // Always send full padding packets.
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000612 if (bytes < kMaxPaddingLength)
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000613 bytes = kMaxPaddingLength;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000614
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000615 uint32_t ssrc;
616 uint16_t sequence_number;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000617 int payload_type;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000618 bool over_rtx;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000619 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000620 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000621 // Only send padding packets following the last packet of a frame,
622 // indicated by the marker bit.
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000623 if (rtx_ == kRtxOff) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000624 // Without RTX we can't send padding in the middle of frames.
625 if (!last_packet_marker_bit_)
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000626 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000627 ssrc = ssrc_;
628 sequence_number = sequence_number_;
629 ++sequence_number_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000630 payload_type = payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000631 over_rtx = false;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000632 } else {
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000633 // Without abs-send-time a media packet must be sent before padding so
634 // that the timestamps used for estimation are correct.
635 if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
636 kRtpExtensionAbsoluteSendTime))
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000637 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000638 ssrc = ssrc_rtx_;
639 sequence_number = sequence_number_rtx_;
640 ++sequence_number_rtx_;
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000641 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_
642 : payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000643 over_rtx = true;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000644 }
645 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000646
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000647 uint8_t padding_packet[IP_PACKET_SIZE];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000648 size_t header_length =
649 CreateRtpHeader(padding_packet, payload_type, ssrc, false, timestamp,
650 sequence_number, std::vector<uint32_t>());
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000651 assert(header_length != static_cast<size_t>(-1));
652 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length);
653 assert(padding_bytes_in_packet <= bytes);
654 size_t length = padding_bytes_in_packet + header_length;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000655 int64_t now_ms = clock_->TimeInMilliseconds();
656
657 RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
658 RTPHeader rtp_header;
659 rtp_parser.Parse(rtp_header);
660
661 if (capture_time_ms > 0) {
662 UpdateTransmissionTimeOffset(
663 padding_packet, length, rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000664 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000665
666 UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
667 if (!SendPacketToNetwork(padding_packet, length))
668 break;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000669 bytes_sent += padding_bytes_in_packet;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000670 UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000671 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000672
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000673 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000674}
675
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000676void RTPSender::SetStorePacketsStatus(bool enable, uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000677 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000678}
679
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000680bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000681 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000682}
niklase@google.com470e71d2011-07-07 08:21:25 +0000683
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000684int32_t RTPSender::ReSendPacket(uint16_t packet_id, int64_t min_resend_time) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000685 size_t length = IP_PACKET_SIZE;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000686 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000687 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000688 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
689 data_buffer, &length,
690 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000691 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000692 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000693 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000694
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000695 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000696 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000697 RTPHeader header;
698 if (!rtp_parser.Parse(header)) {
699 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000700 return -1;
701 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000702 // Convert from TickTime to Clock since capture_time_ms is based on
703 // TickTime.
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000704 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_;
705 if (!paced_sender_->SendPacket(
706 PacedSender::kHighPriority, header.ssrc, header.sequenceNumber,
707 corrected_capture_tims_ms, length - header.headerLength, true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000708 // We can't send the packet right now.
709 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000710 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000711 }
712 }
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000713 int rtx = kRtxOff;
714 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000715 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000716 rtx = rtx_;
717 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000718 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000719 (rtx & kRtxRetransmitted) > 0, true) ?
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000720 static_cast<int32_t>(length) : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000721}
722
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000723bool RTPSender::SendPacketToNetwork(const uint8_t *packet, size_t size) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000724 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000725 if (transport_) {
726 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000727 }
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000728 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
729 "RTPSender::SendPacketToNetwork", "size", size, "sent",
730 bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000731 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000732 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000733 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000734 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000735 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000736 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000737}
738
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000739int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000740 if (!video_)
741 return -1;
742 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000743}
744
745int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000746 if (!video_)
747 return -1;
mflodman0828a0c2015-03-31 15:29:23 +0200748 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000749}
750
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000751void RTPSender::OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000752 int64_t avg_rtt) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000753 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
754 "RTPSender::OnReceivedNACK", "num_seqnum",
755 nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000756 const int64_t now = clock_->TimeInMilliseconds();
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000757 uint32_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000758 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000759
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000760 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000761 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000762 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000763 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000764 return;
765 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000766
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000767 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
768 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000769 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000770 if (bytes_sent > 0) {
771 bytes_re_sent += bytes_sent;
772 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000773 // The packet has previously been resent.
774 // Try resending next packet in the list.
775 continue;
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000776 } else {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000777 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000778 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
779 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000780 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000781 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000782 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000783 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000784 // kbits/s * ms = bits => bits/8 = bytes
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000785 size_t target_bytes =
786 (static_cast<size_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000787 if (bytes_re_sent > target_bytes) {
788 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000789 }
790 }
791 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000792 if (bytes_re_sent > 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000793 UpdateNACKBitRate(bytes_re_sent, now);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000794 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000795}
796
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000797bool RTPSender::ProcessNACKBitRate(uint32_t now) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000798 uint32_t num = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000799 size_t byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000800 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000801 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000802
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000803 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000804
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000805 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000806 return true;
807 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000808 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000809 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000810 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000811 break;
812 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000813 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000814 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000815 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000816 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000817 if (num == NACK_BYTECOUNT_SIZE) {
818 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000819 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000820 if (nack_byte_count_times_[num - 1] <= now) {
821 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000822 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000823 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000824 return (byte_count * 8) < (target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000825}
826
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000827void RTPSender::UpdateNACKBitRate(uint32_t bytes, int64_t now) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000828 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000829 if (bytes == 0)
830 return;
831 nack_bitrate_.Update(bytes);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000832 // Save bitrate statistics.
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000833 // Shift all but first time.
834 for (int i = NACK_BYTECOUNT_SIZE - 2; i >= 0; i--) {
835 nack_byte_count_[i + 1] = nack_byte_count_[i];
836 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000837 }
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000838 nack_byte_count_[0] = bytes;
839 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000840}
841
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000842// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000843bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000844 int64_t capture_time_ms,
845 bool retransmission) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000846 size_t length = IP_PACKET_SIZE;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000847 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000848 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000849
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000850 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
851 0,
852 retransmission,
853 data_buffer,
854 &length,
855 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000856 // Packet cannot be found. Allow sending to continue.
857 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000858 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000859 if (!retransmission && capture_time_ms > 0) {
860 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
861 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000862 int rtx;
863 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000864 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000865 rtx = rtx_;
866 }
867 return PrepareAndSendPacket(data_buffer,
868 length,
869 capture_time_ms,
870 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000871 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000872}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000873
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000874bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000875 size_t length,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000876 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000877 bool send_over_rtx,
878 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000879 uint8_t *buffer_to_send_ptr = buffer;
880
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000881 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000882 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000883 rtp_parser.Parse(rtp_header);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000884 if (!is_retransmit && rtp_header.markerBit) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000885 TRACE_EVENT_ASYNC_END0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PacedSend",
886 capture_time_ms);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000887 }
888
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000889 TRACE_EVENT_INSTANT2(
890 TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PrepareAndSendPacket",
891 "timestamp", rtp_header.timestamp, "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000892
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000893 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000894 if (send_over_rtx) {
895 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000896 buffer_to_send_ptr = data_buffer_rtx;
897 }
898
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000899 int64_t now_ms = clock_->TimeInMilliseconds();
900 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000901 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
902 diff_ms);
903 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000904 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000905 if (ret) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000906 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000907 media_has_been_sent_ = true;
908 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000909 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
910 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000911 return ret;
912}
913
914void RTPSender::UpdateRtpStats(const uint8_t* buffer,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000915 size_t packet_length,
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000916 const RTPHeader& header,
917 bool is_rtx,
918 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000919 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000920 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000921 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000922
923 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000924 if (is_rtx) {
925 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000926 } else {
927 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000928 }
929
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000930 total_bitrate_sent_.Update(packet_length);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000931
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000932 if (counters->first_packet_time_ms == -1) {
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000933 counters->first_packet_time_ms = clock_->TimeInMilliseconds();
934 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000935 if (IsFecPacket(buffer, header)) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000936 counters->fec.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000937 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000938 if (is_retransmit) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000939 counters->retransmitted.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000940 }
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000941 counters->transmitted.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000942
943 if (rtp_stats_callback_) {
944 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
945 }
946}
947
948bool RTPSender::IsFecPacket(const uint8_t* buffer,
949 const RTPHeader& header) const {
950 if (!video_) {
951 return false;
952 }
953 bool fec_enabled;
954 uint8_t pt_red;
955 uint8_t pt_fec;
956 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
957 return fec_enabled &&
958 header.payloadType == pt_red &&
959 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000960}
961
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000962size_t RTPSender::TimeToSendPadding(size_t bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000963 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000964 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000965 if (!sending_media_) return 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000966 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000967 if (bytes == 0)
968 return 0;
969 size_t bytes_sent = TrySendRedundantPayloads(bytes);
970 if (bytes_sent < bytes)
971 bytes_sent += TrySendPadData(bytes - bytes_sent);
972 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000973}
974
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000975// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000976int32_t RTPSender::SendToNetwork(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000977 uint8_t *buffer, size_t payload_length, size_t rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000978 int64_t capture_time_ms, StorageType storage,
979 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000980 RtpUtility::RtpHeaderParser rtp_parser(buffer,
981 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000982 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000983 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000984
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000985 int64_t now_ms = clock_->TimeInMilliseconds();
986
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000987 // |capture_time_ms| <= 0 is considered invalid.
988 // TODO(holmer): This should be changed all over Video Engine so that negative
989 // time is consider invalid, while 0 is considered a valid time.
990 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000991 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000992 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000993 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000994
995 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
996 rtp_header, now_ms);
997
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000998 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000999 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
1000 max_payload_length_, capture_time_ms,
1001 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +00001002 return -1;
1003 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001004
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +00001005 if (paced_sender_ && storage != kDontStore) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +00001006 // Correct offset between implementations of millisecond time stamps in
1007 // TickTime and Clock.
1008 int64_t corrected_time_ms = capture_time_ms + clock_delta_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +00001009 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +00001010 rtp_header.sequenceNumber, corrected_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +00001011 payload_length, false)) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +00001012 if (last_capture_time_ms_sent_ == 0 ||
1013 corrected_time_ms > last_capture_time_ms_sent_) {
1014 last_capture_time_ms_sent_ = corrected_time_ms;
sprang@webrtc.org0200f702015-02-16 12:06:00 +00001015 TRACE_EVENT_ASYNC_BEGIN1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
1016 "PacedSend", corrected_time_ms,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +00001017 "capture_time_ms", corrected_time_ms);
1018 }
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +00001019 // We can't send the packet right now.
1020 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +00001021 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001022 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +00001023 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001024 if (capture_time_ms > 0) {
1025 UpdateDelayStatistics(capture_time_ms, now_ms);
1026 }
sprang@webrtc.org43c88392015-01-29 09:09:17 +00001027
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001028 size_t length = payload_length + rtp_header_length;
sprang@webrtc.orgc957ffc2015-02-02 13:08:02 +00001029 bool sent = SendPacketToNetwork(buffer, length);
1030
1031 if (storage != kDontStore) {
1032 // Mark the packet as sent in the history even if send failed. Dropping a
1033 // packet here should be treated as any other packet drop so we should be
1034 // ready for a retransmission.
1035 packet_history_.SetSent(rtp_header.sequenceNumber);
1036 }
1037 if (!sent)
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001038 return -1;
sprang@webrtc.orgc957ffc2015-02-02 13:08:02 +00001039
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001040 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001041 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001042 media_has_been_sent_ = true;
1043 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001044 UpdateRtpStats(buffer, length, rtp_header, false, false);
1045 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +00001046}
1047
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001048void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001049 uint32_t ssrc;
1050 int avg_delay_ms = 0;
1051 int max_delay_ms = 0;
1052 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001053 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001054 ssrc = ssrc_;
1055 }
1056 {
1057 CriticalSectionScoped cs(statistics_crit_.get());
1058 // TODO(holmer): Compute this iteratively instead.
1059 send_delays_[now_ms] = now_ms - capture_time_ms;
1060 send_delays_.erase(send_delays_.begin(),
1061 send_delays_.lower_bound(now_ms -
1062 kSendSideDelayWindowMs));
1063 }
1064 if (send_side_delay_observer_ &&
1065 GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
1066 send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
1067 max_delay_ms, ssrc);
1068 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001069}
1070
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001071void RTPSender::ProcessBitrate() {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001072 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001073 total_bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001074 nack_bitrate_.Process();
1075 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001076 return;
1077 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001078 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +00001079}
1080
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001081size_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001082 CriticalSectionScoped lock(send_critsect_.get());
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001083 size_t rtp_header_length = kRtpHeaderLength;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001084 rtp_header_length += sizeof(uint32_t) * csrcs_.size();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001085 rtp_header_length += RtpHeaderExtensionTotalLength();
1086 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001087}
1088
mflodman0828a0c2015-03-31 15:29:23 +02001089uint16_t RTPSender::IncrementSequenceNumber() {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001090 CriticalSectionScoped cs(send_critsect_.get());
mflodman0828a0c2015-03-31 15:29:23 +02001091 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001092}
1093
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001094void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001095 uint32_t ssrc;
1096 uint32_t ssrc_rtx;
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001097 bool report_rtx;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001098 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001099 CriticalSectionScoped ssrc_lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001100 ssrc = ssrc_;
1101 ssrc_rtx = ssrc_rtx_;
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001102 report_rtx = rtx_ != kRtxOff;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001103 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001104 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001105 rtp_stats_ = StreamDataCounters();
1106 rtx_rtp_stats_ = StreamDataCounters();
1107 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001108 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001109 if (report_rtx)
1110 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001111 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001112}
1113
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001114void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1115 StreamDataCounters* rtx_stats) const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001116 CriticalSectionScoped lock(statistics_crit_.get());
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001117 *rtp_stats = rtp_stats_;
1118 *rtx_stats = rtx_rtp_stats_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001119}
1120
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001121size_t RTPSender::CreateRtpHeader(uint8_t* header,
1122 int8_t payload_type,
1123 uint32_t ssrc,
1124 bool marker_bit,
1125 uint32_t timestamp,
1126 uint16_t sequence_number,
1127 const std::vector<uint32_t>& csrcs) const {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001128 header[0] = 0x80; // version 2.
1129 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001130 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001131 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001132 }
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001133 ByteWriter<uint16_t>::WriteBigEndian(header + 2, sequence_number);
1134 ByteWriter<uint32_t>::WriteBigEndian(header + 4, timestamp);
1135 ByteWriter<uint32_t>::WriteBigEndian(header + 8, ssrc);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001136 int32_t rtp_header_length = kRtpHeaderLength;
niklase@google.com470e71d2011-07-07 08:21:25 +00001137
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001138 if (csrcs.size() > 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001139 uint8_t *ptr = &header[rtp_header_length];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001140 for (size_t i = 0; i < csrcs.size(); ++i) {
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001141 ByteWriter<uint32_t>::WriteBigEndian(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001142 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001143 }
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001144 header[0] = (header[0] & 0xf0) | csrcs.size();
niklase@google.com470e71d2011-07-07 08:21:25 +00001145
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001146 // Update length of header.
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001147 rtp_header_length += sizeof(uint32_t) * csrcs.size();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001148 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001149
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001150 uint16_t len =
1151 BuildRTPHeaderExtension(header + rtp_header_length, marker_bit);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001152 if (len > 0) {
1153 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001154 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001155 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001156 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001157}
1158
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001159int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001160 int8_t payload_type,
1161 bool marker_bit,
1162 uint32_t capture_timestamp,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001163 int64_t capture_time_ms,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001164 bool timestamp_provided,
1165 bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001166 assert(payload_type >= 0);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001167 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001168
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001169 if (timestamp_provided) {
1170 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001171 } else {
1172 // Make a unique time stamp.
1173 // We can't inc by the actual time, since then we increase the risk of back
1174 // timing.
1175 timestamp_++;
1176 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001177 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001178 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001179 capture_time_ms_ = capture_time_ms;
1180 last_packet_marker_bit_ = marker_bit;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001181 return CreateRtpHeader(data_buffer, payload_type, ssrc_, marker_bit,
1182 timestamp_, sequence_number, csrcs_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001183}
1184
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001185uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer,
1186 bool marker_bit) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001187 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001188 return 0;
1189 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001190 // RTP header extension, RFC 3550.
1191 // 0 1 2 3
1192 // 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
1193 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1194 // | defined by profile | length |
1195 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1196 // | header extension |
1197 // | .... |
1198 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001199 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001200 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001201
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001202 // Add extension ID (0xBEDE).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001203 ByteWriter<uint16_t>::WriteBigEndian(data_buffer,
1204 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001205
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001206 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001207 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001208
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001209 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001210 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001211 uint8_t block_length = 0;
sprang@webrtc.org30933902015-03-17 14:33:12 +00001212 uint8_t* extension_data = &data_buffer[kHeaderLength + total_block_length];
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001213 switch (type) {
1214 case kRtpExtensionTransmissionTimeOffset:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001215 block_length = BuildTransmissionTimeOffsetExtension(extension_data);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001216 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001217 case kRtpExtensionAudioLevel:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001218 block_length = BuildAudioLevelExtension(extension_data);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001219 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001220 case kRtpExtensionAbsoluteSendTime:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001221 block_length = BuildAbsoluteSendTimeExtension(extension_data);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001222 break;
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001223 case kRtpExtensionVideoRotation:
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -07001224 block_length = BuildVideoRotationExtension(extension_data);
sprang@webrtc.org30933902015-03-17 14:33:12 +00001225 break;
1226 case kRtpExtensionTransportSequenceNumber:
1227 block_length = BuildTransportSequenceNumberExtension(extension_data);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001228 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001229 default:
1230 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001231 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001232 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001233 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001234 }
1235 if (total_block_length == 0) {
1236 // No extension added.
1237 return 0;
1238 }
sprang@webrtc.org30933902015-03-17 14:33:12 +00001239 // Add padding elements until we've filled a 32 bit block.
1240 size_t padding_bytes =
1241 RtpUtility::Word32Align(total_block_length) - total_block_length;
1242 if (padding_bytes > 0) {
1243 memset(&data_buffer[kHeaderLength + total_block_length], 0, padding_bytes);
1244 total_block_length += padding_bytes;
1245 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001246 // Set header length (in number of Word32, header excluded).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001247 ByteWriter<uint16_t>::WriteBigEndian(data_buffer + kPosLength,
1248 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001249 // Total added length.
1250 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001251}
1252
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001253uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1254 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001255 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1256 //
1257 // The transmission time is signaled to the receiver in-band using the
1258 // general mechanism for RTP header extensions [RFC5285]. The payload
1259 // of this extension (the transmitted value) is a 24-bit signed integer.
1260 // When added to the RTP timestamp of the packet, it represents the
1261 // "effective" RTP transmission time of the packet, on the RTP
1262 // timescale.
1263 //
1264 // The form of the transmission offset extension block:
1265 //
1266 // 0 1 2 3
1267 // 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
1268 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1269 // | ID | len=2 | transmission offset |
1270 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001271
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001272 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001273 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001274 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1275 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001276 // Not registered.
1277 return 0;
1278 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001279 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001280 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001281 data_buffer[pos++] = (id << 4) + len;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001282 ByteWriter<int32_t, 3>::WriteBigEndian(data_buffer + pos,
1283 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001284 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001285 assert(pos == kTransmissionTimeOffsetLength);
1286 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001287}
1288
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001289uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1290 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1291 //
1292 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1293 //
1294 // The form of the audio level extension block:
1295 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001296 // 0 1
1297 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1298 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1299 // | ID | len=0 |V| level |
1300 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001301 //
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001302
1303 // Get id defined by user.
1304 uint8_t id;
1305 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1306 // Not registered.
1307 return 0;
1308 }
1309 size_t pos = 0;
1310 const uint8_t len = 0;
1311 data_buffer[pos++] = (id << 4) + len;
1312 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001313 assert(pos == kAudioLevelLength);
1314 return kAudioLevelLength;
1315}
1316
1317uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001318 // Absolute send time in RTP streams.
1319 //
1320 // The absolute send time is signaled to the receiver in-band using the
1321 // general mechanism for RTP header extensions [RFC5285]. The payload
1322 // of this extension (the transmitted value) is a 24-bit unsigned integer
1323 // containing the sender's current time in seconds as a fixed point number
1324 // with 18 bits fractional part.
1325 //
1326 // The form of the absolute send time extension block:
1327 //
1328 // 0 1 2 3
1329 // 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
1330 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1331 // | ID | len=2 | absolute send time |
1332 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1333
1334 // Get id defined by user.
1335 uint8_t id;
1336 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1337 &id) != 0) {
1338 // Not registered.
1339 return 0;
1340 }
1341 size_t pos = 0;
1342 const uint8_t len = 2;
1343 data_buffer[pos++] = (id << 4) + len;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001344 ByteWriter<uint32_t, 3>::WriteBigEndian(data_buffer + pos,
1345 absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001346 pos += 3;
1347 assert(pos == kAbsoluteSendTimeLength);
1348 return kAbsoluteSendTimeLength;
1349}
1350
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001351uint8_t RTPSender::BuildVideoRotationExtension(uint8_t* data_buffer) const {
1352 // Coordination of Video Orientation in RTP streams.
1353 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001354 // Coordination of Video Orientation consists in signaling of the current
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001355 // orientation of the image captured on the sender side to the receiver for
1356 // appropriate rendering and displaying.
1357 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001358 // 0 1
1359 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1360 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1361 // | ID | len=0 |0 0 0 0 C F R R|
1362 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001363 //
1364
1365 // Get id defined by user.
1366 uint8_t id;
1367 if (rtp_header_extension_map_.GetId(kRtpExtensionVideoRotation, &id) != 0) {
1368 // Not registered.
1369 return 0;
1370 }
1371 size_t pos = 0;
1372 const uint8_t len = 0;
1373 data_buffer[pos++] = (id << 4) + len;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +00001374 data_buffer[pos++] = ConvertVideoRotationToCVOByte(rotation_);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001375 assert(pos == kVideoRotationLength);
1376 return kVideoRotationLength;
1377}
1378
sprang@webrtc.org30933902015-03-17 14:33:12 +00001379uint8_t RTPSender::BuildTransportSequenceNumberExtension(
1380 uint8_t* data_buffer) const {
1381 // 0 1 2
1382 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
1383 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1384 // | ID | L=1 |transport wide sequence number |
1385 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1386
1387 // Get id defined by user.
1388 uint8_t id;
1389 if (rtp_header_extension_map_.GetId(kRtpExtensionTransportSequenceNumber,
1390 &id) != 0) {
1391 // Not registered.
1392 return 0;
1393 }
1394 size_t pos = 0;
1395 const uint8_t len = 1;
1396 data_buffer[pos++] = (id << 4) + len;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001397 ByteWriter<uint16_t>::WriteBigEndian(data_buffer + pos,
1398 transport_sequence_number_);
sprang@webrtc.org30933902015-03-17 14:33:12 +00001399 pos += 2;
1400 assert(pos == kTransportSequenceNumberLength);
1401 return kTransportSequenceNumberLength;
1402}
1403
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001404bool RTPSender::FindHeaderExtensionPosition(RTPExtensionType type,
1405 const uint8_t* rtp_packet,
1406 size_t rtp_packet_length,
1407 const RTPHeader& rtp_header,
1408 size_t* position) const {
1409 // Get length until start of header extension block.
1410 int extension_block_pos =
1411 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(type);
1412 if (extension_block_pos < 0) {
1413 LOG(LS_WARNING) << "Failed to find extension position for " << type
1414 << " as it is not registered.";
1415 return false;
1416 }
1417
1418 HeaderExtension header_extension(type);
1419
1420 size_t block_pos =
1421 kRtpHeaderLength + rtp_header.numCSRCs + extension_block_pos;
1422 if (rtp_packet_length < block_pos + header_extension.length ||
1423 rtp_header.headerLength < block_pos + header_extension.length) {
1424 LOG(LS_WARNING) << "Failed to find extension position for " << type
1425 << " as the length is invalid.";
1426 return false;
1427 }
1428
1429 // Verify that header contains extension.
1430 if (!((rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs] == 0xBE) &&
1431 (rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs + 1] == 0xDE))) {
1432 LOG(LS_WARNING) << "Failed to find extension position for " << type
1433 << "as hdr extension not found.";
1434 return false;
1435 }
1436
1437 *position = block_pos;
1438 return true;
1439}
1440
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001441void RTPSender::UpdateTransmissionTimeOffset(uint8_t* rtp_packet,
1442 size_t rtp_packet_length,
1443 const RTPHeader& rtp_header,
1444 int64_t time_diff_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001445 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001446 // Get id.
1447 uint8_t id = 0;
1448 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1449 &id) != 0) {
1450 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001451 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001452 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001453
1454 size_t block_pos = 0;
1455 if (!FindHeaderExtensionPosition(kRtpExtensionTransmissionTimeOffset,
1456 rtp_packet, rtp_packet_length, rtp_header,
1457 &block_pos)) {
1458 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001459 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001460 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001461
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001462 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001463 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001464 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001465 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001466 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001467 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001468 // Update transmission offset field (converting to a 90 kHz timestamp).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001469 ByteWriter<int32_t, 3>::WriteBigEndian(rtp_packet + block_pos + 1,
1470 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001471}
1472
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001473bool RTPSender::UpdateAudioLevel(uint8_t* rtp_packet,
1474 size_t rtp_packet_length,
1475 const RTPHeader& rtp_header,
1476 bool is_voiced,
1477 uint8_t dBov) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001478 CriticalSectionScoped cs(send_critsect_.get());
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001479
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001480 // Get id.
1481 uint8_t id = 0;
1482 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1483 // Not registered.
1484 return false;
1485 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001486
1487 size_t block_pos = 0;
1488 if (!FindHeaderExtensionPosition(kRtpExtensionAudioLevel, rtp_packet,
1489 rtp_packet_length, rtp_header, &block_pos)) {
1490 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001491 return false;
1492 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001493
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001494 // Verify first byte in block.
1495 const uint8_t first_block_byte = (id << 4) + 0;
1496 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001497 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001498 return false;
1499 }
1500 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1501 return true;
1502}
1503
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001504bool RTPSender::UpdateVideoRotation(uint8_t* rtp_packet,
1505 size_t rtp_packet_length,
1506 const RTPHeader& rtp_header,
1507 VideoRotation rotation) const {
1508 CriticalSectionScoped cs(send_critsect_.get());
1509
1510 // Get id.
1511 uint8_t id = 0;
1512 if (rtp_header_extension_map_.GetId(kRtpExtensionVideoRotation, &id) != 0) {
1513 // Not registered.
1514 return false;
1515 }
1516
1517 size_t block_pos = 0;
1518 if (!FindHeaderExtensionPosition(kRtpExtensionVideoRotation, rtp_packet,
1519 rtp_packet_length, rtp_header, &block_pos)) {
1520 LOG(LS_WARNING) << "Failed to update video rotation (CVO).";
1521 return false;
1522 }
1523 // Get length until start of header extension block.
1524 int extension_block_pos =
1525 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1526 kRtpExtensionVideoRotation);
1527 if (extension_block_pos < 0) {
1528 // The feature is not enabled.
1529 return false;
1530 }
1531
1532 // Verify first byte in block.
1533 const uint8_t first_block_byte = (id << 4) + 0;
1534 if (rtp_packet[block_pos] != first_block_byte) {
1535 LOG(LS_WARNING) << "Failed to update CVO.";
1536 return false;
1537 }
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +00001538 rtp_packet[block_pos + 1] = ConvertVideoRotationToCVOByte(rotation);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001539 return true;
1540}
1541
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001542void RTPSender::UpdateAbsoluteSendTime(uint8_t* rtp_packet,
1543 size_t rtp_packet_length,
1544 const RTPHeader& rtp_header,
1545 int64_t now_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001546 CriticalSectionScoped cs(send_critsect_.get());
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001547
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001548 // Get id.
1549 uint8_t id = 0;
1550 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1551 &id) != 0) {
1552 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001553 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001554 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001555 // Get length until start of header extension block.
1556 int extension_block_pos =
1557 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1558 kRtpExtensionAbsoluteSendTime);
1559 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001560 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001561 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001562 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001563 size_t block_pos =
1564 kRtpHeaderLength + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001565 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001566 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001567 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001568 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001569 }
1570 // Verify that header contains extension.
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001571 if (!((rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs] == 0xBE) &&
1572 (rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001573 LOG(LS_WARNING)
1574 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001575 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001576 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001577 // Verify first byte in block.
1578 const uint8_t first_block_byte = (id << 4) + 2;
1579 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001580 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001581 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001582 }
1583 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1584 // fractional part).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001585 ByteWriter<uint32_t, 3>::WriteBigEndian(rtp_packet + block_pos + 1,
1586 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001587}
1588
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001589void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001590 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001591 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001592 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001593
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001594 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001595 SetStartTimestamp(RTPtime, false);
1596 } else {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001597 CriticalSectionScoped lock(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001598 if (!ssrc_forced_) {
1599 // Generate a new SSRC.
1600 ssrc_db_.ReturnSSRC(ssrc_);
1601 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001602 bitrates_->set_ssrc(ssrc_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001603 }
1604 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001605 if (!sequence_number_forced_ && !ssrc_forced_) {
1606 // Generate a new sequence number.
1607 sequence_number_ =
1608 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001609 }
1610 }
1611}
1612
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001613void RTPSender::SetSendingMediaStatus(bool enabled) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001614 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001615 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001616}
1617
1618bool RTPSender::SendingMedia() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001619 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001620 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001621}
1622
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001623uint32_t RTPSender::Timestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001624 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001625 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001626}
1627
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001628void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001629 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001630 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001631 start_timestamp_forced_ = true;
1632 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001633 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001634 if (!start_timestamp_forced_) {
1635 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001636 }
1637 }
1638}
1639
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001640uint32_t RTPSender::StartTimestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001641 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001642 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001643}
1644
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001645uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001646 // If configured via API, return 0.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001647 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001648
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001649 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001650 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001651 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001652 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001653 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001654 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001655}
1656
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001657void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001658 // This is configured via the API.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001659 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +00001660
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001661 if (ssrc_ == ssrc && ssrc_forced_) {
1662 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001663 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001664 ssrc_forced_ = true;
1665 ssrc_db_.ReturnSSRC(ssrc_);
1666 ssrc_db_.RegisterSSRC(ssrc);
1667 ssrc_ = ssrc;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001668 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001669 if (!sequence_number_forced_) {
1670 sequence_number_ =
1671 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001672 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001673}
1674
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001675uint32_t RTPSender::SSRC() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001676 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001677 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001678}
1679
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001680void RTPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
1681 assert(csrcs.size() <= kRtpCsrcSize);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001682 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001683 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001684}
1685
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001686void RTPSender::SetSequenceNumber(uint16_t seq) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001687 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001688 sequence_number_forced_ = true;
1689 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001690}
1691
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001692uint16_t RTPSender::SequenceNumber() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001693 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001694 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001695}
1696
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001697// Audio.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001698int32_t RTPSender::SendTelephoneEvent(uint8_t key,
1699 uint16_t time_ms,
1700 uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001701 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001702 return -1;
1703 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001704 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001705}
1706
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001707int32_t RTPSender::SetAudioPacketSize(uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001708 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001709 return -1;
1710 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001711 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001712}
1713
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001714int32_t RTPSender::SetAudioLevel(uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001715 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001716}
1717
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001718int32_t RTPSender::SetRED(int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001719 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001720 return -1;
1721 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001722 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001723}
1724
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001725int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001726 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001727 return -1;
1728 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001729 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001730}
1731
mflodman0828a0c2015-03-31 15:29:23 +02001732// Video
1733VideoCodecInformation *RTPSender::CodecInformationVideo() {
1734 if (audio_configured_) {
1735 return NULL;
1736 }
1737 return video_->CodecInformationVideo();
1738}
1739
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001740RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001741 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001742 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001743}
1744
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001745uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001746 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001747 return 0;
1748 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001749 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001750}
1751
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001752int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001753 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001754 return -1;
1755 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001756 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001757}
1758
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001759int32_t RTPSender::SetGenericFECStatus(bool enable,
1760 uint8_t payload_type_red,
1761 uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001762 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001763 return -1;
1764 }
mflodman0828a0c2015-03-31 15:29:23 +02001765 return video_->SetGenericFECStatus(enable, payload_type_red,
1766 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001767}
1768
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00001769int32_t RTPSender::GenericFECStatus(bool* enable,
1770 uint8_t* payload_type_red,
1771 uint8_t* payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001772 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001773 return -1;
1774 }
mflodman0828a0c2015-03-31 15:29:23 +02001775 return video_->GenericFECStatus(
1776 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001777}
1778
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001779int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001780 const FecProtectionParams *delta_params,
1781 const FecProtectionParams *key_params) {
1782 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001783 return -1;
1784 }
mflodman0828a0c2015-03-31 15:29:23 +02001785 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001786}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001787
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001788void RTPSender::BuildRtxPacket(uint8_t* buffer, size_t* length,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001789 uint8_t* buffer_rtx) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001790 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001791 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001792 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001793 RtpUtility::RtpHeaderParser rtp_parser(
1794 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001795
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001796 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001797 rtp_parser.Parse(rtp_header);
1798
1799 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001800 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001801
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00001802 // Replace payload type, if a specific type is set for RTX.
1803 if (payload_type_rtx_ != -1) {
1804 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001805 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001806 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1807 }
1808
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001809 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001810 uint8_t *ptr = data_buffer_rtx + 2;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001811 ByteWriter<uint16_t>::WriteBigEndian(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001812
1813 // Replace SSRC.
1814 ptr += 6;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001815 ByteWriter<uint32_t>::WriteBigEndian(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001816
1817 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001818 ptr = data_buffer_rtx + rtp_header.headerLength;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001819 ByteWriter<uint16_t>::WriteBigEndian(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001820 ptr += 2;
1821
1822 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001823 memcpy(ptr, buffer + rtp_header.headerLength,
1824 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001825 *length += 2;
1826}
1827
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001828void RTPSender::RegisterRtpStatisticsCallback(
1829 StreamDataCountersCallback* callback) {
1830 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001831 rtp_stats_callback_ = callback;
1832}
1833
1834StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1835 CriticalSectionScoped cs(statistics_crit_.get());
1836 return rtp_stats_callback_;
1837}
1838
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001839uint32_t RTPSender::BitrateSent() const {
1840 return total_bitrate_sent_.BitrateLast();
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001841}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001842
1843void RTPSender::SetRtpState(const RtpState& rtp_state) {
1844 SetStartTimestamp(rtp_state.start_timestamp, true);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001845 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001846 sequence_number_ = rtp_state.sequence_number;
1847 sequence_number_forced_ = true;
1848 timestamp_ = rtp_state.timestamp;
1849 capture_time_ms_ = rtp_state.capture_time_ms;
1850 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001851 media_has_been_sent_ = rtp_state.media_has_been_sent;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001852}
1853
1854RtpState RTPSender::GetRtpState() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001855 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001856
1857 RtpState state;
1858 state.sequence_number = sequence_number_;
1859 state.start_timestamp = start_timestamp_;
1860 state.timestamp = timestamp_;
1861 state.capture_time_ms = capture_time_ms_;
1862 state.last_timestamp_time_ms = last_timestamp_time_ms_;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001863 state.media_has_been_sent = media_has_been_sent_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001864
1865 return state;
1866}
1867
1868void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001869 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001870 sequence_number_rtx_ = rtp_state.sequence_number;
1871}
1872
1873RtpState RTPSender::GetRtxRtpState() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001874 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001875
1876 RtpState state;
1877 state.sequence_number = sequence_number_rtx_;
1878 state.start_timestamp = start_timestamp_;
1879
1880 return state;
1881}
1882
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001883} // namespace webrtc