blob: 20ebd4451c48f2284fb8f173ef25b934568bf65f [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_sender_audio.h"
16#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
17#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
18#include "webrtc/system_wrappers/interface/trace.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000019#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000022
stefan@webrtc.orga8179622013-06-04 13:47:36 +000023// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
24const int kMaxPaddingLength = 224;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +000025const int kSendSideDelayWindowMs = 1000;
stefan@webrtc.orga8179622013-06-04 13:47:36 +000026
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";
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000036 }
37 return "";
38}
39
40} // namespace
41
sprang@webrtc.org096e8d92013-12-11 14:07:33 +000042RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
43 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),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000047 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org096e8d92013-12-11 14:07:33 +000048 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_(),
52 transmission_time_offset_(0), absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000053 // NACK.
sprang@webrtc.org096e8d92013-12-11 14:07:33 +000054 nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000055 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000056 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +000057 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org096e8d92013-12-11 14:07:33 +000058 frame_count_observer_(NULL), rtp_stats_callback_(NULL),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +000059 // RTP variables
60 start_time_stamp_forced_(false),
sprang@webrtc.org096e8d92013-12-11 14:07:33 +000061 start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
62 remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false),
63 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 audio_;
101 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000102
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000103 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104}
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000106void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000107 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000108}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000109
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000110uint16_t RTPSender::ActualSendBitrateKbit() const {
sprang@webrtc.org096e8d92013-12-11 14:07:33 +0000111 return (uint16_t)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000112}
113
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000114uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000115 if (video_) {
116 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000117 }
118 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000119}
120
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000121uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000122 if (video_) {
123 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000124 }
125 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000126}
127
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000128uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000129 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000130}
131
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000132bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
133 int* max_send_delay_ms) const {
134 CriticalSectionScoped cs(statistics_crit_.get());
135 SendDelayMap::const_iterator it = send_delays_.upper_bound(
136 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
137 if (!sending_media_ || it == send_delays_.end())
138 return false;
139 int num_delays = 0;
140 for (; it != send_delays_.end(); ++it) {
141 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
142 *avg_send_delay_ms += it->second;
143 ++num_delays;
144 }
145 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
146 return true;
147}
148
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000149int32_t RTPSender::SetTransmissionTimeOffset(
150 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000151 if (transmission_time_offset > (0x800000 - 1) ||
152 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000153 return -1;
154 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000155 CriticalSectionScoped cs(send_critsect_);
156 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000157 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000158}
159
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000160int32_t RTPSender::SetAbsoluteSendTime(
161 const uint32_t absolute_send_time) {
162 if (absolute_send_time > 0xffffff) { // UWord24.
163 return -1;
164 }
165 CriticalSectionScoped cs(send_critsect_);
166 absolute_send_time_ = absolute_send_time;
167 return 0;
168}
169
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000170int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
171 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000172 CriticalSectionScoped cs(send_critsect_);
173 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000174}
175
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000176int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000177 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000178 CriticalSectionScoped cs(send_critsect_);
179 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000180}
181
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000182uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000183 CriticalSectionScoped cs(send_critsect_);
184 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000185}
186
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000187int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000188 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000189 const int8_t payload_number, const uint32_t frequency,
190 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000191 assert(payload_name);
192 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000193
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000194 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000195 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000196
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000197 if (payload_type_map_.end() != it) {
198 // We already use this payload type.
199 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000200 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000201
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000202 // Check if it's the same as we already have.
203 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000204 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000205 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000206 payload->typeSpecific.Audio.frequency == frequency &&
207 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000208 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000209 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000210 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000211 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000212 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000213 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000214 return 0;
215 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000216 }
217 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000218 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000219 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000220 ModuleRTPUtility::Payload *payload = NULL;
221 if (audio_configured_) {
222 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
223 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000224 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000225 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
226 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000227 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000228 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000229 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000230 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000231 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000232}
233
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000234int32_t RTPSender::DeRegisterSendPayload(
235 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000236 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000237
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000238 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000239 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000240
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000241 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000242 return -1;
243 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000244 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000245 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000246 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000247 return 0;
248}
niklase@google.com470e71d2011-07-07 08:21:25 +0000249
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000250int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000251
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000252int RTPSender::SendPayloadFrequency() const {
253 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
254}
niklase@google.com470e71d2011-07-07 08:21:25 +0000255
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000256int32_t RTPSender::SetMaxPayloadLength(
257 const uint16_t max_payload_length,
258 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000259 // Sanity check.
260 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
261 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
262 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000263 return -1;
264 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000265 CriticalSectionScoped cs(send_critsect_);
266 max_payload_length_ = max_payload_length;
267 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000268
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000269 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
270 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000271 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000272}
273
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000274uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000275 if (audio_configured_) {
276 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000277 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000278 return max_payload_length_ - RTPHeaderLength() -
279 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
280 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000281 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000282}
283
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000284uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000285 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000286}
287
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000288uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000289
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000290void RTPSender::SetRTXStatus(int mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000291 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000292 rtx_ = mode;
293 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000294 if (set_ssrc) {
295 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000296 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000297 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000298 }
299 }
300}
301
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000302void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000303 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000304 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000305 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000306 *ssrc = ssrc_rtx_;
307 *payload_type = payload_type_rtx_;
308}
309
310
311void RTPSender::SetRtxPayloadType(int payload_type) {
312 CriticalSectionScoped cs(send_critsect_);
313 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000314}
315
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000316int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
317 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000318 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000319
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000320 if (payload_type < 0) {
321 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
322 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000323 return -1;
324 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000325 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000326 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000327 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000328 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000329 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000330 // And it's a match...
331 return 0;
332 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000333 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000334 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000335 if (payload_type_ == payload_type) {
336 if (!audio_configured_) {
337 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000338 }
339 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000340 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000341 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000342 payload_type_map_.find(payload_type);
343 if (it == payload_type_map_.end()) {
344 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
345 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000346 return -1;
347 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000348 payload_type_ = payload_type;
349 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000350 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000351 if (!payload->audio && !audio_configured_) {
352 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
353 *video_type = payload->typeSpecific.Video.videoCodecType;
354 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000355 }
356 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000357}
358
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000359int32_t RTPSender::SendOutgoingData(
360 const FrameType frame_type, const int8_t payload_type,
361 const uint32_t capture_timestamp, int64_t capture_time_ms,
362 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000363 const RTPFragmentationHeader *fragmentation,
364 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000365 {
366 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000367 CriticalSectionScoped cs(send_critsect_);
368 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000369 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000370 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000371 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000372 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000373 if (CheckPayloadType(payload_type, &video_type) != 0) {
374 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
375 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000376 __FUNCTION__, payload_type);
377 return -1;
378 }
379
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000380 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000381 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000382 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
383 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000384 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000385 frame_type == kFrameEmpty);
386
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000387 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
388 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000389 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000390 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
391 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000392 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000393
394 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000395 if (paced_sender_->Enabled()) {
396 // Padding is driven by the pacer and not by the encoder.
397 return 0;
398 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000399 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000400 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000401 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000402 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
403 capture_timestamp, capture_time_ms,
404 payload_data, payload_size,
405 fragmentation, codec_info,
406 rtp_type_hdr);
407
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000408 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000409
410 CriticalSectionScoped cs(statistics_crit_.get());
411 uint32_t frame_count = ++frame_counts_[frame_type];
412 if (frame_count_observer_) {
413 frame_count_observer_->FrameCountUpdated(frame_type,
414 frame_count,
415 ssrc_);
416 }
417
418 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000419}
420
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000421int RTPSender::SendRedundantPayloads(int payload_type, int bytes_to_send) {
422 if (!(rtx_ & kRtxRedundantPayloads))
423 return 0;
424 uint8_t buffer[IP_PACKET_SIZE];
425 int bytes_left = bytes_to_send;
426 while (bytes_left > 0) {
427 uint16_t length = bytes_left;
428 int64_t capture_time_ms;
429 if (!packet_history_.GetBestFittingPacket(buffer, &length,
430 &capture_time_ms)) {
431 break;
432 }
433 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true))
434 return -1;
435 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
436 RTPHeader rtp_header;
437 rtp_parser.Parse(rtp_header);
438 bytes_left -= length - rtp_header.headerLength;
439 }
440 return bytes_to_send - bytes_left;
441}
442
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000443bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000444 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000445 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000446 // Current bitrate since last estimate(1 second) averaged with the
447 // estimate since then, to get the most up to date bitrate.
sprang@webrtc.org096e8d92013-12-11 14:07:33 +0000448 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000449 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000450 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000451 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000452 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000453 int bytes = 0;
454 if (current_bitrate == 0) {
455 // Start up phase. Send one 33.3 ms batch to start with.
456 bytes = (bitrate_diff / 8) / 30;
457 } else {
458 bytes = (bitrate_diff / 8);
459 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000460 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000461 if (bytes > bytes_cap) {
462 bytes = bytes_cap;
463 }
464 }
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000465 uint32_t timestamp;
466 {
467 CriticalSectionScoped cs(send_critsect_);
468 // Add the random RTP timestamp offset and store the capture time for
469 // later calculation of the send time offset.
470 timestamp = start_time_stamp_ + capture_timestamp;
471 timestamp_ = timestamp;
472 capture_time_ms_ = capture_time_ms;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000473 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000474 }
475 int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
476 bytes, kDontRetransmit, false, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000477 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
478 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000479}
480
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000481int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
482 int32_t bytes) {
483 int padding_bytes_in_packet = kMaxPaddingLength;
484 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000485 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000486 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000487 packet[0] |= 0x20; // Set padding bit.
488 int32_t *data =
489 reinterpret_cast<int32_t *>(&(packet[header_length]));
490
491 // Fill data buffer with random data.
492 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
493 data[j] = rand(); // NOLINT
494 }
495 // Set number of padding bytes in the last byte of the packet.
496 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
497 return padding_bytes_in_packet;
498}
499
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000500int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
501 int64_t capture_time_ms, int32_t bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000502 StorageType store, bool force_full_size_packets,
503 bool only_pad_after_markerbit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000504 // Drop this packet if we're not sending media packets.
505 if (!sending_media_) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000506 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000507 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000508 int padding_bytes_in_packet = 0;
509 int bytes_sent = 0;
510 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000511 // Always send full padding packets.
512 if (force_full_size_packets && bytes < kMaxPaddingLength)
513 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000514 if (bytes < kMaxPaddingLength) {
515 if (force_full_size_packets) {
516 bytes = kMaxPaddingLength;
517 } else {
518 // Round to the nearest multiple of 32.
519 bytes = (bytes + 16) & 0xffe0;
520 }
521 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000522 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000523 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000524 break;
525 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000526 uint32_t ssrc;
527 uint16_t sequence_number;
528 {
529 CriticalSectionScoped cs(send_critsect_);
530 // Only send padding packets following the last packet of a frame,
531 // indicated by the marker bit.
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000532 if (only_pad_after_markerbit && !last_packet_marker_bit_)
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000533 return bytes_sent;
534 if (rtx_ == kRtxOff) {
535 ssrc = ssrc_;
536 sequence_number = sequence_number_;
537 ++sequence_number_;
538 } else {
539 ssrc = ssrc_rtx_;
540 sequence_number = sequence_number_rtx_;
541 ++sequence_number_rtx_;
542 }
543 }
544 uint8_t padding_packet[IP_PACKET_SIZE];
545 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
546 false, timestamp, sequence_number, NULL,
547 0);
548 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
549 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000550 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
551 header_length, capture_time_ms, store,
552 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000553 // Error sending the packet.
554 break;
555 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000556 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000557 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000558 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000559}
560
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000561void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000562 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000563 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000564}
565
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000566bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000567 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000568}
niklase@google.com470e71d2011-07-07 08:21:25 +0000569
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000570int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
571 uint16_t length = IP_PACKET_SIZE;
572 uint8_t data_buffer[IP_PACKET_SIZE];
573 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000574 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000575 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
576 data_buffer, &length,
577 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000578 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000579 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000580 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000581
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000582 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
583 RTPHeader header;
stefan@webrtc.org79b63202013-12-04 13:34:28 +0000584 if (!rtp_parser.Parse(header)) {
585 assert(false);
586 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
587 "Failed to parse RTP header of packet to be retransmitted.");
588 return -1;
589 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000590 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000591 "timestamp", header.timestamp,
592 "seqnum", header.sequenceNumber);
593
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000594 if (paced_sender_) {
595 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000596 header.ssrc,
597 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000598 capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000599 length - header.headerLength,
600 true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000601 // We can't send the packet right now.
602 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000603 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000604 }
605 }
606
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000607 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000608 if ((rtx_ & kRtxRetransmitted) > 0) {
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000609 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
610 buffer_to_send_ptr = data_buffer_rtx;
611 }
612
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000613 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000614 UpdateRtpStats(buffer_to_send_ptr, length, header, rtx_ != kRtxOff, true);
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000615 return length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000616 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000617 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000618}
619
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000620bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
621 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000622 if (transport_) {
623 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000624 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000625 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
626 "size", size, "sent", bytes_sent);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000627 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000628 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000629 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
630 "Transport failed to send packet");
631 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000632 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000633 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000634}
635
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000636int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000637 if (!video_)
638 return -1;
639 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000640}
641
642int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000643 if (!video_)
644 return -1;
645 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000646}
647
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000648void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000649 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000650 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000651 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
652 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000653 const int64_t now = clock_->TimeInMilliseconds();
654 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000655
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000656 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000657 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000658 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000659 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000660 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000661 return;
662 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000663
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000664 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
665 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000666 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000667 if (bytes_sent > 0) {
668 bytes_re_sent += bytes_sent;
669 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000670 // The packet has previously been resent.
671 // Try resending next packet in the list.
672 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000673 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000674 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000675 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000676 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000677 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000678 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000679 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000680 // Delay bandwidth estimate (RTT * BW).
681 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000682 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000683 uint32_t target_bytes =
684 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000685 if (bytes_re_sent > target_bytes) {
686 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000687 }
688 }
689 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000690 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000691 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000692 UpdateNACKBitRate(bytes_re_sent, now);
693 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000694 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000695}
696
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000697bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
698 uint32_t num = 0;
699 int32_t byte_count = 0;
700 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000701
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000702 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000703
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000704 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000705 return true;
706 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000707 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
708 if ((now - nack_byte_count_times_[num]) > avg_interval) {
709 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000710 break;
711 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000712 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000713 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000714 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000715 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000716 if (num == NACK_BYTECOUNT_SIZE) {
717 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000718 // during the last msg_interval.
719 time_interval = now - nack_byte_count_times_[num - 1];
720 if (time_interval < 0) {
721 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000722 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000723 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000724 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000725}
726
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000727void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
728 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000729 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000730
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000731 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000732 if (bytes > 0) {
733 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000734 // Add padding length.
735 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000736 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000737 if (nack_byte_count_times_[0] == 0) {
738 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000739 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000740 // Shift.
741 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
742 nack_byte_count_[i + 1] = nack_byte_count_[i];
743 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000744 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000745 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000746 nack_byte_count_[0] = bytes;
747 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000748 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000749 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000750}
751
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000752// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000753bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000754 int64_t capture_time_ms,
755 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000756 uint16_t length = IP_PACKET_SIZE;
757 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000758 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000759
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000760 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
761 0,
762 retransmission,
763 data_buffer,
764 &length,
765 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000766 // Packet cannot be found. Allow sending to continue.
767 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000768 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000769 if (!retransmission && capture_time_ms > 0) {
770 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
771 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000772 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
773 retransmission && (rtx_ & kRtxRetransmitted) > 0);
774}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000775
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000776bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
777 uint16_t length,
778 int64_t capture_time_ms,
779 bool send_over_rtx) {
780 uint8_t *buffer_to_send_ptr = buffer;
781
782 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000783 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000784 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000785 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000786 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000787 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000788
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000789 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000790 if (send_over_rtx) {
791 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000792 buffer_to_send_ptr = data_buffer_rtx;
793 }
794
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000795 int64_t now_ms = clock_->TimeInMilliseconds();
796 int64_t diff_ms = now_ms - capture_time_ms;
797 bool updated_transmission_time_offset =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000798 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
799 diff_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000800 bool updated_abs_send_time =
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000801 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000802 if (updated_transmission_time_offset || updated_abs_send_time) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000803 // Update stored packet in case of receiving a re-transmission request.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000804 packet_history_.ReplaceRTPHeader(buffer_to_send_ptr,
805 rtp_header.sequenceNumber,
806 rtp_header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000807 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000808
809 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
810 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, false, false);
811 return ret;
812}
813
814void RTPSender::UpdateRtpStats(const uint8_t* buffer,
815 uint32_t size,
816 const RTPHeader& header,
817 bool is_rtx,
818 bool is_retransmit) {
819 CriticalSectionScoped lock(statistics_crit_.get());
820 StreamDataCounters* counters;
821 uint32_t ssrc;
822 if (is_rtx) {
823 counters = &rtx_rtp_stats_;
824 ssrc = ssrc_rtx_;
825 } else {
826 counters = &rtp_stats_;
827 ssrc = ssrc_;
828 }
829
sprang@webrtc.org096e8d92013-12-11 14:07:33 +0000830 Bitrate::Update(size);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000831 ++counters->packets;
832 if (IsFecPacket(buffer, header)) {
833 ++counters->fec_packets;
834 }
835
836 if (is_retransmit) {
837 ++counters->retransmitted_packets;
838 } else {
839 counters->bytes += size - (header.headerLength + header.paddingLength);
840 counters->header_bytes += header.headerLength;
841 counters->padding_bytes += header.paddingLength;
842 }
843
844 if (rtp_stats_callback_) {
845 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
846 }
847}
848
849bool RTPSender::IsFecPacket(const uint8_t* buffer,
850 const RTPHeader& header) const {
851 if (!video_) {
852 return false;
853 }
854 bool fec_enabled;
855 uint8_t pt_red;
856 uint8_t pt_fec;
857 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
858 return fec_enabled &&
859 header.payloadType == pt_red &&
860 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000861}
862
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000863int RTPSender::TimeToSendPadding(int bytes) {
864 if (!sending_media_) {
865 return 0;
866 }
867 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000868 int64_t capture_time_ms;
869 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000870 {
871 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000872 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_ :
873 payload_type_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000874 timestamp = timestamp_;
875 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000876 if (last_timestamp_time_ms_ > 0) {
877 timestamp +=
878 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
879 capture_time_ms +=
880 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
881 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000882 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000883 int bytes_sent = SendRedundantPayloads(payload_type, bytes);
884 bytes -= bytes_sent;
885 if (bytes > 0) {
886 int padding_sent = SendPadData(payload_type, timestamp, capture_time_ms,
887 bytes, kDontStore, true, true);
888 bytes_sent += padding_sent;
889 }
890 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000891}
892
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000893// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000894int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000895 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000896 int64_t capture_time_ms, StorageType storage,
897 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000898 ModuleRTPUtility::RTPHeaderParser rtp_parser(
899 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000900 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000901 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000902
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000903 int64_t now_ms = clock_->TimeInMilliseconds();
904
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000905 // |capture_time_ms| <= 0 is considered invalid.
906 // TODO(holmer): This should be changed all over Video Engine so that negative
907 // time is consider invalid, while 0 is considered a valid time.
908 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000909 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000910 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000911 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000912
913 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
914 rtp_header, now_ms);
915
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000916 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000917 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
918 max_payload_length_, capture_time_ms,
919 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000920 return -1;
921 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000922
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000923 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000924 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
925 rtp_header.sequenceNumber, capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000926 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000927 // We can't send the packet right now.
928 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000929 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000930 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000931 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000932 if (capture_time_ms > 0) {
933 UpdateDelayStatistics(capture_time_ms, now_ms);
934 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000935 uint32_t length = payload_length + rtp_header_length;
936 if (!SendPacketToNetwork(buffer, length))
937 return -1;
938 UpdateRtpStats(buffer, length, rtp_header, false, false);
939 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000940}
941
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000942void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
943 CriticalSectionScoped cs(statistics_crit_.get());
944 send_delays_[now_ms] = now_ms - capture_time_ms;
945 send_delays_.erase(send_delays_.begin(),
946 send_delays_.lower_bound(now_ms - kSendSideDelayWindowMs));
947}
948
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000949void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000950 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org096e8d92013-12-11 14:07:33 +0000951 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000952 nack_bitrate_.Process();
953 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000954 return;
955 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000956 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000957}
958
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000959uint16_t RTPSender::RTPHeaderLength() const {
960 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000961 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000962 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000963 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000964 rtp_header_length += RtpHeaderExtensionTotalLength();
965 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000966}
967
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000968uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000969 CriticalSectionScoped cs(send_critsect_);
970 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000971}
972
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000973void RTPSender::ResetDataCounters() {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000974 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000975 rtp_stats_ = StreamDataCounters();
976 rtx_rtp_stats_ = StreamDataCounters();
977 if (rtp_stats_callback_) {
978 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc_);
979 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx_);
980 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000981}
982
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000983uint32_t RTPSender::Packets() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000984 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000985 return rtp_stats_.packets + rtx_rtp_stats_.packets;
niklase@google.com470e71d2011-07-07 08:21:25 +0000986}
987
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000988// Number of sent RTP bytes.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000989uint32_t RTPSender::Bytes() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000990 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000991 return rtp_stats_.bytes + rtx_rtp_stats_.bytes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000992}
993
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000994int RTPSender::CreateRTPHeader(
995 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
996 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
997 uint8_t num_csrcs) const {
998 header[0] = 0x80; // version 2.
999 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001000 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001001 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001002 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001003 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1004 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1005 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001006 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001007
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001008 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001009 if (num_csrcs > 0) {
1010 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001011 // error
1012 assert(false);
1013 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001014 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001015 uint8_t *ptr = &header[rtp_header_length];
1016 for (int i = 0; i < num_csrcs; ++i) {
1017 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001018 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001019 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001020 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001021
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001022 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001023 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001024 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001025
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001026 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1027 if (len > 0) {
1028 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001029 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001030 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001031 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001032}
1033
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001034int32_t RTPSender::BuildRTPheader(
1035 uint8_t *data_buffer, const int8_t payload_type,
1036 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001037 int64_t capture_time_ms, const bool time_stamp_provided,
1038 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001039 assert(payload_type >= 0);
1040 CriticalSectionScoped cs(send_critsect_);
1041
1042 if (time_stamp_provided) {
1043 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001044 } else {
1045 // Make a unique time stamp.
1046 // We can't inc by the actual time, since then we increase the risk of back
1047 // timing.
1048 timestamp_++;
1049 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001050 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001051 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001052 capture_time_ms_ = capture_time_ms;
1053 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001054 int csrcs_length = 0;
1055 if (include_csrcs_)
1056 csrcs_length = num_csrcs_;
1057 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1058 timestamp_, sequence_number, csrcs_, csrcs_length);
1059}
1060
1061uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001062 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001063 return 0;
1064 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001065 // RTP header extension, RFC 3550.
1066 // 0 1 2 3
1067 // 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
1068 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1069 // | defined by profile | length |
1070 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1071 // | header extension |
1072 // | .... |
1073 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001074 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001075 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001076
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001077 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001078 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001079 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001080
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001081 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001082 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001083
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001084 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001085 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001086 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001087 switch (type) {
1088 case kRtpExtensionTransmissionTimeOffset:
1089 block_length = BuildTransmissionTimeOffsetExtension(
1090 data_buffer + kHeaderLength + total_block_length);
1091 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001092 case kRtpExtensionAudioLevel:
1093 // Because AudioLevel is handled specially by RTPSenderAudio, we pretend
1094 // we don't have to care about it here, which is true until we wan't to
1095 // use it together with any of the other extensions we support.
1096 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001097 case kRtpExtensionAbsoluteSendTime:
1098 block_length = BuildAbsoluteSendTimeExtension(
1099 data_buffer + kHeaderLength + total_block_length);
1100 break;
1101 default:
1102 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001103 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001104 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001105 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001106 }
1107 if (total_block_length == 0) {
1108 // No extension added.
1109 return 0;
1110 }
1111 // Set header length (in number of Word32, header excluded).
1112 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001113 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001114 total_block_length / 4);
1115 // Total added length.
1116 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001117}
1118
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001119uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1120 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001121 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1122 //
1123 // The transmission time is signaled to the receiver in-band using the
1124 // general mechanism for RTP header extensions [RFC5285]. The payload
1125 // of this extension (the transmitted value) is a 24-bit signed integer.
1126 // When added to the RTP timestamp of the packet, it represents the
1127 // "effective" RTP transmission time of the packet, on the RTP
1128 // timescale.
1129 //
1130 // The form of the transmission offset extension block:
1131 //
1132 // 0 1 2 3
1133 // 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
1134 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1135 // | ID | len=2 | transmission offset |
1136 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001137
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001138 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001139 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001140 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1141 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001142 // Not registered.
1143 return 0;
1144 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001145 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001146 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001147 data_buffer[pos++] = (id << 4) + len;
1148 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1149 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001150 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001151 assert(pos == kTransmissionTimeOffsetLength);
1152 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001153}
1154
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001155uint8_t RTPSender::BuildAbsoluteSendTimeExtension(
1156 uint8_t* data_buffer) const {
1157 // Absolute send time in RTP streams.
1158 //
1159 // The absolute send time is signaled to the receiver in-band using the
1160 // general mechanism for RTP header extensions [RFC5285]. The payload
1161 // of this extension (the transmitted value) is a 24-bit unsigned integer
1162 // containing the sender's current time in seconds as a fixed point number
1163 // with 18 bits fractional part.
1164 //
1165 // The form of the absolute send time extension block:
1166 //
1167 // 0 1 2 3
1168 // 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
1169 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1170 // | ID | len=2 | absolute send time |
1171 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1172
1173 // Get id defined by user.
1174 uint8_t id;
1175 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1176 &id) != 0) {
1177 // Not registered.
1178 return 0;
1179 }
1180 size_t pos = 0;
1181 const uint8_t len = 2;
1182 data_buffer[pos++] = (id << 4) + len;
1183 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1184 absolute_send_time_);
1185 pos += 3;
1186 assert(pos == kAbsoluteSendTimeLength);
1187 return kAbsoluteSendTimeLength;
1188}
1189
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001190bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001191 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001192 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001193 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001194
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001195 // Get length until start of header extension block.
1196 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001197 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001198 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001199 if (extension_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001200 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001201 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001202 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001203 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001204 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001205 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001206 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001207 block_pos + kTransmissionTimeOffsetLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001208 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001209 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001210 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001211 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001212 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001213 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1214 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001215 WEBRTC_TRACE(
1216 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001217 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001218 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001219 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001220 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001221 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001222 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1223 &id) != 0) {
1224 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001225 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001226 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001227 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001228 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001229 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001230 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001231 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001232 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001233 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001234 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001235 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001236 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001237 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001238 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001239}
1240
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001241bool RTPSender::UpdateAbsoluteSendTime(
1242 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001243 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001244 CriticalSectionScoped cs(send_critsect_);
1245
1246 // Get length until start of header extension block.
1247 int extension_block_pos =
1248 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1249 kRtpExtensionAbsoluteSendTime);
1250 if (extension_block_pos < 0) {
1251 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1252 "Failed to update absolute send time, not registered.");
1253 return false;
1254 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001255 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001256 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001257 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001258 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1259 "Failed to update absolute send time, invalid length.");
1260 return false;
1261 }
1262 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001263 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1264 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001265 WEBRTC_TRACE(
1266 kTraceStream, kTraceRtpRtcp, id_,
1267 "Failed to update absolute send time, hdr extension not found.");
1268 return false;
1269 }
1270 // Get id.
1271 uint8_t id = 0;
1272 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1273 &id) != 0) {
1274 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1275 "Failed to update absolute send time, no id.");
1276 return false;
1277 }
1278 // Verify first byte in block.
1279 const uint8_t first_block_byte = (id << 4) + 2;
1280 if (rtp_packet[block_pos] != first_block_byte) {
1281 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
1282 "Failed to update absolute send time.");
1283 return false;
1284 }
1285 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1286 // fractional part).
1287 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1288 ((now_ms << 18) / 1000) & 0x00ffffff);
1289 return true;
1290}
1291
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001292void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001293 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001294 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001295 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001296
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001297 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001298 SetStartTimestamp(RTPtime, false);
1299 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001300 if (!ssrc_forced_) {
1301 // Generate a new SSRC.
1302 ssrc_db_.ReturnSSRC(ssrc_);
1303 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001304 }
1305 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001306 if (!sequence_number_forced_ && !ssrc_forced_) {
1307 // Generate a new sequence number.
1308 sequence_number_ =
1309 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001310 }
1311 }
1312}
1313
1314void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001315 CriticalSectionScoped cs(send_critsect_);
1316 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001317}
1318
1319bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001320 CriticalSectionScoped cs(send_critsect_);
1321 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001322}
1323
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001324uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001325 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001326 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001327}
1328
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001329void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001330 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001331 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001332 start_time_stamp_forced_ = force;
1333 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001334 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001335 if (!start_time_stamp_forced_) {
1336 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001337 }
1338 }
1339}
1340
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001341uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001342 CriticalSectionScoped cs(send_critsect_);
1343 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001344}
1345
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001346uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001347 // If configured via API, return 0.
1348 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001349
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001350 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001351 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001352 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001353 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1354 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001355}
1356
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001357void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001358 // This is configured via the API.
1359 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001360
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001361 if (ssrc_ == ssrc && ssrc_forced_) {
1362 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001363 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001364 ssrc_forced_ = true;
1365 ssrc_db_.ReturnSSRC(ssrc_);
1366 ssrc_db_.RegisterSSRC(ssrc);
1367 ssrc_ = ssrc;
1368 if (!sequence_number_forced_) {
1369 sequence_number_ =
1370 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001371 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001372}
1373
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001374uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001375 CriticalSectionScoped cs(send_critsect_);
1376 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001377}
1378
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001379void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001380 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001381}
1382
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001383void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1384 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001385 assert(arr_length <= kRtpCsrcSize);
1386 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001387
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001388 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001389 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001390 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001391 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001392}
1393
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001394int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001395 assert(arr_of_csrc);
1396 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001397 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1398 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001399 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001400 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001401}
1402
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001403void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001404 CriticalSectionScoped cs(send_critsect_);
1405 sequence_number_forced_ = true;
1406 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001407}
1408
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001409uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001410 CriticalSectionScoped cs(send_critsect_);
1411 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001412}
1413
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001414// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001415int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1416 const uint16_t time_ms,
1417 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001418 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001419 return -1;
1420 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001421 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001422}
1423
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001424bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001425 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001426 return false;
1427 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001428 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001429}
1430
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001431int32_t RTPSender::SetAudioPacketSize(
1432 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001433 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001434 return -1;
1435 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001436 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001437}
1438
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001439int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1440 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001441 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001442 return -1;
1443 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001444 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001445}
1446
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001447int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1448 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001449 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001450}
1451
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001452int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001453 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001454}
1455
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001456int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001457 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001458 return -1;
1459 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001460 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001461}
1462
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001463int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001464 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001465 return -1;
1466 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001467 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001468}
1469
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001470// Video
1471VideoCodecInformation *RTPSender::CodecInformationVideo() {
1472 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001473 return NULL;
1474 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001475 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001476}
1477
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001478RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001479 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001480 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001481}
1482
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001483uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001484 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001485 return 0;
1486 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001487 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001488}
1489
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001490int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001491 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001492 return -1;
1493 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001494 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001495}
1496
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001497int32_t RTPSender::SetGenericFECStatus(
1498 const bool enable, const uint8_t payload_type_red,
1499 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001500 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001501 return -1;
1502 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001503 return video_->SetGenericFECStatus(enable, payload_type_red,
1504 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001505}
1506
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001507int32_t RTPSender::GenericFECStatus(
1508 bool *enable, uint8_t *payload_type_red,
1509 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001510 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001511 return -1;
1512 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001513 return video_->GenericFECStatus(
1514 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001515}
1516
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001517int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001518 const FecProtectionParams *delta_params,
1519 const FecProtectionParams *key_params) {
1520 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001521 return -1;
1522 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001523 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001524}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001525
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001526void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1527 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001528 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001529 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001530 // Add RTX header.
1531 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001532 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001533
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001534 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001535 rtp_parser.Parse(rtp_header);
1536
1537 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001538 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001539
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001540 // Replace payload type, if a specific type is set for RTX.
1541 if (payload_type_rtx_ != -1) {
1542 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001543 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001544 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1545 }
1546
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001547 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001548 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001549 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1550
1551 // Replace SSRC.
1552 ptr += 6;
1553 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1554
1555 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001556 ptr = data_buffer_rtx + rtp_header.headerLength;
1557 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001558 ptr += 2;
1559
1560 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001561 memcpy(ptr, buffer + rtp_header.headerLength,
1562 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001563 *length += 2;
1564}
1565
sprang@webrtc.org71f055f2013-12-04 15:09:27 +00001566void RTPSender::RegisterFrameCountObserver(FrameCountObserver* observer) {
1567 CriticalSectionScoped cs(statistics_crit_.get());
1568 if (observer != NULL)
1569 assert(frame_count_observer_ == NULL);
1570 frame_count_observer_ = observer;
1571}
1572
1573FrameCountObserver* RTPSender::GetFrameCountObserver() const {
1574 CriticalSectionScoped cs(statistics_crit_.get());
1575 return frame_count_observer_;
1576}
1577
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001578void RTPSender::RegisterRtpStatisticsCallback(
1579 StreamDataCountersCallback* callback) {
1580 CriticalSectionScoped cs(statistics_crit_.get());
1581 if (callback != NULL)
1582 assert(rtp_stats_callback_ == NULL);
1583 rtp_stats_callback_ = callback;
1584}
1585
1586StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1587 CriticalSectionScoped cs(statistics_crit_.get());
1588 return rtp_stats_callback_;
1589}
1590
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001591} // namespace webrtc