blob: 5bbf3c5393c9378acca692b734871b0de379ae01 [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);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000513 if (paced_sender_) {
514 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
515 rtp_header.header.ssrc,
516 rtp_header.header.sequenceNumber,
517 capture_time_ms,
518 length)) {
519 // We can't send the packet right now.
520 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000521 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000522 }
523 }
524
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000525 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000526 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000527 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000528 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000529}
530
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000531bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
532 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000533 if (transport_) {
534 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000535 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000536 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
537 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000538 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000539 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000540 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
541 "Transport failed to send packet");
542 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000543 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000544 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000545}
546
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000547int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000548 if (!video_)
549 return -1;
550 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000551}
552
553int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000554 if (!video_)
555 return -1;
556 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000557}
558
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000559void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000560 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000561 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000562 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
563 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000564 const int64_t now = clock_->TimeInMilliseconds();
565 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000566
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000567 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000568 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000569 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000570 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000571 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000572 return;
573 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000574
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000575 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
576 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000577 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000578 if (bytes_sent > 0) {
579 bytes_re_sent += bytes_sent;
580 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000581 // The packet has previously been resent.
582 // Try resending next packet in the list.
583 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000584 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000585 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000586 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000587 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000588 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000589 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000590 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000591 // Delay bandwidth estimate (RTT * BW).
592 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000593 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000594 uint32_t target_bytes =
595 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000596 if (bytes_re_sent > target_bytes) {
597 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000598 }
599 }
600 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000601 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000602 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000603 UpdateNACKBitRate(bytes_re_sent, now);
604 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000605 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000606}
607
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000608bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
609 uint32_t num = 0;
610 int32_t byte_count = 0;
611 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000612
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000613 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000614
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000615 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000616 return true;
617 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000618 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
619 if ((now - nack_byte_count_times_[num]) > avg_interval) {
620 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000621 break;
622 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000623 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000624 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000625 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000626 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000627 if (num == NACK_BYTECOUNT_SIZE) {
628 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000629 // during the last msg_interval.
630 time_interval = now - nack_byte_count_times_[num - 1];
631 if (time_interval < 0) {
632 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000633 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000634 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000635 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000636}
637
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000638void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
639 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000640 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000641
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000642 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000643 if (bytes > 0) {
644 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000645 // Add padding length.
646 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000647 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000648 if (nack_byte_count_times_[0] == 0) {
649 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000650 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000651 // Shift.
652 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
653 nack_byte_count_[i + 1] = nack_byte_count_[i];
654 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000655 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000656 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000657 nack_byte_count_[0] = bytes;
658 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000659 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000660 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000661}
662
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000663// Called from pacer when we can send the packet.
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000664void RTPSender::TimeToSendPacket(uint16_t sequence_number,
665 int64_t capture_time_ms) {
666 StorageType type;
667 uint16_t length = IP_PACKET_SIZE;
668 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000669 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000670
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000671 if (packet_history_ == NULL) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000672 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000673 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000674 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
675 &stored_time_ms, &type)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000676 return;
677 }
678 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000679
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000680 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000681 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000682 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000683 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
684 "timestamp", rtp_header.header.timestamp,
685 "seqnum", sequence_number);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000686
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000687 int64_t now_ms = clock_->TimeInMilliseconds();
688 int64_t diff_ms = now_ms - capture_time_ms;
689 bool updated_transmission_time_offset =
690 UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms);
691 bool updated_abs_send_time =
692 UpdateAbsoluteSendTime(data_buffer, length, rtp_header, now_ms);
693 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000694 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000695 packet_history_->ReplaceRTPHeader(data_buffer,
696 rtp_header.header.sequenceNumber,
697 rtp_header.header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000698 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000699 SendPacketToNetwork(data_buffer, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000700}
701
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000702// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000703int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000704 uint8_t *buffer, int payload_length, int rtp_header_length,
705 int64_t capture_time_ms, StorageType storage) {
706 ModuleRTPUtility::RTPHeaderParser rtp_parser(
707 buffer, payload_length + rtp_header_length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000708 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000709 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000710
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000711 int64_t now_ms = clock_->TimeInMilliseconds();
712
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000713 // |capture_time_ms| <= 0 is considered invalid.
714 // TODO(holmer): This should be changed all over Video Engine so that negative
715 // time is consider invalid, while 0 is considered a valid time.
716 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000717 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000718 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000719 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000720
721 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
722 rtp_header, now_ms);
723
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000724 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000725 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
726 max_payload_length_, capture_time_ms,
727 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000728 return -1;
729 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000730
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000731 // Create and send RTX Packet.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000732 // TODO(pwesin): This should be moved to its own code path triggered by pacer.
733 bool rtx_sent = false;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000734 if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000735 uint16_t length_rtx = payload_length + rtp_header_length;
736 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000737 BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000738 if (!SendPacketToNetwork(data_buffer_rtx, length_rtx)) return -1;
739 rtx_sent = true;
740 }
741 {
742 // Update send statistics prior to pacer.
743 CriticalSectionScoped cs(send_critsect_);
744 Bitrate::Update(payload_length + rtp_header_length);
745 ++packets_sent_;
746 payload_bytes_sent_ += payload_length;
747 if (rtx_sent) {
748 // The RTX packet.
749 ++packets_sent_;
750 payload_bytes_sent_ += payload_length;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000751 }
752 }
753
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000754 if (paced_sender_ && storage != kDontStore) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000755 if (!paced_sender_->SendPacket(
756 PacedSender::kNormalPriority, rtp_header.header.ssrc,
757 rtp_header.header.sequenceNumber, capture_time_ms,
758 payload_length + rtp_header_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000759 // We can't send the packet right now.
760 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000761 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000762 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000763 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000764 if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
765 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000766 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000767 return -1;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000768}
769
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000770void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000771 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000772 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000773 nack_bitrate_.Process();
774 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000775 return;
776 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000777 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000778}
779
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000780uint16_t RTPSender::RTPHeaderLength() const {
781 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000782 if (include_csrcs_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000783 rtp_header_length += sizeof(uint32_t) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000784 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000785 rtp_header_length += RtpHeaderExtensionTotalLength();
786 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000787}
788
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000789uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000790 CriticalSectionScoped cs(send_critsect_);
791 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000792}
793
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000794void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000795 packets_sent_ = 0;
796 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000797}
798
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000799uint32_t RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000800 // Don't use critsect to avoid potential deadlock.
801 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000802}
803
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000804// Number of sent RTP bytes.
805// Don't use critsect to avoid potental deadlock.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000806uint32_t RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000807 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000808}
809
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000810int32_t RTPSender::BuildRTPheader(
811 uint8_t *data_buffer, const int8_t payload_type,
812 const bool marker_bit, const uint32_t capture_time_stamp,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000813 const bool time_stamp_provided, const bool inc_sequence_number) {
814 assert(payload_type >= 0);
815 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000816
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000817 data_buffer[0] = static_cast<uint8_t>(0x80); // version 2.
818 data_buffer[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000819 if (marker_bit) {
820 data_buffer[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000821 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000822 if (time_stamp_provided) {
823 time_stamp_ = start_time_stamp_ + capture_time_stamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000824 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000825 // Make a unique time stamp.
826 // We can't inc by the actual time, since then we increase the risk of back
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000827 // timing.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000828 time_stamp_++;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000829 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000830 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + 2, sequence_number_);
831 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 4, time_stamp_);
832 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 8, ssrc_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000833 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000834
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000835 // Add the CSRCs if any.
836 if (include_csrcs_ && csrcs_ > 0) {
837 if (csrcs_ > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000838 // error
839 assert(false);
840 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000841 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000842 uint8_t *ptr = &data_buffer[rtp_header_length];
843 for (uint32_t i = 0; i < csrcs_; ++i) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000844 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrc_[i]);
845 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000846 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000847 data_buffer[0] = (data_buffer[0] & 0xf0) | csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000848
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000849 // Update length of header.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000850 rtp_header_length += sizeof(uint32_t) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000851 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000852 sequence_number_++; // Prepare for next packet.
niklase@google.com470e71d2011-07-07 08:21:25 +0000853
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000854 uint16_t len = BuildRTPHeaderExtension(data_buffer + rtp_header_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000855 if (len) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000856 data_buffer[0] |= 0x10; // Set extension bit.
857 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000858 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000859 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000860}
861
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000862uint16_t RTPSender::BuildRTPHeaderExtension(
863 uint8_t *data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000864 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000865 return 0;
866 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000867 // RTP header extension, RFC 3550.
868 // 0 1 2 3
869 // 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
870 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
871 // | defined by profile | length |
872 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
873 // | header extension |
874 // | .... |
875 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000876 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000877 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000878
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000879 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000880 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000881 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000882
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000883 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000884 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000885
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000886 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000887 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000888 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000889 switch (type) {
890 case kRtpExtensionTransmissionTimeOffset:
891 block_length = BuildTransmissionTimeOffsetExtension(
892 data_buffer + kHeaderLength + total_block_length);
893 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +0000894 case kRtpExtensionAudioLevel:
895 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
896 // we don't have to care about it here, which is true until we wan't to
897 // use it together with any of the other extensions we support.
898 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000899 case kRtpExtensionAbsoluteSendTime:
900 block_length = BuildAbsoluteSendTimeExtension(
901 data_buffer + kHeaderLength + total_block_length);
902 break;
903 default:
904 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000905 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000906 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000907 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000908 }
909 if (total_block_length == 0) {
910 // No extension added.
911 return 0;
912 }
913 // Set header length (in number of Word32, header excluded).
914 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000915 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000916 total_block_length / 4);
917 // Total added length.
918 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000919}
920
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000921uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
922 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000923 // From RFC 5450: Transmission Time Offsets in RTP Streams.
924 //
925 // The transmission time is signaled to the receiver in-band using the
926 // general mechanism for RTP header extensions [RFC5285]. The payload
927 // of this extension (the transmitted value) is a 24-bit signed integer.
928 // When added to the RTP timestamp of the packet, it represents the
929 // "effective" RTP transmission time of the packet, on the RTP
930 // timescale.
931 //
932 // The form of the transmission offset extension block:
933 //
934 // 0 1 2 3
935 // 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
936 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
937 // | ID | len=2 | transmission offset |
938 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000939
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000940 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000941 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000942 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
943 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000944 // Not registered.
945 return 0;
946 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000947 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000948 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000949 data_buffer[pos++] = (id << 4) + len;
950 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
951 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000952 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000953 assert(pos == kTransmissionTimeOffsetLength);
954 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000955}
956
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000957uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
958 uint8_t* data_buffer) const {
959 // Absolute send time in RTP streams.
960 //
961 // The absolute send time is signaled to the receiver in-band using the
962 // general mechanism for RTP header extensions [RFC5285]. The payload
963 // of this extension (the transmitted value) is a 24-bit unsigned integer
964 // containing the sender's current time in seconds as a fixed point number
965 // with 18 bits fractional part.
966 //
967 // The form of the absolute send time extension block:
968 //
969 // 0 1 2 3
970 // 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
971 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
972 // | ID | len=2 | absolute send time |
973 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
974
975 // Get id defined by user.
976 uint8_t id;
977 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
978 &id) != 0) {
979 // Not registered.
980 return 0;
981 }
982 size_t pos = 0;
983 const uint8_t len = 2;
984 data_buffer[pos++] = (id << 4) + len;
985 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
986 absolute_send_time_);
987 pos += 3;
988 assert(pos == kAbsoluteSendTimeLength);
989 return kAbsoluteSendTimeLength;
990}
991
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000992bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000993 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
994 const WebRtcRTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000995 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000996
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000997 // Get length until start of header extension block.
998 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000999 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001000 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001001 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001002 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001003 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001004 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001005 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001006 int block_pos = 12 + rtp_header.header.numCSRCs + extension_block_pos;
1007 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
1008 rtp_header.header.headerLength <
1009 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001010 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001011 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001012 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001013 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001014 // Verify that header contains extension.
1015 if (!((rtp_packet[12 + rtp_header.header.numCSRCs] == 0xBE) &&
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001016 (rtp_packet[12 + rtp_header.header.numCSRCs + 1] == 0xDE))) {
1017 WEBRTC_TRACE(
1018 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001019 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001020 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001021 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001022 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001023 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001024 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1025 &id) != 0) {
1026 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001027 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001028 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001029 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001030 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001031 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001032 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001033 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001034 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001035 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001036 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001037 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001038 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001039 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001040 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001041}
1042
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001043bool RTPSender::UpdateAbsoluteSendTime(
1044 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
1045 const WebRtcRTPHeader &rtp_header, const int64_t now_ms) const {
1046 CriticalSectionScoped cs(send_critsect_);
1047
1048 // Get length until start of header extension block.
1049 int extension_block_pos =
1050 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1051 kRtpExtensionAbsoluteSendTime);
1052 if (extension_block_pos < 0) {
1053 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1054 "Failed to update absolute send time, not registered.");
1055 return false;
1056 }
1057 int block_pos = 12 + rtp_header.header.numCSRCs + extension_block_pos;
1058 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
1059 rtp_header.header.headerLength < block_pos + kAbsoluteSendTimeLength) {
1060 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1061 "Failed to update absolute send time, invalid length.");
1062 return false;
1063 }
1064 // Verify that header contains extension.
1065 if (!((rtp_packet[12 + rtp_header.header.numCSRCs] == 0xBE) &&
1066 (rtp_packet[12 + rtp_header.header.numCSRCs + 1] == 0xDE))) {
1067 WEBRTC_TRACE(
1068 kTraceStream, kTraceRtpRtcp, id_,
1069 "Failed to update absolute send time, hdr extension not found.");
1070 return false;
1071 }
1072 // Get id.
1073 uint8_t id = 0;
1074 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1075 &id) != 0) {
1076 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1077 "Failed to update absolute send time, no id.");
1078 return false;
1079 }
1080 // Verify first byte in block.
1081 const uint8_t first_block_byte = (id << 4) + 2;
1082 if (rtp_packet[block_pos] != first_block_byte) {
1083 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1084 "Failed to update absolute send time.");
1085 return false;
1086 }
1087 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1088 // fractional part).
1089 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1090 ((now_ms << 18) / 1000) & 0x00ffffff);
1091 return true;
1092}
1093
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001094void RTPSender::SetSendingStatus(const bool enabled) {
1095 if (enabled) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001096 uint32_t frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001097 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001098 uint32_t frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001099
1100 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001101 switch (frequency) {
1102 case 8000:
1103 case 12000:
1104 case 16000:
1105 case 24000:
1106 case 32000:
1107 break;
1108 default:
1109 assert(false);
1110 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001111 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +00001112 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001113 } else {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +00001114 frequency_hz = kDefaultVideoFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001115 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001116 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001117
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001118 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001119 SetStartTimestamp(RTPtime, false);
1120 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001121 if (!ssrc_forced_) {
1122 // Generate a new SSRC.
1123 ssrc_db_.ReturnSSRC(ssrc_);
1124 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001125 }
1126 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001127 if (!sequence_number_forced_ && !ssrc_forced_) {
1128 // Generate a new sequence number.
1129 sequence_number_ =
1130 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001131 }
1132 }
1133}
1134
1135void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001136 CriticalSectionScoped cs(send_critsect_);
1137 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001138}
1139
1140bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001141 CriticalSectionScoped cs(send_critsect_);
1142 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001143}
1144
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001145uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001146 CriticalSectionScoped cs(send_critsect_);
1147 return time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001148}
1149
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001150void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001151 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001152 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001153 start_time_stamp_forced_ = force;
1154 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001155 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001156 if (!start_time_stamp_forced_) {
1157 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001158 }
1159 }
1160}
1161
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001162uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001163 CriticalSectionScoped cs(send_critsect_);
1164 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001165}
1166
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001167uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001168 // If configured via API, return 0.
1169 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001170
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001171 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001172 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001173 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001174 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1175 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001176}
1177
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001178void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001179 // This is configured via the API.
1180 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001181
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001182 if (ssrc_ == ssrc && ssrc_forced_) {
1183 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001184 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001185 ssrc_forced_ = true;
1186 ssrc_db_.ReturnSSRC(ssrc_);
1187 ssrc_db_.RegisterSSRC(ssrc);
1188 ssrc_ = ssrc;
1189 if (!sequence_number_forced_) {
1190 sequence_number_ =
1191 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001192 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001193}
1194
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001195uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001196 CriticalSectionScoped cs(send_critsect_);
1197 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001198}
1199
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001200void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001201 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001202}
1203
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001204void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1205 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001206 assert(arr_length <= kRtpCsrcSize);
1207 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001208
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001209 for (int i = 0; i < arr_length; i++) {
1210 csrc_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001211 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001212 csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001213}
1214
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001215int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001216 assert(arr_of_csrc);
1217 CriticalSectionScoped cs(send_critsect_);
1218 for (int i = 0; i < csrcs_ && i < kRtpCsrcSize; i++) {
1219 arr_of_csrc[i] = csrc_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001220 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001221 return csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001222}
1223
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001224void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001225 CriticalSectionScoped cs(send_critsect_);
1226 sequence_number_forced_ = true;
1227 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001228}
1229
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001230uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001231 CriticalSectionScoped cs(send_critsect_);
1232 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001233}
1234
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001235// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001236int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1237 const uint16_t time_ms,
1238 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001239 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001240 return -1;
1241 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001242 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001243}
1244
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001245bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001246 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001247 return false;
1248 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001249 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001250}
1251
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001252int32_t RTPSender::SetAudioPacketSize(
1253 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001254 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001255 return -1;
1256 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001257 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001258}
1259
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001260int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1261 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001262 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001263 return -1;
1264 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001265 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001266}
1267
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001268int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1269 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001270 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001271}
1272
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001273int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001274 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001275}
1276
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001277int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001278 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001279 return -1;
1280 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001281 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001282}
1283
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001284int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001285 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001286 return -1;
1287 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001288 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001289}
1290
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001291// Video
1292VideoCodecInformation *RTPSender::CodecInformationVideo() {
1293 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001294 return NULL;
1295 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001296 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001297}
1298
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001299RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001300 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001301 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001302}
1303
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001304uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001305 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001306 return 0;
1307 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001308 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001309}
1310
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001311int32_t RTPSender::SendRTPIntraRequest() {
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 video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001316}
1317
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001318int32_t RTPSender::SetGenericFECStatus(
1319 const bool enable, const uint8_t payload_type_red,
1320 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001321 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001322 return -1;
1323 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001324 return video_->SetGenericFECStatus(enable, payload_type_red,
1325 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001326}
1327
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001328int32_t RTPSender::GenericFECStatus(
1329 bool *enable, uint8_t *payload_type_red,
1330 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001331 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001332 return -1;
1333 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001334 return video_->GenericFECStatus(
1335 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001336}
1337
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001338int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001339 const FecProtectionParams *delta_params,
1340 const FecProtectionParams *key_params) {
1341 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001342 return -1;
1343 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001344 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001345}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001346
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001347void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1348 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001349 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001350 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001351 // Add RTX header.
1352 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001353 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001354
1355 WebRtcRTPHeader rtp_header;
1356 rtp_parser.Parse(rtp_header);
1357
1358 // Add original RTP header.
1359 memcpy(data_buffer_rtx, buffer, rtp_header.header.headerLength);
1360
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001361 // Replace payload type, if a specific type is set for RTX.
1362 if (payload_type_rtx_ != -1) {
1363 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
1364 if (rtp_header.header.markerBit)
1365 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1366 }
1367
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001368 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001369 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001370 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1371
1372 // Replace SSRC.
1373 ptr += 6;
1374 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1375
1376 // Add OSN (original sequence number).
1377 ptr = data_buffer_rtx + rtp_header.header.headerLength;
1378 ModuleRTPUtility::AssignUWord16ToBuffer(ptr,
1379 rtp_header.header.sequenceNumber);
1380 ptr += 2;
1381
1382 // Add original payload data.
1383 memcpy(ptr, buffer + rtp_header.header.headerLength,
1384 *length - rtp_header.header.headerLength);
1385 *length += 2;
1386}
1387
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001388} // namespace webrtc