blob: b8d44f13321a78cfe3fe7e57701ede461e70cf92 [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 }
mflodmanfcf54bd2015-04-14 21:28:08 +0200326 int32_t ret_val = 0;
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_) {
mflodmanfcf54bd2015-04-14 21:28:08 +0200329 // TODO(mflodman): Change to CreateAudioPayload and make static.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000330 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
331 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000332 } else {
mflodmanfcf54bd2015-04-14 21:28:08 +0200333 payload = video_->CreateVideoPayload(payload_name, payload_number, rate);
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,
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000492 const RTPVideoHeader* rtp_hdr) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000493 uint32_t ssrc;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000494 {
495 // Drop this packet if we're not sending media packets.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000496 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000497 ssrc = ssrc_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000498 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000499 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000500 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000501 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000502 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000503 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000504 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000505 return -1;
506 }
507
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000508 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000509 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000510 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
511 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000512 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000513 frame_type == kFrameEmpty);
514
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000515 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
516 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000517 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000518 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
519 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000520 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000521
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000522 if (frame_type == kFrameEmpty)
523 return 0;
524
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000525 ret_val =
526 video_->SendVideo(video_type, frame_type, payload_type,
527 capture_timestamp, capture_time_ms, payload_data,
mflodmanfcf54bd2015-04-14 21:28:08 +0200528 payload_size, fragmentation, rtp_hdr);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000529 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000530
531 CriticalSectionScoped cs(statistics_crit_.get());
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000532 // Note: This is currently only counting for video.
533 if (frame_type == kVideoFrameKey) {
534 ++frame_counts_.key_frames;
535 } else if (frame_type == kVideoFrameDelta) {
536 ++frame_counts_.delta_frames;
537 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000538 if (frame_count_observer_) {
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000539 frame_count_observer_->FrameCountUpdated(frame_counts_, ssrc);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000540 }
541
542 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000543}
544
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000545size_t RTPSender::TrySendRedundantPayloads(size_t bytes_to_send) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000546 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000547 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000548 if ((rtx_ & kRtxRedundantPayloads) == 0)
549 return 0;
550 }
551
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000552 uint8_t buffer[IP_PACKET_SIZE];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000553 int bytes_left = static_cast<int>(bytes_to_send);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000554 while (bytes_left > 0) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000555 size_t length = bytes_left;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000556 int64_t capture_time_ms;
557 if (!packet_history_.GetBestFittingPacket(buffer, &length,
558 &capture_time_ms)) {
559 break;
560 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000561 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000562 break;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000563 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000564 RTPHeader rtp_header;
565 rtp_parser.Parse(rtp_header);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000566 bytes_left -= static_cast<int>(length - rtp_header.headerLength);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000567 }
568 return bytes_to_send - bytes_left;
569}
570
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000571size_t RTPSender::BuildPaddingPacket(uint8_t* packet, size_t header_length) {
572 size_t padding_bytes_in_packet = kMaxPaddingLength;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000573 packet[0] |= 0x20; // Set padding bit.
574 int32_t *data =
575 reinterpret_cast<int32_t *>(&(packet[header_length]));
576
577 // Fill data buffer with random data.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000578 for (size_t j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000579 data[j] = rand(); // NOLINT
580 }
581 // Set number of padding bytes in the last byte of the packet.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000582 packet[header_length + padding_bytes_in_packet - 1] =
583 static_cast<uint8_t>(padding_bytes_in_packet);
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000584 return padding_bytes_in_packet;
585}
586
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000587size_t RTPSender::TrySendPadData(size_t bytes) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000588 int64_t capture_time_ms;
589 uint32_t timestamp;
590 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000591 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000592 timestamp = timestamp_;
593 capture_time_ms = capture_time_ms_;
594 if (last_timestamp_time_ms_ > 0) {
595 timestamp +=
596 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
597 capture_time_ms +=
598 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
599 }
600 }
601 return SendPadData(timestamp, capture_time_ms, bytes);
602}
603
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000604size_t RTPSender::SendPadData(uint32_t timestamp,
605 int64_t capture_time_ms,
606 size_t bytes) {
607 size_t padding_bytes_in_packet = 0;
608 size_t bytes_sent = 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000609 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000610 // Always send full padding packets.
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000611 if (bytes < kMaxPaddingLength)
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000612 bytes = kMaxPaddingLength;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000613
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000614 uint32_t ssrc;
615 uint16_t sequence_number;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000616 int payload_type;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000617 bool over_rtx;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000618 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000619 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000620 // Only send padding packets following the last packet of a frame,
621 // indicated by the marker bit.
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000622 if (rtx_ == kRtxOff) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000623 // Without RTX we can't send padding in the middle of frames.
624 if (!last_packet_marker_bit_)
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000625 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000626 ssrc = ssrc_;
627 sequence_number = sequence_number_;
628 ++sequence_number_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000629 payload_type = payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000630 over_rtx = false;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000631 } else {
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000632 // Without abs-send-time a media packet must be sent before padding so
633 // that the timestamps used for estimation are correct.
634 if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
635 kRtpExtensionAbsoluteSendTime))
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000636 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000637 ssrc = ssrc_rtx_;
638 sequence_number = sequence_number_rtx_;
639 ++sequence_number_rtx_;
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000640 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_
641 : payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000642 over_rtx = true;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000643 }
644 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000645
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000646 uint8_t padding_packet[IP_PACKET_SIZE];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000647 size_t header_length =
648 CreateRtpHeader(padding_packet, payload_type, ssrc, false, timestamp,
649 sequence_number, std::vector<uint32_t>());
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000650 assert(header_length != static_cast<size_t>(-1));
651 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length);
652 assert(padding_bytes_in_packet <= bytes);
653 size_t length = padding_bytes_in_packet + header_length;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000654 int64_t now_ms = clock_->TimeInMilliseconds();
655
656 RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
657 RTPHeader rtp_header;
658 rtp_parser.Parse(rtp_header);
659
660 if (capture_time_ms > 0) {
661 UpdateTransmissionTimeOffset(
662 padding_packet, length, rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000663 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000664
665 UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
666 if (!SendPacketToNetwork(padding_packet, length))
667 break;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000668 bytes_sent += padding_bytes_in_packet;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000669 UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000670 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000671
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000672 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000673}
674
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000675void RTPSender::SetStorePacketsStatus(bool enable, uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000676 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000677}
678
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000679bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000680 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000681}
niklase@google.com470e71d2011-07-07 08:21:25 +0000682
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000683int32_t RTPSender::ReSendPacket(uint16_t packet_id, int64_t min_resend_time) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000684 size_t length = IP_PACKET_SIZE;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000685 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000686 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000687 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
688 data_buffer, &length,
689 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000690 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000691 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000692 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000693
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000694 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000695 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000696 RTPHeader header;
697 if (!rtp_parser.Parse(header)) {
698 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000699 return -1;
700 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000701 // Convert from TickTime to Clock since capture_time_ms is based on
702 // TickTime.
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000703 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_;
704 if (!paced_sender_->SendPacket(
705 PacedSender::kHighPriority, header.ssrc, header.sequenceNumber,
706 corrected_capture_tims_ms, length - header.headerLength, true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000707 // We can't send the packet right now.
708 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000709 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000710 }
711 }
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000712 int rtx = kRtxOff;
713 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000714 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000715 rtx = rtx_;
716 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000717 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000718 (rtx & kRtxRetransmitted) > 0, true) ?
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000719 static_cast<int32_t>(length) : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000720}
721
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000722bool RTPSender::SendPacketToNetwork(const uint8_t *packet, size_t size) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000723 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000724 if (transport_) {
725 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000726 }
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000727 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
728 "RTPSender::SendPacketToNetwork", "size", size, "sent",
729 bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000730 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000731 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000732 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000733 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000734 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000735 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000736}
737
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000738int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000739 if (!video_)
740 return -1;
741 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000742}
743
744int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000745 if (!video_)
746 return -1;
mflodmanfcf54bd2015-04-14 21:28:08 +0200747 video_->SetSelectiveRetransmissions(settings);
748 return 0;
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
mflodmanfcf54bd2015-04-14 21:28:08 +02001089uint16_t RTPSender::AllocateSequenceNumber(uint16_t packets_to_send) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001090 CriticalSectionScoped cs(send_critsect_.get());
mflodmanfcf54bd2015-04-14 21:28:08 +02001091 uint16_t first_allocated_sequence_number = sequence_number_;
1092 sequence_number_ += packets_to_send;
1093 return first_allocated_sequence_number;
niklase@google.com470e71d2011-07-07 08:21:25 +00001094}
1095
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001096void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001097 uint32_t ssrc;
1098 uint32_t ssrc_rtx;
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001099 bool report_rtx;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001100 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001101 CriticalSectionScoped ssrc_lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001102 ssrc = ssrc_;
1103 ssrc_rtx = ssrc_rtx_;
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001104 report_rtx = rtx_ != kRtxOff;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001105 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001106 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001107 rtp_stats_ = StreamDataCounters();
1108 rtx_rtp_stats_ = StreamDataCounters();
1109 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001110 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001111 if (report_rtx)
1112 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001113 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001114}
1115
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001116void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1117 StreamDataCounters* rtx_stats) const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001118 CriticalSectionScoped lock(statistics_crit_.get());
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001119 *rtp_stats = rtp_stats_;
1120 *rtx_stats = rtx_rtp_stats_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001121}
1122
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001123size_t RTPSender::CreateRtpHeader(uint8_t* header,
1124 int8_t payload_type,
1125 uint32_t ssrc,
1126 bool marker_bit,
1127 uint32_t timestamp,
1128 uint16_t sequence_number,
1129 const std::vector<uint32_t>& csrcs) const {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001130 header[0] = 0x80; // version 2.
1131 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001132 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001133 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001134 }
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001135 ByteWriter<uint16_t>::WriteBigEndian(header + 2, sequence_number);
1136 ByteWriter<uint32_t>::WriteBigEndian(header + 4, timestamp);
1137 ByteWriter<uint32_t>::WriteBigEndian(header + 8, ssrc);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001138 int32_t rtp_header_length = kRtpHeaderLength;
niklase@google.com470e71d2011-07-07 08:21:25 +00001139
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001140 if (csrcs.size() > 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001141 uint8_t *ptr = &header[rtp_header_length];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001142 for (size_t i = 0; i < csrcs.size(); ++i) {
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001143 ByteWriter<uint32_t>::WriteBigEndian(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001144 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001145 }
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001146 header[0] = (header[0] & 0xf0) | csrcs.size();
niklase@google.com470e71d2011-07-07 08:21:25 +00001147
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001148 // Update length of header.
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001149 rtp_header_length += sizeof(uint32_t) * csrcs.size();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001150 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001151
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001152 uint16_t len =
1153 BuildRTPHeaderExtension(header + rtp_header_length, marker_bit);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001154 if (len > 0) {
1155 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001156 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001157 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001158 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001159}
1160
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001161int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001162 int8_t payload_type,
1163 bool marker_bit,
1164 uint32_t capture_timestamp,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001165 int64_t capture_time_ms,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001166 bool timestamp_provided,
1167 bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001168 assert(payload_type >= 0);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001169 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001170
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001171 if (timestamp_provided) {
1172 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001173 } else {
1174 // Make a unique time stamp.
1175 // We can't inc by the actual time, since then we increase the risk of back
1176 // timing.
1177 timestamp_++;
1178 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001179 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001180 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001181 capture_time_ms_ = capture_time_ms;
1182 last_packet_marker_bit_ = marker_bit;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001183 return CreateRtpHeader(data_buffer, payload_type, ssrc_, marker_bit,
1184 timestamp_, sequence_number, csrcs_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001185}
1186
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001187uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer,
1188 bool marker_bit) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001189 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001190 return 0;
1191 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001192 // RTP header extension, RFC 3550.
1193 // 0 1 2 3
1194 // 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
1195 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1196 // | defined by profile | length |
1197 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1198 // | header extension |
1199 // | .... |
1200 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001201 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001202 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001203
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001204 // Add extension ID (0xBEDE).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001205 ByteWriter<uint16_t>::WriteBigEndian(data_buffer,
1206 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001207
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001208 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001209 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001210
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001211 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001212 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001213 uint8_t block_length = 0;
sprang@webrtc.org30933902015-03-17 14:33:12 +00001214 uint8_t* extension_data = &data_buffer[kHeaderLength + total_block_length];
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001215 switch (type) {
1216 case kRtpExtensionTransmissionTimeOffset:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001217 block_length = BuildTransmissionTimeOffsetExtension(extension_data);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001218 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001219 case kRtpExtensionAudioLevel:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001220 block_length = BuildAudioLevelExtension(extension_data);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001221 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001222 case kRtpExtensionAbsoluteSendTime:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001223 block_length = BuildAbsoluteSendTimeExtension(extension_data);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001224 break;
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001225 case kRtpExtensionVideoRotation:
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -07001226 block_length = BuildVideoRotationExtension(extension_data);
sprang@webrtc.org30933902015-03-17 14:33:12 +00001227 break;
1228 case kRtpExtensionTransportSequenceNumber:
1229 block_length = BuildTransportSequenceNumberExtension(extension_data);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001230 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001231 default:
1232 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001233 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001234 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001235 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001236 }
1237 if (total_block_length == 0) {
1238 // No extension added.
1239 return 0;
1240 }
sprang@webrtc.org30933902015-03-17 14:33:12 +00001241 // Add padding elements until we've filled a 32 bit block.
1242 size_t padding_bytes =
1243 RtpUtility::Word32Align(total_block_length) - total_block_length;
1244 if (padding_bytes > 0) {
1245 memset(&data_buffer[kHeaderLength + total_block_length], 0, padding_bytes);
1246 total_block_length += padding_bytes;
1247 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001248 // Set header length (in number of Word32, header excluded).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001249 ByteWriter<uint16_t>::WriteBigEndian(data_buffer + kPosLength,
1250 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001251 // Total added length.
1252 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001253}
1254
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001255uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1256 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001257 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1258 //
1259 // The transmission time is signaled to the receiver in-band using the
1260 // general mechanism for RTP header extensions [RFC5285]. The payload
1261 // of this extension (the transmitted value) is a 24-bit signed integer.
1262 // When added to the RTP timestamp of the packet, it represents the
1263 // "effective" RTP transmission time of the packet, on the RTP
1264 // timescale.
1265 //
1266 // The form of the transmission offset extension block:
1267 //
1268 // 0 1 2 3
1269 // 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
1270 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1271 // | ID | len=2 | transmission offset |
1272 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001273
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001274 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001275 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001276 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1277 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001278 // Not registered.
1279 return 0;
1280 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001281 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001282 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001283 data_buffer[pos++] = (id << 4) + len;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001284 ByteWriter<int32_t, 3>::WriteBigEndian(data_buffer + pos,
1285 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001286 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001287 assert(pos == kTransmissionTimeOffsetLength);
1288 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001289}
1290
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001291uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1292 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1293 //
1294 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1295 //
1296 // The form of the audio level extension block:
1297 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001298 // 0 1
1299 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1300 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1301 // | ID | len=0 |V| level |
1302 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001303 //
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001304
1305 // Get id defined by user.
1306 uint8_t id;
1307 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1308 // Not registered.
1309 return 0;
1310 }
1311 size_t pos = 0;
1312 const uint8_t len = 0;
1313 data_buffer[pos++] = (id << 4) + len;
1314 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001315 assert(pos == kAudioLevelLength);
1316 return kAudioLevelLength;
1317}
1318
1319uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001320 // Absolute send time in RTP streams.
1321 //
1322 // The absolute send time is signaled to the receiver in-band using the
1323 // general mechanism for RTP header extensions [RFC5285]. The payload
1324 // of this extension (the transmitted value) is a 24-bit unsigned integer
1325 // containing the sender's current time in seconds as a fixed point number
1326 // with 18 bits fractional part.
1327 //
1328 // The form of the absolute send time extension block:
1329 //
1330 // 0 1 2 3
1331 // 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
1332 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1333 // | ID | len=2 | absolute send time |
1334 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1335
1336 // Get id defined by user.
1337 uint8_t id;
1338 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1339 &id) != 0) {
1340 // Not registered.
1341 return 0;
1342 }
1343 size_t pos = 0;
1344 const uint8_t len = 2;
1345 data_buffer[pos++] = (id << 4) + len;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001346 ByteWriter<uint32_t, 3>::WriteBigEndian(data_buffer + pos,
1347 absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001348 pos += 3;
1349 assert(pos == kAbsoluteSendTimeLength);
1350 return kAbsoluteSendTimeLength;
1351}
1352
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001353uint8_t RTPSender::BuildVideoRotationExtension(uint8_t* data_buffer) const {
1354 // Coordination of Video Orientation in RTP streams.
1355 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001356 // Coordination of Video Orientation consists in signaling of the current
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001357 // orientation of the image captured on the sender side to the receiver for
1358 // appropriate rendering and displaying.
1359 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001360 // 0 1
1361 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1362 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1363 // | ID | len=0 |0 0 0 0 C F R R|
1364 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001365 //
1366
1367 // Get id defined by user.
1368 uint8_t id;
1369 if (rtp_header_extension_map_.GetId(kRtpExtensionVideoRotation, &id) != 0) {
1370 // Not registered.
1371 return 0;
1372 }
1373 size_t pos = 0;
1374 const uint8_t len = 0;
1375 data_buffer[pos++] = (id << 4) + len;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +00001376 data_buffer[pos++] = ConvertVideoRotationToCVOByte(rotation_);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001377 assert(pos == kVideoRotationLength);
1378 return kVideoRotationLength;
1379}
1380
sprang@webrtc.org30933902015-03-17 14:33:12 +00001381uint8_t RTPSender::BuildTransportSequenceNumberExtension(
1382 uint8_t* data_buffer) const {
1383 // 0 1 2
1384 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
1385 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1386 // | ID | L=1 |transport wide sequence number |
1387 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1388
1389 // Get id defined by user.
1390 uint8_t id;
1391 if (rtp_header_extension_map_.GetId(kRtpExtensionTransportSequenceNumber,
1392 &id) != 0) {
1393 // Not registered.
1394 return 0;
1395 }
1396 size_t pos = 0;
1397 const uint8_t len = 1;
1398 data_buffer[pos++] = (id << 4) + len;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001399 ByteWriter<uint16_t>::WriteBigEndian(data_buffer + pos,
1400 transport_sequence_number_);
sprang@webrtc.org30933902015-03-17 14:33:12 +00001401 pos += 2;
1402 assert(pos == kTransportSequenceNumberLength);
1403 return kTransportSequenceNumberLength;
1404}
1405
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001406bool RTPSender::FindHeaderExtensionPosition(RTPExtensionType type,
1407 const uint8_t* rtp_packet,
1408 size_t rtp_packet_length,
1409 const RTPHeader& rtp_header,
1410 size_t* position) const {
1411 // Get length until start of header extension block.
1412 int extension_block_pos =
1413 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(type);
1414 if (extension_block_pos < 0) {
1415 LOG(LS_WARNING) << "Failed to find extension position for " << type
1416 << " as it is not registered.";
1417 return false;
1418 }
1419
1420 HeaderExtension header_extension(type);
1421
1422 size_t block_pos =
1423 kRtpHeaderLength + rtp_header.numCSRCs + extension_block_pos;
1424 if (rtp_packet_length < block_pos + header_extension.length ||
1425 rtp_header.headerLength < block_pos + header_extension.length) {
1426 LOG(LS_WARNING) << "Failed to find extension position for " << type
1427 << " as the length is invalid.";
1428 return false;
1429 }
1430
1431 // Verify that header contains extension.
1432 if (!((rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs] == 0xBE) &&
1433 (rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs + 1] == 0xDE))) {
1434 LOG(LS_WARNING) << "Failed to find extension position for " << type
1435 << "as hdr extension not found.";
1436 return false;
1437 }
1438
1439 *position = block_pos;
1440 return true;
1441}
1442
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001443void RTPSender::UpdateTransmissionTimeOffset(uint8_t* rtp_packet,
1444 size_t rtp_packet_length,
1445 const RTPHeader& rtp_header,
1446 int64_t time_diff_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001447 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001448 // Get id.
1449 uint8_t id = 0;
1450 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1451 &id) != 0) {
1452 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001453 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001454 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001455
1456 size_t block_pos = 0;
1457 if (!FindHeaderExtensionPosition(kRtpExtensionTransmissionTimeOffset,
1458 rtp_packet, rtp_packet_length, rtp_header,
1459 &block_pos)) {
1460 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001461 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001462 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001463
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001464 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001465 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001466 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001467 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001468 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001469 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001470 // Update transmission offset field (converting to a 90 kHz timestamp).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001471 ByteWriter<int32_t, 3>::WriteBigEndian(rtp_packet + block_pos + 1,
1472 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001473}
1474
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001475bool RTPSender::UpdateAudioLevel(uint8_t* rtp_packet,
1476 size_t rtp_packet_length,
1477 const RTPHeader& rtp_header,
1478 bool is_voiced,
1479 uint8_t dBov) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001480 CriticalSectionScoped cs(send_critsect_.get());
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001481
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001482 // Get id.
1483 uint8_t id = 0;
1484 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1485 // Not registered.
1486 return false;
1487 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001488
1489 size_t block_pos = 0;
1490 if (!FindHeaderExtensionPosition(kRtpExtensionAudioLevel, rtp_packet,
1491 rtp_packet_length, rtp_header, &block_pos)) {
1492 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001493 return false;
1494 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001495
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001496 // Verify first byte in block.
1497 const uint8_t first_block_byte = (id << 4) + 0;
1498 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001499 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001500 return false;
1501 }
1502 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1503 return true;
1504}
1505
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001506bool RTPSender::UpdateVideoRotation(uint8_t* rtp_packet,
1507 size_t rtp_packet_length,
1508 const RTPHeader& rtp_header,
1509 VideoRotation rotation) const {
1510 CriticalSectionScoped cs(send_critsect_.get());
1511
1512 // Get id.
1513 uint8_t id = 0;
1514 if (rtp_header_extension_map_.GetId(kRtpExtensionVideoRotation, &id) != 0) {
1515 // Not registered.
1516 return false;
1517 }
1518
1519 size_t block_pos = 0;
1520 if (!FindHeaderExtensionPosition(kRtpExtensionVideoRotation, rtp_packet,
1521 rtp_packet_length, rtp_header, &block_pos)) {
1522 LOG(LS_WARNING) << "Failed to update video rotation (CVO).";
1523 return false;
1524 }
1525 // Get length until start of header extension block.
1526 int extension_block_pos =
1527 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1528 kRtpExtensionVideoRotation);
1529 if (extension_block_pos < 0) {
1530 // The feature is not enabled.
1531 return false;
1532 }
1533
1534 // Verify first byte in block.
1535 const uint8_t first_block_byte = (id << 4) + 0;
1536 if (rtp_packet[block_pos] != first_block_byte) {
1537 LOG(LS_WARNING) << "Failed to update CVO.";
1538 return false;
1539 }
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +00001540 rtp_packet[block_pos + 1] = ConvertVideoRotationToCVOByte(rotation);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001541 return true;
1542}
1543
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001544void RTPSender::UpdateAbsoluteSendTime(uint8_t* rtp_packet,
1545 size_t rtp_packet_length,
1546 const RTPHeader& rtp_header,
1547 int64_t now_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001548 CriticalSectionScoped cs(send_critsect_.get());
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001549
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001550 // Get id.
1551 uint8_t id = 0;
1552 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1553 &id) != 0) {
1554 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001555 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001556 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001557 // Get length until start of header extension block.
1558 int extension_block_pos =
1559 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1560 kRtpExtensionAbsoluteSendTime);
1561 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001562 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001563 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001564 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001565 size_t block_pos =
1566 kRtpHeaderLength + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001567 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001568 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001569 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001570 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001571 }
1572 // Verify that header contains extension.
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001573 if (!((rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs] == 0xBE) &&
1574 (rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001575 LOG(LS_WARNING)
1576 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001577 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001578 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001579 // Verify first byte in block.
1580 const uint8_t first_block_byte = (id << 4) + 2;
1581 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001582 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001583 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001584 }
1585 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1586 // fractional part).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001587 ByteWriter<uint32_t, 3>::WriteBigEndian(rtp_packet + block_pos + 1,
1588 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001589}
1590
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001591void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001592 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001593 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001594 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001595
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001596 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001597 SetStartTimestamp(RTPtime, false);
1598 } else {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001599 CriticalSectionScoped lock(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001600 if (!ssrc_forced_) {
1601 // Generate a new SSRC.
1602 ssrc_db_.ReturnSSRC(ssrc_);
1603 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001604 bitrates_->set_ssrc(ssrc_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001605 }
1606 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001607 if (!sequence_number_forced_ && !ssrc_forced_) {
1608 // Generate a new sequence number.
1609 sequence_number_ =
1610 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001611 }
1612 }
1613}
1614
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001615void RTPSender::SetSendingMediaStatus(bool enabled) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001616 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001617 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001618}
1619
1620bool RTPSender::SendingMedia() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001621 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001622 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001623}
1624
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001625uint32_t RTPSender::Timestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001626 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001627 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001628}
1629
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001630void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001631 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001632 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001633 start_timestamp_forced_ = true;
1634 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001635 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001636 if (!start_timestamp_forced_) {
1637 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001638 }
1639 }
1640}
1641
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001642uint32_t RTPSender::StartTimestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001643 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001644 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001645}
1646
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001647uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001648 // If configured via API, return 0.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001649 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001650
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001651 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001652 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001653 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001654 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001655 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001656 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001657}
1658
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001659void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001660 // This is configured via the API.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001661 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +00001662
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001663 if (ssrc_ == ssrc && ssrc_forced_) {
1664 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001665 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001666 ssrc_forced_ = true;
1667 ssrc_db_.ReturnSSRC(ssrc_);
1668 ssrc_db_.RegisterSSRC(ssrc);
1669 ssrc_ = ssrc;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001670 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001671 if (!sequence_number_forced_) {
1672 sequence_number_ =
1673 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001674 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001675}
1676
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001677uint32_t RTPSender::SSRC() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001678 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001679 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001680}
1681
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001682void RTPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
1683 assert(csrcs.size() <= kRtpCsrcSize);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001684 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001685 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001686}
1687
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001688void RTPSender::SetSequenceNumber(uint16_t seq) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001689 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001690 sequence_number_forced_ = true;
1691 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001692}
1693
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001694uint16_t RTPSender::SequenceNumber() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001695 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001696 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001697}
1698
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001699// Audio.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001700int32_t RTPSender::SendTelephoneEvent(uint8_t key,
1701 uint16_t time_ms,
1702 uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001703 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001704 return -1;
1705 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001706 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001707}
1708
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001709int32_t RTPSender::SetAudioPacketSize(uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001710 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001711 return -1;
1712 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001713 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001714}
1715
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001716int32_t RTPSender::SetAudioLevel(uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001717 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001718}
1719
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001720int32_t RTPSender::SetRED(int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001721 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001722 return -1;
1723 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001724 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001725}
1726
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001727int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001728 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001729 return -1;
1730 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001731 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001732}
1733
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001734RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001735 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001736 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001737}
1738
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001739uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001740 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001741 return 0;
1742 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001743 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001744}
1745
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001746int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001747 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001748 return -1;
1749 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001750 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001751}
1752
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001753int32_t RTPSender::SetGenericFECStatus(bool enable,
1754 uint8_t payload_type_red,
1755 uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001756 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001757 return -1;
1758 }
mflodmanfcf54bd2015-04-14 21:28:08 +02001759 video_->SetGenericFECStatus(enable, payload_type_red, payload_type_fec);
1760 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001761}
1762
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00001763int32_t RTPSender::GenericFECStatus(bool* enable,
1764 uint8_t* payload_type_red,
1765 uint8_t* payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001766 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001767 return -1;
1768 }
mflodmanfcf54bd2015-04-14 21:28:08 +02001769 video_->GenericFECStatus(*enable, *payload_type_red, *payload_type_fec);
1770 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001771}
1772
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001773int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001774 const FecProtectionParams *delta_params,
1775 const FecProtectionParams *key_params) {
1776 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001777 return -1;
1778 }
mflodmanfcf54bd2015-04-14 21:28:08 +02001779 video_->SetFecParameters(delta_params, key_params);
1780 return 0;
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001781}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001782
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001783void RTPSender::BuildRtxPacket(uint8_t* buffer, size_t* length,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001784 uint8_t* buffer_rtx) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001785 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001786 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001787 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001788 RtpUtility::RtpHeaderParser rtp_parser(
1789 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001790
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001791 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001792 rtp_parser.Parse(rtp_header);
1793
1794 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001795 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001796
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00001797 // Replace payload type, if a specific type is set for RTX.
1798 if (payload_type_rtx_ != -1) {
1799 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001800 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001801 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1802 }
1803
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001804 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001805 uint8_t *ptr = data_buffer_rtx + 2;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001806 ByteWriter<uint16_t>::WriteBigEndian(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001807
1808 // Replace SSRC.
1809 ptr += 6;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001810 ByteWriter<uint32_t>::WriteBigEndian(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001811
1812 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001813 ptr = data_buffer_rtx + rtp_header.headerLength;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001814 ByteWriter<uint16_t>::WriteBigEndian(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001815 ptr += 2;
1816
1817 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001818 memcpy(ptr, buffer + rtp_header.headerLength,
1819 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001820 *length += 2;
1821}
1822
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001823void RTPSender::RegisterRtpStatisticsCallback(
1824 StreamDataCountersCallback* callback) {
1825 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001826 rtp_stats_callback_ = callback;
1827}
1828
1829StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1830 CriticalSectionScoped cs(statistics_crit_.get());
1831 return rtp_stats_callback_;
1832}
1833
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001834uint32_t RTPSender::BitrateSent() const {
1835 return total_bitrate_sent_.BitrateLast();
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001836}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001837
1838void RTPSender::SetRtpState(const RtpState& rtp_state) {
1839 SetStartTimestamp(rtp_state.start_timestamp, true);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001840 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001841 sequence_number_ = rtp_state.sequence_number;
1842 sequence_number_forced_ = true;
1843 timestamp_ = rtp_state.timestamp;
1844 capture_time_ms_ = rtp_state.capture_time_ms;
1845 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001846 media_has_been_sent_ = rtp_state.media_has_been_sent;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001847}
1848
1849RtpState RTPSender::GetRtpState() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001850 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001851
1852 RtpState state;
1853 state.sequence_number = sequence_number_;
1854 state.start_timestamp = start_timestamp_;
1855 state.timestamp = timestamp_;
1856 state.capture_time_ms = capture_time_ms_;
1857 state.last_timestamp_time_ms = last_timestamp_time_ms_;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001858 state.media_has_been_sent = media_has_been_sent_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001859
1860 return state;
1861}
1862
1863void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001864 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001865 sequence_number_rtx_ = rtp_state.sequence_number;
1866}
1867
1868RtpState RTPSender::GetRtxRtpState() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001869 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001870
1871 RtpState state;
1872 state.sequence_number = sequence_number_rtx_;
1873 state.start_timestamp = start_timestamp_;
1874
1875 return state;
1876}
1877
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001878} // namespace webrtc