blob: 0031a0eeda3f9686d10766b4e29e3dd4231777bd [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000011#include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
12
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <stdlib.h> // srand
niklase@google.com470e71d2011-07-07 08:21:25 +000014
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000015#include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
16#include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
17#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
18#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
19#include "webrtc/system_wrappers/interface/trace.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000020#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
stefan@webrtc.orga8179622013-06-04 13:47:36 +000024// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
25const int kMaxPaddingLength = 224;
26
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000027namespace {
28
29const char* FrameTypeToString(const FrameType frame_type) {
30 switch (frame_type) {
31 case kFrameEmpty: return "empty";
32 case kAudioFrameSpeech: return "audio_speech";
33 case kAudioFrameCN: return "audio_cn";
34 case kVideoFrameKey: return "video_key";
35 case kVideoFrameDelta: return "video_delta";
36 case kVideoFrameGolden: return "video_golden";
37 case kVideoFrameAltRef: return "video_altref";
38 }
39 return "";
40}
41
42} // namespace
43
pbos@webrtc.org2f446732013-04-08 11:08:41 +000044RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000045 Transport *transport, RtpAudioFeedback *audio_feedback,
46 PacedSender *paced_sender)
47 : Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL),
48 video_(NULL), paced_sender_(paced_sender),
49 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
50 transport_(transport), sending_media_(true), // Default to sending media.
51 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
52 target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1),
53 payload_type_map_(), rtp_header_extension_map_(),
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +000054 transmission_time_offset_(0), absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000055 // NACK.
56 nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
57 packet_history_(new RTPPacketHistory(clock)),
58 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +000059 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000060 packets_sent_(0), payload_bytes_sent_(0), start_time_stamp_forced_(false),
61 start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000062 remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false),
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +000063 timestamp_(0), capture_time_ms_(0), last_timestamp_time_ms_(0),
64 last_packet_marker_bit_(false), num_csrcs_(0), csrcs_(),
65 include_csrcs_(true), rtx_(kRtxOff), payload_type_rtx_(-1) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000066 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
67 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +000068 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000069 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000070 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000071 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000072 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
73 // Random start, 16 bits. Can't be 0.
74 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
75 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +000076
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000077 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000078 audio_ = new RTPSenderAudio(id, clock_, this);
79 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000080 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000081 video_ = new RTPSenderVideo(id, clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000082 }
83 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000084}
85
pwestin@webrtc.org00741872012-01-19 15:56:10 +000086RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000087 if (remote_ssrc_ != 0) {
88 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000089 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000090 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +000091
pwestin@webrtc.org00741872012-01-19 15:56:10 +000092 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000093 delete send_critsect_;
94 while (!payload_type_map_.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000095 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000096 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +000097 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000098 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000099 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000100 delete packet_history_;
101 delete audio_;
102 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000103
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000104 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105}
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000107void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000108 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000109}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000110
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000111uint16_t RTPSender::ActualSendBitrateKbit() const {
112 return (uint16_t)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000113}
114
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000115uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000116 if (video_) {
117 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000118 }
119 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000120}
121
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000122uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000123 if (video_) {
124 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000125 }
126 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000127}
128
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000129uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000130 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000131}
132
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000133int32_t RTPSender::SetTransmissionTimeOffset(
134 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000135 if (transmission_time_offset > (0x800000 - 1) ||
136 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000137 return -1;
138 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000139 CriticalSectionScoped cs(send_critsect_);
140 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000141 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000142}
143
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000144int32_t RTPSender::SetAbsoluteSendTime(
145 const uint32_t absolute_send_time) {
146 if (absolute_send_time > 0xffffff) { // UWord24.
147 return -1;
148 }
149 CriticalSectionScoped cs(send_critsect_);
150 absolute_send_time_ = absolute_send_time;
151 return 0;
152}
153
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000154int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
155 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000156 CriticalSectionScoped cs(send_critsect_);
157 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000158}
159
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000160int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000161 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000162 CriticalSectionScoped cs(send_critsect_);
163 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000164}
165
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000166uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000167 CriticalSectionScoped cs(send_critsect_);
168 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000169}
170
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000171int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000172 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000173 const int8_t payload_number, const uint32_t frequency,
174 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000175 assert(payload_name);
176 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000178 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000179 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000180
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000181 if (payload_type_map_.end() != it) {
182 // We already use this payload type.
183 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000184 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000185
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000186 // Check if it's the same as we already have.
187 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000188 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000189 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000190 payload->typeSpecific.Audio.frequency == frequency &&
191 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000192 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000193 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000194 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000195 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000196 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000197 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000198 return 0;
199 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 }
201 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000202 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000203 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000204 ModuleRTPUtility::Payload *payload = NULL;
205 if (audio_configured_) {
206 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
207 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000208 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000209 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
210 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000211 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000212 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000213 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000214 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000215 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000216}
217
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000218int32_t RTPSender::DeRegisterSendPayload(
219 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000220 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000221
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000222 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000223 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000224
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000225 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000226 return -1;
227 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000228 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000229 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000230 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000231 return 0;
232}
niklase@google.com470e71d2011-07-07 08:21:25 +0000233
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000234int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000235
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000236int RTPSender::SendPayloadFrequency() const {
237 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
238}
niklase@google.com470e71d2011-07-07 08:21:25 +0000239
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000240int32_t RTPSender::SetMaxPayloadLength(
241 const uint16_t max_payload_length,
242 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000243 // Sanity check.
244 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
245 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
246 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000247 return -1;
248 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000249 CriticalSectionScoped cs(send_critsect_);
250 max_payload_length_ = max_payload_length;
251 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000252
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000253 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
254 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000255 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000256}
257
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000258uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000259 if (audio_configured_) {
260 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000261 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000262 return max_payload_length_ - RTPHeaderLength() -
263 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
264 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000265 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000266}
267
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000268uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000269 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000270}
271
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000272uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000273
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000274void RTPSender::SetRTXStatus(RtxMode mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000275 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000276 rtx_ = mode;
277 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000278 if (set_ssrc) {
279 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000280 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000281 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000282 }
283 }
284}
285
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000286void RTPSender::RTXStatus(RtxMode* mode, uint32_t* ssrc,
287 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000288 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000289 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000290 *ssrc = ssrc_rtx_;
291 *payload_type = payload_type_rtx_;
292}
293
294
295void RTPSender::SetRtxPayloadType(int payload_type) {
296 CriticalSectionScoped cs(send_critsect_);
297 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000298}
299
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000300int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
301 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000302 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000303
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000304 if (payload_type < 0) {
305 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
306 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000307 return -1;
308 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000309 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000310 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000311 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000312 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000313 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000314 // And it's a match...
315 return 0;
316 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000317 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000318 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000319 if (payload_type_ == payload_type) {
320 if (!audio_configured_) {
321 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000322 }
323 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000324 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000325 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000326 payload_type_map_.find(payload_type);
327 if (it == payload_type_map_.end()) {
328 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
329 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000330 return -1;
331 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000332 payload_type_ = payload_type;
333 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000334 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000335 if (!payload->audio && !audio_configured_) {
336 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
337 *video_type = payload->typeSpecific.Video.videoCodecType;
338 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000339 }
340 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000341}
342
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000343int32_t RTPSender::SendOutgoingData(
344 const FrameType frame_type, const int8_t payload_type,
345 const uint32_t capture_timestamp, int64_t capture_time_ms,
346 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000347 const RTPFragmentationHeader *fragmentation,
348 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000349 {
350 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000351 CriticalSectionScoped cs(send_critsect_);
352 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000353 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000354 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000355 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000356 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000357 if (CheckPayloadType(payload_type, &video_type) != 0) {
358 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
359 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000360 __FUNCTION__, payload_type);
361 return -1;
362 }
363
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000364 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000365 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
366 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000367 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000368 frame_type == kFrameEmpty);
369
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000370 return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
371 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000372 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000373 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
374 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000375 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000376
377 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000378 if (paced_sender_->Enabled()) {
379 // Padding is driven by the pacer and not by the encoder.
380 return 0;
381 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000382 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000383 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000384 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000385 return video_->SendVideo(video_type, frame_type, payload_type,
386 capture_timestamp, capture_time_ms, payload_data,
387 payload_size, fragmentation, codec_info,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000388 rtp_type_hdr);
389 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000390}
391
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000392bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000393 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000394 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000395 // Current bitrate since last estimate(1 second) averaged with the
396 // estimate since then, to get the most up to date bitrate.
397 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000398 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000399 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000400 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000401 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000402 int bytes = 0;
403 if (current_bitrate == 0) {
404 // Start up phase. Send one 33.3 ms batch to start with.
405 bytes = (bitrate_diff / 8) / 30;
406 } else {
407 bytes = (bitrate_diff / 8);
408 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000409 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000410 if (bytes > bytes_cap) {
411 bytes = bytes_cap;
412 }
413 }
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000414 uint32_t timestamp;
415 {
416 CriticalSectionScoped cs(send_critsect_);
417 // Add the random RTP timestamp offset and store the capture time for
418 // later calculation of the send time offset.
419 timestamp = start_time_stamp_ + capture_timestamp;
420 timestamp_ = timestamp;
421 capture_time_ms_ = capture_time_ms;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000422 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000423 }
424 int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
425 bytes, kDontRetransmit, false, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000426 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
427 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000428}
429
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000430int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
431 int32_t bytes) {
432 int padding_bytes_in_packet = kMaxPaddingLength;
433 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000434 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000435 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000436 packet[0] |= 0x20; // Set padding bit.
437 int32_t *data =
438 reinterpret_cast<int32_t *>(&(packet[header_length]));
439
440 // Fill data buffer with random data.
441 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
442 data[j] = rand(); // NOLINT
443 }
444 // Set number of padding bytes in the last byte of the packet.
445 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
446 return padding_bytes_in_packet;
447}
448
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000449int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
450 int64_t capture_time_ms, int32_t bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000451 StorageType store, bool force_full_size_packets,
452 bool only_pad_after_markerbit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000453 // Drop this packet if we're not sending media packets.
454 if (!sending_media_) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000455 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000456 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000457 int padding_bytes_in_packet = 0;
458 int bytes_sent = 0;
459 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000460 // Always send full padding packets.
461 if (force_full_size_packets && bytes < kMaxPaddingLength)
462 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000463 if (bytes < kMaxPaddingLength) {
464 if (force_full_size_packets) {
465 bytes = kMaxPaddingLength;
466 } else {
467 // Round to the nearest multiple of 32.
468 bytes = (bytes + 16) & 0xffe0;
469 }
470 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000471 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000472 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000473 break;
474 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000475 uint32_t ssrc;
476 uint16_t sequence_number;
477 {
478 CriticalSectionScoped cs(send_critsect_);
479 // Only send padding packets following the last packet of a frame,
480 // indicated by the marker bit.
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000481 if (only_pad_after_markerbit && !last_packet_marker_bit_)
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000482 return bytes_sent;
483 if (rtx_ == kRtxOff) {
484 ssrc = ssrc_;
485 sequence_number = sequence_number_;
486 ++sequence_number_;
487 } else {
488 ssrc = ssrc_rtx_;
489 sequence_number = sequence_number_rtx_;
490 ++sequence_number_rtx_;
491 }
492 }
493 uint8_t padding_packet[IP_PACKET_SIZE];
494 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
495 false, timestamp, sequence_number, NULL,
496 0);
497 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
498 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000499 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
500 header_length, capture_time_ms, store,
501 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000502 // Error sending the packet.
503 break;
504 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000505 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000506 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000507 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000508}
509
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000510void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000511 const uint16_t number_to_store) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000512 packet_history_->SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000513}
514
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000515bool RTPSender::StorePackets() const {
516 return packet_history_->StorePackets();
517}
niklase@google.com470e71d2011-07-07 08:21:25 +0000518
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000519int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
520 uint16_t length = IP_PACKET_SIZE;
521 uint8_t data_buffer[IP_PACKET_SIZE];
522 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000523 int64_t capture_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000524 StorageType type;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000525 if (!packet_history_->GetRTPPacket(packet_id, min_resend_time, data_buffer,
526 &length, &capture_time_ms, &type)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000527 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000528 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000529 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000530 if (length == 0 || type == kDontRetransmit) {
531 // No bytes copied (packet recently resent, skip resending) or
532 // packet should not be retransmitted.
533 return 0;
534 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000535
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000536 // Store the time when the packet was last sent or added to pacer.
537 packet_history_->UpdateResendTime(packet_id);
538
539 {
540 // Update send statistics prior to pacer.
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000541 CriticalSectionScoped lock(statistics_crit_.get());
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000542 Bitrate::Update(length);
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000543 ++packets_sent_;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000544 // We on purpose don't add to payload_bytes_sent_ since this is a
545 // re-transmit and not new payload data.
546 }
547
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000548
549 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
550 RTPHeader header;
551 rtp_parser.Parse(header);
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000552 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000553 "timestamp", header.timestamp,
554 "seqnum", header.sequenceNumber);
555
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000556 if (paced_sender_) {
557 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000558 header.ssrc,
559 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000560 capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000561 length - header.headerLength,
562 true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000563 // We can't send the packet right now.
564 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000565 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000566 }
567 }
568
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000569 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
570 if (rtx_ != kRtxOff) {
571 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
572 buffer_to_send_ptr = data_buffer_rtx;
573 }
574
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000575 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000576 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000577 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000578 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000579}
580
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000581bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
582 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000583 if (transport_) {
584 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000585 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000586 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
587 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000588 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000589 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000590 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
591 "Transport failed to send packet");
592 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000593 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000594 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000595}
596
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000597int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000598 if (!video_)
599 return -1;
600 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000601}
602
603int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000604 if (!video_)
605 return -1;
606 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000607}
608
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000609void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000610 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000611 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000612 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
613 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000614 const int64_t now = clock_->TimeInMilliseconds();
615 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000616
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000617 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000618 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000619 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000620 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000621 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000622 return;
623 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000624
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000625 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
626 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000627 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000628 if (bytes_sent > 0) {
629 bytes_re_sent += bytes_sent;
630 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000631 // The packet has previously been resent.
632 // Try resending next packet in the list.
633 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000634 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000635 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000636 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000637 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000638 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000639 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000640 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000641 // Delay bandwidth estimate (RTT * BW).
642 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000643 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000644 uint32_t target_bytes =
645 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000646 if (bytes_re_sent > target_bytes) {
647 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000648 }
649 }
650 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000651 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000652 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000653 UpdateNACKBitRate(bytes_re_sent, now);
654 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000655 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000656}
657
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000658bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
659 uint32_t num = 0;
660 int32_t byte_count = 0;
661 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000662
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000663 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000664
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000665 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000666 return true;
667 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000668 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
669 if ((now - nack_byte_count_times_[num]) > avg_interval) {
670 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000671 break;
672 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000673 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000674 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000675 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000676 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000677 if (num == NACK_BYTECOUNT_SIZE) {
678 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000679 // during the last msg_interval.
680 time_interval = now - nack_byte_count_times_[num - 1];
681 if (time_interval < 0) {
682 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000683 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000684 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000685 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000686}
687
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000688void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
689 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000690 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000691
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000692 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000693 if (bytes > 0) {
694 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000695 // Add padding length.
696 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000697 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000698 if (nack_byte_count_times_[0] == 0) {
699 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000700 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000701 // Shift.
702 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
703 nack_byte_count_[i + 1] = nack_byte_count_[i];
704 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000706 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000707 nack_byte_count_[0] = bytes;
708 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000709 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000710 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000711}
712
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000713// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000714bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000715 int64_t capture_time_ms,
716 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000717 StorageType type;
718 uint16_t length = IP_PACKET_SIZE;
719 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000720 int64_t stored_time_ms;
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000721 uint8_t *buffer_to_send_ptr = data_buffer;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000722
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000723 if (packet_history_ == NULL) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000724 // Packet cannot be found. Allow sending to continue.
725 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000726 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000727 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
728 &stored_time_ms, &type)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000729 // Packet cannot be found. Allow sending to continue.
730 return true;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000731 }
732 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000733
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000734 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000735 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000736 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000737 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000738 "timestamp", rtp_header.timestamp,
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000739 "seqnum", sequence_number);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000740
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000741 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
742 if (retransmission && rtx_ != kRtxOff) {
743 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
744 buffer_to_send_ptr = data_buffer_rtx;
745 }
746
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000747 int64_t now_ms = clock_->TimeInMilliseconds();
748 int64_t diff_ms = now_ms - capture_time_ms;
749 bool updated_transmission_time_offset =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000750 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
751 diff_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000752 bool updated_abs_send_time =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000753 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000754 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000755 // Update stored packet in case of receiving a re-transmission request.
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000756 packet_history_->ReplaceRTPHeader(buffer_to_send_ptr,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000757 rtp_header.sequenceNumber,
758 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000759 }
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000760 return SendPacketToNetwork(buffer_to_send_ptr, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000761}
762
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000763int RTPSender::TimeToSendPadding(int bytes) {
764 if (!sending_media_) {
765 return 0;
766 }
767 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000768 int64_t capture_time_ms;
769 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000770 {
771 CriticalSectionScoped cs(send_critsect_);
772 payload_type = (rtx_ == kRtxOff) ? payload_type_ : payload_type_rtx_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000773 timestamp = timestamp_;
774 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000775 if (last_timestamp_time_ms_ > 0) {
776 timestamp +=
777 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
778 capture_time_ms +=
779 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
780 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000781 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000782 return SendPadData(payload_type, timestamp, capture_time_ms, bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000783 kDontStore, true, true);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000784}
785
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000786// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000787int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000788 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000789 int64_t capture_time_ms, StorageType storage,
790 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000791 ModuleRTPUtility::RTPHeaderParser rtp_parser(
792 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000793 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000794 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000795
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000796 int64_t now_ms = clock_->TimeInMilliseconds();
797
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000798 // |capture_time_ms| <= 0 is considered invalid.
799 // TODO(holmer): This should be changed all over Video Engine so that negative
800 // time is consider invalid, while 0 is considered a valid time.
801 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000802 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000803 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000804 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000805
806 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
807 rtp_header, now_ms);
808
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000809 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000810 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
811 max_payload_length_, capture_time_ms,
812 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000813 return -1;
814 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000815
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000816 // Create and send RTX Packet.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000817 // TODO(pwesin): This should be moved to its own code path triggered by pacer.
818 bool rtx_sent = false;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000819 if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000820 uint16_t length_rtx = payload_length + rtp_header_length;
821 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000822 BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000823 if (!SendPacketToNetwork(data_buffer_rtx, length_rtx)) return -1;
824 rtx_sent = true;
825 }
826 {
827 // Update send statistics prior to pacer.
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000828 CriticalSectionScoped lock(statistics_crit_.get());
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000829 Bitrate::Update(payload_length + rtp_header_length);
830 ++packets_sent_;
831 payload_bytes_sent_ += payload_length;
832 if (rtx_sent) {
833 // The RTX packet.
834 ++packets_sent_;
835 payload_bytes_sent_ += payload_length;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000836 }
837 }
838
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000839 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000840 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
841 rtp_header.sequenceNumber, capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000842 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000843 // We can't send the packet right now.
844 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000845 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000846 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000847 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000848 if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
849 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000850 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000851 return -1;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000852}
853
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000854void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000855 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000856 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000857 nack_bitrate_.Process();
858 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000859 return;
860 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000861 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000862}
863
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000864uint16_t RTPSender::RTPHeaderLength() const {
865 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000866 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000867 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000868 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000869 rtp_header_length += RtpHeaderExtensionTotalLength();
870 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000871}
872
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000873uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000874 CriticalSectionScoped cs(send_critsect_);
875 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000876}
877
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000878void RTPSender::ResetDataCounters() {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000879 CriticalSectionScoped lock(statistics_crit_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000880 packets_sent_ = 0;
881 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000882}
883
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000884uint32_t RTPSender::Packets() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000885 CriticalSectionScoped lock(statistics_crit_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000886 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000887}
888
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000889// Number of sent RTP bytes.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000890uint32_t RTPSender::Bytes() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000891 CriticalSectionScoped lock(statistics_crit_.get());
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000892 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000893}
894
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000895int RTPSender::CreateRTPHeader(
896 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
897 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
898 uint8_t num_csrcs) const {
899 header[0] = 0x80; // version 2.
900 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000901 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000902 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000903 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000904 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
905 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
906 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000907 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000908
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000909 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000910 if (num_csrcs > 0) {
911 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000912 // error
913 assert(false);
914 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000915 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000916 uint8_t *ptr = &header[rtp_header_length];
917 for (int i = 0; i < num_csrcs; ++i) {
918 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000919 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000920 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000921 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000922
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000923 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000924 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000925 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000926
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000927 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
928 if (len > 0) {
929 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000930 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000931 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000932 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000933}
934
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000935int32_t RTPSender::BuildRTPheader(
936 uint8_t *data_buffer, const int8_t payload_type,
937 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000938 int64_t capture_time_ms, const bool time_stamp_provided,
939 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000940 assert(payload_type >= 0);
941 CriticalSectionScoped cs(send_critsect_);
942
943 if (time_stamp_provided) {
944 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000945 } else {
946 // Make a unique time stamp.
947 // We can't inc by the actual time, since then we increase the risk of back
948 // timing.
949 timestamp_++;
950 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000951 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000952 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000953 capture_time_ms_ = capture_time_ms;
954 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000955 int csrcs_length = 0;
956 if (include_csrcs_)
957 csrcs_length = num_csrcs_;
958 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
959 timestamp_, sequence_number, csrcs_, csrcs_length);
960}
961
962uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000963 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000964 return 0;
965 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000966 // RTP header extension, RFC 3550.
967 // 0 1 2 3
968 // 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
969 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
970 // | defined by profile | length |
971 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
972 // | header extension |
973 // | .... |
974 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000975 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000976 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000977
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000978 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000979 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +0000980 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000981
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000982 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000983 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000984
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000985 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000986 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000987 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000988 switch (type) {
989 case kRtpExtensionTransmissionTimeOffset:
990 block_length = BuildTransmissionTimeOffsetExtension(
991 data_buffer + kHeaderLength + total_block_length);
992 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +0000993 case kRtpExtensionAudioLevel:
994 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
995 // we don't have to care about it here, which is true until we wan't to
996 // use it together with any of the other extensions we support.
997 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000998 case kRtpExtensionAbsoluteSendTime:
999 block_length = BuildAbsoluteSendTimeExtension(
1000 data_buffer + kHeaderLength + total_block_length);
1001 break;
1002 default:
1003 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001004 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001005 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001006 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001007 }
1008 if (total_block_length == 0) {
1009 // No extension added.
1010 return 0;
1011 }
1012 // Set header length (in number of Word32, header excluded).
1013 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001014 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001015 total_block_length / 4);
1016 // Total added length.
1017 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001018}
1019
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001020uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1021 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001022 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1023 //
1024 // The transmission time is signaled to the receiver in-band using the
1025 // general mechanism for RTP header extensions [RFC5285]. The payload
1026 // of this extension (the transmitted value) is a 24-bit signed integer.
1027 // When added to the RTP timestamp of the packet, it represents the
1028 // "effective" RTP transmission time of the packet, on the RTP
1029 // timescale.
1030 //
1031 // The form of the transmission offset extension block:
1032 //
1033 // 0 1 2 3
1034 // 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
1035 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1036 // | ID | len=2 | transmission offset |
1037 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001038
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001039 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001040 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001041 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1042 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001043 // Not registered.
1044 return 0;
1045 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001046 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001047 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001048 data_buffer[pos++] = (id << 4) + len;
1049 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1050 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001051 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001052 assert(pos == kTransmissionTimeOffsetLength);
1053 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001054}
1055
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001056uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
1057 uint8_t* data_buffer) const {
1058 // Absolute send time in RTP streams.
1059 //
1060 // The absolute send time is signaled to the receiver in-band using the
1061 // general mechanism for RTP header extensions [RFC5285]. The payload
1062 // of this extension (the transmitted value) is a 24-bit unsigned integer
1063 // containing the sender's current time in seconds as a fixed point number
1064 // with 18 bits fractional part.
1065 //
1066 // The form of the absolute send time extension block:
1067 //
1068 // 0 1 2 3
1069 // 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
1070 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1071 // | ID | len=2 | absolute send time |
1072 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1073
1074 // Get id defined by user.
1075 uint8_t id;
1076 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1077 &id) != 0) {
1078 // Not registered.
1079 return 0;
1080 }
1081 size_t pos = 0;
1082 const uint8_t len = 2;
1083 data_buffer[pos++] = (id << 4) + len;
1084 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1085 absolute_send_time_);
1086 pos += 3;
1087 assert(pos == kAbsoluteSendTimeLength);
1088 return kAbsoluteSendTimeLength;
1089}
1090
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001091bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001092 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001093 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001094 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001095
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001096 // Get length until start of header extension block.
1097 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001098 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001099 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001100 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001101 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001102 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001103 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001104 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001105 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001106 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001107 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001108 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001109 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001110 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001111 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001112 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001113 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001114 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1115 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001116 WEBRTC_TRACE(
1117 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001118 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001119 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001120 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001121 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001122 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001123 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1124 &id) != 0) {
1125 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001126 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001127 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001128 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001129 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001130 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001131 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001132 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001133 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001134 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001135 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001136 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001137 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001138 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001139 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001140}
1141
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001142bool RTPSender::UpdateAbsoluteSendTime(
1143 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001144 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001145 CriticalSectionScoped cs(send_critsect_);
1146
1147 // Get length until start of header extension block.
1148 int extension_block_pos =
1149 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1150 kRtpExtensionAbsoluteSendTime);
1151 if (extension_block_pos < 0) {
1152 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1153 "Failed to update absolute send time, not registered.");
1154 return false;
1155 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001156 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001157 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001158 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001159 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1160 "Failed to update absolute send time, invalid length.");
1161 return false;
1162 }
1163 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001164 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1165 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001166 WEBRTC_TRACE(
1167 kTraceStream, kTraceRtpRtcp, id_,
1168 "Failed to update absolute send time, hdr extension not found.");
1169 return false;
1170 }
1171 // Get id.
1172 uint8_t id = 0;
1173 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1174 &id) != 0) {
1175 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1176 "Failed to update absolute send time, no id.");
1177 return false;
1178 }
1179 // Verify first byte in block.
1180 const uint8_t first_block_byte = (id << 4) + 2;
1181 if (rtp_packet[block_pos] != first_block_byte) {
1182 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1183 "Failed to update absolute send time.");
1184 return false;
1185 }
1186 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1187 // fractional part).
1188 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1189 ((now_ms << 18) / 1000) & 0x00ffffff);
1190 return true;
1191}
1192
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001193void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001194 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001195 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001196 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001197
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001198 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001199 SetStartTimestamp(RTPtime, false);
1200 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001201 if (!ssrc_forced_) {
1202 // Generate a new SSRC.
1203 ssrc_db_.ReturnSSRC(ssrc_);
1204 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001205 }
1206 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001207 if (!sequence_number_forced_ && !ssrc_forced_) {
1208 // Generate a new sequence number.
1209 sequence_number_ =
1210 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001211 }
1212 }
1213}
1214
1215void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001216 CriticalSectionScoped cs(send_critsect_);
1217 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001218}
1219
1220bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001221 CriticalSectionScoped cs(send_critsect_);
1222 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001223}
1224
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001225uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001226 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001227 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001228}
1229
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001230void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001231 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001232 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001233 start_time_stamp_forced_ = force;
1234 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001235 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001236 if (!start_time_stamp_forced_) {
1237 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001238 }
1239 }
1240}
1241
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001242uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001243 CriticalSectionScoped cs(send_critsect_);
1244 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001245}
1246
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001247uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001248 // If configured via API, return 0.
1249 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001250
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001251 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001252 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001253 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001254 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1255 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001256}
1257
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001258void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001259 // This is configured via the API.
1260 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001261
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001262 if (ssrc_ == ssrc && ssrc_forced_) {
1263 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001264 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001265 ssrc_forced_ = true;
1266 ssrc_db_.ReturnSSRC(ssrc_);
1267 ssrc_db_.RegisterSSRC(ssrc);
1268 ssrc_ = ssrc;
1269 if (!sequence_number_forced_) {
1270 sequence_number_ =
1271 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001272 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001273}
1274
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001275uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001276 CriticalSectionScoped cs(send_critsect_);
1277 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001278}
1279
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001280void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001281 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001282}
1283
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001284void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1285 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001286 assert(arr_length <= kRtpCsrcSize);
1287 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001288
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001289 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001290 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001291 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001292 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001293}
1294
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001295int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001296 assert(arr_of_csrc);
1297 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001298 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1299 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001300 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001301 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001302}
1303
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001304void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001305 CriticalSectionScoped cs(send_critsect_);
1306 sequence_number_forced_ = true;
1307 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001308}
1309
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001310uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001311 CriticalSectionScoped cs(send_critsect_);
1312 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001313}
1314
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001315// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001316int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1317 const uint16_t time_ms,
1318 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001319 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001320 return -1;
1321 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001322 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001323}
1324
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001325bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001326 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001327 return false;
1328 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001329 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001330}
1331
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001332int32_t RTPSender::SetAudioPacketSize(
1333 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001334 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001335 return -1;
1336 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001337 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001338}
1339
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001340int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1341 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001342 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001343 return -1;
1344 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001345 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001346}
1347
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001348int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1349 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001350 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001351}
1352
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001353int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001354 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001355}
1356
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001357int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001358 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001359 return -1;
1360 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001361 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001362}
1363
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001364int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001365 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001366 return -1;
1367 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001368 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001369}
1370
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001371// Video
1372VideoCodecInformation *RTPSender::CodecInformationVideo() {
1373 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001374 return NULL;
1375 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001376 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001377}
1378
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001379RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001380 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001381 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001382}
1383
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001384uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001385 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001386 return 0;
1387 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001388 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001389}
1390
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001391int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001392 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001393 return -1;
1394 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001395 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001396}
1397
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001398int32_t RTPSender::SetGenericFECStatus(
1399 const bool enable, const uint8_t payload_type_red,
1400 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001401 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001402 return -1;
1403 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001404 return video_->SetGenericFECStatus(enable, payload_type_red,
1405 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001406}
1407
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001408int32_t RTPSender::GenericFECStatus(
1409 bool *enable, uint8_t *payload_type_red,
1410 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001411 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001412 return -1;
1413 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001414 return video_->GenericFECStatus(
1415 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001416}
1417
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001418int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001419 const FecProtectionParams *delta_params,
1420 const FecProtectionParams *key_params) {
1421 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001422 return -1;
1423 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001424 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001425}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001426
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001427void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1428 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001429 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001430 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001431 // Add RTX header.
1432 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001433 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001434
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001435 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001436 rtp_parser.Parse(rtp_header);
1437
1438 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001439 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001440
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001441 // Replace payload type, if a specific type is set for RTX.
1442 if (payload_type_rtx_ != -1) {
1443 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001444 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001445 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1446 }
1447
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001448 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001449 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001450 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1451
1452 // Replace SSRC.
1453 ptr += 6;
1454 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1455
1456 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001457 ptr = data_buffer_rtx + rtp_header.headerLength;
1458 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001459 ptr += 2;
1460
1461 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001462 memcpy(ptr, buffer + rtp_header.headerLength,
1463 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001464 *length += 2;
1465}
1466
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001467} // namespace webrtc