blob: 35f885a3e25725c4eea47e53fbe41a762819af91 [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),
sprang@webrtc.org30933902015-03-17 14:33:12 +0000132 transport_sequence_number_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000133 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000134 nack_byte_count_times_(),
135 nack_byte_count_(),
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000136 nack_bitrate_(clock, bitrates_->retransmit_bitrate_observer()),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000137 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000138 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000139 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000140 rtp_stats_callback_(NULL),
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000141 frame_count_observer_(frame_count_observer),
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000142 send_side_delay_observer_(send_side_delay_observer),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000143 // RTP variables
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000144 start_timestamp_forced_(false),
145 start_timestamp_(0),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000146 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
147 remote_ssrc_(0),
148 sequence_number_forced_(false),
149 ssrc_forced_(false),
150 timestamp_(0),
151 capture_time_ms_(0),
152 last_timestamp_time_ms_(0),
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000153 media_has_been_sent_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000154 last_packet_marker_bit_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000155 csrcs_(),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000156 rtx_(kRtxOff),
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000157 payload_type_rtx_(-1),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000158 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000159 target_bitrate_(0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000160 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
161 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000162 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000163 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000164 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000165 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000166 bitrates_->set_ssrc(ssrc_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000167 // Random start, 16 bits. Can't be 0.
168 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
169 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000170}
171
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000172RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000173 if (remote_ssrc_ != 0) {
174 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000175 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000176 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000178 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000179 while (!payload_type_map_.empty()) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000180 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000181 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000182 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000183 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000184 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000185}
niklase@google.com470e71d2011-07-07 08:21:25 +0000186
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000187void RTPSender::SetTargetBitrate(uint32_t bitrate) {
188 CriticalSectionScoped cs(target_bitrate_critsect_.get());
189 target_bitrate_ = bitrate;
190}
191
192uint32_t RTPSender::GetTargetBitrate() {
193 CriticalSectionScoped cs(target_bitrate_critsect_.get());
194 return target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000195}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000196
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000197uint16_t RTPSender::ActualSendBitrateKbit() const {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000198 return (uint16_t)(total_bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000199}
200
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000201uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000202 if (video_) {
203 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000204 }
205 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000206}
207
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000208uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000209 if (video_) {
210 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000211 }
212 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000213}
214
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000215uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000216 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000217}
218
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000219bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
220 int* max_send_delay_ms) const {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000221 CriticalSectionScoped lock(statistics_crit_.get());
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000222 SendDelayMap::const_iterator it = send_delays_.upper_bound(
223 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000224 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000225 return false;
226 int num_delays = 0;
227 for (; it != send_delays_.end(); ++it) {
228 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
229 *avg_send_delay_ms += it->second;
230 ++num_delays;
231 }
232 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
233 return true;
234}
235
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000236int32_t RTPSender::SetTransmissionTimeOffset(int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000237 if (transmission_time_offset > (0x800000 - 1) ||
238 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000239 return -1;
240 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000241 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000242 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000243 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000244}
245
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000246int32_t RTPSender::SetAbsoluteSendTime(uint32_t absolute_send_time) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000247 if (absolute_send_time > 0xffffff) { // UWord24.
248 return -1;
249 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000250 CriticalSectionScoped cs(send_critsect_.get());
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000251 absolute_send_time_ = absolute_send_time;
252 return 0;
253}
254
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000255void RTPSender::SetVideoRotation(VideoRotation rotation) {
256 CriticalSectionScoped cs(send_critsect_.get());
257 rotation_ = rotation;
258}
259
sprang@webrtc.org30933902015-03-17 14:33:12 +0000260int32_t RTPSender::SetTransportSequenceNumber(uint16_t sequence_number) {
261 CriticalSectionScoped cs(send_critsect_.get());
262 transport_sequence_number_ = sequence_number;
263 return 0;
264}
265
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000266int32_t RTPSender::RegisterRtpHeaderExtension(RTPExtensionType type,
267 uint8_t id) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000268 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000269 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000270}
271
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000272bool RTPSender::IsRtpHeaderExtensionRegistered(RTPExtensionType type) {
273 CriticalSectionScoped cs(send_critsect_.get());
274 return rtp_header_extension_map_.IsRegistered(type);
275}
276
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000277int32_t RTPSender::DeregisterRtpHeaderExtension(RTPExtensionType type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000278 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000279 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000280}
281
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000282size_t RTPSender::RtpHeaderExtensionTotalLength() const {
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_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000285}
286
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000287int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000288 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000289 int8_t payload_number,
290 uint32_t frequency,
291 uint8_t channels,
292 uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000293 assert(payload_name);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000294 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000295
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000296 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000297 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000298
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000299 if (payload_type_map_.end() != it) {
300 // We already use this payload type.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000301 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000302 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000303
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000304 // Check if it's the same as we already have.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000305 if (RtpUtility::StringCompare(
306 payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000307 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000308 payload->typeSpecific.Audio.frequency == frequency &&
309 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000310 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000311 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000312 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000313 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000314 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000315 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000316 return 0;
317 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000318 }
319 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000320 }
mflodman0828a0c2015-03-31 15:29:23 +0200321 int32_t ret_val = -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000322 RtpUtility::Payload* payload = NULL;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000323 if (audio_configured_) {
324 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
325 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000326 } else {
mflodman0828a0c2015-03-31 15:29:23 +0200327 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
328 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000329 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000330 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000331 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000332 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000333 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000334}
335
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000336int32_t RTPSender::DeRegisterSendPayload(int8_t payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000337 CriticalSectionScoped lock(send_critsect_.get());
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000338
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000339 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000340 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000341
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000342 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000343 return -1;
344 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000345 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000346 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000347 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000348 return 0;
349}
niklase@google.com470e71d2011-07-07 08:21:25 +0000350
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000351void RTPSender::SetSendPayloadType(int8_t payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000352 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000353 payload_type_ = payload_type;
354}
355
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000356int8_t RTPSender::SendPayloadType() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000357 CriticalSectionScoped cs(send_critsect_.get());
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000358 return payload_type_;
359}
niklase@google.com470e71d2011-07-07 08:21:25 +0000360
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000361int RTPSender::SendPayloadFrequency() const {
362 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
363}
niklase@google.com470e71d2011-07-07 08:21:25 +0000364
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000365int32_t RTPSender::SetMaxPayloadLength(size_t max_payload_length,
366 uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000367 // Sanity check.
368 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000369 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000370 return -1;
371 }
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000372 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000373 max_payload_length_ = max_payload_length;
374 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000375 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000376}
377
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000378size_t RTPSender::MaxDataPayloadLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000379 int rtx;
380 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000381 CriticalSectionScoped rtx_lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000382 rtx = rtx_;
383 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000384 if (audio_configured_) {
385 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000386 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000387 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
388 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000389 - ((rtx) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000390 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000391}
392
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000393size_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000394 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000395}
396
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000397uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000398
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000399void RTPSender::SetRtxStatus(int mode) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000400 CriticalSectionScoped cs(send_critsect_.get());
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000401 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000402}
403
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000404int RTPSender::RtxStatus() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000405 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000406 return rtx_;
407}
408
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000409void RTPSender::SetRtxSsrc(uint32_t ssrc) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000410 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000411 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000412}
413
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000414uint32_t RTPSender::RtxSsrc() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000415 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000416 return ssrc_rtx_;
417}
418
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000419void RTPSender::SetRtxPayloadType(int payload_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000420 CriticalSectionScoped cs(send_critsect_.get());
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000421 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000422}
423
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000424int32_t RTPSender::CheckPayloadType(int8_t payload_type,
425 RtpVideoCodecTypes* video_type) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000426 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000428 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000429 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000430 return -1;
431 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000432 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000433 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000434 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000435 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000436 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000437 // And it's a match...
438 return 0;
439 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000440 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000441 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000442 if (payload_type_ == payload_type) {
443 if (!audio_configured_) {
444 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000445 }
446 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000447 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000448 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000449 payload_type_map_.find(payload_type);
450 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000451 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000452 return -1;
453 }
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000454 SetSendPayloadType(payload_type);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000455 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000456 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000457 if (!payload->audio && !audio_configured_) {
458 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
459 *video_type = payload->typeSpecific.Video.videoCodecType;
460 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000461 }
462 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000463}
464
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000465int32_t RTPSender::SendOutgoingData(FrameType frame_type,
466 int8_t payload_type,
467 uint32_t capture_timestamp,
468 int64_t capture_time_ms,
469 const uint8_t* payload_data,
470 size_t payload_size,
471 const RTPFragmentationHeader* fragmentation,
mflodman0828a0c2015-03-31 15:29:23 +0200472 VideoCodecInformation* codec_info,
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000473 const RTPVideoHeader* rtp_hdr) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000474 uint32_t ssrc;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000475 {
476 // Drop this packet if we're not sending media packets.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000477 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000478 ssrc = ssrc_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000479 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000480 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000481 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000482 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000483 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000484 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000485 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000486 return -1;
487 }
488
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000489 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000490 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000491 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
492 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000493 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000494 frame_type == kFrameEmpty);
495
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000496 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
497 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000498 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000499 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
500 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000501 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000502
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000503 if (frame_type == kFrameEmpty)
504 return 0;
505
guoweis@webrtc.org45362892015-03-04 22:55:15 +0000506 ret_val =
507 video_->SendVideo(video_type, frame_type, payload_type,
508 capture_timestamp, capture_time_ms, payload_data,
mflodman0828a0c2015-03-31 15:29:23 +0200509 payload_size, fragmentation, codec_info, rtp_hdr);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000510 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000511
512 CriticalSectionScoped cs(statistics_crit_.get());
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000513 // Note: This is currently only counting for video.
514 if (frame_type == kVideoFrameKey) {
515 ++frame_counts_.key_frames;
516 } else if (frame_type == kVideoFrameDelta) {
517 ++frame_counts_.delta_frames;
518 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000519 if (frame_count_observer_) {
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000520 frame_count_observer_->FrameCountUpdated(frame_counts_, ssrc);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000521 }
522
523 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000524}
525
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000526size_t RTPSender::TrySendRedundantPayloads(size_t bytes_to_send) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000527 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000528 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000529 if ((rtx_ & kRtxRedundantPayloads) == 0)
530 return 0;
531 }
532
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000533 uint8_t buffer[IP_PACKET_SIZE];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000534 int bytes_left = static_cast<int>(bytes_to_send);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000535 while (bytes_left > 0) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000536 size_t length = bytes_left;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000537 int64_t capture_time_ms;
538 if (!packet_history_.GetBestFittingPacket(buffer, &length,
539 &capture_time_ms)) {
540 break;
541 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000542 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000543 break;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000544 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000545 RTPHeader rtp_header;
546 rtp_parser.Parse(rtp_header);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000547 bytes_left -= static_cast<int>(length - rtp_header.headerLength);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000548 }
549 return bytes_to_send - bytes_left;
550}
551
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000552size_t RTPSender::BuildPaddingPacket(uint8_t* packet, size_t header_length) {
553 size_t padding_bytes_in_packet = kMaxPaddingLength;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000554 packet[0] |= 0x20; // Set padding bit.
555 int32_t *data =
556 reinterpret_cast<int32_t *>(&(packet[header_length]));
557
558 // Fill data buffer with random data.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000559 for (size_t j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000560 data[j] = rand(); // NOLINT
561 }
562 // Set number of padding bytes in the last byte of the packet.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000563 packet[header_length + padding_bytes_in_packet - 1] =
564 static_cast<uint8_t>(padding_bytes_in_packet);
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000565 return padding_bytes_in_packet;
566}
567
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000568size_t RTPSender::TrySendPadData(size_t bytes) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000569 int64_t capture_time_ms;
570 uint32_t timestamp;
571 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000572 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000573 timestamp = timestamp_;
574 capture_time_ms = capture_time_ms_;
575 if (last_timestamp_time_ms_ > 0) {
576 timestamp +=
577 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
578 capture_time_ms +=
579 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
580 }
581 }
582 return SendPadData(timestamp, capture_time_ms, bytes);
583}
584
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000585size_t RTPSender::SendPadData(uint32_t timestamp,
586 int64_t capture_time_ms,
587 size_t bytes) {
588 size_t padding_bytes_in_packet = 0;
589 size_t bytes_sent = 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000590 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000591 // Always send full padding packets.
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000592 if (bytes < kMaxPaddingLength)
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000593 bytes = kMaxPaddingLength;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000594
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000595 uint32_t ssrc;
596 uint16_t sequence_number;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000597 int payload_type;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000598 bool over_rtx;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000599 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000600 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000601 // Only send padding packets following the last packet of a frame,
602 // indicated by the marker bit.
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000603 if (rtx_ == kRtxOff) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000604 // Without RTX we can't send padding in the middle of frames.
605 if (!last_packet_marker_bit_)
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000606 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000607 ssrc = ssrc_;
608 sequence_number = sequence_number_;
609 ++sequence_number_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000610 payload_type = payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000611 over_rtx = false;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000612 } else {
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000613 // Without abs-send-time a media packet must be sent before padding so
614 // that the timestamps used for estimation are correct.
615 if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
616 kRtpExtensionAbsoluteSendTime))
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000617 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000618 ssrc = ssrc_rtx_;
619 sequence_number = sequence_number_rtx_;
620 ++sequence_number_rtx_;
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +0000621 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_
622 : payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000623 over_rtx = true;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000624 }
625 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000626
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000627 uint8_t padding_packet[IP_PACKET_SIZE];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000628 size_t header_length =
629 CreateRtpHeader(padding_packet, payload_type, ssrc, false, timestamp,
630 sequence_number, std::vector<uint32_t>());
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000631 assert(header_length != static_cast<size_t>(-1));
632 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length);
633 assert(padding_bytes_in_packet <= bytes);
634 size_t length = padding_bytes_in_packet + header_length;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000635 int64_t now_ms = clock_->TimeInMilliseconds();
636
637 RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
638 RTPHeader rtp_header;
639 rtp_parser.Parse(rtp_header);
640
641 if (capture_time_ms > 0) {
642 UpdateTransmissionTimeOffset(
643 padding_packet, length, rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000644 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000645
646 UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
647 if (!SendPacketToNetwork(padding_packet, length))
648 break;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000649 bytes_sent += padding_bytes_in_packet;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000650 UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000651 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000652
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000653 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000654}
655
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000656void RTPSender::SetStorePacketsStatus(bool enable, uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000657 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000658}
659
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000660bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000661 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000662}
niklase@google.com470e71d2011-07-07 08:21:25 +0000663
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000664int32_t RTPSender::ReSendPacket(uint16_t packet_id, int64_t min_resend_time) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000665 size_t length = IP_PACKET_SIZE;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000666 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000667 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000668 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
669 data_buffer, &length,
670 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000671 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000672 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000673 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000674
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000675 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000676 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000677 RTPHeader header;
678 if (!rtp_parser.Parse(header)) {
679 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000680 return -1;
681 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000682 // Convert from TickTime to Clock since capture_time_ms is based on
683 // TickTime.
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000684 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_;
685 if (!paced_sender_->SendPacket(
686 PacedSender::kHighPriority, header.ssrc, header.sequenceNumber,
687 corrected_capture_tims_ms, length - header.headerLength, true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000688 // We can't send the packet right now.
689 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000690 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000691 }
692 }
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000693 int rtx = kRtxOff;
694 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000695 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000696 rtx = rtx_;
697 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000698 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000699 (rtx & kRtxRetransmitted) > 0, true) ?
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000700 static_cast<int32_t>(length) : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000701}
702
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000703bool RTPSender::SendPacketToNetwork(const uint8_t *packet, size_t size) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000704 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000705 if (transport_) {
706 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000707 }
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000708 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
709 "RTPSender::SendPacketToNetwork", "size", size, "sent",
710 bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000711 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000712 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000713 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000714 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000715 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000716 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000717}
718
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000719int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000720 if (!video_)
721 return -1;
722 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000723}
724
725int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000726 if (!video_)
727 return -1;
mflodman0828a0c2015-03-31 15:29:23 +0200728 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000729}
730
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000731void RTPSender::OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000732 int64_t avg_rtt) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000733 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
734 "RTPSender::OnReceivedNACK", "num_seqnum",
735 nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000736 const int64_t now = clock_->TimeInMilliseconds();
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000737 uint32_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000738 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000739
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000740 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000741 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000742 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000743 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000744 return;
745 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000746
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000747 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
748 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000749 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000750 if (bytes_sent > 0) {
751 bytes_re_sent += bytes_sent;
752 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000753 // The packet has previously been resent.
754 // Try resending next packet in the list.
755 continue;
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000756 } else {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000757 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000758 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
759 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000760 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000761 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000762 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000763 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000764 // kbits/s * ms = bits => bits/8 = bytes
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000765 size_t target_bytes =
766 (static_cast<size_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000767 if (bytes_re_sent > target_bytes) {
768 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000769 }
770 }
771 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000772 if (bytes_re_sent > 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000773 UpdateNACKBitRate(bytes_re_sent, now);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000774 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000775}
776
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000777bool RTPSender::ProcessNACKBitRate(uint32_t now) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000778 uint32_t num = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000779 size_t byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000780 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000781 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000782
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000783 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000784
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000785 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000786 return true;
787 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000788 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000789 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000790 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000791 break;
792 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000793 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000794 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000795 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000796 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000797 if (num == NACK_BYTECOUNT_SIZE) {
798 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000799 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000800 if (nack_byte_count_times_[num - 1] <= now) {
801 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000802 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000803 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000804 return (byte_count * 8) < (target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000805}
806
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000807void RTPSender::UpdateNACKBitRate(uint32_t bytes, int64_t now) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000808 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000809 if (bytes == 0)
810 return;
811 nack_bitrate_.Update(bytes);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000812 // Save bitrate statistics.
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000813 // Shift all but first time.
814 for (int i = NACK_BYTECOUNT_SIZE - 2; i >= 0; i--) {
815 nack_byte_count_[i + 1] = nack_byte_count_[i];
816 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000817 }
stefan@webrtc.org11d81762014-12-19 09:52:24 +0000818 nack_byte_count_[0] = bytes;
819 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000820}
821
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000822// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000823bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000824 int64_t capture_time_ms,
825 bool retransmission) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000826 size_t length = IP_PACKET_SIZE;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000827 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000828 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000829
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000830 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
831 0,
832 retransmission,
833 data_buffer,
834 &length,
835 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000836 // Packet cannot be found. Allow sending to continue.
837 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000838 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000839 if (!retransmission && capture_time_ms > 0) {
840 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
841 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000842 int rtx;
843 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000844 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000845 rtx = rtx_;
846 }
847 return PrepareAndSendPacket(data_buffer,
848 length,
849 capture_time_ms,
850 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000851 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000852}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000853
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000854bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000855 size_t length,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000856 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000857 bool send_over_rtx,
858 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000859 uint8_t *buffer_to_send_ptr = buffer;
860
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000861 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000862 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000863 rtp_parser.Parse(rtp_header);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000864 if (!is_retransmit && rtp_header.markerBit) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000865 TRACE_EVENT_ASYNC_END0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PacedSend",
866 capture_time_ms);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000867 }
868
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000869 TRACE_EVENT_INSTANT2(
870 TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PrepareAndSendPacket",
871 "timestamp", rtp_header.timestamp, "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000872
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000873 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000874 if (send_over_rtx) {
875 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000876 buffer_to_send_ptr = data_buffer_rtx;
877 }
878
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000879 int64_t now_ms = clock_->TimeInMilliseconds();
880 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000881 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
882 diff_ms);
883 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000884 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000885 if (ret) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000886 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000887 media_has_been_sent_ = true;
888 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000889 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
890 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000891 return ret;
892}
893
894void RTPSender::UpdateRtpStats(const uint8_t* buffer,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000895 size_t packet_length,
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000896 const RTPHeader& header,
897 bool is_rtx,
898 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000899 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000900 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000901 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000902
903 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000904 if (is_rtx) {
905 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000906 } else {
907 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000908 }
909
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000910 total_bitrate_sent_.Update(packet_length);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000911
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000912 if (counters->first_packet_time_ms == -1) {
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000913 counters->first_packet_time_ms = clock_->TimeInMilliseconds();
914 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000915 if (IsFecPacket(buffer, header)) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000916 counters->fec.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000917 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000918 if (is_retransmit) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000919 counters->retransmitted.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000920 }
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000921 counters->transmitted.AddPacket(packet_length, header);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000922
923 if (rtp_stats_callback_) {
924 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
925 }
926}
927
928bool RTPSender::IsFecPacket(const uint8_t* buffer,
929 const RTPHeader& header) const {
930 if (!video_) {
931 return false;
932 }
933 bool fec_enabled;
934 uint8_t pt_red;
935 uint8_t pt_fec;
936 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
937 return fec_enabled &&
938 header.payloadType == pt_red &&
939 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000940}
941
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000942size_t RTPSender::TimeToSendPadding(size_t bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000943 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +0000944 CriticalSectionScoped cs(send_critsect_.get());
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000945 if (!sending_media_) return 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000946 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000947 if (bytes == 0)
948 return 0;
949 size_t bytes_sent = TrySendRedundantPayloads(bytes);
950 if (bytes_sent < bytes)
951 bytes_sent += TrySendPadData(bytes - bytes_sent);
952 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000953}
954
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000955// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000956int32_t RTPSender::SendToNetwork(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000957 uint8_t *buffer, size_t payload_length, size_t rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000958 int64_t capture_time_ms, StorageType storage,
959 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000960 RtpUtility::RtpHeaderParser rtp_parser(buffer,
961 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000962 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000963 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000964
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000965 int64_t now_ms = clock_->TimeInMilliseconds();
966
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000967 // |capture_time_ms| <= 0 is considered invalid.
968 // TODO(holmer): This should be changed all over Video Engine so that negative
969 // time is consider invalid, while 0 is considered a valid time.
970 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000971 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000972 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000973 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000974
975 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
976 rtp_header, now_ms);
977
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000978 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000979 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
980 max_payload_length_, capture_time_ms,
981 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000982 return -1;
983 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000984
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000985 if (paced_sender_ && storage != kDontStore) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000986 // Correct offset between implementations of millisecond time stamps in
987 // TickTime and Clock.
988 int64_t corrected_time_ms = capture_time_ms + clock_delta_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000989 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000990 rtp_header.sequenceNumber, corrected_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000991 payload_length, false)) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000992 if (last_capture_time_ms_sent_ == 0 ||
993 corrected_time_ms > last_capture_time_ms_sent_) {
994 last_capture_time_ms_sent_ = corrected_time_ms;
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000995 TRACE_EVENT_ASYNC_BEGIN1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
996 "PacedSend", corrected_time_ms,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000997 "capture_time_ms", corrected_time_ms);
998 }
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000999 // We can't send the packet right now.
1000 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +00001001 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001002 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +00001003 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001004 if (capture_time_ms > 0) {
1005 UpdateDelayStatistics(capture_time_ms, now_ms);
1006 }
sprang@webrtc.org43c88392015-01-29 09:09:17 +00001007
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001008 size_t length = payload_length + rtp_header_length;
sprang@webrtc.orgc957ffc2015-02-02 13:08:02 +00001009 bool sent = SendPacketToNetwork(buffer, length);
1010
1011 if (storage != kDontStore) {
1012 // Mark the packet as sent in the history even if send failed. Dropping a
1013 // packet here should be treated as any other packet drop so we should be
1014 // ready for a retransmission.
1015 packet_history_.SetSent(rtp_header.sequenceNumber);
1016 }
1017 if (!sent)
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001018 return -1;
sprang@webrtc.orgc957ffc2015-02-02 13:08:02 +00001019
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001020 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001021 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001022 media_has_been_sent_ = true;
1023 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001024 UpdateRtpStats(buffer, length, rtp_header, false, false);
1025 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +00001026}
1027
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001028void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001029 uint32_t ssrc;
1030 int avg_delay_ms = 0;
1031 int max_delay_ms = 0;
1032 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001033 CriticalSectionScoped lock(send_critsect_.get());
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001034 ssrc = ssrc_;
1035 }
1036 {
1037 CriticalSectionScoped cs(statistics_crit_.get());
1038 // TODO(holmer): Compute this iteratively instead.
1039 send_delays_[now_ms] = now_ms - capture_time_ms;
1040 send_delays_.erase(send_delays_.begin(),
1041 send_delays_.lower_bound(now_ms -
1042 kSendSideDelayWindowMs));
1043 }
1044 if (send_side_delay_observer_ &&
1045 GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
1046 send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
1047 max_delay_ms, ssrc);
1048 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001049}
1050
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001051void RTPSender::ProcessBitrate() {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001052 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001053 total_bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001054 nack_bitrate_.Process();
1055 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001056 return;
1057 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001058 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +00001059}
1060
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001061size_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001062 CriticalSectionScoped lock(send_critsect_.get());
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001063 size_t rtp_header_length = kRtpHeaderLength;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001064 rtp_header_length += sizeof(uint32_t) * csrcs_.size();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001065 rtp_header_length += RtpHeaderExtensionTotalLength();
1066 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001067}
1068
mflodman0828a0c2015-03-31 15:29:23 +02001069uint16_t RTPSender::IncrementSequenceNumber() {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001070 CriticalSectionScoped cs(send_critsect_.get());
mflodman0828a0c2015-03-31 15:29:23 +02001071 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001072}
1073
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001074void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001075 uint32_t ssrc;
1076 uint32_t ssrc_rtx;
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001077 bool report_rtx;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001078 {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001079 CriticalSectionScoped ssrc_lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001080 ssrc = ssrc_;
1081 ssrc_rtx = ssrc_rtx_;
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001082 report_rtx = rtx_ != kRtxOff;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001083 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001084 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001085 rtp_stats_ = StreamDataCounters();
1086 rtx_rtp_stats_ = StreamDataCounters();
1087 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001088 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
pbos@webrtc.org49096de2015-02-24 22:37:52 +00001089 if (report_rtx)
1090 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001091 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001092}
1093
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001094void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1095 StreamDataCounters* rtx_stats) const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001096 CriticalSectionScoped lock(statistics_crit_.get());
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001097 *rtp_stats = rtp_stats_;
1098 *rtx_stats = rtx_rtp_stats_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001099}
1100
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001101size_t RTPSender::CreateRtpHeader(uint8_t* header,
1102 int8_t payload_type,
1103 uint32_t ssrc,
1104 bool marker_bit,
1105 uint32_t timestamp,
1106 uint16_t sequence_number,
1107 const std::vector<uint32_t>& csrcs) const {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001108 header[0] = 0x80; // version 2.
1109 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001110 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001111 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001112 }
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001113 ByteWriter<uint16_t>::WriteBigEndian(header + 2, sequence_number);
1114 ByteWriter<uint32_t>::WriteBigEndian(header + 4, timestamp);
1115 ByteWriter<uint32_t>::WriteBigEndian(header + 8, ssrc);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001116 int32_t rtp_header_length = kRtpHeaderLength;
niklase@google.com470e71d2011-07-07 08:21:25 +00001117
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001118 if (csrcs.size() > 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001119 uint8_t *ptr = &header[rtp_header_length];
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001120 for (size_t i = 0; i < csrcs.size(); ++i) {
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001121 ByteWriter<uint32_t>::WriteBigEndian(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001122 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001123 }
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001124 header[0] = (header[0] & 0xf0) | csrcs.size();
niklase@google.com470e71d2011-07-07 08:21:25 +00001125
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001126 // Update length of header.
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001127 rtp_header_length += sizeof(uint32_t) * csrcs.size();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001128 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001129
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001130 uint16_t len =
1131 BuildRTPHeaderExtension(header + rtp_header_length, marker_bit);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001132 if (len > 0) {
1133 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001134 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001135 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001136 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001137}
1138
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001139int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001140 int8_t payload_type,
1141 bool marker_bit,
1142 uint32_t capture_timestamp,
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001143 int64_t capture_time_ms,
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001144 bool timestamp_provided,
1145 bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001146 assert(payload_type >= 0);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001147 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001148
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001149 if (timestamp_provided) {
1150 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001151 } else {
1152 // Make a unique time stamp.
1153 // We can't inc by the actual time, since then we increase the risk of back
1154 // timing.
1155 timestamp_++;
1156 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001157 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001158 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001159 capture_time_ms_ = capture_time_ms;
1160 last_packet_marker_bit_ = marker_bit;
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001161 return CreateRtpHeader(data_buffer, payload_type, ssrc_, marker_bit,
1162 timestamp_, sequence_number, csrcs_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001163}
1164
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001165uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer,
1166 bool marker_bit) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001167 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001168 return 0;
1169 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001170 // RTP header extension, RFC 3550.
1171 // 0 1 2 3
1172 // 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
1173 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1174 // | defined by profile | length |
1175 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1176 // | header extension |
1177 // | .... |
1178 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001179 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001180 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001181
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001182 // Add extension ID (0xBEDE).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001183 ByteWriter<uint16_t>::WriteBigEndian(data_buffer,
1184 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001185
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001186 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001187 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001188
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001189 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001190 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001191 uint8_t block_length = 0;
sprang@webrtc.org30933902015-03-17 14:33:12 +00001192 uint8_t* extension_data = &data_buffer[kHeaderLength + total_block_length];
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001193 switch (type) {
1194 case kRtpExtensionTransmissionTimeOffset:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001195 block_length = BuildTransmissionTimeOffsetExtension(extension_data);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001196 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001197 case kRtpExtensionAudioLevel:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001198 block_length = BuildAudioLevelExtension(extension_data);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001199 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001200 case kRtpExtensionAbsoluteSendTime:
sprang@webrtc.org30933902015-03-17 14:33:12 +00001201 block_length = BuildAbsoluteSendTimeExtension(extension_data);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001202 break;
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001203 case kRtpExtensionVideoRotation:
Minyue31331cf2015-04-01 16:19:58 +02001204 if (marker_bit)
1205 block_length = BuildVideoRotationExtension(extension_data);
sprang@webrtc.org30933902015-03-17 14:33:12 +00001206 break;
1207 case kRtpExtensionTransportSequenceNumber:
1208 block_length = BuildTransportSequenceNumberExtension(extension_data);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001209 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001210 default:
1211 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001212 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001213 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001214 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001215 }
1216 if (total_block_length == 0) {
1217 // No extension added.
1218 return 0;
1219 }
sprang@webrtc.org30933902015-03-17 14:33:12 +00001220 // Add padding elements until we've filled a 32 bit block.
1221 size_t padding_bytes =
1222 RtpUtility::Word32Align(total_block_length) - total_block_length;
1223 if (padding_bytes > 0) {
1224 memset(&data_buffer[kHeaderLength + total_block_length], 0, padding_bytes);
1225 total_block_length += padding_bytes;
1226 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001227 // Set header length (in number of Word32, header excluded).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001228 ByteWriter<uint16_t>::WriteBigEndian(data_buffer + kPosLength,
1229 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001230 // Total added length.
1231 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001232}
1233
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001234uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1235 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001236 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1237 //
1238 // The transmission time is signaled to the receiver in-band using the
1239 // general mechanism for RTP header extensions [RFC5285]. The payload
1240 // of this extension (the transmitted value) is a 24-bit signed integer.
1241 // When added to the RTP timestamp of the packet, it represents the
1242 // "effective" RTP transmission time of the packet, on the RTP
1243 // timescale.
1244 //
1245 // The form of the transmission offset extension block:
1246 //
1247 // 0 1 2 3
1248 // 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
1249 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1250 // | ID | len=2 | transmission offset |
1251 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001252
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001253 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001254 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001255 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1256 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001257 // Not registered.
1258 return 0;
1259 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001260 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001261 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001262 data_buffer[pos++] = (id << 4) + len;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001263 ByteWriter<int32_t, 3>::WriteBigEndian(data_buffer + pos,
1264 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001265 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001266 assert(pos == kTransmissionTimeOffsetLength);
1267 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001268}
1269
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001270uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1271 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1272 //
1273 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1274 //
1275 // The form of the audio level extension block:
1276 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001277 // 0 1
1278 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1279 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1280 // | ID | len=0 |V| level |
1281 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001282 //
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001283
1284 // Get id defined by user.
1285 uint8_t id;
1286 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1287 // Not registered.
1288 return 0;
1289 }
1290 size_t pos = 0;
1291 const uint8_t len = 0;
1292 data_buffer[pos++] = (id << 4) + len;
1293 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001294 assert(pos == kAudioLevelLength);
1295 return kAudioLevelLength;
1296}
1297
1298uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001299 // Absolute send time in RTP streams.
1300 //
1301 // The absolute send time is signaled to the receiver in-band using the
1302 // general mechanism for RTP header extensions [RFC5285]. The payload
1303 // of this extension (the transmitted value) is a 24-bit unsigned integer
1304 // containing the sender's current time in seconds as a fixed point number
1305 // with 18 bits fractional part.
1306 //
1307 // The form of the absolute send time extension block:
1308 //
1309 // 0 1 2 3
1310 // 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
1311 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1312 // | ID | len=2 | absolute send time |
1313 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1314
1315 // Get id defined by user.
1316 uint8_t id;
1317 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1318 &id) != 0) {
1319 // Not registered.
1320 return 0;
1321 }
1322 size_t pos = 0;
1323 const uint8_t len = 2;
1324 data_buffer[pos++] = (id << 4) + len;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001325 ByteWriter<uint32_t, 3>::WriteBigEndian(data_buffer + pos,
1326 absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001327 pos += 3;
1328 assert(pos == kAbsoluteSendTimeLength);
1329 return kAbsoluteSendTimeLength;
1330}
1331
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001332uint8_t RTPSender::BuildVideoRotationExtension(uint8_t* data_buffer) const {
1333 // Coordination of Video Orientation in RTP streams.
1334 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001335 // Coordination of Video Orientation consists in signaling of the current
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001336 // orientation of the image captured on the sender side to the receiver for
1337 // appropriate rendering and displaying.
1338 //
sprang@webrtc.org30933902015-03-17 14:33:12 +00001339 // 0 1
1340 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
1341 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1342 // | ID | len=0 |0 0 0 0 C F R R|
1343 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001344 //
1345
1346 // Get id defined by user.
1347 uint8_t id;
1348 if (rtp_header_extension_map_.GetId(kRtpExtensionVideoRotation, &id) != 0) {
1349 // Not registered.
1350 return 0;
1351 }
1352 size_t pos = 0;
1353 const uint8_t len = 0;
1354 data_buffer[pos++] = (id << 4) + len;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +00001355 data_buffer[pos++] = ConvertVideoRotationToCVOByte(rotation_);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001356 assert(pos == kVideoRotationLength);
1357 return kVideoRotationLength;
1358}
1359
sprang@webrtc.org30933902015-03-17 14:33:12 +00001360uint8_t RTPSender::BuildTransportSequenceNumberExtension(
1361 uint8_t* data_buffer) const {
1362 // 0 1 2
1363 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
1364 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1365 // | ID | L=1 |transport wide sequence number |
1366 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1367
1368 // Get id defined by user.
1369 uint8_t id;
1370 if (rtp_header_extension_map_.GetId(kRtpExtensionTransportSequenceNumber,
1371 &id) != 0) {
1372 // Not registered.
1373 return 0;
1374 }
1375 size_t pos = 0;
1376 const uint8_t len = 1;
1377 data_buffer[pos++] = (id << 4) + len;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001378 ByteWriter<uint16_t>::WriteBigEndian(data_buffer + pos,
1379 transport_sequence_number_);
sprang@webrtc.org30933902015-03-17 14:33:12 +00001380 pos += 2;
1381 assert(pos == kTransportSequenceNumberLength);
1382 return kTransportSequenceNumberLength;
1383}
1384
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001385bool RTPSender::FindHeaderExtensionPosition(RTPExtensionType type,
1386 const uint8_t* rtp_packet,
1387 size_t rtp_packet_length,
1388 const RTPHeader& rtp_header,
1389 size_t* position) const {
1390 // Get length until start of header extension block.
1391 int extension_block_pos =
1392 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(type);
1393 if (extension_block_pos < 0) {
1394 LOG(LS_WARNING) << "Failed to find extension position for " << type
1395 << " as it is not registered.";
1396 return false;
1397 }
1398
1399 HeaderExtension header_extension(type);
1400
1401 size_t block_pos =
1402 kRtpHeaderLength + rtp_header.numCSRCs + extension_block_pos;
1403 if (rtp_packet_length < block_pos + header_extension.length ||
1404 rtp_header.headerLength < block_pos + header_extension.length) {
1405 LOG(LS_WARNING) << "Failed to find extension position for " << type
1406 << " as the length is invalid.";
1407 return false;
1408 }
1409
1410 // Verify that header contains extension.
1411 if (!((rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs] == 0xBE) &&
1412 (rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs + 1] == 0xDE))) {
1413 LOG(LS_WARNING) << "Failed to find extension position for " << type
1414 << "as hdr extension not found.";
1415 return false;
1416 }
1417
1418 *position = block_pos;
1419 return true;
1420}
1421
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001422void RTPSender::UpdateTransmissionTimeOffset(uint8_t* rtp_packet,
1423 size_t rtp_packet_length,
1424 const RTPHeader& rtp_header,
1425 int64_t time_diff_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001426 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001427 // Get id.
1428 uint8_t id = 0;
1429 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1430 &id) != 0) {
1431 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001432 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001433 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001434
1435 size_t block_pos = 0;
1436 if (!FindHeaderExtensionPosition(kRtpExtensionTransmissionTimeOffset,
1437 rtp_packet, rtp_packet_length, rtp_header,
1438 &block_pos)) {
1439 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001440 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001441 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001442
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001443 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001444 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001445 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001446 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001447 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001448 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001449 // Update transmission offset field (converting to a 90 kHz timestamp).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001450 ByteWriter<int32_t, 3>::WriteBigEndian(rtp_packet + block_pos + 1,
1451 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001452}
1453
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001454bool RTPSender::UpdateAudioLevel(uint8_t* rtp_packet,
1455 size_t rtp_packet_length,
1456 const RTPHeader& rtp_header,
1457 bool is_voiced,
1458 uint8_t dBov) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001459 CriticalSectionScoped cs(send_critsect_.get());
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001460
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001461 // Get id.
1462 uint8_t id = 0;
1463 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1464 // Not registered.
1465 return false;
1466 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001467
1468 size_t block_pos = 0;
1469 if (!FindHeaderExtensionPosition(kRtpExtensionAudioLevel, rtp_packet,
1470 rtp_packet_length, rtp_header, &block_pos)) {
1471 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001472 return false;
1473 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001474
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001475 // Verify first byte in block.
1476 const uint8_t first_block_byte = (id << 4) + 0;
1477 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001478 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001479 return false;
1480 }
1481 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1482 return true;
1483}
1484
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001485bool RTPSender::UpdateVideoRotation(uint8_t* rtp_packet,
1486 size_t rtp_packet_length,
1487 const RTPHeader& rtp_header,
1488 VideoRotation rotation) const {
1489 CriticalSectionScoped cs(send_critsect_.get());
1490
1491 // Get id.
1492 uint8_t id = 0;
1493 if (rtp_header_extension_map_.GetId(kRtpExtensionVideoRotation, &id) != 0) {
1494 // Not registered.
1495 return false;
1496 }
1497
1498 size_t block_pos = 0;
1499 if (!FindHeaderExtensionPosition(kRtpExtensionVideoRotation, rtp_packet,
1500 rtp_packet_length, rtp_header, &block_pos)) {
1501 LOG(LS_WARNING) << "Failed to update video rotation (CVO).";
1502 return false;
1503 }
1504 // Get length until start of header extension block.
1505 int extension_block_pos =
1506 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1507 kRtpExtensionVideoRotation);
1508 if (extension_block_pos < 0) {
1509 // The feature is not enabled.
1510 return false;
1511 }
1512
1513 // Verify first byte in block.
1514 const uint8_t first_block_byte = (id << 4) + 0;
1515 if (rtp_packet[block_pos] != first_block_byte) {
1516 LOG(LS_WARNING) << "Failed to update CVO.";
1517 return false;
1518 }
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +00001519 rtp_packet[block_pos + 1] = ConvertVideoRotationToCVOByte(rotation);
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001520 return true;
1521}
1522
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001523void RTPSender::UpdateAbsoluteSendTime(uint8_t* rtp_packet,
1524 size_t rtp_packet_length,
1525 const RTPHeader& rtp_header,
1526 int64_t now_ms) const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001527 CriticalSectionScoped cs(send_critsect_.get());
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001528
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001529 // Get id.
1530 uint8_t id = 0;
1531 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1532 &id) != 0) {
1533 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001534 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001535 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001536 // Get length until start of header extension block.
1537 int extension_block_pos =
1538 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1539 kRtpExtensionAbsoluteSendTime);
1540 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001541 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001542 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001543 }
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001544 size_t block_pos =
1545 kRtpHeaderLength + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001546 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001547 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001548 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001549 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001550 }
1551 // Verify that header contains extension.
guoweis@webrtc.org45362892015-03-04 22:55:15 +00001552 if (!((rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs] == 0xBE) &&
1553 (rtp_packet[kRtpHeaderLength + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001554 LOG(LS_WARNING)
1555 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001556 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001557 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001558 // Verify first byte in block.
1559 const uint8_t first_block_byte = (id << 4) + 2;
1560 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001561 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001562 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001563 }
1564 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1565 // fractional part).
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001566 ByteWriter<uint32_t, 3>::WriteBigEndian(rtp_packet + block_pos + 1,
1567 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001568}
1569
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001570void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001571 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001572 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001573 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001574
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001575 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001576 SetStartTimestamp(RTPtime, false);
1577 } else {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001578 CriticalSectionScoped lock(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001579 if (!ssrc_forced_) {
1580 // Generate a new SSRC.
1581 ssrc_db_.ReturnSSRC(ssrc_);
1582 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001583 bitrates_->set_ssrc(ssrc_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001584 }
1585 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001586 if (!sequence_number_forced_ && !ssrc_forced_) {
1587 // Generate a new sequence number.
1588 sequence_number_ =
1589 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001590 }
1591 }
1592}
1593
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001594void RTPSender::SetSendingMediaStatus(bool enabled) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001595 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001596 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001597}
1598
1599bool RTPSender::SendingMedia() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001600 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001601 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001602}
1603
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001604uint32_t RTPSender::Timestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001605 CriticalSectionScoped cs(send_critsect_.get());
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001606 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001607}
1608
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001609void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001610 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001611 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001612 start_timestamp_forced_ = true;
1613 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001614 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001615 if (!start_timestamp_forced_) {
1616 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001617 }
1618 }
1619}
1620
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001621uint32_t RTPSender::StartTimestamp() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001622 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001623 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001624}
1625
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001626uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001627 // If configured via API, return 0.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001628 CriticalSectionScoped cs(send_critsect_.get());
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001629
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001630 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001631 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001632 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001633 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001634 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001635 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001636}
1637
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001638void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001639 // This is configured via the API.
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001640 CriticalSectionScoped cs(send_critsect_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +00001641
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001642 if (ssrc_ == ssrc && ssrc_forced_) {
1643 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001644 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001645 ssrc_forced_ = true;
1646 ssrc_db_.ReturnSSRC(ssrc_);
1647 ssrc_db_.RegisterSSRC(ssrc);
1648 ssrc_ = ssrc;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001649 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001650 if (!sequence_number_forced_) {
1651 sequence_number_ =
1652 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001653 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001654}
1655
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001656uint32_t RTPSender::SSRC() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001657 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001658 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001659}
1660
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001661void RTPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
1662 assert(csrcs.size() <= kRtpCsrcSize);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001663 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001664 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001665}
1666
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001667void RTPSender::SetSequenceNumber(uint16_t seq) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001668 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001669 sequence_number_forced_ = true;
1670 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001671}
1672
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001673uint16_t RTPSender::SequenceNumber() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001674 CriticalSectionScoped cs(send_critsect_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001675 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001676}
1677
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001678// Audio.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001679int32_t RTPSender::SendTelephoneEvent(uint8_t key,
1680 uint16_t time_ms,
1681 uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001682 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001683 return -1;
1684 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001685 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001686}
1687
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001688int32_t RTPSender::SetAudioPacketSize(uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001689 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001690 return -1;
1691 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001692 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001693}
1694
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001695int32_t RTPSender::SetAudioLevel(uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001696 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001697}
1698
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001699int32_t RTPSender::SetRED(int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001700 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001701 return -1;
1702 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001703 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001704}
1705
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001706int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001707 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001708 return -1;
1709 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001710 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001711}
1712
mflodman0828a0c2015-03-31 15:29:23 +02001713// Video
1714VideoCodecInformation *RTPSender::CodecInformationVideo() {
1715 if (audio_configured_) {
1716 return NULL;
1717 }
1718 return video_->CodecInformationVideo();
1719}
1720
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001721RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001722 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001723 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001724}
1725
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001726uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001727 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001728 return 0;
1729 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001730 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001731}
1732
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001733int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001734 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001735 return -1;
1736 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001737 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001738}
1739
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001740int32_t RTPSender::SetGenericFECStatus(bool enable,
1741 uint8_t payload_type_red,
1742 uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001743 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001744 return -1;
1745 }
mflodman0828a0c2015-03-31 15:29:23 +02001746 return video_->SetGenericFECStatus(enable, payload_type_red,
1747 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001748}
1749
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00001750int32_t RTPSender::GenericFECStatus(bool* enable,
1751 uint8_t* payload_type_red,
1752 uint8_t* payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001753 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001754 return -1;
1755 }
mflodman0828a0c2015-03-31 15:29:23 +02001756 return video_->GenericFECStatus(
1757 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001758}
1759
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001760int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001761 const FecProtectionParams *delta_params,
1762 const FecProtectionParams *key_params) {
1763 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001764 return -1;
1765 }
mflodman0828a0c2015-03-31 15:29:23 +02001766 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001767}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001768
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001769void RTPSender::BuildRtxPacket(uint8_t* buffer, size_t* length,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001770 uint8_t* buffer_rtx) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001771 CriticalSectionScoped cs(send_critsect_.get());
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001772 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001773 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001774 RtpUtility::RtpHeaderParser rtp_parser(
1775 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001776
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001777 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001778 rtp_parser.Parse(rtp_header);
1779
1780 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001781 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001782
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00001783 // Replace payload type, if a specific type is set for RTX.
1784 if (payload_type_rtx_ != -1) {
1785 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001786 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001787 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1788 }
1789
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001790 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001791 uint8_t *ptr = data_buffer_rtx + 2;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001792 ByteWriter<uint16_t>::WriteBigEndian(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001793
1794 // Replace SSRC.
1795 ptr += 6;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001796 ByteWriter<uint32_t>::WriteBigEndian(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001797
1798 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001799 ptr = data_buffer_rtx + rtp_header.headerLength;
sprang@webrtc.org779c3d12015-03-17 16:42:49 +00001800 ByteWriter<uint16_t>::WriteBigEndian(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001801 ptr += 2;
1802
1803 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001804 memcpy(ptr, buffer + rtp_header.headerLength,
1805 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001806 *length += 2;
1807}
1808
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001809void RTPSender::RegisterRtpStatisticsCallback(
1810 StreamDataCountersCallback* callback) {
1811 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001812 rtp_stats_callback_ = callback;
1813}
1814
1815StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1816 CriticalSectionScoped cs(statistics_crit_.get());
1817 return rtp_stats_callback_;
1818}
1819
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001820uint32_t RTPSender::BitrateSent() const {
1821 return total_bitrate_sent_.BitrateLast();
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001822}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001823
1824void RTPSender::SetRtpState(const RtpState& rtp_state) {
1825 SetStartTimestamp(rtp_state.start_timestamp, true);
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001826 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001827 sequence_number_ = rtp_state.sequence_number;
1828 sequence_number_forced_ = true;
1829 timestamp_ = rtp_state.timestamp;
1830 capture_time_ms_ = rtp_state.capture_time_ms;
1831 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001832 media_has_been_sent_ = rtp_state.media_has_been_sent;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001833}
1834
1835RtpState RTPSender::GetRtpState() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001836 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001837
1838 RtpState state;
1839 state.sequence_number = sequence_number_;
1840 state.start_timestamp = start_timestamp_;
1841 state.timestamp = timestamp_;
1842 state.capture_time_ms = capture_time_ms_;
1843 state.last_timestamp_time_ms = last_timestamp_time_ms_;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001844 state.media_has_been_sent = media_has_been_sent_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001845
1846 return state;
1847}
1848
1849void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001850 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001851 sequence_number_rtx_ = rtp_state.sequence_number;
1852}
1853
1854RtpState RTPSender::GetRtxRtpState() const {
pbos@webrtc.org7c4d20f2015-02-12 12:20:08 +00001855 CriticalSectionScoped lock(send_critsect_.get());
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001856
1857 RtpState state;
1858 state.sequence_number = sequence_number_rtx_;
1859 state.start_timestamp = start_timestamp_;
1860
1861 return state;
1862}
1863
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001864} // namespace webrtc