blob: 6e59a2d704208f69ee74cf1a369c7d389cac6845 [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"
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000018#include "webrtc/system_wrappers/interface/logging.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.org6811b6e2013-12-13 09:46:59 +000042RTPSender::RTPSender(const int32_t id,
43 const bool audio,
44 Clock* clock,
45 Transport* transport,
46 RtpAudioFeedback* audio_feedback,
47 PacedSender* paced_sender)
48 : clock_(clock),
49 bitrate_sent_(clock, this),
50 id_(id),
51 audio_configured_(audio),
52 audio_(NULL),
53 video_(NULL),
54 paced_sender_(paced_sender),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000055 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000056 transport_(transport),
57 sending_media_(true), // Default to sending media.
58 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000059 packet_over_head_(28),
60 payload_type_(-1),
61 payload_type_map_(),
62 rtp_header_extension_map_(),
63 transmission_time_offset_(0),
64 absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000065 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000066 nack_byte_count_times_(),
67 nack_byte_count_(),
68 nack_bitrate_(clock, NULL),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +000069 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000070 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +000071 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000072 frame_count_observer_(NULL),
73 rtp_stats_callback_(NULL),
74 bitrate_callback_(NULL),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +000075 // RTP variables
76 start_time_stamp_forced_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000077 start_time_stamp_(0),
78 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
79 remote_ssrc_(0),
80 sequence_number_forced_(false),
81 ssrc_forced_(false),
82 timestamp_(0),
83 capture_time_ms_(0),
84 last_timestamp_time_ms_(0),
85 last_packet_marker_bit_(false),
86 num_csrcs_(0),
87 csrcs_(),
88 include_csrcs_(true),
89 rtx_(kRtxOff),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +000090 payload_type_rtx_(-1),
91 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
92 target_bitrate_kbps_(0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000093 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
94 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +000095 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000096 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000097 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000098 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000099 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
100 // Random start, 16 bits. Can't be 0.
101 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
102 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000104 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000105 audio_ = new RTPSenderAudio(id, clock_, this);
106 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000107 } else {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000108 video_ = new RTPSenderVideo(clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000109 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000110}
111
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000112RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000113 if (remote_ssrc_ != 0) {
114 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000115 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000116 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000118 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000119 delete send_critsect_;
120 while (!payload_type_map_.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000121 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000122 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000123 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000124 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000125 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000126 delete audio_;
127 delete video_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128}
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000130void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000131 SetTargetBitrateKbps(static_cast<uint16_t>(bits / 1000));
niklase@google.com470e71d2011-07-07 08:21:25 +0000132}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000133
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000134uint16_t RTPSender::ActualSendBitrateKbit() const {
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000135 return (uint16_t)(bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000136}
137
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000138uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000139 if (video_) {
140 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000141 }
142 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000143}
144
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000145uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000146 if (video_) {
147 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000148 }
149 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000150}
151
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000152uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000153 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000154}
155
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000156bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
157 int* max_send_delay_ms) const {
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000158 if (!SendingMedia())
159 return false;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000160 CriticalSectionScoped cs(statistics_crit_.get());
161 SendDelayMap::const_iterator it = send_delays_.upper_bound(
162 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000163 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000164 return false;
165 int num_delays = 0;
166 for (; it != send_delays_.end(); ++it) {
167 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
168 *avg_send_delay_ms += it->second;
169 ++num_delays;
170 }
171 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
172 return true;
173}
174
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000175int32_t RTPSender::SetTransmissionTimeOffset(
176 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000177 if (transmission_time_offset > (0x800000 - 1) ||
178 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000179 return -1;
180 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000181 CriticalSectionScoped cs(send_critsect_);
182 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000183 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000184}
185
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000186int32_t RTPSender::SetAbsoluteSendTime(
187 const uint32_t absolute_send_time) {
188 if (absolute_send_time > 0xffffff) { // UWord24.
189 return -1;
190 }
191 CriticalSectionScoped cs(send_critsect_);
192 absolute_send_time_ = absolute_send_time;
193 return 0;
194}
195
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000196int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
197 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000198 CriticalSectionScoped cs(send_critsect_);
199 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000200}
201
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000202int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000203 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000204 CriticalSectionScoped cs(send_critsect_);
205 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000206}
207
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000208uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000209 CriticalSectionScoped cs(send_critsect_);
210 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000211}
212
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000213int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000214 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000215 const int8_t payload_number, const uint32_t frequency,
216 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000217 assert(payload_name);
218 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000219
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000220 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000221 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000222
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000223 if (payload_type_map_.end() != it) {
224 // We already use this payload type.
225 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000226 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000228 // Check if it's the same as we already have.
229 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000230 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000231 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000232 payload->typeSpecific.Audio.frequency == frequency &&
233 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000234 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000235 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000236 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000237 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000238 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000239 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000240 return 0;
241 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000242 }
243 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000244 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000245 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000246 ModuleRTPUtility::Payload *payload = NULL;
247 if (audio_configured_) {
248 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
249 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000250 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000251 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
252 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000253 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000254 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000255 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000256 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000257 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000258}
259
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000260int32_t RTPSender::DeRegisterSendPayload(
261 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000262 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000263
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000264 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000265 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000266
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000267 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000268 return -1;
269 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000270 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000271 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000272 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000273 return 0;
274}
niklase@google.com470e71d2011-07-07 08:21:25 +0000275
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000276int8_t RTPSender::SendPayloadType() const {
277 CriticalSectionScoped cs(send_critsect_);
278 return payload_type_;
279}
niklase@google.com470e71d2011-07-07 08:21:25 +0000280
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000281int RTPSender::SendPayloadFrequency() const {
282 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
283}
niklase@google.com470e71d2011-07-07 08:21:25 +0000284
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000285int32_t RTPSender::SetMaxPayloadLength(
286 const uint16_t max_payload_length,
287 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000288 // Sanity check.
289 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000290 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000291 return -1;
292 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000293 CriticalSectionScoped cs(send_critsect_);
294 max_payload_length_ = max_payload_length;
295 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000296 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000297}
298
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000299uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000300 if (audio_configured_) {
301 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000302 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000303 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
304 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
305 - ((rtx_) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000306 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000307}
308
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000309uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000310 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000311}
312
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000313uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000314
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000315void RTPSender::SetRTXStatus(int mode) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000316 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000317 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000318}
319
320void RTPSender::SetRtxSsrc(uint32_t ssrc) {
321 CriticalSectionScoped cs(send_critsect_);
322 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000323}
324
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000325void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000326 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000327 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000328 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000329 *ssrc = ssrc_rtx_;
330 *payload_type = payload_type_rtx_;
331}
332
333
334void RTPSender::SetRtxPayloadType(int payload_type) {
335 CriticalSectionScoped cs(send_critsect_);
336 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000337}
338
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000339int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
340 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000341 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000342
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000343 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000344 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000345 return -1;
346 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000347 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000348 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000349 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000350 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000351 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000352 // And it's a match...
353 return 0;
354 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000355 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000356 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000357 if (payload_type_ == payload_type) {
358 if (!audio_configured_) {
359 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000360 }
361 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000362 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000363 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000364 payload_type_map_.find(payload_type);
365 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000366 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000367 return -1;
368 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000369 payload_type_ = payload_type;
370 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000371 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000372 if (!payload->audio && !audio_configured_) {
373 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
374 *video_type = payload->typeSpecific.Video.videoCodecType;
375 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000376 }
377 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000378}
379
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000380int32_t RTPSender::SendOutgoingData(
381 const FrameType frame_type, const int8_t payload_type,
382 const uint32_t capture_timestamp, int64_t capture_time_ms,
383 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000384 const RTPFragmentationHeader *fragmentation,
385 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000386 {
387 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000388 CriticalSectionScoped cs(send_critsect_);
389 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000390 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000391 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000392 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000393 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000394 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000395 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000396 return -1;
397 }
398
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000399 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000400 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000401 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
402 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000403 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000404 frame_type == kFrameEmpty);
405
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000406 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
407 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000408 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000409 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
410 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000411 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000412
413 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000414 if (paced_sender_->Enabled()) {
415 // Padding is driven by the pacer and not by the encoder.
416 return 0;
417 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000418 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000419 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000420 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000421 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
422 capture_timestamp, capture_time_ms,
423 payload_data, payload_size,
424 fragmentation, codec_info,
425 rtp_type_hdr);
426
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000427 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000428
429 CriticalSectionScoped cs(statistics_crit_.get());
430 uint32_t frame_count = ++frame_counts_[frame_type];
431 if (frame_count_observer_) {
432 frame_count_observer_->FrameCountUpdated(frame_type,
433 frame_count,
434 ssrc_);
435 }
436
437 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000438}
439
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000440int RTPSender::SendRedundantPayloads(int payload_type, int bytes_to_send) {
441 if (!(rtx_ & kRtxRedundantPayloads))
442 return 0;
443 uint8_t buffer[IP_PACKET_SIZE];
444 int bytes_left = bytes_to_send;
445 while (bytes_left > 0) {
446 uint16_t length = bytes_left;
447 int64_t capture_time_ms;
448 if (!packet_history_.GetBestFittingPacket(buffer, &length,
449 &capture_time_ms)) {
450 break;
451 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000452 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000453 return -1;
454 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
455 RTPHeader rtp_header;
456 rtp_parser.Parse(rtp_header);
457 bytes_left -= length - rtp_header.headerLength;
458 }
459 return bytes_to_send - bytes_left;
460}
461
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000462bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000463 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000464 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000465 // Current bitrate since last estimate(1 second) averaged with the
466 // estimate since then, to get the most up to date bitrate.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000467 uint32_t current_bitrate = bitrate_sent_.BitrateNow();
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000468 uint16_t target_bitrate_kbps = GetTargetBitrateKbps();
469 int bitrate_diff = target_bitrate_kbps * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000470 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000471 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000472 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000473 int bytes = 0;
474 if (current_bitrate == 0) {
475 // Start up phase. Send one 33.3 ms batch to start with.
476 bytes = (bitrate_diff / 8) / 30;
477 } else {
478 bytes = (bitrate_diff / 8);
479 // Cap at 200 ms of target send data.
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000480 int bytes_cap = target_bitrate_kbps * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000481 if (bytes > bytes_cap) {
482 bytes = bytes_cap;
483 }
484 }
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000485 uint32_t timestamp;
486 {
487 CriticalSectionScoped cs(send_critsect_);
488 // Add the random RTP timestamp offset and store the capture time for
489 // later calculation of the send time offset.
490 timestamp = start_time_stamp_ + capture_timestamp;
491 timestamp_ = timestamp;
492 capture_time_ms_ = capture_time_ms;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000493 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000494 }
495 int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
496 bytes, kDontRetransmit, false, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000497 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
498 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000499}
500
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000501int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
502 int32_t bytes) {
503 int padding_bytes_in_packet = kMaxPaddingLength;
504 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000505 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000506 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000507 packet[0] |= 0x20; // Set padding bit.
508 int32_t *data =
509 reinterpret_cast<int32_t *>(&(packet[header_length]));
510
511 // Fill data buffer with random data.
512 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
513 data[j] = rand(); // NOLINT
514 }
515 // Set number of padding bytes in the last byte of the packet.
516 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
517 return padding_bytes_in_packet;
518}
519
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000520int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
521 int64_t capture_time_ms, int32_t bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000522 StorageType store, bool force_full_size_packets,
523 bool only_pad_after_markerbit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000524 // Drop this packet if we're not sending media packets.
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000525 if (!SendingMedia()) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000526 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000527 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000528 int padding_bytes_in_packet = 0;
529 int bytes_sent = 0;
530 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000531 // Always send full padding packets.
532 if (force_full_size_packets && bytes < kMaxPaddingLength)
533 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000534 if (bytes < kMaxPaddingLength) {
535 if (force_full_size_packets) {
536 bytes = kMaxPaddingLength;
537 } else {
538 // Round to the nearest multiple of 32.
539 bytes = (bytes + 16) & 0xffe0;
540 }
541 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000542 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000543 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000544 break;
545 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000546 uint32_t ssrc;
547 uint16_t sequence_number;
548 {
549 CriticalSectionScoped cs(send_critsect_);
550 // Only send padding packets following the last packet of a frame,
551 // indicated by the marker bit.
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000552 if (only_pad_after_markerbit && !last_packet_marker_bit_)
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000553 return bytes_sent;
554 if (rtx_ == kRtxOff) {
555 ssrc = ssrc_;
556 sequence_number = sequence_number_;
557 ++sequence_number_;
558 } else {
559 ssrc = ssrc_rtx_;
560 sequence_number = sequence_number_rtx_;
561 ++sequence_number_rtx_;
562 }
563 }
564 uint8_t padding_packet[IP_PACKET_SIZE];
565 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
566 false, timestamp, sequence_number, NULL,
567 0);
568 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
569 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000570 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
571 header_length, capture_time_ms, store,
572 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000573 // Error sending the packet.
574 break;
575 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000576 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000577 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000578 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000579}
580
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000581void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000582 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000583 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000584}
585
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000586bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000587 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000588}
niklase@google.com470e71d2011-07-07 08:21:25 +0000589
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000590int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
591 uint16_t length = IP_PACKET_SIZE;
592 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000593 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000594 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
595 data_buffer, &length,
596 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000597 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000598 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000599 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000600
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000601 if (paced_sender_) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000602 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
603 RTPHeader header;
604 if (!rtp_parser.Parse(header)) {
605 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000606 return -1;
607 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000608 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000609 header.ssrc,
610 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000611 capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000612 length - header.headerLength,
613 true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000614 // We can't send the packet right now.
615 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000616 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000617 }
618 }
619
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000620 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org16395222014-03-19 19:34:07 +0000621 (rtx_ & kRtxRetransmitted) > 0, true) ?
622 length : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000623}
624
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000625bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
626 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000627 if (transport_) {
628 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000629 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000630 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
631 "size", size, "sent", bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000632 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000633 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000634 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000635 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000636 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000637 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000638}
639
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000640int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000641 if (!video_)
642 return -1;
643 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000644}
645
646int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000647 if (!video_)
648 return -1;
649 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000650}
651
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000652void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000653 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000654 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000655 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
656 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000657 const int64_t now = clock_->TimeInMilliseconds();
658 uint32_t bytes_re_sent = 0;
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000659 uint16_t target_bitrate_kbps = GetTargetBitrateKbps();
niklase@google.com470e71d2011-07-07 08:21:25 +0000660
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000661 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000662 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000663 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
664 << target_bitrate_kbps;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000665 return;
666 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000667
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000668 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
669 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000670 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000671 if (bytes_sent > 0) {
672 bytes_re_sent += bytes_sent;
673 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000674 // The packet has previously been resent.
675 // Try resending next packet in the list.
676 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000677 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000678 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000679 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
680 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000681 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000682 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000683 // Delay bandwidth estimate (RTT * BW).
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000684 if (target_bitrate_kbps != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000685 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000686 uint32_t target_bytes =
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000687 (static_cast<uint32_t>(target_bitrate_kbps) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000688 if (bytes_re_sent > target_bytes) {
689 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000690 }
691 }
692 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000693 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000694 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000695 UpdateNACKBitRate(bytes_re_sent, now);
696 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000697 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000698}
699
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000700bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
701 uint32_t num = 0;
702 int32_t byte_count = 0;
703 const uint32_t avg_interval = 1000;
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000704 uint16_t target_bitrate_kbps = GetTargetBitrateKbps();
niklase@google.com470e71d2011-07-07 08:21:25 +0000705
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000706 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000707
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000708 if (target_bitrate_kbps == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000709 return true;
710 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000711 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
712 if ((now - nack_byte_count_times_[num]) > avg_interval) {
713 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000714 break;
715 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000716 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000717 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000718 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000719 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000720 if (num == NACK_BYTECOUNT_SIZE) {
721 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000722 // during the last msg_interval.
723 time_interval = now - nack_byte_count_times_[num - 1];
724 if (time_interval < 0) {
725 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000726 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000727 }
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000728 return (byte_count * 8) < (target_bitrate_kbps * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000729}
730
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000731void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
732 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000733 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000734
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000735 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000736 if (bytes > 0) {
737 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000738 // Add padding length.
739 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000740 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000741 if (nack_byte_count_times_[0] == 0) {
742 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000743 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000744 // Shift.
745 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
746 nack_byte_count_[i + 1] = nack_byte_count_[i];
747 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000748 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000749 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000750 nack_byte_count_[0] = bytes;
751 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000752 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000753 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000754}
755
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000756// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000757bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000758 int64_t capture_time_ms,
759 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000760 uint16_t length = IP_PACKET_SIZE;
761 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000762 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000763
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000764 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
765 0,
766 retransmission,
767 data_buffer,
768 &length,
769 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000770 // Packet cannot be found. Allow sending to continue.
771 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000772 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000773 if (!retransmission && capture_time_ms > 0) {
774 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
775 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000776 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000777 retransmission && (rtx_ & kRtxRetransmitted) > 0,
778 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000779}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000780
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000781bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
782 uint16_t length,
783 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000784 bool send_over_rtx,
785 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000786 uint8_t *buffer_to_send_ptr = buffer;
787
788 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000789 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000790 rtp_parser.Parse(rtp_header);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000791 TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000792 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000793 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000794
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000795 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000796 if (send_over_rtx) {
797 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000798 buffer_to_send_ptr = data_buffer_rtx;
799 }
800
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000801 int64_t now_ms = clock_->TimeInMilliseconds();
802 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000803 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
804 diff_ms);
805 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000806 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000807 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
808 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000809 return ret;
810}
811
812void RTPSender::UpdateRtpStats(const uint8_t* buffer,
813 uint32_t size,
814 const RTPHeader& header,
815 bool is_rtx,
816 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000817 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000818 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
819 uint32_t ssrc = SSRC();
820
821 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000822 if (is_rtx) {
823 counters = &rtx_rtp_stats_;
824 ssrc = ssrc_rtx_;
825 } else {
826 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000827 }
828
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000829 bitrate_sent_.Update(size);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000830 ++counters->packets;
831 if (IsFecPacket(buffer, header)) {
832 ++counters->fec_packets;
833 }
834
835 if (is_retransmit) {
836 ++counters->retransmitted_packets;
837 } else {
838 counters->bytes += size - (header.headerLength + header.paddingLength);
839 counters->header_bytes += header.headerLength;
840 counters->padding_bytes += header.paddingLength;
841 }
842
843 if (rtp_stats_callback_) {
844 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
845 }
846}
847
848bool RTPSender::IsFecPacket(const uint8_t* buffer,
849 const RTPHeader& header) const {
850 if (!video_) {
851 return false;
852 }
853 bool fec_enabled;
854 uint8_t pt_red;
855 uint8_t pt_fec;
856 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
857 return fec_enabled &&
858 header.payloadType == pt_red &&
859 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000860}
861
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000862int RTPSender::TimeToSendPadding(int bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000863 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000864 int64_t capture_time_ms;
865 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000866 {
867 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000868 if (!sending_media_) {
869 return 0;
870 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000871 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_ :
872 payload_type_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000873 timestamp = timestamp_;
874 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000875 if (last_timestamp_time_ms_ > 0) {
876 timestamp +=
877 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
878 capture_time_ms +=
879 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
880 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000881 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000882 int bytes_sent = SendRedundantPayloads(payload_type, bytes);
883 bytes -= bytes_sent;
884 if (bytes > 0) {
885 int padding_sent = SendPadData(payload_type, timestamp, capture_time_ms,
886 bytes, kDontStore, true, true);
887 bytes_sent += padding_sent;
888 }
889 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000890}
891
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000892// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000893int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000894 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000895 int64_t capture_time_ms, StorageType storage,
896 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000897 ModuleRTPUtility::RTPHeaderParser rtp_parser(
898 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000899 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000900 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000901
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000902 int64_t now_ms = clock_->TimeInMilliseconds();
903
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000904 // |capture_time_ms| <= 0 is considered invalid.
905 // TODO(holmer): This should be changed all over Video Engine so that negative
906 // time is consider invalid, while 0 is considered a valid time.
907 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000908 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000909 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000910 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000911
912 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
913 rtp_header, now_ms);
914
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000915 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000916 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
917 max_payload_length_, capture_time_ms,
918 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000919 return -1;
920 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000921
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000922 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000923 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
924 rtp_header.sequenceNumber, capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000925 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000926 // We can't send the packet right now.
927 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000928 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000929 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000930 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000931 if (capture_time_ms > 0) {
932 UpdateDelayStatistics(capture_time_ms, now_ms);
933 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000934 uint32_t length = payload_length + rtp_header_length;
935 if (!SendPacketToNetwork(buffer, length))
936 return -1;
937 UpdateRtpStats(buffer, length, rtp_header, false, false);
938 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000939}
940
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000941void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
942 CriticalSectionScoped cs(statistics_crit_.get());
943 send_delays_[now_ms] = now_ms - capture_time_ms;
944 send_delays_.erase(send_delays_.begin(),
945 send_delays_.lower_bound(now_ms - kSendSideDelayWindowMs));
946}
947
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000948void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000949 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000950 bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000951 nack_bitrate_.Process();
952 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000953 return;
954 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000955 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000956}
957
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000958uint16_t RTPSender::RTPHeaderLength() const {
959 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000960 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000961 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000962 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000963 rtp_header_length += RtpHeaderExtensionTotalLength();
964 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000965}
966
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000967uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000968 CriticalSectionScoped cs(send_critsect_);
969 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000970}
971
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000972void RTPSender::ResetDataCounters() {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000973 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000974 rtp_stats_ = StreamDataCounters();
975 rtx_rtp_stats_ = StreamDataCounters();
976 if (rtp_stats_callback_) {
977 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc_);
978 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx_);
979 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000980}
981
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000982uint32_t RTPSender::Packets() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000983 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000984 return rtp_stats_.packets + rtx_rtp_stats_.packets;
niklase@google.com470e71d2011-07-07 08:21:25 +0000985}
986
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000987// Number of sent RTP bytes.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000988uint32_t RTPSender::Bytes() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000989 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000990 return rtp_stats_.bytes + rtx_rtp_stats_.bytes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000991}
992
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000993int RTPSender::CreateRTPHeader(
994 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
995 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
996 uint8_t num_csrcs) const {
997 header[0] = 0x80; // version 2.
998 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000999 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001000 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001001 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001002 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1003 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1004 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001005 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001006
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001007 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001008 if (num_csrcs > 0) {
1009 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001010 // error
1011 assert(false);
1012 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001013 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001014 uint8_t *ptr = &header[rtp_header_length];
1015 for (int i = 0; i < num_csrcs; ++i) {
1016 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001017 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001018 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001019 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001020
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001021 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001022 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001023 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001024
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001025 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1026 if (len > 0) {
1027 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001028 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001029 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001030 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001031}
1032
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001033int32_t RTPSender::BuildRTPheader(
1034 uint8_t *data_buffer, const int8_t payload_type,
1035 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001036 int64_t capture_time_ms, const bool time_stamp_provided,
1037 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001038 assert(payload_type >= 0);
1039 CriticalSectionScoped cs(send_critsect_);
1040
1041 if (time_stamp_provided) {
1042 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001043 } else {
1044 // Make a unique time stamp.
1045 // We can't inc by the actual time, since then we increase the risk of back
1046 // timing.
1047 timestamp_++;
1048 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001049 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001050 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001051 capture_time_ms_ = capture_time_ms;
1052 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001053 int csrcs_length = 0;
1054 if (include_csrcs_)
1055 csrcs_length = num_csrcs_;
1056 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1057 timestamp_, sequence_number, csrcs_, csrcs_length);
1058}
1059
1060uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001061 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001062 return 0;
1063 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001064 // RTP header extension, RFC 3550.
1065 // 0 1 2 3
1066 // 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
1067 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1068 // | defined by profile | length |
1069 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1070 // | header extension |
1071 // | .... |
1072 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001073 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001074 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001075
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001076 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001077 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001078 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001079
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001080 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001081 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001082
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001083 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001084 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001085 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001086 switch (type) {
1087 case kRtpExtensionTransmissionTimeOffset:
1088 block_length = BuildTransmissionTimeOffsetExtension(
1089 data_buffer + kHeaderLength + total_block_length);
1090 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001091 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001092 block_length = BuildAudioLevelExtension(
1093 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001094 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001095 case kRtpExtensionAbsoluteSendTime:
1096 block_length = BuildAbsoluteSendTimeExtension(
1097 data_buffer + kHeaderLength + total_block_length);
1098 break;
1099 default:
1100 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001101 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001102 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001103 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001104 }
1105 if (total_block_length == 0) {
1106 // No extension added.
1107 return 0;
1108 }
1109 // Set header length (in number of Word32, header excluded).
1110 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001111 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001112 total_block_length / 4);
1113 // Total added length.
1114 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001115}
1116
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001117uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1118 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001119 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1120 //
1121 // The transmission time is signaled to the receiver in-band using the
1122 // general mechanism for RTP header extensions [RFC5285]. The payload
1123 // of this extension (the transmitted value) is a 24-bit signed integer.
1124 // When added to the RTP timestamp of the packet, it represents the
1125 // "effective" RTP transmission time of the packet, on the RTP
1126 // timescale.
1127 //
1128 // The form of the transmission offset extension block:
1129 //
1130 // 0 1 2 3
1131 // 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
1132 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1133 // | ID | len=2 | transmission offset |
1134 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001135
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001136 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001137 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001138 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1139 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001140 // Not registered.
1141 return 0;
1142 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001143 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001144 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001145 data_buffer[pos++] = (id << 4) + len;
1146 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1147 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001148 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001149 assert(pos == kTransmissionTimeOffsetLength);
1150 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001151}
1152
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001153uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1154 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1155 //
1156 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1157 //
1158 // The form of the audio level extension block:
1159 //
1160 // 0 1 2 3
1161 // 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
1162 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1163 // | ID | len=0 |V| level | 0x00 | 0x00 |
1164 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1165 //
1166 // Note that we always include 2 pad bytes, which will result in legal and
1167 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1168 // are implemented. Right now the pad bytes would anyway be required at end
1169 // of the extension block, so it makes no difference.
1170
1171 // Get id defined by user.
1172 uint8_t id;
1173 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1174 // Not registered.
1175 return 0;
1176 }
1177 size_t pos = 0;
1178 const uint8_t len = 0;
1179 data_buffer[pos++] = (id << 4) + len;
1180 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1181 data_buffer[pos++] = 0; // Padding.
1182 data_buffer[pos++] = 0; // Padding.
1183 // kAudioLevelLength is including pad bytes.
1184 assert(pos == kAudioLevelLength);
1185 return kAudioLevelLength;
1186}
1187
1188uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001189 // Absolute send time in RTP streams.
1190 //
1191 // The absolute send time is signaled to the receiver in-band using the
1192 // general mechanism for RTP header extensions [RFC5285]. The payload
1193 // of this extension (the transmitted value) is a 24-bit unsigned integer
1194 // containing the sender's current time in seconds as a fixed point number
1195 // with 18 bits fractional part.
1196 //
1197 // The form of the absolute send time extension block:
1198 //
1199 // 0 1 2 3
1200 // 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
1201 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1202 // | ID | len=2 | absolute send time |
1203 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1204
1205 // Get id defined by user.
1206 uint8_t id;
1207 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1208 &id) != 0) {
1209 // Not registered.
1210 return 0;
1211 }
1212 size_t pos = 0;
1213 const uint8_t len = 2;
1214 data_buffer[pos++] = (id << 4) + len;
1215 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1216 absolute_send_time_);
1217 pos += 3;
1218 assert(pos == kAbsoluteSendTimeLength);
1219 return kAbsoluteSendTimeLength;
1220}
1221
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001222void RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001223 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001224 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001225 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001226 // Get id.
1227 uint8_t id = 0;
1228 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1229 &id) != 0) {
1230 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001231 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001232 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001233 // Get length until start of header extension block.
1234 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001235 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001236 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001237 if (extension_block_pos < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001238 LOG(LS_WARNING)
1239 << "Failed to update transmission time offset, not registered.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001240 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001241 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001242 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001243 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001244 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001245 block_pos + kTransmissionTimeOffsetLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001246 LOG(LS_WARNING)
1247 << "Failed to update transmission time offset, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001248 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001249 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001250 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001251 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1252 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001253 LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1254 "extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001255 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001256 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001257 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001258 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001259 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001260 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001261 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001262 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001263 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001264 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001265 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001266}
1267
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001268bool RTPSender::UpdateAudioLevel(uint8_t *rtp_packet,
1269 const uint16_t rtp_packet_length,
1270 const RTPHeader &rtp_header,
1271 const bool is_voiced,
1272 const uint8_t dBov) const {
1273 CriticalSectionScoped cs(send_critsect_);
1274
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001275 // Get id.
1276 uint8_t id = 0;
1277 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1278 // Not registered.
1279 return false;
1280 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001281 // Get length until start of header extension block.
1282 int extension_block_pos =
1283 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1284 kRtpExtensionAudioLevel);
1285 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001286 // The feature is not enabled.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001287 return false;
1288 }
1289 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1290 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1291 rtp_header.headerLength < block_pos + kAudioLevelLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001292 LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001293 return false;
1294 }
1295 // Verify that header contains extension.
1296 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1297 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001298 LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001299 return false;
1300 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001301 // Verify first byte in block.
1302 const uint8_t first_block_byte = (id << 4) + 0;
1303 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001304 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001305 return false;
1306 }
1307 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1308 return true;
1309}
1310
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001311void RTPSender::UpdateAbsoluteSendTime(
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001312 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001313 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001314 CriticalSectionScoped cs(send_critsect_);
1315
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001316 // Get id.
1317 uint8_t id = 0;
1318 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1319 &id) != 0) {
1320 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001321 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001322 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001323 // Get length until start of header extension block.
1324 int extension_block_pos =
1325 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1326 kRtpExtensionAbsoluteSendTime);
1327 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001328 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001329 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001330 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001331 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001332 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001333 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001334 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001335 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001336 }
1337 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001338 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1339 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001340 LOG(LS_WARNING)
1341 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001342 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001343 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001344 // Verify first byte in block.
1345 const uint8_t first_block_byte = (id << 4) + 2;
1346 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001347 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001348 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001349 }
1350 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1351 // fractional part).
1352 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1353 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001354}
1355
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001356void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001357 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001358 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001359 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001360
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001361 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001362 SetStartTimestamp(RTPtime, false);
1363 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001364 if (!ssrc_forced_) {
1365 // Generate a new SSRC.
1366 ssrc_db_.ReturnSSRC(ssrc_);
1367 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001368 }
1369 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001370 if (!sequence_number_forced_ && !ssrc_forced_) {
1371 // Generate a new sequence number.
1372 sequence_number_ =
1373 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001374 }
1375 }
1376}
1377
1378void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001379 CriticalSectionScoped cs(send_critsect_);
1380 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001381}
1382
1383bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001384 CriticalSectionScoped cs(send_critsect_);
1385 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001386}
1387
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001388uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001389 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001390 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001391}
1392
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001393void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001394 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001395 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001396 start_time_stamp_forced_ = force;
1397 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001398 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001399 if (!start_time_stamp_forced_) {
1400 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001401 }
1402 }
1403}
1404
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001405uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001406 CriticalSectionScoped cs(send_critsect_);
1407 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001408}
1409
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001410uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001411 // If configured via API, return 0.
1412 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001413
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001414 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001415 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001416 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001417 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1418 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001419}
1420
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001421void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001422 // This is configured via the API.
1423 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001424
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001425 if (ssrc_ == ssrc && ssrc_forced_) {
1426 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001427 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001428 ssrc_forced_ = true;
1429 ssrc_db_.ReturnSSRC(ssrc_);
1430 ssrc_db_.RegisterSSRC(ssrc);
1431 ssrc_ = ssrc;
1432 if (!sequence_number_forced_) {
1433 sequence_number_ =
1434 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001435 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001436}
1437
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001438uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001439 CriticalSectionScoped cs(send_critsect_);
1440 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001441}
1442
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001443void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001444 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001445}
1446
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001447void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1448 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001449 assert(arr_length <= kRtpCsrcSize);
1450 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001451
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001452 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001453 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001454 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001455 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001456}
1457
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001458int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001459 assert(arr_of_csrc);
1460 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001461 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1462 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001463 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001464 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001465}
1466
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001467void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001468 CriticalSectionScoped cs(send_critsect_);
1469 sequence_number_forced_ = true;
1470 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001471}
1472
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001473uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001474 CriticalSectionScoped cs(send_critsect_);
1475 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001476}
1477
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001478// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001479int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1480 const uint16_t time_ms,
1481 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001482 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001483 return -1;
1484 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001485 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001486}
1487
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001488bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001489 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001490 return false;
1491 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001492 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001493}
1494
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001495int32_t RTPSender::SetAudioPacketSize(
1496 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001497 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001498 return -1;
1499 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001500 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001501}
1502
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001503int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001504 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001505}
1506
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001507int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001508 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001509 return -1;
1510 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001511 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001512}
1513
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001514int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001515 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001516 return -1;
1517 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001518 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001519}
1520
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001521// Video
1522VideoCodecInformation *RTPSender::CodecInformationVideo() {
1523 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001524 return NULL;
1525 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001526 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001527}
1528
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001529RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001530 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001531 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001532}
1533
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001534uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001535 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001536 return 0;
1537 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001538 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001539}
1540
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001541int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001542 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001543 return -1;
1544 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001545 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001546}
1547
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001548int32_t RTPSender::SetGenericFECStatus(
1549 const bool enable, const uint8_t payload_type_red,
1550 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001551 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001552 return -1;
1553 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001554 return video_->SetGenericFECStatus(enable, payload_type_red,
1555 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001556}
1557
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001558int32_t RTPSender::GenericFECStatus(
1559 bool *enable, uint8_t *payload_type_red,
1560 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001561 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001562 return -1;
1563 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001564 return video_->GenericFECStatus(
1565 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001566}
1567
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001568int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001569 const FecProtectionParams *delta_params,
1570 const FecProtectionParams *key_params) {
1571 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001572 return -1;
1573 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001574 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001575}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001576
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001577void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1578 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001579 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001580 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001581 // Add RTX header.
1582 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001583 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001584
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001585 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001586 rtp_parser.Parse(rtp_header);
1587
1588 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001589 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001590
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001591 // Replace payload type, if a specific type is set for RTX.
1592 if (payload_type_rtx_ != -1) {
1593 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001594 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001595 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1596 }
1597
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001598 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001599 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001600 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1601
1602 // Replace SSRC.
1603 ptr += 6;
1604 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1605
1606 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001607 ptr = data_buffer_rtx + rtp_header.headerLength;
1608 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001609 ptr += 2;
1610
1611 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001612 memcpy(ptr, buffer + rtp_header.headerLength,
1613 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001614 *length += 2;
1615}
1616
sprang@webrtc.org71f055f2013-12-04 15:09:27 +00001617void RTPSender::RegisterFrameCountObserver(FrameCountObserver* observer) {
1618 CriticalSectionScoped cs(statistics_crit_.get());
1619 if (observer != NULL)
1620 assert(frame_count_observer_ == NULL);
1621 frame_count_observer_ = observer;
1622}
1623
1624FrameCountObserver* RTPSender::GetFrameCountObserver() const {
1625 CriticalSectionScoped cs(statistics_crit_.get());
1626 return frame_count_observer_;
1627}
1628
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001629void RTPSender::RegisterRtpStatisticsCallback(
1630 StreamDataCountersCallback* callback) {
1631 CriticalSectionScoped cs(statistics_crit_.get());
1632 if (callback != NULL)
1633 assert(rtp_stats_callback_ == NULL);
1634 rtp_stats_callback_ = callback;
1635}
1636
1637StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1638 CriticalSectionScoped cs(statistics_crit_.get());
1639 return rtp_stats_callback_;
1640}
1641
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001642void RTPSender::RegisterBitrateObserver(BitrateStatisticsObserver* observer) {
1643 CriticalSectionScoped cs(statistics_crit_.get());
1644 if (observer != NULL)
1645 assert(bitrate_callback_ == NULL);
1646 bitrate_callback_ = observer;
1647}
1648
1649BitrateStatisticsObserver* RTPSender::GetBitrateObserver() const {
1650 CriticalSectionScoped cs(statistics_crit_.get());
1651 return bitrate_callback_;
1652}
1653
1654uint32_t RTPSender::BitrateSent() const { return bitrate_sent_.BitrateLast(); }
1655
1656void RTPSender::BitrateUpdated(const BitrateStatistics& stats) {
1657 CriticalSectionScoped cs(statistics_crit_.get());
1658 if (bitrate_callback_) {
1659 bitrate_callback_->Notify(stats, ssrc_);
1660 }
1661}
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +00001662
1663void RTPSender::SetTargetBitrateKbps(uint16_t bitrate_kbps) {
1664 CriticalSectionScoped cs(target_bitrate_critsect_.get());
1665 target_bitrate_kbps_ = bitrate_kbps;
1666}
1667
1668uint16_t RTPSender::GetTargetBitrateKbps() {
1669 CriticalSectionScoped cs(target_bitrate_critsect_.get());
1670 return target_bitrate_kbps_;
1671}
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001672} // namespace webrtc