blob: cb2df9f10234274342a19fe3670d9f0a7b281bda [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
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000013#include <cstdlib> // srand
niklase@google.com470e71d2011-07-07 08:21:25 +000014
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000015#include "webrtc/modules/pacing/include/paced_sender.h"
16#include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
17#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"
20#include "webrtc/system_wrappers/interface/trace.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000021#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
stefan@webrtc.orga8179622013-06-04 13:47:36 +000025// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
26const int kMaxPaddingLength = 224;
27
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000028namespace {
29
30const char* FrameTypeToString(const FrameType frame_type) {
31 switch (frame_type) {
32 case kFrameEmpty: return "empty";
33 case kAudioFrameSpeech: return "audio_speech";
34 case kAudioFrameCN: return "audio_cn";
35 case kVideoFrameKey: return "video_key";
36 case kVideoFrameDelta: return "video_delta";
37 case kVideoFrameGolden: return "video_golden";
38 case kVideoFrameAltRef: return "video_altref";
39 }
40 return "";
41}
42
43} // namespace
44
pbos@webrtc.org2f446732013-04-08 11:08:41 +000045RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000046 Transport *transport, RtpAudioFeedback *audio_feedback,
47 PacedSender *paced_sender)
48 : Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL),
49 video_(NULL), paced_sender_(paced_sender),
50 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
51 transport_(transport), sending_media_(true), // Default to sending media.
52 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
53 target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1),
54 payload_type_map_(), rtp_header_extension_map_(),
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +000055 transmission_time_offset_(0), absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000056 // NACK.
57 nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
58 packet_history_(new RTPPacketHistory(clock)),
59 // Statistics
60 packets_sent_(0), payload_bytes_sent_(0), start_time_stamp_forced_(false),
61 start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000062 remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false),
stefan@webrtc.orga8179622013-06-04 13:47:36 +000063 timestamp_(0), num_csrcs_(0), csrcs_(), include_csrcs_(true),
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +000064 rtx_(kRtxOff), payload_type_rtx_(-1) {
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 packet_history_;
100 delete audio_;
101 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000102
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000103 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104}
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000106void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000107 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000108}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000109
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000110uint16_t RTPSender::ActualSendBitrateKbit() const {
111 return (uint16_t)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000112}
113
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000114uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000115 if (video_) {
116 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000117 }
118 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000119}
120
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000121uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000122 if (video_) {
123 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000124 }
125 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000126}
127
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000128uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000129 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000130}
131
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000132int32_t RTPSender::SetTransmissionTimeOffset(
133 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000134 if (transmission_time_offset > (0x800000 - 1) ||
135 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000136 return -1;
137 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000138 CriticalSectionScoped cs(send_critsect_);
139 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000140 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000141}
142
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000143int32_t RTPSender::SetAbsoluteSendTime(
144 const uint32_t absolute_send_time) {
145 if (absolute_send_time > 0xffffff) { // UWord24.
146 return -1;
147 }
148 CriticalSectionScoped cs(send_critsect_);
149 absolute_send_time_ = absolute_send_time;
150 return 0;
151}
152
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000153int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
154 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000155 CriticalSectionScoped cs(send_critsect_);
156 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000157}
158
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000159int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000160 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000161 CriticalSectionScoped cs(send_critsect_);
162 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000163}
164
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000165uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000166 CriticalSectionScoped cs(send_critsect_);
167 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000168}
169
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000170int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000171 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000172 const int8_t payload_number, const uint32_t frequency,
173 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000174 assert(payload_name);
175 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000177 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000178 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000180 if (payload_type_map_.end() != it) {
181 // We already use this payload type.
182 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000183 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000185 // Check if it's the same as we already have.
186 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000187 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000188 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000189 payload->typeSpecific.Audio.frequency == frequency &&
190 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000191 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000192 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000193 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000194 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000195 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000196 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000197 return 0;
198 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000199 }
200 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000201 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000202 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000203 ModuleRTPUtility::Payload *payload = NULL;
204 if (audio_configured_) {
205 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
206 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000207 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000208 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
209 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000210 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000211 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000212 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000213 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000214 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215}
216
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000217int32_t RTPSender::DeRegisterSendPayload(
218 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000219 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000220
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000221 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000222 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000223
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000224 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000225 return -1;
226 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000227 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000228 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000229 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000230 return 0;
231}
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000233int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000234
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000235int RTPSender::SendPayloadFrequency() const { return audio_->AudioFrequency(); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000237int32_t RTPSender::SetMaxPayloadLength(
238 const uint16_t max_payload_length,
239 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000240 // Sanity check.
241 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
242 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
243 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000244 return -1;
245 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000246 CriticalSectionScoped cs(send_critsect_);
247 max_payload_length_ = max_payload_length;
248 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000249
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000250 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
251 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000252 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000253}
254
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000255uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000256 if (audio_configured_) {
257 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000258 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000259 return max_payload_length_ - RTPHeaderLength() -
260 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
261 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000262 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000263}
264
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000265uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000266 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000267}
268
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000269uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000270
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000271void RTPSender::SetRTXStatus(RtxMode mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000272 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000273 rtx_ = mode;
274 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000275 if (set_ssrc) {
276 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000277 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000278 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000279 }
280 }
281}
282
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000283void RTPSender::RTXStatus(RtxMode* mode, uint32_t* ssrc,
284 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000285 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000286 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000287 *ssrc = ssrc_rtx_;
288 *payload_type = payload_type_rtx_;
289}
290
291
292void RTPSender::SetRtxPayloadType(int payload_type) {
293 CriticalSectionScoped cs(send_critsect_);
294 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000295}
296
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000297int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
298 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000299 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000301 if (payload_type < 0) {
302 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
303 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000304 return -1;
305 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000306 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000307 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000308 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000309 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000310 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000311 // And it's a match...
312 return 0;
313 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000314 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000315 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000316 if (payload_type_ == payload_type) {
317 if (!audio_configured_) {
318 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000319 }
320 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000321 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000322 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000323 payload_type_map_.find(payload_type);
324 if (it == payload_type_map_.end()) {
325 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
326 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000327 return -1;
328 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000329 payload_type_ = payload_type;
330 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000331 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000332 if (!payload->audio && !audio_configured_) {
333 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
334 *video_type = payload->typeSpecific.Video.videoCodecType;
335 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000336 }
337 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000338}
339
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000340int32_t RTPSender::SendOutgoingData(
341 const FrameType frame_type, const int8_t payload_type,
342 const uint32_t capture_timestamp, int64_t capture_time_ms,
343 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000344 const RTPFragmentationHeader *fragmentation,
345 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000346 {
347 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000348 CriticalSectionScoped cs(send_critsect_);
349 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000350 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000351 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000352 }
pbos@webrtc.org8911ce42013-03-18 16:39:03 +0000353 RtpVideoCodecTypes video_type = kRtpGenericVideo;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000354 if (CheckPayloadType(payload_type, &video_type) != 0) {
355 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
356 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000357 __FUNCTION__, payload_type);
358 return -1;
359 }
360
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000361 if (frame_type == kVideoFrameKey) {
362 TRACE_EVENT_INSTANT1("webrtc_rtp", "SendKeyFrame",
363 "timestamp", capture_timestamp);
364 } else {
365 TRACE_EVENT_INSTANT2("webrtc_rtp", "SendFrame",
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000366 "timestamp", capture_timestamp,
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000367 "frame_type", FrameTypeToString(frame_type));
368 }
369
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000370 if (audio_configured_) {
371 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000372 frame_type == kFrameEmpty);
373
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000374 return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
375 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000376 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000377 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000378
379 if (frame_type == kFrameEmpty) {
380 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000381 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000382 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000383 return video_->SendVideo(video_type, frame_type, payload_type,
384 capture_timestamp, capture_time_ms, payload_data,
385 payload_size, fragmentation, codec_info,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000386 rtp_type_hdr);
387 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000388}
389
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000390bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000391 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000392 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000393 // Current bitrate since last estimate(1 second) averaged with the
394 // estimate since then, to get the most up to date bitrate.
395 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000396 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000397 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000398 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000399 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000400 int bytes = 0;
401 if (current_bitrate == 0) {
402 // Start up phase. Send one 33.3 ms batch to start with.
403 bytes = (bitrate_diff / 8) / 30;
404 } else {
405 bytes = (bitrate_diff / 8);
406 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000407 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000408 if (bytes > bytes_cap) {
409 bytes = bytes_cap;
410 }
411 }
412 return SendPadData(payload_type, capture_timestamp, capture_time_ms, bytes);
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000413}
414
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000415int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
416 int32_t bytes) {
417 int padding_bytes_in_packet = kMaxPaddingLength;
418 if (bytes < kMaxPaddingLength) {
419 // Round to the nearest multiple of 32.
420 padding_bytes_in_packet = (bytes + 16) & 0xffe0;
421 }
422 if (padding_bytes_in_packet < 32) {
423 // Sanity don't send empty packets.
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000424 return 0;
425 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000426 packet[0] |= 0x20; // Set padding bit.
427 int32_t *data =
428 reinterpret_cast<int32_t *>(&(packet[header_length]));
429
430 // Fill data buffer with random data.
431 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
432 data[j] = rand(); // NOLINT
433 }
434 // Set number of padding bytes in the last byte of the packet.
435 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
436 return padding_bytes_in_packet;
437}
438
439bool RTPSender::SendPadData(int8_t payload_type, uint32_t capture_timestamp,
440 int64_t capture_time_ms, int32_t bytes) {
441 // Drop this packet if we're not sending media packets.
442 if (!sending_media_) {
443 return true;
444 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000445 uint8_t data_buffer[IP_PACKET_SIZE];
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000446 for (; bytes > 0; bytes -= kMaxPaddingLength) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000447 // Correct seq num, timestamp and payload type.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000448 int header_length = BuildRTPheader(data_buffer,
449 payload_type,
450 false, // No markerbit.
451 capture_timestamp,
452 true, // Timestamp provided.
453 true); // Increment sequence number.
454 int padding_bytes_in_packet = BuildPaddingPacket(data_buffer, header_length,
455 bytes);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000456 // Send the packet.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000457 if (SendToNetwork(data_buffer, padding_bytes_in_packet, header_length,
458 capture_time_ms, kDontRetransmit) < 0) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000459 // Error sending the packet.
460 break;
461 }
462 }
463 if (bytes > 31) { // 31 due to our modulus 32.
464 // We did not manage to send all bytes.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000465 return false;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000466 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000467 return true;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000468}
469
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000470void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000471 const uint16_t number_to_store) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000472 packet_history_->SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000473}
474
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000475bool RTPSender::StorePackets() const {
476 return packet_history_->StorePackets();
477}
niklase@google.com470e71d2011-07-07 08:21:25 +0000478
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000479int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
480 uint16_t length = IP_PACKET_SIZE;
481 uint8_t data_buffer[IP_PACKET_SIZE];
482 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000483 int64_t capture_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000484 StorageType type;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000485 if (!packet_history_->GetRTPPacket(packet_id, min_resend_time, data_buffer,
486 &length, &capture_time_ms, &type)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000487 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000488 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000489 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000490 if (length == 0 || type == kDontRetransmit) {
491 // No bytes copied (packet recently resent, skip resending) or
492 // packet should not be retransmitted.
493 return 0;
494 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000495
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000496 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000497 if (rtx_ != kRtxOff) {
498 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000499 buffer_to_send_ptr = data_buffer_rtx;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000500 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000501
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000502 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000503 RTPHeader header;
504 rtp_parser.Parse(header);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000505
506 // Store the time when the packet was last sent or added to pacer.
507 packet_history_->UpdateResendTime(packet_id);
508
509 {
510 // Update send statistics prior to pacer.
511 CriticalSectionScoped cs(send_critsect_);
512 Bitrate::Update(length);
513 packets_sent_++;
514 // We on purpose don't add to payload_bytes_sent_ since this is a
515 // re-transmit and not new payload data.
516 }
517
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000518 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000519 "timestamp", header.timestamp,
520 "seqnum", header.sequenceNumber);
521
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000522 if (paced_sender_) {
523 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000524 header.ssrc,
525 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000526 capture_time_ms,
527 length)) {
528 // We can't send the packet right now.
529 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000530 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000531 }
532 }
533
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000534 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000535 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000536 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000537 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000538}
539
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000540bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
541 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000542 if (transport_) {
543 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000544 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000545 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
546 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000547 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000548 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000549 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
550 "Transport failed to send packet");
551 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000552 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000553 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000554}
555
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000556int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000557 if (!video_)
558 return -1;
559 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000560}
561
562int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000563 if (!video_)
564 return -1;
565 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000566}
567
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000568void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000569 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000570 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000571 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
572 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000573 const int64_t now = clock_->TimeInMilliseconds();
574 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000575
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000576 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000577 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000578 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000579 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000580 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000581 return;
582 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000583
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000584 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
585 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000586 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000587 if (bytes_sent > 0) {
588 bytes_re_sent += bytes_sent;
589 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000590 // The packet has previously been resent.
591 // Try resending next packet in the list.
592 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000593 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000594 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000595 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000596 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000597 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000598 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000599 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000600 // Delay bandwidth estimate (RTT * BW).
601 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000602 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000603 uint32_t target_bytes =
604 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000605 if (bytes_re_sent > target_bytes) {
606 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000607 }
608 }
609 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000610 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000611 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000612 UpdateNACKBitRate(bytes_re_sent, now);
613 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000614 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000615}
616
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000617bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
618 uint32_t num = 0;
619 int32_t byte_count = 0;
620 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000621
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000622 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000623
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000624 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000625 return true;
626 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000627 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
628 if ((now - nack_byte_count_times_[num]) > avg_interval) {
629 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000630 break;
631 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000632 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000633 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000634 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000635 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000636 if (num == NACK_BYTECOUNT_SIZE) {
637 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000638 // during the last msg_interval.
639 time_interval = now - nack_byte_count_times_[num - 1];
640 if (time_interval < 0) {
641 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000642 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000643 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000644 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000645}
646
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000647void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
648 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000649 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000650
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000651 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000652 if (bytes > 0) {
653 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000654 // Add padding length.
655 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000656 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000657 if (nack_byte_count_times_[0] == 0) {
658 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000659 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000660 // Shift.
661 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
662 nack_byte_count_[i + 1] = nack_byte_count_[i];
663 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000664 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000665 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000666 nack_byte_count_[0] = bytes;
667 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000668 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000669 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000670}
671
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000672// Called from pacer when we can send the packet.
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000673void RTPSender::TimeToSendPacket(uint16_t sequence_number,
674 int64_t capture_time_ms) {
675 StorageType type;
676 uint16_t length = IP_PACKET_SIZE;
677 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000678 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000679
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000680 if (packet_history_ == NULL) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000681 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000682 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000683 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
684 &stored_time_ms, &type)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000685 return;
686 }
687 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000688
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000689 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000690 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000691 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000692 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000693 "timestamp", rtp_header.timestamp,
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000694 "seqnum", sequence_number);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000695
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000696 int64_t now_ms = clock_->TimeInMilliseconds();
697 int64_t diff_ms = now_ms - capture_time_ms;
698 bool updated_transmission_time_offset =
699 UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms);
700 bool updated_abs_send_time =
701 UpdateAbsoluteSendTime(data_buffer, length, rtp_header, now_ms);
702 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000703 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000704 packet_history_->ReplaceRTPHeader(data_buffer,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000705 rtp_header.sequenceNumber,
706 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000707 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000708 SendPacketToNetwork(data_buffer, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000709}
710
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000711// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000712int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000713 uint8_t *buffer, int payload_length, int rtp_header_length,
714 int64_t capture_time_ms, StorageType storage) {
715 ModuleRTPUtility::RTPHeaderParser rtp_parser(
716 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000717 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000718 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000719
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000720 int64_t now_ms = clock_->TimeInMilliseconds();
721
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000722 // |capture_time_ms| <= 0 is considered invalid.
723 // TODO(holmer): This should be changed all over Video Engine so that negative
724 // time is consider invalid, while 0 is considered a valid time.
725 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000726 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000727 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000728 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000729
730 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
731 rtp_header, now_ms);
732
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000733 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000734 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
735 max_payload_length_, capture_time_ms,
736 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000737 return -1;
738 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000739
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000740 // Create and send RTX Packet.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000741 // TODO(pwesin): This should be moved to its own code path triggered by pacer.
742 bool rtx_sent = false;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000743 if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000744 uint16_t length_rtx = payload_length + rtp_header_length;
745 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000746 BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000747 if (!SendPacketToNetwork(data_buffer_rtx, length_rtx)) return -1;
748 rtx_sent = true;
749 }
750 {
751 // Update send statistics prior to pacer.
752 CriticalSectionScoped cs(send_critsect_);
753 Bitrate::Update(payload_length + rtp_header_length);
754 ++packets_sent_;
755 payload_bytes_sent_ += payload_length;
756 if (rtx_sent) {
757 // The RTX packet.
758 ++packets_sent_;
759 payload_bytes_sent_ += payload_length;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000760 }
761 }
762
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000763 if (paced_sender_ && storage != kDontStore) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000764 if (!paced_sender_->SendPacket(
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000765 PacedSender::kNormalPriority, rtp_header.ssrc,
766 rtp_header.sequenceNumber, capture_time_ms,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000767 payload_length + rtp_header_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000768 // We can't send the packet right now.
769 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000770 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000771 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000772 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000773 if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
774 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000775 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000776 return -1;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000777}
778
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000779void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000780 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000781 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000782 nack_bitrate_.Process();
783 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000784 return;
785 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000786 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000787}
788
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000789uint16_t RTPSender::RTPHeaderLength() const {
790 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000791 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000792 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000793 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000794 rtp_header_length += RtpHeaderExtensionTotalLength();
795 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000796}
797
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000798uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000799 CriticalSectionScoped cs(send_critsect_);
800 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000801}
802
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000803void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000804 packets_sent_ = 0;
805 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000806}
807
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000808uint32_t RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000809 // Don't use critsect to avoid potential deadlock.
810 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000811}
812
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000813// Number of sent RTP bytes.
814// Don't use critsect to avoid potental deadlock.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000815uint32_t RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000816 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000817}
818
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000819int RTPSender::CreateRTPHeader(
820 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
821 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
822 uint8_t num_csrcs) const {
823 header[0] = 0x80; // version 2.
824 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000825 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000826 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000827 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000828 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
829 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
830 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000831 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000832
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000833 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000834 if (num_csrcs > 0) {
835 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000836 // error
837 assert(false);
838 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000839 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000840 uint8_t *ptr = &header[rtp_header_length];
841 for (int i = 0; i < num_csrcs; ++i) {
842 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000843 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000844 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000845 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000846
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000847 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000848 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000849 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000850
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000851 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
852 if (len > 0) {
853 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000854 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000855 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000856 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000857}
858
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000859int32_t RTPSender::BuildRTPheader(
860 uint8_t *data_buffer, const int8_t payload_type,
861 const bool marker_bit, const uint32_t capture_timestamp,
862 const bool time_stamp_provided, const bool inc_sequence_number) {
863 assert(payload_type >= 0);
864 CriticalSectionScoped cs(send_critsect_);
865
866 if (time_stamp_provided) {
867 timestamp_ = start_time_stamp_ + capture_timestamp;
868 capture_timestamp_ = capture_timestamp;
869 } else {
870 // Make a unique time stamp.
871 // We can't inc by the actual time, since then we increase the risk of back
872 // timing.
873 timestamp_++;
874 }
875 uint32_t sequence_number = sequence_number_++;
876 int csrcs_length = 0;
877 if (include_csrcs_)
878 csrcs_length = num_csrcs_;
879 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
880 timestamp_, sequence_number, csrcs_, csrcs_length);
881}
882
883uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000884 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000885 return 0;
886 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000887 // RTP header extension, RFC 3550.
888 // 0 1 2 3
889 // 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
890 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
891 // | defined by profile | length |
892 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
893 // | header extension |
894 // | .... |
895 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000896 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000897 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000898
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000899 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000900 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000901 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000902
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000903 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000904 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000905
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000906 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000907 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000908 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000909 switch (type) {
910 case kRtpExtensionTransmissionTimeOffset:
911 block_length = BuildTransmissionTimeOffsetExtension(
912 data_buffer + kHeaderLength + total_block_length);
913 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +0000914 case kRtpExtensionAudioLevel:
915 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
916 // we don't have to care about it here, which is true until we wan't to
917 // use it together with any of the other extensions we support.
918 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000919 case kRtpExtensionAbsoluteSendTime:
920 block_length = BuildAbsoluteSendTimeExtension(
921 data_buffer + kHeaderLength + total_block_length);
922 break;
923 default:
924 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000925 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000926 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000927 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000928 }
929 if (total_block_length == 0) {
930 // No extension added.
931 return 0;
932 }
933 // Set header length (in number of Word32, header excluded).
934 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000935 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000936 total_block_length / 4);
937 // Total added length.
938 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000939}
940
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000941uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
942 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000943 // From RFC 5450: Transmission Time Offsets in RTP Streams.
944 //
945 // The transmission time is signaled to the receiver in-band using the
946 // general mechanism for RTP header extensions [RFC5285]. The payload
947 // of this extension (the transmitted value) is a 24-bit signed integer.
948 // When added to the RTP timestamp of the packet, it represents the
949 // "effective" RTP transmission time of the packet, on the RTP
950 // timescale.
951 //
952 // The form of the transmission offset extension block:
953 //
954 // 0 1 2 3
955 // 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
956 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
957 // | ID | len=2 | transmission offset |
958 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000959
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000960 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000961 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000962 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
963 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000964 // Not registered.
965 return 0;
966 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000967 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000968 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000969 data_buffer[pos++] = (id << 4) + len;
970 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
971 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000972 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000973 assert(pos == kTransmissionTimeOffsetLength);
974 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000975}
976
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000977uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
978 uint8_t* data_buffer) const {
979 // Absolute send time in RTP streams.
980 //
981 // The absolute send time is signaled to the receiver in-band using the
982 // general mechanism for RTP header extensions [RFC5285]. The payload
983 // of this extension (the transmitted value) is a 24-bit unsigned integer
984 // containing the sender's current time in seconds as a fixed point number
985 // with 18 bits fractional part.
986 //
987 // The form of the absolute send time extension block:
988 //
989 // 0 1 2 3
990 // 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
991 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
992 // | ID | len=2 | absolute send time |
993 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
994
995 // Get id defined by user.
996 uint8_t id;
997 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
998 &id) != 0) {
999 // Not registered.
1000 return 0;
1001 }
1002 size_t pos = 0;
1003 const uint8_t len = 2;
1004 data_buffer[pos++] = (id << 4) + len;
1005 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1006 absolute_send_time_);
1007 pos += 3;
1008 assert(pos == kAbsoluteSendTimeLength);
1009 return kAbsoluteSendTimeLength;
1010}
1011
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001012bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001013 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001014 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001015 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001016
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001017 // Get length until start of header extension block.
1018 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001019 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001020 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001021 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001022 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001023 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001024 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001025 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001026 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001027 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001028 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001029 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001030 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001031 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001032 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001033 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001034 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001035 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1036 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001037 WEBRTC_TRACE(
1038 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001039 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001040 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001041 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001042 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001043 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001044 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1045 &id) != 0) {
1046 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001047 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001048 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001049 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001050 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001051 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001052 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001053 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001054 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001055 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001056 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001057 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001058 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001059 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001060 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001061}
1062
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001063bool RTPSender::UpdateAbsoluteSendTime(
1064 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001065 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001066 CriticalSectionScoped cs(send_critsect_);
1067
1068 // Get length until start of header extension block.
1069 int extension_block_pos =
1070 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1071 kRtpExtensionAbsoluteSendTime);
1072 if (extension_block_pos < 0) {
1073 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1074 "Failed to update absolute send time, not registered.");
1075 return false;
1076 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001077 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001078 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001079 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001080 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1081 "Failed to update absolute send time, invalid length.");
1082 return false;
1083 }
1084 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001085 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1086 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001087 WEBRTC_TRACE(
1088 kTraceStream, kTraceRtpRtcp, id_,
1089 "Failed to update absolute send time, hdr extension not found.");
1090 return false;
1091 }
1092 // Get id.
1093 uint8_t id = 0;
1094 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1095 &id) != 0) {
1096 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1097 "Failed to update absolute send time, no id.");
1098 return false;
1099 }
1100 // Verify first byte in block.
1101 const uint8_t first_block_byte = (id << 4) + 2;
1102 if (rtp_packet[block_pos] != first_block_byte) {
1103 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1104 "Failed to update absolute send time.");
1105 return false;
1106 }
1107 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1108 // fractional part).
1109 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1110 ((now_ms << 18) / 1000) & 0x00ffffff);
1111 return true;
1112}
1113
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001114void RTPSender::SetSendingStatus(const bool enabled) {
1115 if (enabled) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001116 uint32_t frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001117 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001118 uint32_t frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001119
1120 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001121 switch (frequency) {
1122 case 8000:
1123 case 12000:
1124 case 16000:
1125 case 24000:
1126 case 32000:
1127 break;
1128 default:
1129 assert(false);
1130 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001131 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +00001132 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001133 } else {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +00001134 frequency_hz = kDefaultVideoFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001135 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001136 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001137
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001138 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001139 SetStartTimestamp(RTPtime, false);
1140 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001141 if (!ssrc_forced_) {
1142 // Generate a new SSRC.
1143 ssrc_db_.ReturnSSRC(ssrc_);
1144 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001145 }
1146 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001147 if (!sequence_number_forced_ && !ssrc_forced_) {
1148 // Generate a new sequence number.
1149 sequence_number_ =
1150 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001151 }
1152 }
1153}
1154
1155void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001156 CriticalSectionScoped cs(send_critsect_);
1157 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001158}
1159
1160bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001161 CriticalSectionScoped cs(send_critsect_);
1162 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001163}
1164
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001165uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001166 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001167 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001168}
1169
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001170void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001171 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001172 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001173 start_time_stamp_forced_ = force;
1174 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001175 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001176 if (!start_time_stamp_forced_) {
1177 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001178 }
1179 }
1180}
1181
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001182uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001183 CriticalSectionScoped cs(send_critsect_);
1184 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001185}
1186
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001187uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001188 // If configured via API, return 0.
1189 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001190
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001191 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001192 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001193 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001194 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1195 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001196}
1197
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001198void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001199 // This is configured via the API.
1200 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001201
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001202 if (ssrc_ == ssrc && ssrc_forced_) {
1203 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001204 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001205 ssrc_forced_ = true;
1206 ssrc_db_.ReturnSSRC(ssrc_);
1207 ssrc_db_.RegisterSSRC(ssrc);
1208 ssrc_ = ssrc;
1209 if (!sequence_number_forced_) {
1210 sequence_number_ =
1211 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001212 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001213}
1214
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001215uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001216 CriticalSectionScoped cs(send_critsect_);
1217 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001218}
1219
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001220void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001221 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001222}
1223
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001224void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1225 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001226 assert(arr_length <= kRtpCsrcSize);
1227 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001228
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001229 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001230 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001231 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001232 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001233}
1234
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001235int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001236 assert(arr_of_csrc);
1237 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001238 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1239 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001240 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001241 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001242}
1243
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001244void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001245 CriticalSectionScoped cs(send_critsect_);
1246 sequence_number_forced_ = true;
1247 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001248}
1249
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001250uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001251 CriticalSectionScoped cs(send_critsect_);
1252 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001253}
1254
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001255// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001256int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1257 const uint16_t time_ms,
1258 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001259 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001260 return -1;
1261 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001262 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001263}
1264
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001265bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001266 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001267 return false;
1268 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001269 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001270}
1271
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001272int32_t RTPSender::SetAudioPacketSize(
1273 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001274 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001275 return -1;
1276 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001277 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001278}
1279
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001280int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1281 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001282 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001283 return -1;
1284 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001285 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001286}
1287
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001288int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1289 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001290 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001291}
1292
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001293int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001294 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001295}
1296
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001297int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001298 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001299 return -1;
1300 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001301 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001302}
1303
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001304int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001305 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001306 return -1;
1307 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001308 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001309}
1310
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001311// Video
1312VideoCodecInformation *RTPSender::CodecInformationVideo() {
1313 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001314 return NULL;
1315 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001316 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001317}
1318
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001319RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001320 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001321 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001322}
1323
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001324uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001325 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001326 return 0;
1327 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001328 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001329}
1330
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001331int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001332 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001333 return -1;
1334 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001335 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001336}
1337
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001338int32_t RTPSender::SetGenericFECStatus(
1339 const bool enable, const uint8_t payload_type_red,
1340 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001341 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001342 return -1;
1343 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001344 return video_->SetGenericFECStatus(enable, payload_type_red,
1345 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001346}
1347
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001348int32_t RTPSender::GenericFECStatus(
1349 bool *enable, uint8_t *payload_type_red,
1350 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001351 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001352 return -1;
1353 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001354 return video_->GenericFECStatus(
1355 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001356}
1357
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001358int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001359 const FecProtectionParams *delta_params,
1360 const FecProtectionParams *key_params) {
1361 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001362 return -1;
1363 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001364 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001365}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001366
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001367void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1368 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001369 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001370 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001371 // Add RTX header.
1372 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001373 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001374
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001375 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001376 rtp_parser.Parse(rtp_header);
1377
1378 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001379 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001380
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001381 // Replace payload type, if a specific type is set for RTX.
1382 if (payload_type_rtx_ != -1) {
1383 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001384 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001385 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1386 }
1387
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001388 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001389 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001390 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1391
1392 // Replace SSRC.
1393 ptr += 6;
1394 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1395
1396 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001397 ptr = data_buffer_rtx + rtp_header.headerLength;
1398 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001399 ptr += 2;
1400
1401 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001402 memcpy(ptr, buffer + rtp_header.headerLength,
1403 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001404 *length += 2;
1405}
1406
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001407} // namespace webrtc