blob: a0e27a8bd8cbdcdf1ad0caea2da4eab9dea13a2c [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
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000025namespace {
26
27const char* FrameTypeToString(const FrameType frame_type) {
28 switch (frame_type) {
29 case kFrameEmpty: return "empty";
30 case kAudioFrameSpeech: return "audio_speech";
31 case kAudioFrameCN: return "audio_cn";
32 case kVideoFrameKey: return "video_key";
33 case kVideoFrameDelta: return "video_delta";
34 case kVideoFrameGolden: return "video_golden";
35 case kVideoFrameAltRef: return "video_altref";
36 }
37 return "";
38}
39
40} // namespace
41
pbos@webrtc.org2f446732013-04-08 11:08:41 +000042RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000043 Transport *transport, RtpAudioFeedback *audio_feedback,
44 PacedSender *paced_sender)
45 : Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL),
46 video_(NULL), paced_sender_(paced_sender),
47 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
48 transport_(transport), sending_media_(true), // Default to sending media.
49 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
50 target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1),
51 payload_type_map_(), rtp_header_extension_map_(),
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +000052 transmission_time_offset_(0), absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000053 // NACK.
54 nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
55 packet_history_(new RTPPacketHistory(clock)),
56 // Statistics
57 packets_sent_(0), payload_bytes_sent_(0), start_time_stamp_forced_(false),
58 start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000059 remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false),
60 time_stamp_(0), csrcs_(0), csrc_(), include_csrcs_(true),
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +000061 rtx_(kRtxOff), payload_type_rtx_(-1) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000062 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
63 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
64 memset(csrc_, 0, sizeof(csrc_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000065 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000066 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000067 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000068 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
69 // Random start, 16 bits. Can't be 0.
70 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
71 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +000072
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000073 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000074 audio_ = new RTPSenderAudio(id, clock_, this);
75 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000076 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000077 video_ = new RTPSenderVideo(id, clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000078 }
79 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000080}
81
pwestin@webrtc.org00741872012-01-19 15:56:10 +000082RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000083 if (remote_ssrc_ != 0) {
84 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000085 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000086 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +000087
pwestin@webrtc.org00741872012-01-19 15:56:10 +000088 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000089 delete send_critsect_;
90 while (!payload_type_map_.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000091 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000092 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +000093 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000094 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000095 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000096 delete packet_history_;
97 delete audio_;
98 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +000099
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000100 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101}
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000103void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000104 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000106
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000107uint16_t RTPSender::ActualSendBitrateKbit() const {
108 return (uint16_t)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000109}
110
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000111uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000112 if (video_) {
113 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000114 }
115 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000116}
117
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000118uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000119 if (video_) {
120 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000121 }
122 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000123}
124
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000125uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000126 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000127}
128
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000129int32_t RTPSender::SetTransmissionTimeOffset(
130 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000131 if (transmission_time_offset > (0x800000 - 1) ||
132 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000133 return -1;
134 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000135 CriticalSectionScoped cs(send_critsect_);
136 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000137 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000138}
139
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000140int32_t RTPSender::SetAbsoluteSendTime(
141 const uint32_t absolute_send_time) {
142 if (absolute_send_time > 0xffffff) { // UWord24.
143 return -1;
144 }
145 CriticalSectionScoped cs(send_critsect_);
146 absolute_send_time_ = absolute_send_time;
147 return 0;
148}
149
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000150int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
151 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000152 CriticalSectionScoped cs(send_critsect_);
153 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000154}
155
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000156int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000157 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000158 CriticalSectionScoped cs(send_critsect_);
159 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000160}
161
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000162uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000163 CriticalSectionScoped cs(send_critsect_);
164 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000165}
166
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000167int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000168 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000169 const int8_t payload_number, const uint32_t frequency,
170 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000171 assert(payload_name);
172 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000173
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000174 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000175 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000177 if (payload_type_map_.end() != it) {
178 // We already use this payload type.
179 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000180 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000181
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000182 // Check if it's the same as we already have.
183 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000184 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000185 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000186 payload->typeSpecific.Audio.frequency == frequency &&
187 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000188 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000189 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000190 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000191 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000192 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000193 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000194 return 0;
195 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000196 }
197 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000198 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000199 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000200 ModuleRTPUtility::Payload *payload = NULL;
201 if (audio_configured_) {
202 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
203 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000204 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000205 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
206 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000207 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000208 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000209 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000210 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000211 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212}
213
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000214int32_t RTPSender::DeRegisterSendPayload(
215 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000216 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000217
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000218 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000219 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000220
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000221 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000222 return -1;
223 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000224 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000225 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000226 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000227 return 0;
228}
niklase@google.com470e71d2011-07-07 08:21:25 +0000229
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000230int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000231
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000232int RTPSender::SendPayloadFrequency() const { return audio_->AudioFrequency(); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000233
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000234int32_t RTPSender::SetMaxPayloadLength(
235 const uint16_t max_payload_length,
236 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000237 // Sanity check.
238 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
239 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
240 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000241 return -1;
242 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000243 CriticalSectionScoped cs(send_critsect_);
244 max_payload_length_ = max_payload_length;
245 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000246
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000247 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
248 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000249 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000250}
251
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000252uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000253 if (audio_configured_) {
254 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000255 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000256 return max_payload_length_ - RTPHeaderLength() -
257 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
258 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000259 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000260}
261
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000262uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000263 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000264}
265
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000266uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000267
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000268void RTPSender::SetRTXStatus(RtxMode mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000269 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000270 rtx_ = mode;
271 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000272 if (set_ssrc) {
273 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000274 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000275 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000276 }
277 }
278}
279
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000280void RTPSender::RTXStatus(RtxMode* mode, uint32_t* ssrc,
281 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000282 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000283 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000284 *ssrc = ssrc_rtx_;
285 *payload_type = payload_type_rtx_;
286}
287
288
289void RTPSender::SetRtxPayloadType(int payload_type) {
290 CriticalSectionScoped cs(send_critsect_);
291 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000292}
293
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000294int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
295 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000296 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000297
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000298 if (payload_type < 0) {
299 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
300 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000301 return -1;
302 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000303 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000304 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000305 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000306 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000307 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000308 // And it's a match...
309 return 0;
310 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000311 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000312 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000313 if (payload_type_ == payload_type) {
314 if (!audio_configured_) {
315 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000316 }
317 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000318 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000319 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000320 payload_type_map_.find(payload_type);
321 if (it == payload_type_map_.end()) {
322 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
323 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000324 return -1;
325 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000326 payload_type_ = payload_type;
327 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000328 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000329 if (!payload->audio && !audio_configured_) {
330 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
331 *video_type = payload->typeSpecific.Video.videoCodecType;
332 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000333 }
334 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000335}
336
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000337int32_t RTPSender::SendOutgoingData(
338 const FrameType frame_type, const int8_t payload_type,
339 const uint32_t capture_timestamp, int64_t capture_time_ms,
340 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000341 const RTPFragmentationHeader *fragmentation,
342 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000343 {
344 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000345 CriticalSectionScoped cs(send_critsect_);
346 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000347 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000348 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000349 }
pbos@webrtc.org8911ce42013-03-18 16:39:03 +0000350 RtpVideoCodecTypes video_type = kRtpGenericVideo;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000351 if (CheckPayloadType(payload_type, &video_type) != 0) {
352 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
353 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000354 __FUNCTION__, payload_type);
355 return -1;
356 }
357
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000358 if (frame_type == kVideoFrameKey) {
359 TRACE_EVENT_INSTANT1("webrtc_rtp", "SendKeyFrame",
360 "timestamp", capture_timestamp);
361 } else {
362 TRACE_EVENT_INSTANT2("webrtc_rtp", "SendFrame",
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000363 "timestamp", capture_timestamp,
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000364 "frame_type", FrameTypeToString(frame_type));
365 }
366
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000367 if (audio_configured_) {
368 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000369 frame_type == kFrameEmpty);
370
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000371 return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
372 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000373 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000374 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000375
376 if (frame_type == kFrameEmpty) {
377 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
378 capture_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000379 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000380 return video_->SendVideo(video_type, frame_type, payload_type,
381 capture_timestamp, capture_time_ms, payload_data,
382 payload_size, fragmentation, codec_info,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000383 rtp_type_hdr);
384 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000385}
386
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000387int32_t RTPSender::SendPaddingAccordingToBitrate(
388 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000389 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000390 // Current bitrate since last estimate(1 second) averaged with the
391 // estimate since then, to get the most up to date bitrate.
392 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000393 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000394 if (bitrate_diff <= 0) {
395 return 0;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000396 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000397 int bytes = 0;
398 if (current_bitrate == 0) {
399 // Start up phase. Send one 33.3 ms batch to start with.
400 bytes = (bitrate_diff / 8) / 30;
401 } else {
402 bytes = (bitrate_diff / 8);
403 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000404 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000405 if (bytes > bytes_cap) {
406 bytes = bytes_cap;
407 }
408 }
409 return SendPadData(payload_type, capture_timestamp, capture_time_ms, bytes);
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000410}
411
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000412int32_t RTPSender::SendPadData(
413 int8_t payload_type, uint32_t capture_timestamp,
414 int64_t capture_time_ms, int32_t bytes) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000415 // Drop this packet if we're not sending media packets.
416 if (!sending_media_) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000417 return 0;
418 }
419 // Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
420 int max_length = 224;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000421 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000422
423 for (; bytes > 0; bytes -= max_length) {
asapersson@webrtc.org63a34f42012-04-20 13:20:27 +0000424 int padding_bytes_in_packet = max_length;
425 if (bytes < max_length) {
426 padding_bytes_in_packet = (bytes + 16) & 0xffe0; // Keep our modulus 32.
427 }
428 if (padding_bytes_in_packet < 32) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000429 // Sanity don't send empty packets.
430 break;
asapersson@webrtc.org63a34f42012-04-20 13:20:27 +0000431 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000432 // Correct seq num, timestamp and payload type.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000433 int header_length = BuildRTPheader(
434 data_buffer, payload_type, false, // No markerbit.
435 capture_timestamp, true, // Timestamp provided.
436 true); // Increment sequence number.
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000437 data_buffer[0] |= 0x20; // Set padding bit.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000438 int32_t *data =
439 reinterpret_cast<int32_t *>(&(data_buffer[header_length]));
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000440
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000441 // Fill data buffer with random data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000442 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
443 data[j] = rand(); // NOLINT
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000444 }
445 // Set number of padding bytes in the last byte of the packet.
446 data_buffer[header_length + padding_bytes_in_packet - 1] =
447 padding_bytes_in_packet;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000448 // Send the packet.
449 if (0 > SendToNetwork(data_buffer, padding_bytes_in_packet, header_length,
450 capture_time_ms, kDontRetransmit)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000451 // Error sending the packet.
452 break;
453 }
454 }
455 if (bytes > 31) { // 31 due to our modulus 32.
456 // We did not manage to send all bytes.
457 return -1;
458 }
459 return 0;
460}
461
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000462void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000463 const uint16_t number_to_store) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000464 packet_history_->SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000465}
466
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000467bool RTPSender::StorePackets() const {
468 return packet_history_->StorePackets();
469}
niklase@google.com470e71d2011-07-07 08:21:25 +0000470
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000471int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
472 uint16_t length = IP_PACKET_SIZE;
473 uint8_t data_buffer[IP_PACKET_SIZE];
474 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000475 int64_t capture_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000476 StorageType type;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000477 if (!packet_history_->GetRTPPacket(packet_id, min_resend_time, data_buffer,
478 &length, &capture_time_ms, &type)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000479 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000480 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000481 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000482 if (length == 0 || type == kDontRetransmit) {
483 // No bytes copied (packet recently resent, skip resending) or
484 // packet should not be retransmitted.
485 return 0;
486 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000487
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000488 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000489 if (rtx_ != kRtxOff) {
490 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000491 buffer_to_send_ptr = data_buffer_rtx;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000492 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000493
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000494 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
495 WebRtcRTPHeader rtp_header;
496 rtp_parser.Parse(rtp_header);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000497
498 // Store the time when the packet was last sent or added to pacer.
499 packet_history_->UpdateResendTime(packet_id);
500
501 {
502 // Update send statistics prior to pacer.
503 CriticalSectionScoped cs(send_critsect_);
504 Bitrate::Update(length);
505 packets_sent_++;
506 // We on purpose don't add to payload_bytes_sent_ since this is a
507 // re-transmit and not new payload data.
508 }
509
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000510 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
511 "timestamp", rtp_header.header.timestamp,
512 "seqnum", rtp_header.header.sequenceNumber);
513
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000514 if (paced_sender_) {
515 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
516 rtp_header.header.ssrc,
517 rtp_header.header.sequenceNumber,
518 capture_time_ms,
519 length)) {
520 // We can't send the packet right now.
521 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000522 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000523 }
524 }
525
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000526 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000527 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000528 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000529 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000530}
531
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000532bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
533 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000534 if (transport_) {
535 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000536 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000537 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
538 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000539 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000540 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000541 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
542 "Transport failed to send packet");
543 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000544 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000545 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000546}
547
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000548int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000549 if (!video_)
550 return -1;
551 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000552}
553
554int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000555 if (!video_)
556 return -1;
557 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000558}
559
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000560void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000561 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000562 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000563 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
564 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000565 const int64_t now = clock_->TimeInMilliseconds();
566 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000567
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000568 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000569 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000570 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000571 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000572 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000573 return;
574 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000575
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000576 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
577 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000578 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000579 if (bytes_sent > 0) {
580 bytes_re_sent += bytes_sent;
581 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000582 // The packet has previously been resent.
583 // Try resending next packet in the list.
584 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000585 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000586 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000587 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000588 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000589 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000590 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000591 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000592 // Delay bandwidth estimate (RTT * BW).
593 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000594 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000595 uint32_t target_bytes =
596 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000597 if (bytes_re_sent > target_bytes) {
598 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000599 }
600 }
601 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000602 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000603 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000604 UpdateNACKBitRate(bytes_re_sent, now);
605 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000606 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000607}
608
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000609bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
610 uint32_t num = 0;
611 int32_t byte_count = 0;
612 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000613
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000614 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000615
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000616 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000617 return true;
618 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000619 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
620 if ((now - nack_byte_count_times_[num]) > avg_interval) {
621 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000622 break;
623 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000624 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000625 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000626 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000627 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000628 if (num == NACK_BYTECOUNT_SIZE) {
629 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000630 // during the last msg_interval.
631 time_interval = now - nack_byte_count_times_[num - 1];
632 if (time_interval < 0) {
633 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000634 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000635 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000636 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000637}
638
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000639void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
640 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000641 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000642
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000643 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000644 if (bytes > 0) {
645 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000646 // Add padding length.
647 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000648 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000649 if (nack_byte_count_times_[0] == 0) {
650 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000651 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000652 // Shift.
653 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
654 nack_byte_count_[i + 1] = nack_byte_count_[i];
655 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000656 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000657 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000658 nack_byte_count_[0] = bytes;
659 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000660 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000661 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000662}
663
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000664// Called from pacer when we can send the packet.
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000665void RTPSender::TimeToSendPacket(uint16_t sequence_number,
666 int64_t capture_time_ms) {
667 StorageType type;
668 uint16_t length = IP_PACKET_SIZE;
669 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000670 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000671
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000672 if (packet_history_ == NULL) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000673 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000674 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000675 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
676 &stored_time_ms, &type)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000677 return;
678 }
679 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000680
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000681 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000682 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000683 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000684 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
685 "timestamp", rtp_header.header.timestamp,
686 "seqnum", sequence_number);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000687
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000688 int64_t now_ms = clock_->TimeInMilliseconds();
689 int64_t diff_ms = now_ms - capture_time_ms;
690 bool updated_transmission_time_offset =
691 UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms);
692 bool updated_abs_send_time =
693 UpdateAbsoluteSendTime(data_buffer, length, rtp_header, now_ms);
694 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000695 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000696 packet_history_->ReplaceRTPHeader(data_buffer,
697 rtp_header.header.sequenceNumber,
698 rtp_header.header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000699 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000700 SendPacketToNetwork(data_buffer, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000701}
702
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000703// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000704int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000705 uint8_t *buffer, int payload_length, int rtp_header_length,
706 int64_t capture_time_ms, StorageType storage) {
707 ModuleRTPUtility::RTPHeaderParser rtp_parser(
708 buffer, payload_length + rtp_header_length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000709 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000710 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000711
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000712 int64_t now_ms = clock_->TimeInMilliseconds();
713
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000714 // |capture_time_ms| <= 0 is considered invalid.
715 // TODO(holmer): This should be changed all over Video Engine so that negative
716 // time is consider invalid, while 0 is considered a valid time.
717 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000718 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000719 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000720 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000721
722 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
723 rtp_header, now_ms);
724
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000725 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000726 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
727 max_payload_length_, capture_time_ms,
728 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000729 return -1;
730 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000731
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000732 // Create and send RTX Packet.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000733 // TODO(pwesin): This should be moved to its own code path triggered by pacer.
734 bool rtx_sent = false;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000735 if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000736 uint16_t length_rtx = payload_length + rtp_header_length;
737 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000738 BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000739 if (!SendPacketToNetwork(data_buffer_rtx, length_rtx)) return -1;
740 rtx_sent = true;
741 }
742 {
743 // Update send statistics prior to pacer.
744 CriticalSectionScoped cs(send_critsect_);
745 Bitrate::Update(payload_length + rtp_header_length);
746 ++packets_sent_;
747 payload_bytes_sent_ += payload_length;
748 if (rtx_sent) {
749 // The RTX packet.
750 ++packets_sent_;
751 payload_bytes_sent_ += payload_length;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000752 }
753 }
754
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000755 if (paced_sender_ && storage != kDontStore) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000756 if (!paced_sender_->SendPacket(
757 PacedSender::kNormalPriority, rtp_header.header.ssrc,
758 rtp_header.header.sequenceNumber, capture_time_ms,
759 payload_length + rtp_header_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000760 // We can't send the packet right now.
761 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000762 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000763 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000764 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000765 if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
766 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000767 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000768 return -1;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000769}
770
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000771void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000772 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000773 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000774 nack_bitrate_.Process();
775 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000776 return;
777 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000778 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000779}
780
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000781uint16_t RTPSender::RTPHeaderLength() const {
782 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000783 if (include_csrcs_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000784 rtp_header_length += sizeof(uint32_t) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000785 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000786 rtp_header_length += RtpHeaderExtensionTotalLength();
787 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000788}
789
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000790uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000791 CriticalSectionScoped cs(send_critsect_);
792 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000793}
794
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000795void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000796 packets_sent_ = 0;
797 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000798}
799
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000800uint32_t RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000801 // Don't use critsect to avoid potential deadlock.
802 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000803}
804
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000805// Number of sent RTP bytes.
806// Don't use critsect to avoid potental deadlock.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000807uint32_t RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000808 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000809}
810
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000811int32_t RTPSender::BuildRTPheader(
812 uint8_t *data_buffer, const int8_t payload_type,
813 const bool marker_bit, const uint32_t capture_time_stamp,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000814 const bool time_stamp_provided, const bool inc_sequence_number) {
815 assert(payload_type >= 0);
816 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000817
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000818 data_buffer[0] = static_cast<uint8_t>(0x80); // version 2.
819 data_buffer[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000820 if (marker_bit) {
821 data_buffer[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000822 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000823 if (time_stamp_provided) {
824 time_stamp_ = start_time_stamp_ + capture_time_stamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000825 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000826 // Make a unique time stamp.
827 // We can't inc by the actual time, since then we increase the risk of back
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000828 // timing.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000829 time_stamp_++;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000830 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000831 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + 2, sequence_number_);
832 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 4, time_stamp_);
833 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 8, ssrc_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000834 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000835
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000836 // Add the CSRCs if any.
837 if (include_csrcs_ && csrcs_ > 0) {
838 if (csrcs_ > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000839 // error
840 assert(false);
841 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000842 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000843 uint8_t *ptr = &data_buffer[rtp_header_length];
844 for (uint32_t i = 0; i < csrcs_; ++i) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000845 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrc_[i]);
846 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000847 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000848 data_buffer[0] = (data_buffer[0] & 0xf0) | csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000849
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000850 // Update length of header.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000851 rtp_header_length += sizeof(uint32_t) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000852 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000853 sequence_number_++; // Prepare for next packet.
niklase@google.com470e71d2011-07-07 08:21:25 +0000854
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000855 uint16_t len = BuildRTPHeaderExtension(data_buffer + rtp_header_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000856 if (len) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000857 data_buffer[0] |= 0x10; // Set extension bit.
858 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000859 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000860 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000861}
862
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000863uint16_t RTPSender::BuildRTPHeaderExtension(
864 uint8_t *data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000865 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000866 return 0;
867 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000868 // RTP header extension, RFC 3550.
869 // 0 1 2 3
870 // 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
871 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
872 // | defined by profile | length |
873 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
874 // | header extension |
875 // | .... |
876 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000877 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000878 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000879
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000880 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000881 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000882 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000883
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000884 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000885 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000886
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000887 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000888 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000889 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000890 switch (type) {
891 case kRtpExtensionTransmissionTimeOffset:
892 block_length = BuildTransmissionTimeOffsetExtension(
893 data_buffer + kHeaderLength + total_block_length);
894 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +0000895 case kRtpExtensionAudioLevel:
896 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
897 // we don't have to care about it here, which is true until we wan't to
898 // use it together with any of the other extensions we support.
899 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000900 case kRtpExtensionAbsoluteSendTime:
901 block_length = BuildAbsoluteSendTimeExtension(
902 data_buffer + kHeaderLength + total_block_length);
903 break;
904 default:
905 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000906 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000907 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000908 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000909 }
910 if (total_block_length == 0) {
911 // No extension added.
912 return 0;
913 }
914 // Set header length (in number of Word32, header excluded).
915 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000916 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000917 total_block_length / 4);
918 // Total added length.
919 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000920}
921
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000922uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
923 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000924 // From RFC 5450: Transmission Time Offsets in RTP Streams.
925 //
926 // The transmission time is signaled to the receiver in-band using the
927 // general mechanism for RTP header extensions [RFC5285]. The payload
928 // of this extension (the transmitted value) is a 24-bit signed integer.
929 // When added to the RTP timestamp of the packet, it represents the
930 // "effective" RTP transmission time of the packet, on the RTP
931 // timescale.
932 //
933 // The form of the transmission offset extension block:
934 //
935 // 0 1 2 3
936 // 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
937 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
938 // | ID | len=2 | transmission offset |
939 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000940
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000941 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000942 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000943 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
944 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000945 // Not registered.
946 return 0;
947 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000948 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000949 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000950 data_buffer[pos++] = (id << 4) + len;
951 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
952 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000953 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000954 assert(pos == kTransmissionTimeOffsetLength);
955 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000956}
957
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000958uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
959 uint8_t* data_buffer) const {
960 // Absolute send time in RTP streams.
961 //
962 // The absolute send time is signaled to the receiver in-band using the
963 // general mechanism for RTP header extensions [RFC5285]. The payload
964 // of this extension (the transmitted value) is a 24-bit unsigned integer
965 // containing the sender's current time in seconds as a fixed point number
966 // with 18 bits fractional part.
967 //
968 // The form of the absolute send time extension block:
969 //
970 // 0 1 2 3
971 // 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
972 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
973 // | ID | len=2 | absolute send time |
974 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
975
976 // Get id defined by user.
977 uint8_t id;
978 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
979 &id) != 0) {
980 // Not registered.
981 return 0;
982 }
983 size_t pos = 0;
984 const uint8_t len = 2;
985 data_buffer[pos++] = (id << 4) + len;
986 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
987 absolute_send_time_);
988 pos += 3;
989 assert(pos == kAbsoluteSendTimeLength);
990 return kAbsoluteSendTimeLength;
991}
992
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000993bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000994 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
995 const WebRtcRTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000996 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000997
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000998 // Get length until start of header extension block.
999 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001000 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001001 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001002 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001003 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001004 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001005 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001006 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001007 int block_pos = 12 + rtp_header.header.numCSRCs + extension_block_pos;
1008 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
1009 rtp_header.header.headerLength <
1010 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001011 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001012 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001013 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001014 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001015 // Verify that header contains extension.
1016 if (!((rtp_packet[12 + rtp_header.header.numCSRCs] == 0xBE) &&
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001017 (rtp_packet[12 + rtp_header.header.numCSRCs + 1] == 0xDE))) {
1018 WEBRTC_TRACE(
1019 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001020 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001021 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001022 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001023 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001024 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001025 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1026 &id) != 0) {
1027 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001028 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001029 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001030 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001031 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001032 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001033 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001034 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001035 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001036 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001037 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001038 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001039 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001040 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001041 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001042}
1043
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001044bool RTPSender::UpdateAbsoluteSendTime(
1045 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
1046 const WebRtcRTPHeader &rtp_header, const int64_t now_ms) const {
1047 CriticalSectionScoped cs(send_critsect_);
1048
1049 // Get length until start of header extension block.
1050 int extension_block_pos =
1051 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1052 kRtpExtensionAbsoluteSendTime);
1053 if (extension_block_pos < 0) {
1054 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1055 "Failed to update absolute send time, not registered.");
1056 return false;
1057 }
1058 int block_pos = 12 + rtp_header.header.numCSRCs + extension_block_pos;
1059 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
1060 rtp_header.header.headerLength < block_pos + kAbsoluteSendTimeLength) {
1061 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1062 "Failed to update absolute send time, invalid length.");
1063 return false;
1064 }
1065 // Verify that header contains extension.
1066 if (!((rtp_packet[12 + rtp_header.header.numCSRCs] == 0xBE) &&
1067 (rtp_packet[12 + rtp_header.header.numCSRCs + 1] == 0xDE))) {
1068 WEBRTC_TRACE(
1069 kTraceStream, kTraceRtpRtcp, id_,
1070 "Failed to update absolute send time, hdr extension not found.");
1071 return false;
1072 }
1073 // Get id.
1074 uint8_t id = 0;
1075 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1076 &id) != 0) {
1077 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1078 "Failed to update absolute send time, no id.");
1079 return false;
1080 }
1081 // Verify first byte in block.
1082 const uint8_t first_block_byte = (id << 4) + 2;
1083 if (rtp_packet[block_pos] != first_block_byte) {
1084 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1085 "Failed to update absolute send time.");
1086 return false;
1087 }
1088 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1089 // fractional part).
1090 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1091 ((now_ms << 18) / 1000) & 0x00ffffff);
1092 return true;
1093}
1094
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001095void RTPSender::SetSendingStatus(const bool enabled) {
1096 if (enabled) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001097 uint32_t frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001098 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001099 uint32_t frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001100
1101 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001102 switch (frequency) {
1103 case 8000:
1104 case 12000:
1105 case 16000:
1106 case 24000:
1107 case 32000:
1108 break;
1109 default:
1110 assert(false);
1111 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001112 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +00001113 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001114 } else {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +00001115 frequency_hz = kDefaultVideoFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001116 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001117 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001118
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001119 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001120 SetStartTimestamp(RTPtime, false);
1121 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001122 if (!ssrc_forced_) {
1123 // Generate a new SSRC.
1124 ssrc_db_.ReturnSSRC(ssrc_);
1125 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001126 }
1127 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001128 if (!sequence_number_forced_ && !ssrc_forced_) {
1129 // Generate a new sequence number.
1130 sequence_number_ =
1131 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001132 }
1133 }
1134}
1135
1136void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001137 CriticalSectionScoped cs(send_critsect_);
1138 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001139}
1140
1141bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001142 CriticalSectionScoped cs(send_critsect_);
1143 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001144}
1145
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001146uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001147 CriticalSectionScoped cs(send_critsect_);
1148 return time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001149}
1150
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001151void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001152 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001153 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001154 start_time_stamp_forced_ = force;
1155 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001156 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001157 if (!start_time_stamp_forced_) {
1158 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001159 }
1160 }
1161}
1162
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001163uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001164 CriticalSectionScoped cs(send_critsect_);
1165 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001166}
1167
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001168uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001169 // If configured via API, return 0.
1170 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001171
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001172 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001173 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001174 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001175 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1176 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001177}
1178
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001179void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001180 // This is configured via the API.
1181 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001182
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001183 if (ssrc_ == ssrc && ssrc_forced_) {
1184 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001185 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001186 ssrc_forced_ = true;
1187 ssrc_db_.ReturnSSRC(ssrc_);
1188 ssrc_db_.RegisterSSRC(ssrc);
1189 ssrc_ = ssrc;
1190 if (!sequence_number_forced_) {
1191 sequence_number_ =
1192 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001193 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001194}
1195
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001196uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001197 CriticalSectionScoped cs(send_critsect_);
1198 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001199}
1200
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001201void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001202 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001203}
1204
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001205void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1206 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001207 assert(arr_length <= kRtpCsrcSize);
1208 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001209
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001210 for (int i = 0; i < arr_length; i++) {
1211 csrc_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001212 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001213 csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001214}
1215
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001216int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001217 assert(arr_of_csrc);
1218 CriticalSectionScoped cs(send_critsect_);
1219 for (int i = 0; i < csrcs_ && i < kRtpCsrcSize; i++) {
1220 arr_of_csrc[i] = csrc_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001221 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001222 return csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001223}
1224
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001225void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001226 CriticalSectionScoped cs(send_critsect_);
1227 sequence_number_forced_ = true;
1228 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001229}
1230
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001231uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001232 CriticalSectionScoped cs(send_critsect_);
1233 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001234}
1235
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001236// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001237int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1238 const uint16_t time_ms,
1239 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001240 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001241 return -1;
1242 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001243 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001244}
1245
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001246bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001247 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001248 return false;
1249 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001250 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001251}
1252
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001253int32_t RTPSender::SetAudioPacketSize(
1254 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001255 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001256 return -1;
1257 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001258 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001259}
1260
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001261int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1262 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001263 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001264 return -1;
1265 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001266 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001267}
1268
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001269int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1270 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001271 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001272}
1273
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001274int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001275 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001276}
1277
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001278int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001279 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001280 return -1;
1281 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001282 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001283}
1284
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001285int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001286 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001287 return -1;
1288 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001289 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001290}
1291
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001292// Video
1293VideoCodecInformation *RTPSender::CodecInformationVideo() {
1294 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001295 return NULL;
1296 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001297 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001298}
1299
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001300RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001301 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001302 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001303}
1304
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001305uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001306 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001307 return 0;
1308 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001309 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001310}
1311
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001312int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001313 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001314 return -1;
1315 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001316 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001317}
1318
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001319int32_t RTPSender::SetGenericFECStatus(
1320 const bool enable, const uint8_t payload_type_red,
1321 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001322 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001323 return -1;
1324 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001325 return video_->SetGenericFECStatus(enable, payload_type_red,
1326 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001327}
1328
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001329int32_t RTPSender::GenericFECStatus(
1330 bool *enable, uint8_t *payload_type_red,
1331 uint8_t *payload_type_fec) const {
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_->GenericFECStatus(
1336 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001337}
1338
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001339int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001340 const FecProtectionParams *delta_params,
1341 const FecProtectionParams *key_params) {
1342 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001343 return -1;
1344 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001345 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001346}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001347
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001348void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1349 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001350 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001351 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001352 // Add RTX header.
1353 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001354 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001355
1356 WebRtcRTPHeader rtp_header;
1357 rtp_parser.Parse(rtp_header);
1358
1359 // Add original RTP header.
1360 memcpy(data_buffer_rtx, buffer, rtp_header.header.headerLength);
1361
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001362 // Replace payload type, if a specific type is set for RTX.
1363 if (payload_type_rtx_ != -1) {
1364 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
1365 if (rtp_header.header.markerBit)
1366 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1367 }
1368
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001369 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001370 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001371 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1372
1373 // Replace SSRC.
1374 ptr += 6;
1375 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1376
1377 // Add OSN (original sequence number).
1378 ptr = data_buffer_rtx + rtp_header.header.headerLength;
1379 ModuleRTPUtility::AssignUWord16ToBuffer(ptr,
1380 rtp_header.header.sequenceNumber);
1381 ptr += 2;
1382
1383 // Add original payload data.
1384 memcpy(ptr, buffer + rtp_header.header.headerLength,
1385 *length - rtp_header.header.headerLength);
1386 *length += 2;
1387}
1388
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001389} // namespace webrtc