blob: cb0c6dc0c2b5cb8bb5ca8afd8b73a7f342569ddd [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000011#include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
12
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <stdlib.h> // srand
niklase@google.com470e71d2011-07-07 08:21:25 +000014
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +000015#include "webrtc/modules/rtp_rtcp/interface/rtp_cvo.h"
sprang@webrtc.org779c3d12015-03-17 16:42:49 +000016#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000017#include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
18#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
19#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000020#include "webrtc/system_wrappers/interface/logging.h"
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000021#include "webrtc/system_wrappers/interface/tick_util.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000022#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000025
stefan@webrtc.orga8179622013-06-04 13:47:36 +000026// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000027const size_t kMaxPaddingLength = 224;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +000028const int kSendSideDelayWindowMs = 1000;
stefan@webrtc.orga8179622013-06-04 13:47:36 +000029
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000030namespace {
31
guoweis@webrtc.org45362892015-03-04 22:55:15 +000032const size_t kRtpHeaderLength = 12;
33
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000034const char* FrameTypeToString(FrameType frame_type) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000035 switch (frame_type) {
36 case kFrameEmpty: return "empty";
37 case kAudioFrameSpeech: return "audio_speech";
38 case kAudioFrameCN: return "audio_cn";
39 case kVideoFrameKey: return "video_key";
40 case kVideoFrameDelta: return "video_delta";
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000041 }
42 return "";
43}
44
45} // namespace
46
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +000047class BitrateAggregator {
48 public:
49 explicit BitrateAggregator(BitrateStatisticsObserver* bitrate_callback)
50 : callback_(bitrate_callback),
51 total_bitrate_observer_(*this),
52 retransmit_bitrate_observer_(*this),
53 ssrc_(0) {}
54
55 void OnStatsUpdated() const {
56 if (callback_)
57 callback_->Notify(total_bitrate_observer_.statistics(),
58 retransmit_bitrate_observer_.statistics(),
59 ssrc_);
60 }
61
62 Bitrate::Observer* total_bitrate_observer() {
63 return &total_bitrate_observer_;
64 }
65 Bitrate::Observer* retransmit_bitrate_observer() {
66 return &retransmit_bitrate_observer_;
67 }
68
69 void set_ssrc(uint32_t ssrc) { ssrc_ = ssrc; }
70
71 private:
72 // We assume that these observers are called on the same thread, which is
73 // true for RtpSender as they are called on the Process thread.
74 class BitrateObserver : public Bitrate::Observer {
75 public:
76 explicit BitrateObserver(const BitrateAggregator& aggregator)
77 : aggregator_(aggregator) {}
78
79 // Implements Bitrate::Observer.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000080 void BitrateUpdated(const BitrateStatistics& stats) override {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +000081 statistics_ = stats;
82 aggregator_.OnStatsUpdated();
83 }
84
85 BitrateStatistics statistics() const { return statistics_; }
86
87 private:
88 BitrateStatistics statistics_;
89 const BitrateAggregator& aggregator_;
90 };
91
92 BitrateStatisticsObserver* const callback_;
93 BitrateObserver total_bitrate_observer_;
94 BitrateObserver retransmit_bitrate_observer_;
95 uint32_t ssrc_;
96};
97
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +000098RTPSender::RTPSender(int32_t id,
99 bool audio,
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000100 Clock* clock,
101 Transport* transport,
102 RtpAudioFeedback* audio_feedback,
andresp@webrtc.orgd11bec42014-07-08 14:32:58 +0000103 PacedSender* paced_sender,
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000104 BitrateStatisticsObserver* bitrate_callback,
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000105 FrameCountObserver* frame_count_observer,
106 SendSideDelayObserver* send_side_delay_observer)
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000107 : clock_(clock),
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000108 // TODO(holmer): Remove this conversion when we remove the use of
109 // TickTime.
110 clock_delta_ms_(clock_->TimeInMilliseconds() -
111 TickTime::MillisecondTimestamp()),
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000112 bitrates_(new BitrateAggregator(bitrate_callback)),
113 total_bitrate_sent_(clock, bitrates_->total_bitrate_observer()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000114 id_(id),
115 audio_configured_(audio),
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000116 audio_(audio ? new RTPSenderAudio(id, clock, this, audio_feedback)
117 : nullptr),
118 video_(audio ? nullptr : new RTPSenderVideo(clock, this)),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000119 paced_sender_(paced_sender),
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000120 last_capture_time_ms_sent_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000121 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000122 transport_(transport),
123 sending_media_(true), // Default to sending media.
124 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000125 packet_over_head_(28),
126 payload_type_(-1),
127 payload_type_map_(),
128 rtp_header_extension_map_(),
129 transmission_time_offset_(0),
130 absolute_send_time_(0),
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000131 rotation_(kVideoRotation_0),
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700132 cvo_mode_(kCVONone),
sprang@webrtc.org30933902015-03-17 14:33:12 +0000133 transport_sequence_number_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000134 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000135 nack_byte_count_times_(),
136 nack_byte_count_(),
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000137 nack_bitrate_(clock, bitrates_->retransmit_bitrate_observer()),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000138 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000139 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000140 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000141 rtp_stats_callback_(NULL),
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000142 frame_count_observer_(frame_count_observer),
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000143 send_side_delay_observer_(send_side_delay_observer),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000144 // RTP variables
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000145 start_timestamp_forced_(false),
146 start_timestamp_(0),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000147 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
148 remote_ssrc_(0),
149 sequence_number_forced_(false),
150 ssrc_forced_(false),
151 timestamp_(0),
152 capture_time_ms_(0),
153 last_timestamp_time_ms_(0),
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000154 media_has_been_sent_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000155 last_packet_marker_bit_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000156 csrcs_(),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000157 rtx_(kRtxOff),
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000158 payload_type_rtx_(-1),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000159 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000160 target_bitrate_(0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000161 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
162 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000163 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000164 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000165 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000166 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000167 bitrates_->set_ssrc(ssrc_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000168 // Random start, 16 bits. Can't be 0.
169 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
170 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000171}
172
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000173RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000174 if (remote_ssrc_ != 0) {
175 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000176 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000177 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000178
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000179 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000180 while (!payload_type_map_.empty()) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000181 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000182 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000183 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000184 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000185 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000186}
niklase@google.com470e71d2011-07-07 08:21:25 +0000187
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000188void RTPSender::SetTargetBitrate(uint32_t bitrate) {
189 CriticalSectionScoped cs(target_bitrate_critsect_.get());
190 target_bitrate_ = bitrate;
191}
192
193uint32_t RTPSender::GetTargetBitrate() {
194 CriticalSectionScoped cs(target_bitrate_critsect_.get());
195 return target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000197
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000198uint16_t RTPSender::ActualSendBitrateKbit() const {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000199 return (uint16_t)(total_bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000200}
201
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000202uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000203 if (video_) {
204 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000205 }
206 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000207}
208
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000209uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000210 if (video_) {
211 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000212 }
213 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000214}
215
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000216uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000217 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000218}
219
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000220bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
221 int* max_send_delay_ms) const {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000222 CriticalSectionScoped lock(statistics_crit_.get());
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000223 SendDelayMap::const_iterator it = send_delays_.upper_bound(
224 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000225 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000226 return false;
227 int num_delays = 0;
228 for (; it != send_delays_.end(); ++it) {
229 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
230 *avg_send_delay_ms += it->second;
231 ++num_delays;
232 }
233 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
234 return true;
235}
236
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000237int32_t RTPSender::SetTransmissionTimeOffset(int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000238 if (transmission_time_offset > (0x800000 - 1) ||
239 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000240 return -1;
241 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000242 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000243 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000244 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000245}
246
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000247int32_t RTPSender::SetAbsoluteSendTime(uint32_t absolute_send_time) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000248 if (absolute_send_time > 0xffffff) { // UWord24.
249 return -1;
250 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000251 CriticalSectionScoped cs(send_critsect_.get());
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000252 absolute_send_time_ = absolute_send_time;
253 return 0;
254}
255
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000256void RTPSender::SetVideoRotation(VideoRotation rotation) {
257 CriticalSectionScoped cs(send_critsect_.get());
258 rotation_ = rotation;
259}
260
sprang@webrtc.org30933902015-03-17 14:33:12 +0000261int32_t RTPSender::SetTransportSequenceNumber(uint16_t sequence_number) {
262 CriticalSectionScoped cs(send_critsect_.get());
263 transport_sequence_number_ = sequence_number;
264 return 0;
265}
266
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000267int32_t RTPSender::RegisterRtpHeaderExtension(RTPExtensionType type,
268 uint8_t id) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000269 CriticalSectionScoped cs(send_critsect_.get());
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700270 if (type == kRtpExtensionVideoRotation) {
271 cvo_mode_ = kCVOInactive;
272 return rtp_header_extension_map_.RegisterInactive(type, id);
273 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000274 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000275}
276
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000277bool RTPSender::IsRtpHeaderExtensionRegistered(RTPExtensionType type) {
278 CriticalSectionScoped cs(send_critsect_.get());
279 return rtp_header_extension_map_.IsRegistered(type);
280}
281
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000282int32_t RTPSender::DeregisterRtpHeaderExtension(RTPExtensionType type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000283 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000284 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000285}
286
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000287size_t RTPSender::RtpHeaderExtensionTotalLength() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000288 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000289 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000290}
291
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000292int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000293 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000294 int8_t payload_number,
295 uint32_t frequency,
296 uint8_t channels,
297 uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000298 assert(payload_name);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000299 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000301 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000302 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000303
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000304 if (payload_type_map_.end() != it) {
305 // We already use this payload type.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000306 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000307 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000308
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000309 // Check if it's the same as we already have.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000310 if (RtpUtility::StringCompare(
311 payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000312 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000313 payload->typeSpecific.Audio.frequency == frequency &&
314 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000315 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000316 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000317 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000318 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000319 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000320 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000321 return 0;
322 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000323 }
324 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000325 }
mflodman0828a0c2015-03-31 15:29:23 +0200326 int32_t ret_val = -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000327 RtpUtility::Payload* payload = NULL;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000328 if (audio_configured_) {
329 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
330 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000331 } else {
mflodman0828a0c2015-03-31 15:29:23 +0200332 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
333 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000334 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000335 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000336 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000337 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000338 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000339}
340
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000341int32_t RTPSender::DeRegisterSendPayload(int8_t payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000342 CriticalSectionScoped lock(send_critsect_.get());
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000343
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000344 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000345 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000346
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000347 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000348 return -1;
349 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000350 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000351 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000352 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000353 return 0;
354}
niklase@google.com470e71d2011-07-07 08:21:25 +0000355
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000356void RTPSender::SetSendPayloadType(int8_t payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000357 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000358 payload_type_ = payload_type;
359}
360
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000361int8_t RTPSender::SendPayloadType() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000362 CriticalSectionScoped cs(send_critsect_.get());
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000363 return payload_type_;
364}
niklase@google.com470e71d2011-07-07 08:21:25 +0000365
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000366int RTPSender::SendPayloadFrequency() const {
367 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
368}
niklase@google.com470e71d2011-07-07 08:21:25 +0000369
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000370int32_t RTPSender::SetMaxPayloadLength(size_t max_payload_length,
371 uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000372 // Sanity check.
373 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000374 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000375 return -1;
376 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000377 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000378 max_payload_length_ = max_payload_length;
379 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000380 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000381}
382
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000383size_t RTPSender::MaxDataPayloadLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000384 int rtx;
385 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000386 CriticalSectionScoped rtx_lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000387 rtx = rtx_;
388 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000389 if (audio_configured_) {
390 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000391 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000392 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
393 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000394 - ((rtx) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000395 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000396}
397
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000398size_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000399 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000400}
401
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000402uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000403
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000404void RTPSender::SetRtxStatus(int mode) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000405 CriticalSectionScoped cs(send_critsect_.get());
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000406 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000407}
408
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000409int RTPSender::RtxStatus() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000410 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000411 return rtx_;
412}
413
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000414void RTPSender::SetRtxSsrc(uint32_t ssrc) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000415 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000416 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000417}
418
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000419uint32_t RTPSender::RtxSsrc() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000420 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000421 return ssrc_rtx_;
422}
423
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000424void RTPSender::SetRtxPayloadType(int payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000425 CriticalSectionScoped cs(send_critsect_.get());
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000426 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000427}
428
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000429int32_t RTPSender::CheckPayloadType(int8_t payload_type,
430 RtpVideoCodecTypes* video_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000431 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000432
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000433 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000434 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000435 return -1;
436 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000437 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000438 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000439 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000440 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000441 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000442 // And it's a match...
443 return 0;
444 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000445 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000446 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000447 if (payload_type_ == payload_type) {
448 if (!audio_configured_) {
449 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000450 }
451 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000452 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000453 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000454 payload_type_map_.find(payload_type);
455 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000456 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000457 return -1;
458 }
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000459 SetSendPayloadType(payload_type);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000460 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000461 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000462 if (!payload->audio && !audio_configured_) {
463 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
464 *video_type = payload->typeSpecific.Video.videoCodecType;
465 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000466 }
467 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000468}
469
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700470RTPSenderInterface::CVOMode RTPSender::ActivateCVORtpHeaderExtension() {
471 if (cvo_mode_ == kCVOInactive) {
472 CriticalSectionScoped cs(send_critsect_.get());
473 if (rtp_header_extension_map_.SetActive(kRtpExtensionVideoRotation, true)) {
474 cvo_mode_ = kCVOActivated;
475 }
476 }
477 return cvo_mode_;
478}
479
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000480int32_t RTPSender::SendOutgoingData(FrameType frame_type,
481 int8_t payload_type,
482 uint32_t capture_timestamp,
483 int64_t capture_time_ms,
484 const uint8_t* payload_data,
485 size_t payload_size,
486 const RTPFragmentationHeader* fragmentation,
mflodman0828a0c2015-03-31 15:29:23 +0200487 VideoCodecInformation* codec_info,
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000488 const RTPVideoHeader* rtp_hdr) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000489 uint32_t ssrc;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000490 {
491 // Drop this packet if we're not sending media packets.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000492 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000493 ssrc = ssrc_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000494 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000495 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000496 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000497 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000498 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000499 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000500 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000501 return -1;
502 }
503
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000504 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000505 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000506 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
507 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000508 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000509 frame_type == kFrameEmpty);
510
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000511 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
512 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000513 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000514 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
515 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000516 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000517
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000518 if (frame_type == kFrameEmpty)
519 return 0;
520
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000521 ret_val =
522 video_->SendVideo(video_type, frame_type, payload_type,
523 capture_timestamp, capture_time_ms, payload_data,
mflodman0828a0c2015-03-31 15:29:23 +0200524 payload_size, fragmentation, codec_info, rtp_hdr);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000525 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000526
527 CriticalSectionScoped cs(statistics_crit_.get());
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000528 // Note: This is currently only counting for video.
529 if (frame_type == kVideoFrameKey) {
530 ++frame_counts_.key_frames;
531 } else if (frame_type == kVideoFrameDelta) {
532 ++frame_counts_.delta_frames;
533 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000534 if (frame_count_observer_) {
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000535 frame_count_observer_->FrameCountUpdated(frame_counts_, ssrc);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000536 }
537
538 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000539}
540
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000541size_t RTPSender::TrySendRedundantPayloads(size_t bytes_to_send) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000542 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000543 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000544 if ((rtx_ & kRtxRedundantPayloads) == 0)
545 return 0;
546 }
547
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000548 uint8_t buffer[IP_PACKET_SIZE];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000549 int bytes_left = static_cast<int>(bytes_to_send);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000550 while (bytes_left > 0) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000551 size_t length = bytes_left;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000552 int64_t capture_time_ms;
553 if (!packet_history_.GetBestFittingPacket(buffer, &length,
554 &capture_time_ms)) {
555 break;
556 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000557 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000558 break;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000559 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000560 RTPHeader rtp_header;
561 rtp_parser.Parse(rtp_header);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000562 bytes_left -= static_cast<int>(length - rtp_header.headerLength);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000563 }
564 return bytes_to_send - bytes_left;
565}
566
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000567size_t RTPSender::BuildPaddingPacket(uint8_t* packet, size_t header_length) {
568 size_t padding_bytes_in_packet = kMaxPaddingLength;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000569 packet[0] |= 0x20; // Set padding bit.
570 int32_t *data =
571 reinterpret_cast<int32_t *>(&(packet[header_length]));
572
573 // Fill data buffer with random data.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000574 for (size_t j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000575 data[j] = rand(); // NOLINT
576 }
577 // Set number of padding bytes in the last byte of the packet.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000578 packet[header_length + padding_bytes_in_packet - 1] =
579 static_cast<uint8_t>(padding_bytes_in_packet);
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000580 return padding_bytes_in_packet;
581}
582
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000583size_t RTPSender::TrySendPadData(size_t bytes) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000584 int64_t capture_time_ms;
585 uint32_t timestamp;
586 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000587 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000588 timestamp = timestamp_;
589 capture_time_ms = capture_time_ms_;
590 if (last_timestamp_time_ms_ > 0) {
591 timestamp +=
592 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
593 capture_time_ms +=
594 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
595 }
596 }
597 return SendPadData(timestamp, capture_time_ms, bytes);
598}
599
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000600size_t RTPSender::SendPadData(uint32_t timestamp,
601 int64_t capture_time_ms,
602 size_t bytes) {
603 size_t padding_bytes_in_packet = 0;
604 size_t bytes_sent = 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000605 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000606 // Always send full padding packets.
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000607 if (bytes < kMaxPaddingLength)
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000608 bytes = kMaxPaddingLength;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000609
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000610 uint32_t ssrc;
611 uint16_t sequence_number;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000612 int payload_type;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000613 bool over_rtx;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000614 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000615 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000616 // Only send padding packets following the last packet of a frame,
617 // indicated by the marker bit.
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000618 if (rtx_ == kRtxOff) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000619 // Without RTX we can't send padding in the middle of frames.
620 if (!last_packet_marker_bit_)
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000621 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000622 ssrc = ssrc_;
623 sequence_number = sequence_number_;
624 ++sequence_number_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000625 payload_type = payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000626 over_rtx = false;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000627 } else {
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000628 // Without abs-send-time a media packet must be sent before padding so
629 // that the timestamps used for estimation are correct.
630 if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
631 kRtpExtensionAbsoluteSendTime))
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000632 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000633 ssrc = ssrc_rtx_;
634 sequence_number = sequence_number_rtx_;
635 ++sequence_number_rtx_;
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000636 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_
637 : payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000638 over_rtx = true;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000639 }
640 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000641
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000642 uint8_t padding_packet[IP_PACKET_SIZE];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000643 size_t header_length =
644 CreateRtpHeader(padding_packet, payload_type, ssrc, false, timestamp,
645 sequence_number, std::vector<uint32_t>());
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000646 assert(header_length != static_cast<size_t>(-1));
647 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length);
648 assert(padding_bytes_in_packet <= bytes);
649 size_t length = padding_bytes_in_packet + header_length;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000650 int64_t now_ms = clock_->TimeInMilliseconds();
651
652 RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
653 RTPHeader rtp_header;
654 rtp_parser.Parse(rtp_header);
655
656 if (capture_time_ms > 0) {
657 UpdateTransmissionTimeOffset(
658 padding_packet, length, rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000659 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000660
661 UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
662 if (!SendPacketToNetwork(padding_packet, length))
663 break;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000664 bytes_sent += padding_bytes_in_packet;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000665 UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000666 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000667
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000668 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000669}
670
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000671void RTPSender::SetStorePacketsStatus(bool enable, uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000672 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000673}
674
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000675bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000676 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000677}
niklase@google.com470e71d2011-07-07 08:21:25 +0000678
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000679int32_t RTPSender::ReSendPacket(uint16_t packet_id, int64_t min_resend_time) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000680 size_t length = IP_PACKET_SIZE;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000681 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000682 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000683 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
684 data_buffer, &length,
685 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000686 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000687 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000688 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000689
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000690 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000691 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000692 RTPHeader header;
693 if (!rtp_parser.Parse(header)) {
694 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000695 return -1;
696 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000697 // Convert from TickTime to Clock since capture_time_ms is based on
698 // TickTime.
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000699 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_;
700 if (!paced_sender_->SendPacket(
701 PacedSender::kHighPriority, header.ssrc, header.sequenceNumber,
702 corrected_capture_tims_ms, length - header.headerLength, true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000703 // We can't send the packet right now.
704 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000705 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000706 }
707 }
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000708 int rtx = kRtxOff;
709 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000710 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000711 rtx = rtx_;
712 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000713 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000714 (rtx & kRtxRetransmitted) > 0, true) ?
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000715 static_cast<int32_t>(length) : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000716}
717
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000718bool RTPSender::SendPacketToNetwork(const uint8_t *packet, size_t size) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000719 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000720 if (transport_) {
721 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000722 }
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000723 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
724 "RTPSender::SendPacketToNetwork", "size", size, "sent",
725 bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000726 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000727 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000728 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000729 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000730 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000731 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000732}
733
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000734int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000735 if (!video_)
736 return -1;
737 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000738}
739
740int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000741 if (!video_)
742 return -1;
mflodman0828a0c2015-03-31 15:29:23 +0200743 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000744}
745
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000746void RTPSender::OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000747 int64_t avg_rtt) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000748 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
749 "RTPSender::OnReceivedNACK", "num_seqnum",
750 nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000751 const int64_t now = clock_->TimeInMilliseconds();
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000752 uint32_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000753 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000754
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000755 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000756 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000757 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000758 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000759 return;
760 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000761
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000762 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
763 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000764 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000765 if (bytes_sent > 0) {
766 bytes_re_sent += bytes_sent;
767 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000768 // The packet has previously been resent.
769 // Try resending next packet in the list.
770 continue;
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000771 } else {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000772 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000773 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
774 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000775 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000776 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000777 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000778 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000779 // kbits/s * ms = bits => bits/8 = bytes
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000780 size_t target_bytes =
781 (static_cast<size_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000782 if (bytes_re_sent > target_bytes) {
783 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000784 }
785 }
786 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000787 if (bytes_re_sent > 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000788 UpdateNACKBitRate(bytes_re_sent, now);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000789 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000790}
791
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000792bool RTPSender::ProcessNACKBitRate(uint32_t now) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000793 uint32_t num = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000794 size_t byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000795 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000796 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000797
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000798 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000799
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000800 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000801 return true;
802 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000803 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000804 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000805 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000806 break;
807 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000808 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000809 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000810 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000811 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000812 if (num == NACK_BYTECOUNT_SIZE) {
813 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000814 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000815 if (nack_byte_count_times_[num - 1] <= now) {
816 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000817 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000818 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000819 return (byte_count * 8) < (target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000820}
821
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000822void RTPSender::UpdateNACKBitRate(uint32_t bytes, int64_t now) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000823 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000824 if (bytes == 0)
825 return;
826 nack_bitrate_.Update(bytes);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000827 // Save bitrate statistics.
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000828 // Shift all but first time.
829 for (int i = NACK_BYTECOUNT_SIZE - 2; i >= 0; i--) {
830 nack_byte_count_[i + 1] = nack_byte_count_[i];
831 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000832 }
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000833 nack_byte_count_[0] = bytes;
834 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000835}
836
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000837// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000838bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000839 int64_t capture_time_ms,
840 bool retransmission) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000841 size_t length = IP_PACKET_SIZE;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000842 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000843 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000844
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000845 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
846 0,
847 retransmission,
848 data_buffer,
849 &length,
850 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000851 // Packet cannot be found. Allow sending to continue.
852 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000853 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000854 if (!retransmission && capture_time_ms > 0) {
855 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
856 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000857 int rtx;
858 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000859 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000860 rtx = rtx_;
861 }
862 return PrepareAndSendPacket(data_buffer,
863 length,
864 capture_time_ms,
865 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000866 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000867}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000868
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000869bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000870 size_t length,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000871 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000872 bool send_over_rtx,
873 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000874 uint8_t *buffer_to_send_ptr = buffer;
875
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000876 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000877 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000878 rtp_parser.Parse(rtp_header);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000879 if (!is_retransmit && rtp_header.markerBit) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000880 TRACE_EVENT_ASYNC_END0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PacedSend",
881 capture_time_ms);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000882 }
883
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000884 TRACE_EVENT_INSTANT2(
885 TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PrepareAndSendPacket",
886 "timestamp", rtp_header.timestamp, "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000887
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000888 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000889 if (send_over_rtx) {
890 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000891 buffer_to_send_ptr = data_buffer_rtx;
892 }
893
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000894 int64_t now_ms = clock_->TimeInMilliseconds();
895 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000896 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
897 diff_ms);
898 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000899 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000900 if (ret) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000901 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000902 media_has_been_sent_ = true;
903 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000904 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
905 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000906 return ret;
907}
908
909void RTPSender::UpdateRtpStats(const uint8_t* buffer,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000910 size_t packet_length,
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000911 const RTPHeader& header,
912 bool is_rtx,
913 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000914 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000915 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000916 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000917
918 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000919 if (is_rtx) {
920 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000921 } else {
922 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000923 }
924
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000925 total_bitrate_sent_.Update(packet_length);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000926
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000927 if (counters->first_packet_time_ms == -1) {
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000928 counters->first_packet_time_ms = clock_->TimeInMilliseconds();
929 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000930 if (IsFecPacket(buffer, header)) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000931 counters->fec.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000932 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000933 if (is_retransmit) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000934 counters->retransmitted.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000935 }
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000936 counters->transmitted.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000937
938 if (rtp_stats_callback_) {
939 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
940 }
941}
942
943bool RTPSender::IsFecPacket(const uint8_t* buffer,
944 const RTPHeader& header) const {
945 if (!video_) {
946 return false;
947 }
948 bool fec_enabled;
949 uint8_t pt_red;
950 uint8_t pt_fec;
951 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
952 return fec_enabled &&
953 header.payloadType == pt_red &&
954 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000955}
956
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000957size_t RTPSender::TimeToSendPadding(size_t bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000958 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000959 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000960 if (!sending_media_) return 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000961 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000962 if (bytes == 0)
963 return 0;
964 size_t bytes_sent = TrySendRedundantPayloads(bytes);
965 if (bytes_sent < bytes)
966 bytes_sent += TrySendPadData(bytes - bytes_sent);
967 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000968}
969
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000970// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000971int32_t RTPSender::SendToNetwork(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000972 uint8_t *buffer, size_t payload_length, size_t rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000973 int64_t capture_time_ms, StorageType storage,
974 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000975 RtpUtility::RtpHeaderParser rtp_parser(buffer,
976 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000977 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000978 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000979
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000980 int64_t now_ms = clock_->TimeInMilliseconds();
981
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000982 // |capture_time_ms| <= 0 is considered invalid.
983 // TODO(holmer): This should be changed all over Video Engine so that negative
984 // time is consider invalid, while 0 is considered a valid time.
985 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000986 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000987 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000988 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000989
990 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
991 rtp_header, now_ms);
992
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000993 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000994 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
995 max_payload_length_, capture_time_ms,
996 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000997 return -1;
998 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000999
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +00001000 if (paced_sender_ && storage != kDontStore) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +00001001 // Correct offset between implementations of millisecond time stamps in
1002 // TickTime and Clock.
1003 int64_t corrected_time_ms = capture_time_ms + clock_delta_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +00001004 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +00001005 rtp_header.sequenceNumber, corrected_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +00001006 payload_length, false)) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +00001007 if (last_capture_time_ms_sent_ == 0 ||
1008 corrected_time_ms > last_capture_time_ms_sent_) {
1009 last_capture_time_ms_sent_ = corrected_time_ms;
sprang@webrtc.org0200f702015-02-16 12:06:00 +00001010 TRACE_EVENT_ASYNC_BEGIN1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
1011 "PacedSend", corrected_time_ms,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +00001012 "capture_time_ms", corrected_time_ms);
1013 }
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +00001014 // We can't send the packet right now.
1015 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +00001016 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001017 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +00001018 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001019 if (capture_time_ms > 0) {
1020 UpdateDelayStatistics(capture_time_ms, now_ms);
1021 }
sprang@webrtc.org43c88392015-01-29 09:09:17 +00001022
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001023 size_t length = payload_length + rtp_header_length;
sprang@webrtc.orgc957ffc2015-02-02 13:08:02 +00001024 bool sent = SendPacketToNetwork(buffer, length);
1025
1026 if (storage != kDontStore) {
1027 // Mark the packet as sent in the history even if send failed. Dropping a
1028 // packet here should be treated as any other packet drop so we should be
1029 // ready for a retransmission.
1030 packet_history_.SetSent(rtp_header.sequenceNumber);
1031 }
1032 if (!sent)
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001033 return -1;
sprang@webrtc.orgc957ffc2015-02-02 13:08:02 +00001034
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001035 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001036 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001037 media_has_been_sent_ = true;
1038 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001039 UpdateRtpStats(buffer, length, rtp_header, false, false);
1040 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +00001041}
1042
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001043void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001044 uint32_t ssrc;
1045 int avg_delay_ms = 0;
1046 int max_delay_ms = 0;
1047 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001048 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001049 ssrc = ssrc_;
1050 }
1051 {
1052 CriticalSectionScoped cs(statistics_crit_.get());
1053 // TODO(holmer): Compute this iteratively instead.
1054 send_delays_[now_ms] = now_ms - capture_time_ms;
1055 send_delays_.erase(send_delays_.begin(),
1056 send_delays_.lower_bound(now_ms -
1057 kSendSideDelayWindowMs));
1058 }
1059 if (send_side_delay_observer_ &&
1060 GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
1061 send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
1062 max_delay_ms, ssrc);
1063 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001064}
1065
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001066void RTPSender::ProcessBitrate() {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001067 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001068 total_bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001069 nack_bitrate_.Process();
1070 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001071 return;
1072 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001073 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +00001074}
1075
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001076size_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001077 CriticalSectionScoped lock(send_critsect_.get());
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001078 size_t rtp_header_length = kRtpHeaderLength;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001079 rtp_header_length += sizeof(uint32_t) * csrcs_.size();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001080 rtp_header_length += RtpHeaderExtensionTotalLength();
1081 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001082}
1083
mflodman0828a0c2015-03-31 15:29:23 +02001084uint16_t RTPSender::IncrementSequenceNumber() {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001085 CriticalSectionScoped cs(send_critsect_.get());
mflodman0828a0c2015-03-31 15:29:23 +02001086 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001087}
1088
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001089void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001090 uint32_t ssrc;
1091 uint32_t ssrc_rtx;
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001092 bool report_rtx;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001093 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001094 CriticalSectionScoped ssrc_lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001095 ssrc = ssrc_;
1096 ssrc_rtx = ssrc_rtx_;
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001097 report_rtx = rtx_ != kRtxOff;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001098 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001099 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001100 rtp_stats_ = StreamDataCounters();
1101 rtx_rtp_stats_ = StreamDataCounters();
1102 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001103 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001104 if (report_rtx)
1105 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001106 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001107}
1108
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001109void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1110 StreamDataCounters* rtx_stats) const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001111 CriticalSectionScoped lock(statistics_crit_.get());
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001112 *rtp_stats = rtp_stats_;
1113 *rtx_stats = rtx_rtp_stats_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001114}
1115
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001116size_t RTPSender::CreateRtpHeader(uint8_t* header,
1117 int8_t payload_type,
1118 uint32_t ssrc,
1119 bool marker_bit,
1120 uint32_t timestamp,
1121 uint16_t sequence_number,
1122 const std::vector<uint32_t>& csrcs) const {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001123 header[0] = 0x80; // version 2.
1124 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001125 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001126 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001127 }
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001128 ByteWriter<uint16_t>::WriteBigEndian(header + 2, sequence_number);
1129 ByteWriter<uint32_t>::WriteBigEndian(header + 4, timestamp);
1130 ByteWriter<uint32_t>::WriteBigEndian(header + 8, ssrc);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001131 int32_t rtp_header_length = kRtpHeaderLength;
niklase@google.com470e71d2011-07-07 08:21:25 +00001132
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001133 if (csrcs.size() > 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001134 uint8_t *ptr = &header[rtp_header_length];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001135 for (size_t i = 0; i < csrcs.size(); ++i) {
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001136 ByteWriter<uint32_t>::WriteBigEndian(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001137 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001138 }
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001139 header[0] = (header[0] & 0xf0) | csrcs.size();
niklase@google.com470e71d2011-07-07 08:21:25 +00001140
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001141 // Update length of header.
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001142 rtp_header_length += sizeof(uint32_t) * csrcs.size();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001143 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001144
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001145 uint16_t len =
1146 BuildRTPHeaderExtension(header + rtp_header_length, marker_bit);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001147 if (len > 0) {
1148 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001149 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001150 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001151 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001152}
1153
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001154int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001155 int8_t payload_type,
1156 bool marker_bit,
1157 uint32_t capture_timestamp,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001158 int64_t capture_time_ms,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001159 bool timestamp_provided,
1160 bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001161 assert(payload_type >= 0);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001162 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001163
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001164 if (timestamp_provided) {
1165 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001166 } else {
1167 // Make a unique time stamp.
1168 // We can't inc by the actual time, since then we increase the risk of back
1169 // timing.
1170 timestamp_++;
1171 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001172 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001173 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001174 capture_time_ms_ = capture_time_ms;
1175 last_packet_marker_bit_ = marker_bit;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001176 return CreateRtpHeader(data_buffer, payload_type, ssrc_, marker_bit,
1177 timestamp_, sequence_number, csrcs_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001178}
1179
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001180uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer,
1181 bool marker_bit) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001182 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001183 return 0;
1184 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001185 // RTP header extension, RFC 3550.
1186 // 0 1 2 3
1187 // 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
1188 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1189 // | defined by profile | length |
1190 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1191 // | header extension |
1192 // | .... |
1193 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001194 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001195 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001196
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001197 // Add extension ID (0xBEDE).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001198 ByteWriter<uint16_t>::WriteBigEndian(data_buffer,
1199 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001200
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001201 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001202 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001203
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001204 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001205 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001206 uint8_t block_length = 0;
sprang@webrtc.org30933902015-03-17 14:33:12 +00001207 uint8_t* extension_data = &data_buffer[kHeaderLength + total_block_length];
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001208 switch (type) {
1209 case kRtpExtensionTransmissionTimeOffset:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001210 block_length = BuildTransmissionTimeOffsetExtension(extension_data);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001211 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001212 case kRtpExtensionAudioLevel:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001213 block_length = BuildAudioLevelExtension(extension_data);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001214 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001215 case kRtpExtensionAbsoluteSendTime:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001216 block_length = BuildAbsoluteSendTimeExtension(extension_data);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001217 break;
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001218 case kRtpExtensionVideoRotation:
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -07001219 block_length = BuildVideoRotationExtension(extension_data);
sprang@webrtc.org30933902015-03-17 14:33:12 +00001220 break;
1221 case kRtpExtensionTransportSequenceNumber:
1222 block_length = BuildTransportSequenceNumberExtension(extension_data);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001223 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001224 default:
1225 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001226 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001227 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001228 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001229 }
1230 if (total_block_length == 0) {
1231 // No extension added.
1232 return 0;
1233 }
sprang@webrtc.org30933902015-03-17 14:33:12 +00001234 // Add padding elements until we've filled a 32 bit block.
1235 size_t padding_bytes =
1236 RtpUtility::Word32Align(total_block_length) - total_block_length;
1237 if (padding_bytes > 0) {
1238 memset(&data_buffer[kHeaderLength + total_block_length], 0, padding_bytes);
1239 total_block_length += padding_bytes;
1240 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001241 // Set header length (in number of Word32, header excluded).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001242 ByteWriter<uint16_t>::WriteBigEndian(data_buffer + kPosLength,
1243 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001244 // Total added length.
1245 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001246}
1247
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001248uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1249 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001250 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1251 //
1252 // The transmission time is signaled to the receiver in-band using the
1253 // general mechanism for RTP header extensions [RFC5285]. The payload
1254 // of this extension (the transmitted value) is a 24-bit signed integer.
1255 // When added to the RTP timestamp of the packet, it represents the
1256 // "effective" RTP transmission time of the packet, on the RTP
1257 // timescale.
1258 //
1259 // The form of the transmission offset extension block:
1260 //
1261 // 0 1 2 3
1262 // 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
1263 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1264 // | ID | len=2 | transmission offset |
1265 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001266
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001267 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001268 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001269 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1270 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001271 // Not registered.
1272 return 0;
1273 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001274 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001275 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001276 data_buffer[pos++] = (id << 4) + len;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001277 ByteWriter<int32_t, 3>::WriteBigEndian(data_buffer + pos,
1278 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001279 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001280 assert(pos == kTransmissionTimeOffsetLength);
1281 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001282}
1283
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001284uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1285 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1286 //
1287 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1288 //
1289 // The form of the audio level extension block:
1290 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001291 // 0 1
1292 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1293 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1294 // | ID | len=0 |V| level |
1295 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001296 //
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001297
1298 // Get id defined by user.
1299 uint8_t id;
1300 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1301 // Not registered.
1302 return 0;
1303 }
1304 size_t pos = 0;
1305 const uint8_t len = 0;
1306 data_buffer[pos++] = (id << 4) + len;
1307 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001308 assert(pos == kAudioLevelLength);
1309 return kAudioLevelLength;
1310}
1311
1312uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001313 // Absolute send time in RTP streams.
1314 //
1315 // The absolute send time is signaled to the receiver in-band using the
1316 // general mechanism for RTP header extensions [RFC5285]. The payload
1317 // of this extension (the transmitted value) is a 24-bit unsigned integer
1318 // containing the sender's current time in seconds as a fixed point number
1319 // with 18 bits fractional part.
1320 //
1321 // The form of the absolute send time extension block:
1322 //
1323 // 0 1 2 3
1324 // 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
1325 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1326 // | ID | len=2 | absolute send time |
1327 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1328
1329 // Get id defined by user.
1330 uint8_t id;
1331 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1332 &id) != 0) {
1333 // Not registered.
1334 return 0;
1335 }
1336 size_t pos = 0;
1337 const uint8_t len = 2;
1338 data_buffer[pos++] = (id << 4) + len;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001339 ByteWriter<uint32_t, 3>::WriteBigEndian(data_buffer + pos,
1340 absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001341 pos += 3;
1342 assert(pos == kAbsoluteSendTimeLength);
1343 return kAbsoluteSendTimeLength;
1344}
1345
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001346uint8_t RTPSender::BuildVideoRotationExtension(uint8_t* data_buffer) const {
1347 // Coordination of Video Orientation in RTP streams.
1348 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001349 // Coordination of Video Orientation consists in signaling of the current
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001350 // orientation of the image captured on the sender side to the receiver for
1351 // appropriate rendering and displaying.
1352 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001353 // 0 1
1354 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1355 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1356 // | ID | len=0 |0 0 0 0 C F R R|
1357 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001358 //
1359
1360 // Get id defined by user.
1361 uint8_t id;
1362 if (rtp_header_extension_map_.GetId(kRtpExtensionVideoRotation, &id) != 0) {
1363 // Not registered.
1364 return 0;
1365 }
1366 size_t pos = 0;
1367 const uint8_t len = 0;
1368 data_buffer[pos++] = (id << 4) + len;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +00001369 data_buffer[pos++] = ConvertVideoRotationToCVOByte(rotation_);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001370 assert(pos == kVideoRotationLength);
1371 return kVideoRotationLength;
1372}
1373
sprang@webrtc.org30933902015-03-17 14:33:12 +00001374uint8_t RTPSender::BuildTransportSequenceNumberExtension(
1375 uint8_t* data_buffer) const {
1376 // 0 1 2
1377 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
1378 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1379 // | ID | L=1 |transport wide sequence number |
1380 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1381
1382 // Get id defined by user.
1383 uint8_t id;
1384 if (rtp_header_extension_map_.GetId(kRtpExtensionTransportSequenceNumber,
1385 &id) != 0) {
1386 // Not registered.
1387 return 0;
1388 }
1389 size_t pos = 0;
1390 const uint8_t len = 1;
1391 data_buffer[pos++] = (id << 4) + len;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001392 ByteWriter<uint16_t>::WriteBigEndian(data_buffer + pos,
1393 transport_sequence_number_);
sprang@webrtc.org30933902015-03-17 14:33:12 +00001394 pos += 2;
1395 assert(pos == kTransportSequenceNumberLength);
1396 return kTransportSequenceNumberLength;
1397}
1398
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001399bool RTPSender::FindHeaderExtensionPosition(RTPExtensionType type,
1400 const uint8_t* rtp_packet,
1401 size_t rtp_packet_length,
1402 const RTPHeader& rtp_header,
1403 size_t* position) const {
1404 // Get length until start of header extension block.
1405 int extension_block_pos =
1406 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(type);
1407 if (extension_block_pos < 0) {
1408 LOG(LS_WARNING) << "Failed to find extension position for " << type
1409 << " as it is not registered.";
1410 return false;
1411 }
1412
1413 HeaderExtension header_extension(type);
1414
1415 size_t block_pos =
1416 kRtpHeaderLength + rtp_header.numCSRCs + extension_block_pos;
1417 if (rtp_packet_length < block_pos + header_extension.length ||
1418 rtp_header.headerLength < block_pos + header_extension.length) {
1419 LOG(LS_WARNING) << "Failed to find extension position for " << type
1420 << " as the length is invalid.";
1421 return false;
1422 }
1423
1424 // Verify that header contains extension.
1425 if (!((rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs] == 0xBE) &&
1426 (rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs + 1] == 0xDE))) {
1427 LOG(LS_WARNING) << "Failed to find extension position for " << type
1428 << "as hdr extension not found.";
1429 return false;
1430 }
1431
1432 *position = block_pos;
1433 return true;
1434}
1435
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001436void RTPSender::UpdateTransmissionTimeOffset(uint8_t* rtp_packet,
1437 size_t rtp_packet_length,
1438 const RTPHeader& rtp_header,
1439 int64_t time_diff_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001440 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001441 // Get id.
1442 uint8_t id = 0;
1443 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1444 &id) != 0) {
1445 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001446 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001447 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001448
1449 size_t block_pos = 0;
1450 if (!FindHeaderExtensionPosition(kRtpExtensionTransmissionTimeOffset,
1451 rtp_packet, rtp_packet_length, rtp_header,
1452 &block_pos)) {
1453 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001454 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001455 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001456
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001457 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001458 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001459 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001460 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 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001463 // Update transmission offset field (converting to a 90 kHz timestamp).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001464 ByteWriter<int32_t, 3>::WriteBigEndian(rtp_packet + block_pos + 1,
1465 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001466}
1467
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001468bool RTPSender::UpdateAudioLevel(uint8_t* rtp_packet,
1469 size_t rtp_packet_length,
1470 const RTPHeader& rtp_header,
1471 bool is_voiced,
1472 uint8_t dBov) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001473 CriticalSectionScoped cs(send_critsect_.get());
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001474
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001475 // Get id.
1476 uint8_t id = 0;
1477 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1478 // Not registered.
1479 return false;
1480 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001481
1482 size_t block_pos = 0;
1483 if (!FindHeaderExtensionPosition(kRtpExtensionAudioLevel, rtp_packet,
1484 rtp_packet_length, rtp_header, &block_pos)) {
1485 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001486 return false;
1487 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001488
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001489 // Verify first byte in block.
1490 const uint8_t first_block_byte = (id << 4) + 0;
1491 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001492 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001493 return false;
1494 }
1495 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1496 return true;
1497}
1498
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001499bool RTPSender::UpdateVideoRotation(uint8_t* rtp_packet,
1500 size_t rtp_packet_length,
1501 const RTPHeader& rtp_header,
1502 VideoRotation rotation) const {
1503 CriticalSectionScoped cs(send_critsect_.get());
1504
1505 // Get id.
1506 uint8_t id = 0;
1507 if (rtp_header_extension_map_.GetId(kRtpExtensionVideoRotation, &id) != 0) {
1508 // Not registered.
1509 return false;
1510 }
1511
1512 size_t block_pos = 0;
1513 if (!FindHeaderExtensionPosition(kRtpExtensionVideoRotation, rtp_packet,
1514 rtp_packet_length, rtp_header, &block_pos)) {
1515 LOG(LS_WARNING) << "Failed to update video rotation (CVO).";
1516 return false;
1517 }
1518 // Get length until start of header extension block.
1519 int extension_block_pos =
1520 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1521 kRtpExtensionVideoRotation);
1522 if (extension_block_pos < 0) {
1523 // The feature is not enabled.
1524 return false;
1525 }
1526
1527 // Verify first byte in block.
1528 const uint8_t first_block_byte = (id << 4) + 0;
1529 if (rtp_packet[block_pos] != first_block_byte) {
1530 LOG(LS_WARNING) << "Failed to update CVO.";
1531 return false;
1532 }
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +00001533 rtp_packet[block_pos + 1] = ConvertVideoRotationToCVOByte(rotation);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001534 return true;
1535}
1536
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001537void RTPSender::UpdateAbsoluteSendTime(uint8_t* rtp_packet,
1538 size_t rtp_packet_length,
1539 const RTPHeader& rtp_header,
1540 int64_t now_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001541 CriticalSectionScoped cs(send_critsect_.get());
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001542
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001543 // Get id.
1544 uint8_t id = 0;
1545 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1546 &id) != 0) {
1547 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001548 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001549 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001550 // Get length until start of header extension block.
1551 int extension_block_pos =
1552 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1553 kRtpExtensionAbsoluteSendTime);
1554 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001555 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001556 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001557 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001558 size_t block_pos =
1559 kRtpHeaderLength + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001560 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001561 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001562 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001563 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001564 }
1565 // Verify that header contains extension.
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001566 if (!((rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs] == 0xBE) &&
1567 (rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001568 LOG(LS_WARNING)
1569 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001570 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001571 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001572 // Verify first byte in block.
1573 const uint8_t first_block_byte = (id << 4) + 2;
1574 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001575 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001576 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001577 }
1578 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1579 // fractional part).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001580 ByteWriter<uint32_t, 3>::WriteBigEndian(rtp_packet + block_pos + 1,
1581 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001582}
1583
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001584void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001585 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001586 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001587 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001588
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001589 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001590 SetStartTimestamp(RTPtime, false);
1591 } else {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001592 CriticalSectionScoped lock(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001593 if (!ssrc_forced_) {
1594 // Generate a new SSRC.
1595 ssrc_db_.ReturnSSRC(ssrc_);
1596 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001597 bitrates_->set_ssrc(ssrc_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001598 }
1599 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001600 if (!sequence_number_forced_ && !ssrc_forced_) {
1601 // Generate a new sequence number.
1602 sequence_number_ =
1603 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001604 }
1605 }
1606}
1607
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001608void RTPSender::SetSendingMediaStatus(bool enabled) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001609 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001610 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001611}
1612
1613bool RTPSender::SendingMedia() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001614 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001615 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001616}
1617
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001618uint32_t RTPSender::Timestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001619 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001620 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001621}
1622
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001623void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001624 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001625 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001626 start_timestamp_forced_ = true;
1627 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001628 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001629 if (!start_timestamp_forced_) {
1630 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001631 }
1632 }
1633}
1634
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001635uint32_t RTPSender::StartTimestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001636 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001637 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001638}
1639
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001640uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001641 // If configured via API, return 0.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001642 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001643
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001644 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001645 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001646 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001647 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001648 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001649 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001650}
1651
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001652void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001653 // This is configured via the API.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001654 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +00001655
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001656 if (ssrc_ == ssrc && ssrc_forced_) {
1657 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001658 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001659 ssrc_forced_ = true;
1660 ssrc_db_.ReturnSSRC(ssrc_);
1661 ssrc_db_.RegisterSSRC(ssrc);
1662 ssrc_ = ssrc;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001663 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001664 if (!sequence_number_forced_) {
1665 sequence_number_ =
1666 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001667 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001668}
1669
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001670uint32_t RTPSender::SSRC() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001671 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001672 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001673}
1674
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001675void RTPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
1676 assert(csrcs.size() <= kRtpCsrcSize);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001677 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001678 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001679}
1680
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001681void RTPSender::SetSequenceNumber(uint16_t seq) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001682 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001683 sequence_number_forced_ = true;
1684 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001685}
1686
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001687uint16_t RTPSender::SequenceNumber() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001688 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001689 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001690}
1691
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001692// Audio.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001693int32_t RTPSender::SendTelephoneEvent(uint8_t key,
1694 uint16_t time_ms,
1695 uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001696 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001697 return -1;
1698 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001699 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001700}
1701
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001702int32_t RTPSender::SetAudioPacketSize(uint16_t packet_size_samples) {
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_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001707}
1708
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001709int32_t RTPSender::SetAudioLevel(uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001710 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001711}
1712
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001713int32_t RTPSender::SetRED(int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001714 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001715 return -1;
1716 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001717 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001718}
1719
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001720int32_t RTPSender::RED(int8_t *payload_type) const {
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_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001725}
1726
mflodman0828a0c2015-03-31 15:29:23 +02001727// Video
1728VideoCodecInformation *RTPSender::CodecInformationVideo() {
1729 if (audio_configured_) {
1730 return NULL;
1731 }
1732 return video_->CodecInformationVideo();
1733}
1734
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001735RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001736 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001737 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001738}
1739
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001740uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001741 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001742 return 0;
1743 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001744 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001745}
1746
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001747int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001748 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001749 return -1;
1750 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001751 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001752}
1753
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001754int32_t RTPSender::SetGenericFECStatus(bool enable,
1755 uint8_t payload_type_red,
1756 uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001757 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001758 return -1;
1759 }
mflodman0828a0c2015-03-31 15:29:23 +02001760 return video_->SetGenericFECStatus(enable, payload_type_red,
1761 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001762}
1763
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00001764int32_t RTPSender::GenericFECStatus(bool* enable,
1765 uint8_t* payload_type_red,
1766 uint8_t* payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001767 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001768 return -1;
1769 }
mflodman0828a0c2015-03-31 15:29:23 +02001770 return video_->GenericFECStatus(
1771 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001772}
1773
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001774int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001775 const FecProtectionParams *delta_params,
1776 const FecProtectionParams *key_params) {
1777 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001778 return -1;
1779 }
mflodman0828a0c2015-03-31 15:29:23 +02001780 return video_->SetFecParameters(delta_params, key_params);
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