blob: 787f8717f18066e6c0ec8fa3a2ce303c975f2f95 [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
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000015#include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
16#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
17#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
18#include "webrtc/system_wrappers/interface/trace.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000019#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000022
stefan@webrtc.orga8179622013-06-04 13:47:36 +000023// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
24const int kMaxPaddingLength = 224;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +000025const int kSendSideDelayWindowMs = 1000;
stefan@webrtc.orga8179622013-06-04 13:47:36 +000026
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000027namespace {
28
29const char* FrameTypeToString(const FrameType frame_type) {
30 switch (frame_type) {
31 case kFrameEmpty: return "empty";
32 case kAudioFrameSpeech: return "audio_speech";
33 case kAudioFrameCN: return "audio_cn";
34 case kVideoFrameKey: return "video_key";
35 case kVideoFrameDelta: return "video_delta";
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000036 }
37 return "";
38}
39
40} // namespace
41
pbos@webrtc.org2f446732013-04-08 11:08:41 +000042RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000043 Transport *transport, RtpAudioFeedback *audio_feedback,
44 PacedSender *paced_sender)
45 : Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL),
46 video_(NULL), paced_sender_(paced_sender),
47 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
48 transport_(transport), sending_media_(true), // Default to sending media.
49 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
50 target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1),
51 payload_type_map_(), rtp_header_extension_map_(),
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +000052 transmission_time_offset_(0), absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000053 // NACK.
54 nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000055 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000056 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +000057 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000058 packets_sent_(0), payload_bytes_sent_(0), start_time_stamp_forced_(false),
59 start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000060 remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false),
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +000061 timestamp_(0), capture_time_ms_(0), last_timestamp_time_ms_(0),
62 last_packet_marker_bit_(false), num_csrcs_(0), csrcs_(),
sprang@webrtc.org71f055f2013-12-04 15:09:27 +000063 include_csrcs_(true), rtx_(kRtxOff), payload_type_rtx_(-1),
64 frame_counts_(), frame_count_observer_(NULL) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000065 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
66 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +000067 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000068 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000069 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000070 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000071 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
72 // Random start, 16 bits. Can't be 0.
73 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
74 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +000075
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000076 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000077 audio_ = new RTPSenderAudio(id, clock_, this);
78 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000079 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000080 video_ = new RTPSenderVideo(id, clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000081 }
82 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
pwestin@webrtc.org00741872012-01-19 15:56:10 +000085RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000086 if (remote_ssrc_ != 0) {
87 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000088 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000089 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
pwestin@webrtc.org00741872012-01-19 15:56:10 +000091 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000092 delete send_critsect_;
93 while (!payload_type_map_.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000094 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000095 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +000096 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000097 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000098 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000099 delete audio_;
100 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000101
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000102 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000103}
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000105void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000106 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000108
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000109uint16_t RTPSender::ActualSendBitrateKbit() const {
110 return (uint16_t)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111}
112
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000113uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000114 if (video_) {
115 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000116 }
117 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000118}
119
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000120uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000121 if (video_) {
122 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000123 }
124 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000125}
126
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000127uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000128 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000129}
130
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000131bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
132 int* max_send_delay_ms) const {
133 CriticalSectionScoped cs(statistics_crit_.get());
134 SendDelayMap::const_iterator it = send_delays_.upper_bound(
135 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
136 if (!sending_media_ || it == send_delays_.end())
137 return false;
138 int num_delays = 0;
139 for (; it != send_delays_.end(); ++it) {
140 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
141 *avg_send_delay_ms += it->second;
142 ++num_delays;
143 }
144 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
145 return true;
146}
147
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000148int32_t RTPSender::SetTransmissionTimeOffset(
149 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000150 if (transmission_time_offset > (0x800000 - 1) ||
151 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000152 return -1;
153 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000154 CriticalSectionScoped cs(send_critsect_);
155 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000156 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000157}
158
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000159int32_t RTPSender::SetAbsoluteSendTime(
160 const uint32_t absolute_send_time) {
161 if (absolute_send_time > 0xffffff) { // UWord24.
162 return -1;
163 }
164 CriticalSectionScoped cs(send_critsect_);
165 absolute_send_time_ = absolute_send_time;
166 return 0;
167}
168
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000169int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
170 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000171 CriticalSectionScoped cs(send_critsect_);
172 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000173}
174
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000175int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000176 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000177 CriticalSectionScoped cs(send_critsect_);
178 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000179}
180
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000181uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000182 CriticalSectionScoped cs(send_critsect_);
183 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000184}
185
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000186int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000187 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000188 const int8_t payload_number, const uint32_t frequency,
189 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000190 assert(payload_name);
191 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000192
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000193 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000194 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000196 if (payload_type_map_.end() != it) {
197 // We already use this payload type.
198 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000199 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000200
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000201 // Check if it's the same as we already have.
202 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000203 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000204 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000205 payload->typeSpecific.Audio.frequency == frequency &&
206 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000207 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000208 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000209 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000210 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000211 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000212 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000213 return 0;
214 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000215 }
216 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000217 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000218 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000219 ModuleRTPUtility::Payload *payload = NULL;
220 if (audio_configured_) {
221 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
222 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000223 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000224 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
225 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000226 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000227 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000228 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000229 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000230 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000231}
232
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000233int32_t RTPSender::DeRegisterSendPayload(
234 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000235 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000236
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000237 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000238 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000239
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000240 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000241 return -1;
242 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000243 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000244 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000245 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000246 return 0;
247}
niklase@google.com470e71d2011-07-07 08:21:25 +0000248
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000249int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000250
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000251int RTPSender::SendPayloadFrequency() const {
252 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
253}
niklase@google.com470e71d2011-07-07 08:21:25 +0000254
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000255int32_t RTPSender::SetMaxPayloadLength(
256 const uint16_t max_payload_length,
257 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000258 // Sanity check.
259 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
260 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
261 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000262 return -1;
263 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000264 CriticalSectionScoped cs(send_critsect_);
265 max_payload_length_ = max_payload_length;
266 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000267
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000268 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
269 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000270 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000271}
272
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000273uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000274 if (audio_configured_) {
275 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000276 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000277 return max_payload_length_ - RTPHeaderLength() -
278 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
279 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000280 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000281}
282
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000283uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000284 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000285}
286
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000287uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000288
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000289void RTPSender::SetRTXStatus(int mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000290 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000291 rtx_ = mode;
292 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000293 if (set_ssrc) {
294 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000295 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000296 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000297 }
298 }
299}
300
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000301void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000302 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000303 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000304 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000305 *ssrc = ssrc_rtx_;
306 *payload_type = payload_type_rtx_;
307}
308
309
310void RTPSender::SetRtxPayloadType(int payload_type) {
311 CriticalSectionScoped cs(send_critsect_);
312 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000313}
314
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000315int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
316 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000317 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000318
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000319 if (payload_type < 0) {
320 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
321 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000322 return -1;
323 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000324 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000325 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000326 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000327 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000328 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000329 // And it's a match...
330 return 0;
331 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000332 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000333 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000334 if (payload_type_ == payload_type) {
335 if (!audio_configured_) {
336 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000337 }
338 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000339 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000340 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000341 payload_type_map_.find(payload_type);
342 if (it == payload_type_map_.end()) {
343 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
344 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000345 return -1;
346 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000347 payload_type_ = payload_type;
348 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000349 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000350 if (!payload->audio && !audio_configured_) {
351 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
352 *video_type = payload->typeSpecific.Video.videoCodecType;
353 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000354 }
355 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000356}
357
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000358int32_t RTPSender::SendOutgoingData(
359 const FrameType frame_type, const int8_t payload_type,
360 const uint32_t capture_timestamp, int64_t capture_time_ms,
361 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000362 const RTPFragmentationHeader *fragmentation,
363 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000364 {
365 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000366 CriticalSectionScoped cs(send_critsect_);
367 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000368 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000369 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000370 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000371 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000372 if (CheckPayloadType(payload_type, &video_type) != 0) {
373 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
374 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000375 __FUNCTION__, payload_type);
376 return -1;
377 }
378
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000379 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000380 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000381 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
382 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000383 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000384 frame_type == kFrameEmpty);
385
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000386 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
387 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000388 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000389 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
390 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000391 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000392
393 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000394 if (paced_sender_->Enabled()) {
395 // Padding is driven by the pacer and not by the encoder.
396 return 0;
397 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000398 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000399 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000400 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000401 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
402 capture_timestamp, capture_time_ms,
403 payload_data, payload_size,
404 fragmentation, codec_info,
405 rtp_type_hdr);
406
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000407 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000408
409 CriticalSectionScoped cs(statistics_crit_.get());
410 uint32_t frame_count = ++frame_counts_[frame_type];
411 if (frame_count_observer_) {
412 frame_count_observer_->FrameCountUpdated(frame_type,
413 frame_count,
414 ssrc_);
415 }
416
417 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000418}
419
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000420int RTPSender::SendRedundantPayloads(int payload_type, int bytes_to_send) {
421 if (!(rtx_ & kRtxRedundantPayloads))
422 return 0;
423 uint8_t buffer[IP_PACKET_SIZE];
424 int bytes_left = bytes_to_send;
425 while (bytes_left > 0) {
426 uint16_t length = bytes_left;
427 int64_t capture_time_ms;
428 if (!packet_history_.GetBestFittingPacket(buffer, &length,
429 &capture_time_ms)) {
430 break;
431 }
432 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true))
433 return -1;
434 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
435 RTPHeader rtp_header;
436 rtp_parser.Parse(rtp_header);
437 bytes_left -= length - rtp_header.headerLength;
438 }
439 return bytes_to_send - bytes_left;
440}
441
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000442bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000443 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000444 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000445 // Current bitrate since last estimate(1 second) averaged with the
446 // estimate since then, to get the most up to date bitrate.
447 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000448 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000449 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000450 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000451 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000452 int bytes = 0;
453 if (current_bitrate == 0) {
454 // Start up phase. Send one 33.3 ms batch to start with.
455 bytes = (bitrate_diff / 8) / 30;
456 } else {
457 bytes = (bitrate_diff / 8);
458 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000459 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000460 if (bytes > bytes_cap) {
461 bytes = bytes_cap;
462 }
463 }
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000464 uint32_t timestamp;
465 {
466 CriticalSectionScoped cs(send_critsect_);
467 // Add the random RTP timestamp offset and store the capture time for
468 // later calculation of the send time offset.
469 timestamp = start_time_stamp_ + capture_timestamp;
470 timestamp_ = timestamp;
471 capture_time_ms_ = capture_time_ms;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000472 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000473 }
474 int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
475 bytes, kDontRetransmit, false, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000476 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
477 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000478}
479
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000480int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
481 int32_t bytes) {
482 int padding_bytes_in_packet = kMaxPaddingLength;
483 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000484 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000485 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000486 packet[0] |= 0x20; // Set padding bit.
487 int32_t *data =
488 reinterpret_cast<int32_t *>(&(packet[header_length]));
489
490 // Fill data buffer with random data.
491 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
492 data[j] = rand(); // NOLINT
493 }
494 // Set number of padding bytes in the last byte of the packet.
495 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
496 return padding_bytes_in_packet;
497}
498
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000499int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
500 int64_t capture_time_ms, int32_t bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000501 StorageType store, bool force_full_size_packets,
502 bool only_pad_after_markerbit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000503 // Drop this packet if we're not sending media packets.
504 if (!sending_media_) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000505 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000506 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000507 int padding_bytes_in_packet = 0;
508 int bytes_sent = 0;
509 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000510 // Always send full padding packets.
511 if (force_full_size_packets && bytes < kMaxPaddingLength)
512 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000513 if (bytes < kMaxPaddingLength) {
514 if (force_full_size_packets) {
515 bytes = kMaxPaddingLength;
516 } else {
517 // Round to the nearest multiple of 32.
518 bytes = (bytes + 16) & 0xffe0;
519 }
520 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000521 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000522 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000523 break;
524 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000525 uint32_t ssrc;
526 uint16_t sequence_number;
527 {
528 CriticalSectionScoped cs(send_critsect_);
529 // Only send padding packets following the last packet of a frame,
530 // indicated by the marker bit.
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000531 if (only_pad_after_markerbit && !last_packet_marker_bit_)
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000532 return bytes_sent;
533 if (rtx_ == kRtxOff) {
534 ssrc = ssrc_;
535 sequence_number = sequence_number_;
536 ++sequence_number_;
537 } else {
538 ssrc = ssrc_rtx_;
539 sequence_number = sequence_number_rtx_;
540 ++sequence_number_rtx_;
541 }
542 }
543 uint8_t padding_packet[IP_PACKET_SIZE];
544 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
545 false, timestamp, sequence_number, NULL,
546 0);
547 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
548 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000549 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
550 header_length, capture_time_ms, store,
551 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000552 // Error sending the packet.
553 break;
554 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000555 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000556 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000557 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000558}
559
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000560void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000561 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000562 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000563}
564
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000565bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000566 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000567}
niklase@google.com470e71d2011-07-07 08:21:25 +0000568
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000569int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
570 uint16_t length = IP_PACKET_SIZE;
571 uint8_t data_buffer[IP_PACKET_SIZE];
572 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000573 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000574 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
575 data_buffer, &length,
576 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000577 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000578 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000579 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000580
581 {
582 // Update send statistics prior to pacer.
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000583 CriticalSectionScoped lock(statistics_crit_.get());
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000584 Bitrate::Update(length);
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000585 ++packets_sent_;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000586 // We on purpose don't add to payload_bytes_sent_ since this is a
587 // re-transmit and not new payload data.
588 }
589
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000590
591 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
592 RTPHeader header;
stefan@webrtc.org79b63202013-12-04 13:34:28 +0000593 if (!rtp_parser.Parse(header)) {
594 assert(false);
595 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
596 "Failed to parse RTP header of packet to be retransmitted.");
597 return -1;
598 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000599 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000600 "timestamp", header.timestamp,
601 "seqnum", header.sequenceNumber);
602
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000603 if (paced_sender_) {
604 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000605 header.ssrc,
606 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000607 capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000608 length - header.headerLength,
609 true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000610 // We can't send the packet right now.
611 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000612 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000613 }
614 }
615
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000616 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000617 if ((rtx_ & kRtxRetransmitted) > 0) {
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000618 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
619 buffer_to_send_ptr = data_buffer_rtx;
620 }
621
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000622 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000623 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000624 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000625 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000626}
627
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000628bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
629 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000630 if (transport_) {
631 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000632 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000633 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
634 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000635 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000636 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000637 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
638 "Transport failed to send packet");
639 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000640 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000641 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000642}
643
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000644int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000645 if (!video_)
646 return -1;
647 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000648}
649
650int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000651 if (!video_)
652 return -1;
653 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000654}
655
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000656void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000657 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000658 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000659 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
660 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000661 const int64_t now = clock_->TimeInMilliseconds();
662 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000663
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000664 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000665 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000666 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000667 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000668 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000669 return;
670 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000671
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000672 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
673 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000674 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000675 if (bytes_sent > 0) {
676 bytes_re_sent += bytes_sent;
677 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000678 // The packet has previously been resent.
679 // Try resending next packet in the list.
680 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000681 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000682 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000683 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000684 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000685 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000686 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000687 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000688 // Delay bandwidth estimate (RTT * BW).
689 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000690 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000691 uint32_t target_bytes =
692 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000693 if (bytes_re_sent > target_bytes) {
694 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000695 }
696 }
697 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000698 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000699 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000700 UpdateNACKBitRate(bytes_re_sent, now);
701 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000702 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000703}
704
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000705bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
706 uint32_t num = 0;
707 int32_t byte_count = 0;
708 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000709
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000710 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000711
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000712 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000713 return true;
714 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000715 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
716 if ((now - nack_byte_count_times_[num]) > avg_interval) {
717 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000718 break;
719 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000720 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000721 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000722 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000723 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000724 if (num == NACK_BYTECOUNT_SIZE) {
725 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000726 // during the last msg_interval.
727 time_interval = now - nack_byte_count_times_[num - 1];
728 if (time_interval < 0) {
729 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000730 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000731 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000732 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000733}
734
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000735void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
736 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000737 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000738
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000739 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000740 if (bytes > 0) {
741 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000742 // Add padding length.
743 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000744 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000745 if (nack_byte_count_times_[0] == 0) {
746 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000747 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000748 // Shift.
749 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
750 nack_byte_count_[i + 1] = nack_byte_count_[i];
751 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000752 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000753 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000754 nack_byte_count_[0] = bytes;
755 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000756 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000757 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000758}
759
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000760// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000761bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000762 int64_t capture_time_ms,
763 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000764 uint16_t length = IP_PACKET_SIZE;
765 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000766 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000767
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000768 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
769 0,
770 retransmission,
771 data_buffer,
772 &length,
773 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000774 // Packet cannot be found. Allow sending to continue.
775 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000776 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000777 if (!retransmission && capture_time_ms > 0) {
778 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
779 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000780 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
781 retransmission && (rtx_ & kRtxRetransmitted) > 0);
782}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000783
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000784bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
785 uint16_t length,
786 int64_t capture_time_ms,
787 bool send_over_rtx) {
788 uint8_t *buffer_to_send_ptr = buffer;
789
790 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000791 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000792 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000793 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000794 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000795 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000796
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000797 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000798 if (send_over_rtx) {
799 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000800 buffer_to_send_ptr = data_buffer_rtx;
801 }
802
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000803 int64_t now_ms = clock_->TimeInMilliseconds();
804 int64_t diff_ms = now_ms - capture_time_ms;
805 bool updated_transmission_time_offset =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000806 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
807 diff_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000808 bool updated_abs_send_time =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000809 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000810 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000811 // Update stored packet in case of receiving a re-transmission request.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000812 packet_history_.ReplaceRTPHeader(buffer_to_send_ptr,
813 rtp_header.sequenceNumber,
814 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000815 }
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000816 return SendPacketToNetwork(buffer_to_send_ptr, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000817}
818
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000819int RTPSender::TimeToSendPadding(int bytes) {
820 if (!sending_media_) {
821 return 0;
822 }
823 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000824 int64_t capture_time_ms;
825 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000826 {
827 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000828 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_ :
829 payload_type_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000830 timestamp = timestamp_;
831 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000832 if (last_timestamp_time_ms_ > 0) {
833 timestamp +=
834 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
835 capture_time_ms +=
836 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
837 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000838 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000839 int bytes_sent = SendRedundantPayloads(payload_type, bytes);
840 bytes -= bytes_sent;
841 if (bytes > 0) {
842 int padding_sent = SendPadData(payload_type, timestamp, capture_time_ms,
843 bytes, kDontStore, true, true);
844 bytes_sent += padding_sent;
845 }
846 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000847}
848
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000849// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000850int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000851 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000852 int64_t capture_time_ms, StorageType storage,
853 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000854 ModuleRTPUtility::RTPHeaderParser rtp_parser(
855 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000856 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000857 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000858
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000859 int64_t now_ms = clock_->TimeInMilliseconds();
860
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000861 // |capture_time_ms| <= 0 is considered invalid.
862 // TODO(holmer): This should be changed all over Video Engine so that negative
863 // time is consider invalid, while 0 is considered a valid time.
864 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000865 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000866 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000867 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000868
869 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
870 rtp_header, now_ms);
871
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000872 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000873 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
874 max_payload_length_, capture_time_ms,
875 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000876 return -1;
877 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000878 {
879 // Update send statistics prior to pacer.
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000880 CriticalSectionScoped lock(statistics_crit_.get());
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000881 Bitrate::Update(payload_length + rtp_header_length);
882 ++packets_sent_;
883 payload_bytes_sent_ += payload_length;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000884 }
885
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000886 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000887 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
888 rtp_header.sequenceNumber, capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000889 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000890 // We can't send the packet right now.
891 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000892 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000893 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000894 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000895 if (capture_time_ms > 0) {
896 UpdateDelayStatistics(capture_time_ms, now_ms);
897 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000898 if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
899 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000900 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000901 return -1;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000902}
903
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000904void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
905 CriticalSectionScoped cs(statistics_crit_.get());
906 send_delays_[now_ms] = now_ms - capture_time_ms;
907 send_delays_.erase(send_delays_.begin(),
908 send_delays_.lower_bound(now_ms - kSendSideDelayWindowMs));
909}
910
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000911void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000912 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000913 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000914 nack_bitrate_.Process();
915 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000916 return;
917 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000918 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000919}
920
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000921uint16_t RTPSender::RTPHeaderLength() const {
922 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000923 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000924 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000925 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000926 rtp_header_length += RtpHeaderExtensionTotalLength();
927 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000928}
929
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000930uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000931 CriticalSectionScoped cs(send_critsect_);
932 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000933}
934
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000935void RTPSender::ResetDataCounters() {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000936 CriticalSectionScoped lock(statistics_crit_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000937 packets_sent_ = 0;
938 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000939}
940
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000941uint32_t RTPSender::Packets() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000942 CriticalSectionScoped lock(statistics_crit_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000943 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000944}
945
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000946// Number of sent RTP bytes.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000947uint32_t RTPSender::Bytes() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000948 CriticalSectionScoped lock(statistics_crit_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000949 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000950}
951
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000952int RTPSender::CreateRTPHeader(
953 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
954 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
955 uint8_t num_csrcs) const {
956 header[0] = 0x80; // version 2.
957 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000958 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000959 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000960 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000961 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
962 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
963 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000964 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000965
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000966 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000967 if (num_csrcs > 0) {
968 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000969 // error
970 assert(false);
971 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000972 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000973 uint8_t *ptr = &header[rtp_header_length];
974 for (int i = 0; i < num_csrcs; ++i) {
975 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000976 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000977 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000978 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000979
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000980 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000981 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000982 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000983
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000984 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
985 if (len > 0) {
986 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000987 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000988 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000989 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000990}
991
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000992int32_t RTPSender::BuildRTPheader(
993 uint8_t *data_buffer, const int8_t payload_type,
994 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000995 int64_t capture_time_ms, const bool time_stamp_provided,
996 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000997 assert(payload_type >= 0);
998 CriticalSectionScoped cs(send_critsect_);
999
1000 if (time_stamp_provided) {
1001 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001002 } else {
1003 // Make a unique time stamp.
1004 // We can't inc by the actual time, since then we increase the risk of back
1005 // timing.
1006 timestamp_++;
1007 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001008 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001009 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001010 capture_time_ms_ = capture_time_ms;
1011 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001012 int csrcs_length = 0;
1013 if (include_csrcs_)
1014 csrcs_length = num_csrcs_;
1015 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1016 timestamp_, sequence_number, csrcs_, csrcs_length);
1017}
1018
1019uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001020 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001021 return 0;
1022 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001023 // RTP header extension, RFC 3550.
1024 // 0 1 2 3
1025 // 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
1026 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1027 // | defined by profile | length |
1028 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1029 // | header extension |
1030 // | .... |
1031 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001032 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001033 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001034
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001035 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001036 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001037 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001038
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001039 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001040 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001041
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001042 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001043 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001044 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001045 switch (type) {
1046 case kRtpExtensionTransmissionTimeOffset:
1047 block_length = BuildTransmissionTimeOffsetExtension(
1048 data_buffer + kHeaderLength + total_block_length);
1049 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001050 case kRtpExtensionAudioLevel:
1051 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
1052 // we don't have to care about it here, which is true until we wan't to
1053 // use it together with any of the other extensions we support.
1054 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001055 case kRtpExtensionAbsoluteSendTime:
1056 block_length = BuildAbsoluteSendTimeExtension(
1057 data_buffer + kHeaderLength + total_block_length);
1058 break;
1059 default:
1060 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001061 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001062 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001063 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001064 }
1065 if (total_block_length == 0) {
1066 // No extension added.
1067 return 0;
1068 }
1069 // Set header length (in number of Word32, header excluded).
1070 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001071 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001072 total_block_length / 4);
1073 // Total added length.
1074 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001075}
1076
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001077uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1078 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001079 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1080 //
1081 // The transmission time is signaled to the receiver in-band using the
1082 // general mechanism for RTP header extensions [RFC5285]. The payload
1083 // of this extension (the transmitted value) is a 24-bit signed integer.
1084 // When added to the RTP timestamp of the packet, it represents the
1085 // "effective" RTP transmission time of the packet, on the RTP
1086 // timescale.
1087 //
1088 // The form of the transmission offset extension block:
1089 //
1090 // 0 1 2 3
1091 // 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
1092 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1093 // | ID | len=2 | transmission offset |
1094 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001095
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001096 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001097 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001098 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1099 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001100 // Not registered.
1101 return 0;
1102 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001103 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001104 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001105 data_buffer[pos++] = (id << 4) + len;
1106 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1107 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001108 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001109 assert(pos == kTransmissionTimeOffsetLength);
1110 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001111}
1112
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001113uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
1114 uint8_t* data_buffer) const {
1115 // Absolute send time in RTP streams.
1116 //
1117 // The absolute send time is signaled to the receiver in-band using the
1118 // general mechanism for RTP header extensions [RFC5285]. The payload
1119 // of this extension (the transmitted value) is a 24-bit unsigned integer
1120 // containing the sender's current time in seconds as a fixed point number
1121 // with 18 bits fractional part.
1122 //
1123 // The form of the absolute send time extension block:
1124 //
1125 // 0 1 2 3
1126 // 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
1127 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1128 // | ID | len=2 | absolute send time |
1129 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1130
1131 // Get id defined by user.
1132 uint8_t id;
1133 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1134 &id) != 0) {
1135 // Not registered.
1136 return 0;
1137 }
1138 size_t pos = 0;
1139 const uint8_t len = 2;
1140 data_buffer[pos++] = (id << 4) + len;
1141 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1142 absolute_send_time_);
1143 pos += 3;
1144 assert(pos == kAbsoluteSendTimeLength);
1145 return kAbsoluteSendTimeLength;
1146}
1147
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001148bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001149 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001150 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001151 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001152
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001153 // Get length until start of header extension block.
1154 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001155 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001156 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001157 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001158 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001159 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001160 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001161 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001162 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001163 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001164 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001165 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001166 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001167 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001168 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001169 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001170 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001171 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1172 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001173 WEBRTC_TRACE(
1174 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001175 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001176 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001177 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001178 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001179 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001180 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1181 &id) != 0) {
1182 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001183 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001184 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001185 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001186 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001187 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001188 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001189 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001190 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001191 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001192 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001193 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001194 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001195 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001196 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001197}
1198
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001199bool RTPSender::UpdateAbsoluteSendTime(
1200 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001201 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001202 CriticalSectionScoped cs(send_critsect_);
1203
1204 // Get length until start of header extension block.
1205 int extension_block_pos =
1206 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1207 kRtpExtensionAbsoluteSendTime);
1208 if (extension_block_pos < 0) {
1209 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1210 "Failed to update absolute send time, not registered.");
1211 return false;
1212 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001213 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001214 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001215 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001216 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1217 "Failed to update absolute send time, invalid length.");
1218 return false;
1219 }
1220 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001221 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1222 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001223 WEBRTC_TRACE(
1224 kTraceStream, kTraceRtpRtcp, id_,
1225 "Failed to update absolute send time, hdr extension not found.");
1226 return false;
1227 }
1228 // Get id.
1229 uint8_t id = 0;
1230 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1231 &id) != 0) {
1232 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1233 "Failed to update absolute send time, no id.");
1234 return false;
1235 }
1236 // Verify first byte in block.
1237 const uint8_t first_block_byte = (id << 4) + 2;
1238 if (rtp_packet[block_pos] != first_block_byte) {
1239 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1240 "Failed to update absolute send time.");
1241 return false;
1242 }
1243 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1244 // fractional part).
1245 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1246 ((now_ms << 18) / 1000) & 0x00ffffff);
1247 return true;
1248}
1249
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001250void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001251 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001252 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001253 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001254
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001255 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001256 SetStartTimestamp(RTPtime, false);
1257 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001258 if (!ssrc_forced_) {
1259 // Generate a new SSRC.
1260 ssrc_db_.ReturnSSRC(ssrc_);
1261 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001262 }
1263 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001264 if (!sequence_number_forced_ && !ssrc_forced_) {
1265 // Generate a new sequence number.
1266 sequence_number_ =
1267 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001268 }
1269 }
1270}
1271
1272void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001273 CriticalSectionScoped cs(send_critsect_);
1274 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001275}
1276
1277bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001278 CriticalSectionScoped cs(send_critsect_);
1279 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001280}
1281
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001282uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001283 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001284 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001285}
1286
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001287void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001288 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001289 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001290 start_time_stamp_forced_ = force;
1291 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001292 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001293 if (!start_time_stamp_forced_) {
1294 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001295 }
1296 }
1297}
1298
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001299uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001300 CriticalSectionScoped cs(send_critsect_);
1301 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001302}
1303
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001304uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001305 // If configured via API, return 0.
1306 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001307
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001308 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001309 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001310 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001311 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1312 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001313}
1314
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001315void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001316 // This is configured via the API.
1317 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001318
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001319 if (ssrc_ == ssrc && ssrc_forced_) {
1320 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001321 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001322 ssrc_forced_ = true;
1323 ssrc_db_.ReturnSSRC(ssrc_);
1324 ssrc_db_.RegisterSSRC(ssrc);
1325 ssrc_ = ssrc;
1326 if (!sequence_number_forced_) {
1327 sequence_number_ =
1328 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001329 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001330}
1331
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001332uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001333 CriticalSectionScoped cs(send_critsect_);
1334 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001335}
1336
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001337void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001338 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001339}
1340
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001341void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1342 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001343 assert(arr_length <= kRtpCsrcSize);
1344 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001345
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001346 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001347 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001348 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001349 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001350}
1351
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001352int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001353 assert(arr_of_csrc);
1354 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001355 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1356 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001357 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001358 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001359}
1360
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001361void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001362 CriticalSectionScoped cs(send_critsect_);
1363 sequence_number_forced_ = true;
1364 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001365}
1366
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001367uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001368 CriticalSectionScoped cs(send_critsect_);
1369 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001370}
1371
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001372// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001373int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1374 const uint16_t time_ms,
1375 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001376 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001377 return -1;
1378 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001379 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001380}
1381
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001382bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001383 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001384 return false;
1385 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001386 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001387}
1388
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001389int32_t RTPSender::SetAudioPacketSize(
1390 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001391 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001392 return -1;
1393 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001394 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001395}
1396
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001397int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1398 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001399 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001400 return -1;
1401 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001402 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001403}
1404
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001405int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1406 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001407 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001408}
1409
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001410int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001411 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001412}
1413
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001414int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001415 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001416 return -1;
1417 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001418 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001419}
1420
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001421int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001422 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001423 return -1;
1424 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001425 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001426}
1427
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001428// Video
1429VideoCodecInformation *RTPSender::CodecInformationVideo() {
1430 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001431 return NULL;
1432 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001433 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001434}
1435
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001436RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001437 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001438 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001439}
1440
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001441uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001442 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001443 return 0;
1444 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001445 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001446}
1447
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001448int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001449 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001450 return -1;
1451 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001452 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001453}
1454
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001455int32_t RTPSender::SetGenericFECStatus(
1456 const bool enable, const uint8_t payload_type_red,
1457 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001458 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001459 return -1;
1460 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001461 return video_->SetGenericFECStatus(enable, payload_type_red,
1462 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001463}
1464
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001465int32_t RTPSender::GenericFECStatus(
1466 bool *enable, uint8_t *payload_type_red,
1467 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001468 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001469 return -1;
1470 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001471 return video_->GenericFECStatus(
1472 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001473}
1474
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001475int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001476 const FecProtectionParams *delta_params,
1477 const FecProtectionParams *key_params) {
1478 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001479 return -1;
1480 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001481 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001482}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001483
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001484void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1485 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001486 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001487 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001488 // Add RTX header.
1489 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001490 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001491
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001492 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001493 rtp_parser.Parse(rtp_header);
1494
1495 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001496 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001497
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001498 // Replace payload type, if a specific type is set for RTX.
1499 if (payload_type_rtx_ != -1) {
1500 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001501 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001502 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1503 }
1504
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001505 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001506 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001507 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1508
1509 // Replace SSRC.
1510 ptr += 6;
1511 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1512
1513 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001514 ptr = data_buffer_rtx + rtp_header.headerLength;
1515 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001516 ptr += 2;
1517
1518 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001519 memcpy(ptr, buffer + rtp_header.headerLength,
1520 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001521 *length += 2;
1522}
1523
sprang@webrtc.org71f055f2013-12-04 15:09:27 +00001524void RTPSender::RegisterFrameCountObserver(FrameCountObserver* observer) {
1525 CriticalSectionScoped cs(statistics_crit_.get());
1526 if (observer != NULL)
1527 assert(frame_count_observer_ == NULL);
1528 frame_count_observer_ = observer;
1529}
1530
1531FrameCountObserver* RTPSender::GetFrameCountObserver() const {
1532 CriticalSectionScoped cs(statistics_crit_.get());
1533 return frame_count_observer_;
1534}
1535
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001536} // namespace webrtc