blob: 613efe7eb55fd790aa11af3039d09c29a545b91f [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/rtp_rtcp/source/rtp_packet_history.h"
16#include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
17#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
18#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
19#include "webrtc/system_wrappers/interface/trace.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000020#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
stefan@webrtc.orga8179622013-06-04 13:47:36 +000024// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
25const int kMaxPaddingLength = 224;
26
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";
36 case kVideoFrameGolden: return "video_golden";
37 case kVideoFrameAltRef: return "video_altref";
38 }
39 return "";
40}
41
42} // namespace
43
pbos@webrtc.org2f446732013-04-08 11:08:41 +000044RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000045 Transport *transport, RtpAudioFeedback *audio_feedback,
46 PacedSender *paced_sender)
47 : Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL),
48 video_(NULL), paced_sender_(paced_sender),
49 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
50 transport_(transport), sending_media_(true), // Default to sending media.
51 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
52 target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1),
53 payload_type_map_(), rtp_header_extension_map_(),
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +000054 transmission_time_offset_(0), absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000055 // NACK.
56 nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
57 packet_history_(new RTPPacketHistory(clock)),
58 // Statistics
59 packets_sent_(0), payload_bytes_sent_(0), start_time_stamp_forced_(false),
60 start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000061 remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false),
stefan@webrtc.orga8179622013-06-04 13:47:36 +000062 timestamp_(0), num_csrcs_(0), csrcs_(), include_csrcs_(true),
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +000063 rtx_(kRtxOff), payload_type_rtx_(-1) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000064 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
65 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +000066 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000067 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000068 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000069 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000070 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
71 // Random start, 16 bits. Can't be 0.
72 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
73 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +000074
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000075 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000076 audio_ = new RTPSenderAudio(id, clock_, this);
77 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000078 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000079 video_ = new RTPSenderVideo(id, clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000080 }
81 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000082}
83
pwestin@webrtc.org00741872012-01-19 15:56:10 +000084RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000085 if (remote_ssrc_ != 0) {
86 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000087 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000088 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +000089
pwestin@webrtc.org00741872012-01-19 15:56:10 +000090 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000091 delete send_critsect_;
92 while (!payload_type_map_.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000093 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000094 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +000095 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000096 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000097 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000098 delete packet_history_;
99 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000131int32_t RTPSender::SetTransmissionTimeOffset(
132 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000133 if (transmission_time_offset > (0x800000 - 1) ||
134 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000135 return -1;
136 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000137 CriticalSectionScoped cs(send_critsect_);
138 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000139 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000140}
141
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000142int32_t RTPSender::SetAbsoluteSendTime(
143 const uint32_t absolute_send_time) {
144 if (absolute_send_time > 0xffffff) { // UWord24.
145 return -1;
146 }
147 CriticalSectionScoped cs(send_critsect_);
148 absolute_send_time_ = absolute_send_time;
149 return 0;
150}
151
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000152int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
153 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000154 CriticalSectionScoped cs(send_critsect_);
155 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000156}
157
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000158int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000159 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000160 CriticalSectionScoped cs(send_critsect_);
161 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000162}
163
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000164uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000165 CriticalSectionScoped cs(send_critsect_);
166 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000167}
168
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000169int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000170 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000171 const int8_t payload_number, const uint32_t frequency,
172 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000173 assert(payload_name);
174 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000175
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000176 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000177 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000178
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000179 if (payload_type_map_.end() != it) {
180 // We already use this payload type.
181 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000182 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000183
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000184 // Check if it's the same as we already have.
185 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000186 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000187 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000188 payload->typeSpecific.Audio.frequency == frequency &&
189 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000190 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000191 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000192 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000193 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000194 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000195 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000196 return 0;
197 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000198 }
199 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000200 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000201 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000202 ModuleRTPUtility::Payload *payload = NULL;
203 if (audio_configured_) {
204 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
205 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000206 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000207 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
208 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000209 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000210 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000211 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000212 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000213 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000214}
215
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000216int32_t RTPSender::DeRegisterSendPayload(
217 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000218 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000219
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000220 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000221 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000222
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000223 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000224 return -1;
225 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000226 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000227 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000228 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000229 return 0;
230}
niklase@google.com470e71d2011-07-07 08:21:25 +0000231
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000232int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000233
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000234int RTPSender::SendPayloadFrequency() const { return audio_->AudioFrequency(); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000235
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000236int32_t RTPSender::SetMaxPayloadLength(
237 const uint16_t max_payload_length,
238 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000239 // Sanity check.
240 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
241 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
242 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000243 return -1;
244 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000245 CriticalSectionScoped cs(send_critsect_);
246 max_payload_length_ = max_payload_length;
247 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000248
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000249 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
250 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000251 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000252}
253
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000254uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000255 if (audio_configured_) {
256 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000257 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000258 return max_payload_length_ - RTPHeaderLength() -
259 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
260 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000261 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000262}
263
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000264uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000265 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000266}
267
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000268uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000269
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000270void RTPSender::SetRTXStatus(RtxMode mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000271 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000272 rtx_ = mode;
273 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000274 if (set_ssrc) {
275 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000276 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000277 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000278 }
279 }
280}
281
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000282void RTPSender::RTXStatus(RtxMode* mode, uint32_t* ssrc,
283 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000284 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000285 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000286 *ssrc = ssrc_rtx_;
287 *payload_type = payload_type_rtx_;
288}
289
290
291void RTPSender::SetRtxPayloadType(int payload_type) {
292 CriticalSectionScoped cs(send_critsect_);
293 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000294}
295
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000296int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
297 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000298 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000299
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000300 if (payload_type < 0) {
301 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
302 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000303 return -1;
304 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000305 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000306 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000307 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000308 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000309 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000310 // And it's a match...
311 return 0;
312 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000313 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000314 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000315 if (payload_type_ == payload_type) {
316 if (!audio_configured_) {
317 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000318 }
319 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000320 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000321 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000322 payload_type_map_.find(payload_type);
323 if (it == payload_type_map_.end()) {
324 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
325 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000326 return -1;
327 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000328 payload_type_ = payload_type;
329 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000330 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000331 if (!payload->audio && !audio_configured_) {
332 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
333 *video_type = payload->typeSpecific.Video.videoCodecType;
334 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000335 }
336 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000337}
338
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000339int32_t RTPSender::SendOutgoingData(
340 const FrameType frame_type, const int8_t payload_type,
341 const uint32_t capture_timestamp, int64_t capture_time_ms,
342 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000343 const RTPFragmentationHeader *fragmentation,
344 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000345 {
346 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000347 CriticalSectionScoped cs(send_critsect_);
348 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000349 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000350 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000351 }
pbos@webrtc.org8911ce42013-03-18 16:39:03 +0000352 RtpVideoCodecTypes video_type = kRtpGenericVideo;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000353 if (CheckPayloadType(payload_type, &video_type) != 0) {
354 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
355 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000356 __FUNCTION__, payload_type);
357 return -1;
358 }
359
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000360 if (frame_type == kVideoFrameKey) {
361 TRACE_EVENT_INSTANT1("webrtc_rtp", "SendKeyFrame",
362 "timestamp", capture_timestamp);
363 } else {
364 TRACE_EVENT_INSTANT2("webrtc_rtp", "SendFrame",
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000365 "timestamp", capture_timestamp,
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000366 "frame_type", FrameTypeToString(frame_type));
367 }
368
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000369 if (audio_configured_) {
370 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000371 frame_type == kFrameEmpty);
372
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000373 return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
374 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000375 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000376 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000377
378 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000379 if (paced_sender_->Enabled()) {
380 // Padding is driven by the pacer and not by the encoder.
381 return 0;
382 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000383 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000384 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000385 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000386 capture_time_ms_ = capture_time_ms;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000387 return video_->SendVideo(video_type, frame_type, payload_type,
388 capture_timestamp, capture_time_ms, payload_data,
389 payload_size, fragmentation, codec_info,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000390 rtp_type_hdr);
391 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000392}
393
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000394bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000395 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000396 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000397 // Current bitrate since last estimate(1 second) averaged with the
398 // estimate since then, to get the most up to date bitrate.
399 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000400 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000401 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000402 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000403 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000404 int bytes = 0;
405 if (current_bitrate == 0) {
406 // Start up phase. Send one 33.3 ms batch to start with.
407 bytes = (bitrate_diff / 8) / 30;
408 } else {
409 bytes = (bitrate_diff / 8);
410 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000411 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000412 if (bytes > bytes_cap) {
413 bytes = bytes_cap;
414 }
415 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000416 int bytes_sent = SendPadData(payload_type, capture_time_ms, bytes,
417 kDontRetransmit, false);
418 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
419 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000420}
421
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000422int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
423 int32_t bytes) {
424 int padding_bytes_in_packet = kMaxPaddingLength;
425 if (bytes < kMaxPaddingLength) {
426 // Round to the nearest multiple of 32.
427 padding_bytes_in_packet = (bytes + 16) & 0xffe0;
428 }
429 if (padding_bytes_in_packet < 32) {
430 // Sanity don't send empty packets.
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000431 return 0;
432 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000433 packet[0] |= 0x20; // Set padding bit.
434 int32_t *data =
435 reinterpret_cast<int32_t *>(&(packet[header_length]));
436
437 // Fill data buffer with random data.
438 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
439 data[j] = rand(); // NOLINT
440 }
441 // Set number of padding bytes in the last byte of the packet.
442 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
443 return padding_bytes_in_packet;
444}
445
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000446int RTPSender::SendPadData(int payload_type, int64_t capture_time_ms,
447 int32_t bytes, StorageType store,
448 bool force_full_size_packets) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000449 // Drop this packet if we're not sending media packets.
450 if (!sending_media_) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000451 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000452 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000453 uint32_t ssrc;
454 uint16_t sequence_number;
455 uint32_t timestamp;
456 {
457 CriticalSectionScoped cs(send_critsect_);
458 timestamp = timestamp_;
459 if (rtx_ == kRtxOff) {
460 ssrc = ssrc_;
461 sequence_number = sequence_number_;
462 ++sequence_number_;
463 } else {
464 ssrc = ssrc_rtx_;
465 sequence_number = sequence_number_rtx_;
466 ++sequence_number_rtx_;
467 }
468 }
469 int padding_bytes_in_packet = 0;
470 int bytes_sent = 0;
471 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
472 // Generate an RTX packet which only contains random padding data.
473 uint8_t padding_packet[IP_PACKET_SIZE];
474 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
475 false, timestamp, sequence_number,
476 NULL, 0);
477 // Always send full padding packets.
478 if (force_full_size_packets && bytes < kMaxPaddingLength)
479 bytes = kMaxPaddingLength;
480 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
481 bytes);
482 if (padding_bytes_in_packet == 0) {
483 break;
484 }
485 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
486 header_length, capture_time_ms, store,
487 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000488 // Error sending the packet.
489 break;
490 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000491 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000492 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000493 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000494}
495
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000496void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000497 const uint16_t number_to_store) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000498 packet_history_->SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000499}
500
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000501bool RTPSender::StorePackets() const {
502 return packet_history_->StorePackets();
503}
niklase@google.com470e71d2011-07-07 08:21:25 +0000504
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000505int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
506 uint16_t length = IP_PACKET_SIZE;
507 uint8_t data_buffer[IP_PACKET_SIZE];
508 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000509 int64_t capture_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000510 StorageType type;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000511 if (!packet_history_->GetRTPPacket(packet_id, min_resend_time, data_buffer,
512 &length, &capture_time_ms, &type)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000513 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000514 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000515 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000516 if (length == 0 || type == kDontRetransmit) {
517 // No bytes copied (packet recently resent, skip resending) or
518 // packet should not be retransmitted.
519 return 0;
520 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000521
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000522 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000523 if (rtx_ != kRtxOff) {
524 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000525 buffer_to_send_ptr = data_buffer_rtx;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000526 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000527
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000528 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000529 RTPHeader header;
530 rtp_parser.Parse(header);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000531
532 // Store the time when the packet was last sent or added to pacer.
533 packet_history_->UpdateResendTime(packet_id);
534
535 {
536 // Update send statistics prior to pacer.
537 CriticalSectionScoped cs(send_critsect_);
538 Bitrate::Update(length);
539 packets_sent_++;
540 // We on purpose don't add to payload_bytes_sent_ since this is a
541 // re-transmit and not new payload data.
542 }
543
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000544 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000545 "timestamp", header.timestamp,
546 "seqnum", header.sequenceNumber);
547
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000548 if (paced_sender_) {
549 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000550 header.ssrc,
551 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000552 capture_time_ms,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000553 length - header.headerLength)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000554 // We can't send the packet right now.
555 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000556 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000557 }
558 }
559
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000560 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000561 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000562 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000563 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000564}
565
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000566bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
567 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000568 if (transport_) {
569 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000570 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000571 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
572 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000573 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000574 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000575 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
576 "Transport failed to send packet");
577 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000578 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000579 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000580}
581
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000582int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000583 if (!video_)
584 return -1;
585 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000586}
587
588int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000589 if (!video_)
590 return -1;
591 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000592}
593
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000594void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000595 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000596 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000597 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
598 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000599 const int64_t now = clock_->TimeInMilliseconds();
600 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000601
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000602 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000603 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000604 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000605 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000606 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000607 return;
608 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000609
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000610 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
611 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000612 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000613 if (bytes_sent > 0) {
614 bytes_re_sent += bytes_sent;
615 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000616 // The packet has previously been resent.
617 // Try resending next packet in the list.
618 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000619 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000620 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000621 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000622 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000623 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000624 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000625 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000626 // Delay bandwidth estimate (RTT * BW).
627 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000628 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000629 uint32_t target_bytes =
630 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000631 if (bytes_re_sent > target_bytes) {
632 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000633 }
634 }
635 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000636 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000637 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000638 UpdateNACKBitRate(bytes_re_sent, now);
639 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000640 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000641}
642
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000643bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
644 uint32_t num = 0;
645 int32_t byte_count = 0;
646 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000647
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000648 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000649
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000650 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000651 return true;
652 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000653 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
654 if ((now - nack_byte_count_times_[num]) > avg_interval) {
655 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000656 break;
657 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000658 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000659 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000660 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000661 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000662 if (num == NACK_BYTECOUNT_SIZE) {
663 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000664 // during the last msg_interval.
665 time_interval = now - nack_byte_count_times_[num - 1];
666 if (time_interval < 0) {
667 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000668 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000669 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000670 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000671}
672
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000673void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
674 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000675 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000676
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000677 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000678 if (bytes > 0) {
679 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000680 // Add padding length.
681 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000682 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000683 if (nack_byte_count_times_[0] == 0) {
684 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000685 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000686 // Shift.
687 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
688 nack_byte_count_[i + 1] = nack_byte_count_[i];
689 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000690 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000691 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000692 nack_byte_count_[0] = bytes;
693 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000694 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000695 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000696}
697
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000698// Called from pacer when we can send the packet.
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000699void RTPSender::TimeToSendPacket(uint16_t sequence_number,
700 int64_t capture_time_ms) {
701 StorageType type;
702 uint16_t length = IP_PACKET_SIZE;
703 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000704 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000705
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000706 if (packet_history_ == NULL) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000707 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000708 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000709 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
710 &stored_time_ms, &type)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000711 return;
712 }
713 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000714
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000715 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000716 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000717 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000718 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000719 "timestamp", rtp_header.timestamp,
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000720 "seqnum", sequence_number);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000721
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000722 int64_t now_ms = clock_->TimeInMilliseconds();
723 int64_t diff_ms = now_ms - capture_time_ms;
724 bool updated_transmission_time_offset =
725 UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms);
726 bool updated_abs_send_time =
727 UpdateAbsoluteSendTime(data_buffer, length, rtp_header, now_ms);
728 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000729 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000730 packet_history_->ReplaceRTPHeader(data_buffer,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000731 rtp_header.sequenceNumber,
732 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000733 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000734 SendPacketToNetwork(data_buffer, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000735}
736
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000737int RTPSender::TimeToSendPadding(int bytes) {
738 if (!sending_media_) {
739 return 0;
740 }
741 int payload_type;
742 {
743 CriticalSectionScoped cs(send_critsect_);
744 payload_type = (rtx_ == kRtxOff) ? payload_type_ : payload_type_rtx_;
745 }
746 return SendPadData(payload_type, capture_time_ms_, bytes, kDontStore,
747 true);
748}
749
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000750// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000751int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000752 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000753 int64_t capture_time_ms, StorageType storage,
754 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000755 ModuleRTPUtility::RTPHeaderParser rtp_parser(
756 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000757 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000758 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000759
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000760 int64_t now_ms = clock_->TimeInMilliseconds();
761
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000762 // |capture_time_ms| <= 0 is considered invalid.
763 // TODO(holmer): This should be changed all over Video Engine so that negative
764 // time is consider invalid, while 0 is considered a valid time.
765 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000766 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000767 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000768 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000769
770 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
771 rtp_header, now_ms);
772
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000773 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000774 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
775 max_payload_length_, capture_time_ms,
776 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000777 return -1;
778 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000779
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000780 // Create and send RTX Packet.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000781 // TODO(pwesin): This should be moved to its own code path triggered by pacer.
782 bool rtx_sent = false;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000783 if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000784 uint16_t length_rtx = payload_length + rtp_header_length;
785 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000786 BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000787 if (!SendPacketToNetwork(data_buffer_rtx, length_rtx)) return -1;
788 rtx_sent = true;
789 }
790 {
791 // Update send statistics prior to pacer.
792 CriticalSectionScoped cs(send_critsect_);
793 Bitrate::Update(payload_length + rtp_header_length);
794 ++packets_sent_;
795 payload_bytes_sent_ += payload_length;
796 if (rtx_sent) {
797 // The RTX packet.
798 ++packets_sent_;
799 payload_bytes_sent_ += payload_length;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000800 }
801 }
802
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000803 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000804 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
805 rtp_header.sequenceNumber, capture_time_ms,
806 payload_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000807 // We can't send the packet right now.
808 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000809 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000810 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000811 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000812 if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
813 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000814 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000815 return -1;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000816}
817
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000818void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000819 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000820 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000821 nack_bitrate_.Process();
822 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000823 return;
824 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000825 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000826}
827
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000828uint16_t RTPSender::RTPHeaderLength() const {
829 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000830 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000831 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000832 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000833 rtp_header_length += RtpHeaderExtensionTotalLength();
834 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000835}
836
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000837uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000838 CriticalSectionScoped cs(send_critsect_);
839 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000840}
841
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000842void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000843 packets_sent_ = 0;
844 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000845}
846
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000847uint32_t RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000848 // Don't use critsect to avoid potential deadlock.
849 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000850}
851
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000852// Number of sent RTP bytes.
853// Don't use critsect to avoid potental deadlock.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000854uint32_t RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000855 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000856}
857
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000858int RTPSender::CreateRTPHeader(
859 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
860 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
861 uint8_t num_csrcs) const {
862 header[0] = 0x80; // version 2.
863 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000864 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000865 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000866 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000867 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
868 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
869 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000870 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000871
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000872 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000873 if (num_csrcs > 0) {
874 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000875 // error
876 assert(false);
877 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000878 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000879 uint8_t *ptr = &header[rtp_header_length];
880 for (int i = 0; i < num_csrcs; ++i) {
881 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000882 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000883 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000884 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000885
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000886 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000887 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000888 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000889
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000890 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
891 if (len > 0) {
892 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000893 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000894 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000895 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000896}
897
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000898int32_t RTPSender::BuildRTPheader(
899 uint8_t *data_buffer, const int8_t payload_type,
900 const bool marker_bit, const uint32_t capture_timestamp,
901 const bool time_stamp_provided, const bool inc_sequence_number) {
902 assert(payload_type >= 0);
903 CriticalSectionScoped cs(send_critsect_);
904
905 if (time_stamp_provided) {
906 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000907 } else {
908 // Make a unique time stamp.
909 // We can't inc by the actual time, since then we increase the risk of back
910 // timing.
911 timestamp_++;
912 }
913 uint32_t sequence_number = sequence_number_++;
914 int csrcs_length = 0;
915 if (include_csrcs_)
916 csrcs_length = num_csrcs_;
917 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
918 timestamp_, sequence_number, csrcs_, csrcs_length);
919}
920
921uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000922 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000923 return 0;
924 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000925 // RTP header extension, RFC 3550.
926 // 0 1 2 3
927 // 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
928 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
929 // | defined by profile | length |
930 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
931 // | header extension |
932 // | .... |
933 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000934 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000935 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000936
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000937 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000938 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000939 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000940
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000941 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000942 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000943
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000944 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000945 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000946 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000947 switch (type) {
948 case kRtpExtensionTransmissionTimeOffset:
949 block_length = BuildTransmissionTimeOffsetExtension(
950 data_buffer + kHeaderLength + total_block_length);
951 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +0000952 case kRtpExtensionAudioLevel:
953 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
954 // we don't have to care about it here, which is true until we wan't to
955 // use it together with any of the other extensions we support.
956 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000957 case kRtpExtensionAbsoluteSendTime:
958 block_length = BuildAbsoluteSendTimeExtension(
959 data_buffer + kHeaderLength + total_block_length);
960 break;
961 default:
962 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000963 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000964 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000965 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000966 }
967 if (total_block_length == 0) {
968 // No extension added.
969 return 0;
970 }
971 // Set header length (in number of Word32, header excluded).
972 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000973 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000974 total_block_length / 4);
975 // Total added length.
976 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000977}
978
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000979uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
980 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000981 // From RFC 5450: Transmission Time Offsets in RTP Streams.
982 //
983 // The transmission time is signaled to the receiver in-band using the
984 // general mechanism for RTP header extensions [RFC5285]. The payload
985 // of this extension (the transmitted value) is a 24-bit signed integer.
986 // When added to the RTP timestamp of the packet, it represents the
987 // "effective" RTP transmission time of the packet, on the RTP
988 // timescale.
989 //
990 // The form of the transmission offset extension block:
991 //
992 // 0 1 2 3
993 // 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
994 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
995 // | ID | len=2 | transmission offset |
996 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000997
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000998 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000999 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001000 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1001 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001002 // Not registered.
1003 return 0;
1004 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001005 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001006 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001007 data_buffer[pos++] = (id << 4) + len;
1008 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1009 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001010 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001011 assert(pos == kTransmissionTimeOffsetLength);
1012 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001013}
1014
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001015uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
1016 uint8_t* data_buffer) const {
1017 // Absolute send time in RTP streams.
1018 //
1019 // The absolute send time is signaled to the receiver in-band using the
1020 // general mechanism for RTP header extensions [RFC5285]. The payload
1021 // of this extension (the transmitted value) is a 24-bit unsigned integer
1022 // containing the sender's current time in seconds as a fixed point number
1023 // with 18 bits fractional part.
1024 //
1025 // The form of the absolute send time extension block:
1026 //
1027 // 0 1 2 3
1028 // 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
1029 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1030 // | ID | len=2 | absolute send time |
1031 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1032
1033 // Get id defined by user.
1034 uint8_t id;
1035 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1036 &id) != 0) {
1037 // Not registered.
1038 return 0;
1039 }
1040 size_t pos = 0;
1041 const uint8_t len = 2;
1042 data_buffer[pos++] = (id << 4) + len;
1043 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1044 absolute_send_time_);
1045 pos += 3;
1046 assert(pos == kAbsoluteSendTimeLength);
1047 return kAbsoluteSendTimeLength;
1048}
1049
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001050bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001051 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001052 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001053 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001054
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001055 // Get length until start of header extension block.
1056 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001057 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001058 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001059 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001060 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001061 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001062 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001063 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001064 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001065 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001066 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001067 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001068 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001069 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001070 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001071 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001072 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001073 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1074 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001075 WEBRTC_TRACE(
1076 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001077 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001078 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001079 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001080 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001081 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001082 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1083 &id) != 0) {
1084 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001085 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001086 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001087 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001088 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001089 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001090 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001091 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001092 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001093 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001094 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001095 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001096 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001097 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001098 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001099}
1100
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001101bool RTPSender::UpdateAbsoluteSendTime(
1102 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001103 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001104 CriticalSectionScoped cs(send_critsect_);
1105
1106 // Get length until start of header extension block.
1107 int extension_block_pos =
1108 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1109 kRtpExtensionAbsoluteSendTime);
1110 if (extension_block_pos < 0) {
1111 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1112 "Failed to update absolute send time, not registered.");
1113 return false;
1114 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001115 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001116 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001117 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001118 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1119 "Failed to update absolute send time, invalid length.");
1120 return false;
1121 }
1122 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001123 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1124 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001125 WEBRTC_TRACE(
1126 kTraceStream, kTraceRtpRtcp, id_,
1127 "Failed to update absolute send time, hdr extension not found.");
1128 return false;
1129 }
1130 // Get id.
1131 uint8_t id = 0;
1132 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1133 &id) != 0) {
1134 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1135 "Failed to update absolute send time, no id.");
1136 return false;
1137 }
1138 // Verify first byte in block.
1139 const uint8_t first_block_byte = (id << 4) + 2;
1140 if (rtp_packet[block_pos] != first_block_byte) {
1141 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1142 "Failed to update absolute send time.");
1143 return false;
1144 }
1145 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1146 // fractional part).
1147 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1148 ((now_ms << 18) / 1000) & 0x00ffffff);
1149 return true;
1150}
1151
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001152void RTPSender::SetSendingStatus(const bool enabled) {
1153 if (enabled) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001154 uint32_t frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001155 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001156 uint32_t frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001157
1158 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001159 switch (frequency) {
1160 case 8000:
1161 case 12000:
1162 case 16000:
1163 case 24000:
1164 case 32000:
1165 break;
1166 default:
1167 assert(false);
1168 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001169 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +00001170 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001171 } else {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +00001172 frequency_hz = kDefaultVideoFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001173 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001174 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001175
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001176 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001177 SetStartTimestamp(RTPtime, false);
1178 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001179 if (!ssrc_forced_) {
1180 // Generate a new SSRC.
1181 ssrc_db_.ReturnSSRC(ssrc_);
1182 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001183 }
1184 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001185 if (!sequence_number_forced_ && !ssrc_forced_) {
1186 // Generate a new sequence number.
1187 sequence_number_ =
1188 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001189 }
1190 }
1191}
1192
1193void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001194 CriticalSectionScoped cs(send_critsect_);
1195 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001196}
1197
1198bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001199 CriticalSectionScoped cs(send_critsect_);
1200 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001201}
1202
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001203uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001204 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001205 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001206}
1207
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001208void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001209 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001210 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001211 start_time_stamp_forced_ = force;
1212 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001213 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001214 if (!start_time_stamp_forced_) {
1215 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001216 }
1217 }
1218}
1219
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001220uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001221 CriticalSectionScoped cs(send_critsect_);
1222 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001223}
1224
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001225uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001226 // If configured via API, return 0.
1227 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001228
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001229 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001230 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001231 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001232 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1233 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001234}
1235
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001236void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001237 // This is configured via the API.
1238 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001239
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001240 if (ssrc_ == ssrc && ssrc_forced_) {
1241 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001242 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001243 ssrc_forced_ = true;
1244 ssrc_db_.ReturnSSRC(ssrc_);
1245 ssrc_db_.RegisterSSRC(ssrc);
1246 ssrc_ = ssrc;
1247 if (!sequence_number_forced_) {
1248 sequence_number_ =
1249 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001250 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001251}
1252
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001253uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001254 CriticalSectionScoped cs(send_critsect_);
1255 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001256}
1257
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001258void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001259 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001260}
1261
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001262void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1263 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001264 assert(arr_length <= kRtpCsrcSize);
1265 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001266
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001267 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001268 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001269 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001270 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001271}
1272
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001273int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001274 assert(arr_of_csrc);
1275 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001276 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1277 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001278 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001279 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001280}
1281
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001282void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001283 CriticalSectionScoped cs(send_critsect_);
1284 sequence_number_forced_ = true;
1285 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001286}
1287
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001288uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001289 CriticalSectionScoped cs(send_critsect_);
1290 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001291}
1292
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001293// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001294int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1295 const uint16_t time_ms,
1296 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001297 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001298 return -1;
1299 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001300 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001301}
1302
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001303bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001304 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001305 return false;
1306 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001307 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001308}
1309
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001310int32_t RTPSender::SetAudioPacketSize(
1311 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001312 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001313 return -1;
1314 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001315 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001316}
1317
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001318int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1319 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001320 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001321 return -1;
1322 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001323 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001324}
1325
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001326int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1327 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001328 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001329}
1330
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001331int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001332 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001333}
1334
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001335int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001336 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001337 return -1;
1338 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001339 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001340}
1341
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001342int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001343 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001344 return -1;
1345 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001346 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001347}
1348
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001349// Video
1350VideoCodecInformation *RTPSender::CodecInformationVideo() {
1351 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001352 return NULL;
1353 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001354 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001355}
1356
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001357RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001358 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001359 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001360}
1361
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001362uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001363 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001364 return 0;
1365 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001366 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001367}
1368
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001369int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001370 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001371 return -1;
1372 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001373 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001374}
1375
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001376int32_t RTPSender::SetGenericFECStatus(
1377 const bool enable, const uint8_t payload_type_red,
1378 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001379 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001380 return -1;
1381 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001382 return video_->SetGenericFECStatus(enable, payload_type_red,
1383 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001384}
1385
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001386int32_t RTPSender::GenericFECStatus(
1387 bool *enable, uint8_t *payload_type_red,
1388 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001389 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001390 return -1;
1391 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001392 return video_->GenericFECStatus(
1393 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001394}
1395
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001396int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001397 const FecProtectionParams *delta_params,
1398 const FecProtectionParams *key_params) {
1399 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001400 return -1;
1401 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001402 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001403}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001404
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001405void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1406 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001407 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001408 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001409 // Add RTX header.
1410 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001411 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001412
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001413 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001414 rtp_parser.Parse(rtp_header);
1415
1416 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001417 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001418
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001419 // Replace payload type, if a specific type is set for RTX.
1420 if (payload_type_rtx_ != -1) {
1421 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001422 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001423 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1424 }
1425
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001426 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001427 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001428 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1429
1430 // Replace SSRC.
1431 ptr += 6;
1432 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1433
1434 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001435 ptr = data_buffer_rtx + rtp_header.headerLength;
1436 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001437 ptr += 2;
1438
1439 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001440 memcpy(ptr, buffer + rtp_header.headerLength,
1441 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001442 *length += 2;
1443}
1444
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001445} // namespace webrtc