blob: 74947693b845982c9fd0e44e4821eca3a7679b8d [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()),
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +000092 target_bitrate_(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
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000130void RTPSender::SetTargetBitrate(uint32_t bitrate) {
131 CriticalSectionScoped cs(target_bitrate_critsect_.get());
132 target_bitrate_ = bitrate;
133}
134
135uint32_t RTPSender::GetTargetBitrate() {
136 CriticalSectionScoped cs(target_bitrate_critsect_.get());
137 return target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000138}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000139
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000140uint16_t RTPSender::ActualSendBitrateKbit() const {
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000141 return (uint16_t)(bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000142}
143
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000144uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000145 if (video_) {
146 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000147 }
148 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000149}
150
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000151uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000152 if (video_) {
153 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000154 }
155 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000156}
157
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000158uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000159 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000160}
161
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000162bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
163 int* max_send_delay_ms) const {
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000164 if (!SendingMedia())
165 return false;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000166 CriticalSectionScoped cs(statistics_crit_.get());
167 SendDelayMap::const_iterator it = send_delays_.upper_bound(
168 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000169 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000170 return false;
171 int num_delays = 0;
172 for (; it != send_delays_.end(); ++it) {
173 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
174 *avg_send_delay_ms += it->second;
175 ++num_delays;
176 }
177 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
178 return true;
179}
180
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000181int32_t RTPSender::SetTransmissionTimeOffset(
182 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000183 if (transmission_time_offset > (0x800000 - 1) ||
184 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000185 return -1;
186 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000187 CriticalSectionScoped cs(send_critsect_);
188 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000189 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000190}
191
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000192int32_t RTPSender::SetAbsoluteSendTime(
193 const uint32_t absolute_send_time) {
194 if (absolute_send_time > 0xffffff) { // UWord24.
195 return -1;
196 }
197 CriticalSectionScoped cs(send_critsect_);
198 absolute_send_time_ = absolute_send_time;
199 return 0;
200}
201
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000202int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
203 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000204 CriticalSectionScoped cs(send_critsect_);
205 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000206}
207
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000208int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000209 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000210 CriticalSectionScoped cs(send_critsect_);
211 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000212}
213
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000214uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000215 CriticalSectionScoped cs(send_critsect_);
216 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000217}
218
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000219int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000220 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000221 const int8_t payload_number, const uint32_t frequency,
222 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000223 assert(payload_name);
224 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000225
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000226 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000227 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000228
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000229 if (payload_type_map_.end() != it) {
230 // We already use this payload type.
231 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000232 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000233
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000234 // Check if it's the same as we already have.
235 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000236 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000237 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000238 payload->typeSpecific.Audio.frequency == frequency &&
239 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000240 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000241 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000242 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000243 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000244 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000245 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000246 return 0;
247 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000248 }
249 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000250 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000251 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000252 ModuleRTPUtility::Payload *payload = NULL;
253 if (audio_configured_) {
254 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
255 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000256 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000257 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
258 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000259 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000260 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000261 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000262 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000263 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000264}
265
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000266int32_t RTPSender::DeRegisterSendPayload(
267 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000268 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000269
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000270 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000271 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000272
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000273 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000274 return -1;
275 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000276 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000277 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000278 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000279 return 0;
280}
niklase@google.com470e71d2011-07-07 08:21:25 +0000281
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000282int8_t RTPSender::SendPayloadType() const {
283 CriticalSectionScoped cs(send_critsect_);
284 return payload_type_;
285}
niklase@google.com470e71d2011-07-07 08:21:25 +0000286
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000287int RTPSender::SendPayloadFrequency() const {
288 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
289}
niklase@google.com470e71d2011-07-07 08:21:25 +0000290
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000291int32_t RTPSender::SetMaxPayloadLength(
292 const uint16_t max_payload_length,
293 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000294 // Sanity check.
295 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000296 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000297 return -1;
298 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000299 CriticalSectionScoped cs(send_critsect_);
300 max_payload_length_ = max_payload_length;
301 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000302 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000303}
304
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000305uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000306 if (audio_configured_) {
307 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000308 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000309 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
310 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
311 - ((rtx_) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000312 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000313}
314
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000315uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000316 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000317}
318
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000319uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000320
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000321void RTPSender::SetRTXStatus(int mode) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000322 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000323 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000324}
325
326void RTPSender::SetRtxSsrc(uint32_t ssrc) {
327 CriticalSectionScoped cs(send_critsect_);
328 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000329}
330
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000331void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000332 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000333 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000334 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000335 *ssrc = ssrc_rtx_;
336 *payload_type = payload_type_rtx_;
337}
338
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000339void RTPSender::SetRtxPayloadType(int payload_type) {
340 CriticalSectionScoped cs(send_critsect_);
341 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000342}
343
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000344int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
345 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000346 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000347
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000348 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000349 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000350 return -1;
351 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000352 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000353 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000354 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000355 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000356 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000357 // And it's a match...
358 return 0;
359 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000360 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000361 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000362 if (payload_type_ == payload_type) {
363 if (!audio_configured_) {
364 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000365 }
366 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000367 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000368 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000369 payload_type_map_.find(payload_type);
370 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000371 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000372 return -1;
373 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000374 payload_type_ = payload_type;
375 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000376 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000377 if (!payload->audio && !audio_configured_) {
378 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
379 *video_type = payload->typeSpecific.Video.videoCodecType;
380 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000381 }
382 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000383}
384
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000385int32_t RTPSender::SendOutgoingData(
386 const FrameType frame_type, const int8_t payload_type,
387 const uint32_t capture_timestamp, int64_t capture_time_ms,
388 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000389 const RTPFragmentationHeader *fragmentation,
390 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000391 {
392 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000393 CriticalSectionScoped cs(send_critsect_);
394 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000395 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000396 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000397 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000398 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000399 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000400 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000401 return -1;
402 }
403
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000404 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000405 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000406 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
407 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000408 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000409 frame_type == kFrameEmpty);
410
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000411 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
412 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000413 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000414 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
415 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000416 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000417
418 if (frame_type == kFrameEmpty) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000419 if (paced_sender_->Enabled()) {
420 // Padding is driven by the pacer and not by the encoder.
421 return 0;
422 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000423 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000424 capture_time_ms) ? 0 : -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000425 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000426 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
427 capture_timestamp, capture_time_ms,
428 payload_data, payload_size,
429 fragmentation, codec_info,
430 rtp_type_hdr);
431
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000432 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000433
434 CriticalSectionScoped cs(statistics_crit_.get());
435 uint32_t frame_count = ++frame_counts_[frame_type];
436 if (frame_count_observer_) {
437 frame_count_observer_->FrameCountUpdated(frame_type,
438 frame_count,
439 ssrc_);
440 }
441
442 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000443}
444
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000445int RTPSender::SendRedundantPayloads(int payload_type, int bytes_to_send) {
446 if (!(rtx_ & kRtxRedundantPayloads))
447 return 0;
448 uint8_t buffer[IP_PACKET_SIZE];
449 int bytes_left = bytes_to_send;
450 while (bytes_left > 0) {
451 uint16_t length = bytes_left;
452 int64_t capture_time_ms;
453 if (!packet_history_.GetBestFittingPacket(buffer, &length,
454 &capture_time_ms)) {
455 break;
456 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000457 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000458 return -1;
459 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
460 RTPHeader rtp_header;
461 rtp_parser.Parse(rtp_header);
462 bytes_left -= length - rtp_header.headerLength;
463 }
464 return bytes_to_send - bytes_left;
465}
466
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000467bool RTPSender::SendPaddingAccordingToBitrate(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000468 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000469 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000470 // Current bitrate since last estimate(1 second) averaged with the
471 // estimate since then, to get the most up to date bitrate.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000472 uint32_t current_bitrate = bitrate_sent_.BitrateNow();
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000473 uint32_t target_bitrate = GetTargetBitrate();
474 int bitrate_diff = target_bitrate - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000475 if (bitrate_diff <= 0) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000476 return true;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000477 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000478 int bytes = 0;
479 if (current_bitrate == 0) {
480 // Start up phase. Send one 33.3 ms batch to start with.
481 bytes = (bitrate_diff / 8) / 30;
482 } else {
483 bytes = (bitrate_diff / 8);
484 // Cap at 200 ms of target send data.
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000485 int bytes_cap = target_bitrate / 1000 * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000486 if (bytes > bytes_cap) {
487 bytes = bytes_cap;
488 }
489 }
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000490 uint32_t timestamp;
491 {
492 CriticalSectionScoped cs(send_critsect_);
493 // Add the random RTP timestamp offset and store the capture time for
494 // later calculation of the send time offset.
495 timestamp = start_time_stamp_ + capture_timestamp;
496 timestamp_ = timestamp;
497 capture_time_ms_ = capture_time_ms;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000498 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000499 }
500 int bytes_sent = SendPadData(payload_type, timestamp, capture_time_ms,
501 bytes, kDontRetransmit, false, false);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000502 // We did not manage to send all bytes. Comparing with 31 due to modulus 32.
503 return bytes - bytes_sent < 31;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000504}
505
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000506int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
507 int32_t bytes) {
508 int padding_bytes_in_packet = kMaxPaddingLength;
509 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000510 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000511 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000512 packet[0] |= 0x20; // Set padding bit.
513 int32_t *data =
514 reinterpret_cast<int32_t *>(&(packet[header_length]));
515
516 // Fill data buffer with random data.
517 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
518 data[j] = rand(); // NOLINT
519 }
520 // Set number of padding bytes in the last byte of the packet.
521 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
522 return padding_bytes_in_packet;
523}
524
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000525int RTPSender::SendPadData(int payload_type, uint32_t timestamp,
526 int64_t capture_time_ms, int32_t bytes,
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000527 StorageType store, bool force_full_size_packets,
528 bool only_pad_after_markerbit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000529 // Drop this packet if we're not sending media packets.
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000530 if (!SendingMedia()) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000531 return bytes;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000532 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000533 int padding_bytes_in_packet = 0;
534 int bytes_sent = 0;
535 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000536 // Always send full padding packets.
537 if (force_full_size_packets && bytes < kMaxPaddingLength)
538 bytes = kMaxPaddingLength;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000539 if (bytes < kMaxPaddingLength) {
540 if (force_full_size_packets) {
541 bytes = kMaxPaddingLength;
542 } else {
543 // Round to the nearest multiple of 32.
544 bytes = (bytes + 16) & 0xffe0;
545 }
546 }
stefan@webrtc.orga4c5abb2013-06-25 15:46:16 +0000547 if (bytes < 32) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000548 // Sanity don't send empty packets.
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000549 break;
550 }
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000551 uint32_t ssrc;
552 uint16_t sequence_number;
553 {
554 CriticalSectionScoped cs(send_critsect_);
555 // Only send padding packets following the last packet of a frame,
556 // indicated by the marker bit.
stefan@webrtc.orgd4f607e2013-08-19 15:55:01 +0000557 if (only_pad_after_markerbit && !last_packet_marker_bit_)
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000558 return bytes_sent;
559 if (rtx_ == kRtxOff) {
560 ssrc = ssrc_;
561 sequence_number = sequence_number_;
562 ++sequence_number_;
563 } else {
564 ssrc = ssrc_rtx_;
565 sequence_number = sequence_number_rtx_;
566 ++sequence_number_rtx_;
567 }
568 }
569 uint8_t padding_packet[IP_PACKET_SIZE];
570 int header_length = CreateRTPHeader(padding_packet, payload_type, ssrc,
571 false, timestamp, sequence_number, NULL,
572 0);
573 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length,
574 bytes);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000575 if (0 > SendToNetwork(padding_packet, padding_bytes_in_packet,
576 header_length, capture_time_ms, store,
577 PacedSender::kLowPriority)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000578 // Error sending the packet.
579 break;
580 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000581 bytes_sent += padding_bytes_in_packet;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000582 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000583 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000584}
585
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000586void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000587 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000588 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000589}
590
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000591bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000592 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000593}
niklase@google.com470e71d2011-07-07 08:21:25 +0000594
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000595int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
596 uint16_t length = IP_PACKET_SIZE;
597 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000598 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000599 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
600 data_buffer, &length,
601 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000602 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000603 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000604 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000605
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000606 if (paced_sender_) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000607 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
608 RTPHeader header;
609 if (!rtp_parser.Parse(header)) {
610 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000611 return -1;
612 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000613 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000614 header.ssrc,
615 header.sequenceNumber,
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000616 capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000617 length - header.headerLength,
618 true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000619 // We can't send the packet right now.
620 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000621 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000622 }
623 }
624
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000625 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org16395222014-03-19 19:34:07 +0000626 (rtx_ & kRtxRetransmitted) > 0, true) ?
627 length : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000628}
629
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000630bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
631 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000632 if (transport_) {
633 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000634 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000635 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
636 "size", size, "sent", bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000637 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000638 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000639 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000640 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000641 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000642 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000643}
644
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000645int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000646 if (!video_)
647 return -1;
648 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000649}
650
651int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000652 if (!video_)
653 return -1;
654 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000655}
656
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000657void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000658 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000659 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000660 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
661 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000662 const int64_t now = clock_->TimeInMilliseconds();
663 uint32_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000664 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000665
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000666 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000667 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000668 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000669 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000670 return;
671 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000672
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000673 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
674 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000675 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000676 if (bytes_sent > 0) {
677 bytes_re_sent += bytes_sent;
678 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000679 // The packet has previously been resent.
680 // Try resending next packet in the list.
681 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000682 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000683 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000684 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
685 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000686 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000687 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000688 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000689 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000690 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000691 uint32_t target_bytes =
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000692 (static_cast<uint32_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000693 if (bytes_re_sent > target_bytes) {
694 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000695 }
696 }
697 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000698 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000699 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000700 UpdateNACKBitRate(bytes_re_sent, now);
701 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000702 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000703}
704
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000705bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
706 uint32_t num = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000707 int byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000708 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000709 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000710
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000711 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000712
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000713 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000714 return true;
715 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000716 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000717 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000718 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000719 break;
720 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000721 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000722 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000723 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000724 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000725 if (num == NACK_BYTECOUNT_SIZE) {
726 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000727 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000728 if (nack_byte_count_times_[num - 1] <= now) {
729 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000730 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000731 }
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000732 return (byte_count * 8) <
733 static_cast<int>(target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000734}
735
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000736void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
737 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000738 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000739
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000740 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000741 if (bytes > 0) {
742 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000743 // Add padding length.
744 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000745 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000746 if (nack_byte_count_times_[0] == 0) {
747 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000748 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000749 // Shift.
750 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
751 nack_byte_count_[i + 1] = nack_byte_count_[i];
752 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000753 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000754 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000755 nack_byte_count_[0] = bytes;
756 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000757 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000758 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000759}
760
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000761// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000762bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000763 int64_t capture_time_ms,
764 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000765 uint16_t length = IP_PACKET_SIZE;
766 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000767 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000768
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000769 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
770 0,
771 retransmission,
772 data_buffer,
773 &length,
774 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000775 // Packet cannot be found. Allow sending to continue.
776 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000777 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000778 if (!retransmission && capture_time_ms > 0) {
779 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
780 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000781 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000782 retransmission && (rtx_ & kRtxRetransmitted) > 0,
783 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000784}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000785
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000786bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
787 uint16_t length,
788 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000789 bool send_over_rtx,
790 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000791 uint8_t *buffer_to_send_ptr = buffer;
792
793 ModuleRTPUtility::RTPHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000794 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000795 rtp_parser.Parse(rtp_header);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000796 TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000797 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000798 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000799
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000800 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000801 if (send_over_rtx) {
802 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000803 buffer_to_send_ptr = data_buffer_rtx;
804 }
805
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000806 int64_t now_ms = clock_->TimeInMilliseconds();
807 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000808 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
809 diff_ms);
810 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000811 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000812 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
813 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000814 return ret;
815}
816
817void RTPSender::UpdateRtpStats(const uint8_t* buffer,
818 uint32_t size,
819 const RTPHeader& header,
820 bool is_rtx,
821 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000822 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000823 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
824 uint32_t ssrc = SSRC();
825
826 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000827 if (is_rtx) {
828 counters = &rtx_rtp_stats_;
829 ssrc = ssrc_rtx_;
830 } else {
831 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000832 }
833
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000834 bitrate_sent_.Update(size);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000835 ++counters->packets;
836 if (IsFecPacket(buffer, header)) {
837 ++counters->fec_packets;
838 }
839
840 if (is_retransmit) {
841 ++counters->retransmitted_packets;
842 } else {
843 counters->bytes += size - (header.headerLength + header.paddingLength);
844 counters->header_bytes += header.headerLength;
845 counters->padding_bytes += header.paddingLength;
846 }
847
848 if (rtp_stats_callback_) {
849 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
850 }
851}
852
853bool RTPSender::IsFecPacket(const uint8_t* buffer,
854 const RTPHeader& header) const {
855 if (!video_) {
856 return false;
857 }
858 bool fec_enabled;
859 uint8_t pt_red;
860 uint8_t pt_fec;
861 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
862 return fec_enabled &&
863 header.payloadType == pt_red &&
864 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000865}
866
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000867int RTPSender::TimeToSendPadding(int bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000868 int payload_type;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000869 int64_t capture_time_ms;
870 uint32_t timestamp;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000871 {
872 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000873 if (!sending_media_) {
874 return 0;
875 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000876 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_ :
877 payload_type_;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000878 timestamp = timestamp_;
879 capture_time_ms = capture_time_ms_;
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +0000880 if (last_timestamp_time_ms_ > 0) {
881 timestamp +=
882 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
883 capture_time_ms +=
884 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
885 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000886 }
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000887 int bytes_sent = SendRedundantPayloads(payload_type, bytes);
888 bytes -= bytes_sent;
889 if (bytes > 0) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000890 int padding_sent = SendPadData(payload_type,
891 timestamp,
892 capture_time_ms,
893 bytes,
894 kDontStore,
895 true,
896 rtx_ == kRtxOff);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000897 bytes_sent += padding_sent;
898 }
899 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000900}
901
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000902// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000903int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000904 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000905 int64_t capture_time_ms, StorageType storage,
906 PacedSender::Priority priority) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000907 ModuleRTPUtility::RTPHeaderParser rtp_parser(
908 buffer, payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000909 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000910 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000911
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000912 int64_t now_ms = clock_->TimeInMilliseconds();
913
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000914 // |capture_time_ms| <= 0 is considered invalid.
915 // TODO(holmer): This should be changed all over Video Engine so that negative
916 // time is consider invalid, while 0 is considered a valid time.
917 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000918 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000919 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000920 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000921
922 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
923 rtp_header, now_ms);
924
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000925 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000926 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
927 max_payload_length_, capture_time_ms,
928 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000929 return -1;
930 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000931
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000932 if (paced_sender_ && storage != kDontStore) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000933 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
934 rtp_header.sequenceNumber, capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000935 payload_length, false)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000936 // We can't send the packet right now.
937 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000938 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000939 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000940 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000941 if (capture_time_ms > 0) {
942 UpdateDelayStatistics(capture_time_ms, now_ms);
943 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000944 uint32_t length = payload_length + rtp_header_length;
945 if (!SendPacketToNetwork(buffer, length))
946 return -1;
947 UpdateRtpStats(buffer, length, rtp_header, false, false);
948 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000949}
950
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000951void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
952 CriticalSectionScoped cs(statistics_crit_.get());
953 send_delays_[now_ms] = now_ms - capture_time_ms;
954 send_delays_.erase(send_delays_.begin(),
955 send_delays_.lower_bound(now_ms - kSendSideDelayWindowMs));
956}
957
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000958void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000959 CriticalSectionScoped cs(send_critsect_);
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000960 bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000961 nack_bitrate_.Process();
962 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000963 return;
964 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000965 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000966}
967
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000968uint16_t RTPSender::RTPHeaderLength() const {
969 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000970 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000971 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000972 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000973 rtp_header_length += RtpHeaderExtensionTotalLength();
974 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000975}
976
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000977uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000978 CriticalSectionScoped cs(send_critsect_);
979 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000980}
981
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000982void RTPSender::ResetDataCounters() {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000983 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000984 rtp_stats_ = StreamDataCounters();
985 rtx_rtp_stats_ = StreamDataCounters();
986 if (rtp_stats_callback_) {
987 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc_);
988 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx_);
989 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000990}
991
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000992uint32_t RTPSender::Packets() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000993 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000994 return rtp_stats_.packets + rtx_rtp_stats_.packets;
niklase@google.com470e71d2011-07-07 08:21:25 +0000995}
996
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000997// Number of sent RTP bytes.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000998uint32_t RTPSender::Bytes() const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000999 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001000 return rtp_stats_.bytes + rtx_rtp_stats_.bytes;
niklase@google.com470e71d2011-07-07 08:21:25 +00001001}
1002
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001003int RTPSender::CreateRTPHeader(
1004 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
1005 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
1006 uint8_t num_csrcs) const {
1007 header[0] = 0x80; // version 2.
1008 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001009 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001010 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001011 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001012 ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1013 ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1014 ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001015 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001016
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001017 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001018 if (num_csrcs > 0) {
1019 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001020 // error
1021 assert(false);
1022 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001023 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001024 uint8_t *ptr = &header[rtp_header_length];
1025 for (int i = 0; i < num_csrcs; ++i) {
1026 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001027 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001028 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001029 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001030
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001031 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001032 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001033 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001034
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001035 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1036 if (len > 0) {
1037 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001038 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001039 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001040 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001041}
1042
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001043int32_t RTPSender::BuildRTPheader(
1044 uint8_t *data_buffer, const int8_t payload_type,
1045 const bool marker_bit, const uint32_t capture_timestamp,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001046 int64_t capture_time_ms, const bool time_stamp_provided,
1047 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001048 assert(payload_type >= 0);
1049 CriticalSectionScoped cs(send_critsect_);
1050
1051 if (time_stamp_provided) {
1052 timestamp_ = start_time_stamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001053 } else {
1054 // Make a unique time stamp.
1055 // We can't inc by the actual time, since then we increase the risk of back
1056 // timing.
1057 timestamp_++;
1058 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001059 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001060 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001061 capture_time_ms_ = capture_time_ms;
1062 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001063 int csrcs_length = 0;
1064 if (include_csrcs_)
1065 csrcs_length = num_csrcs_;
1066 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1067 timestamp_, sequence_number, csrcs_, csrcs_length);
1068}
1069
1070uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001071 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001072 return 0;
1073 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001074 // RTP header extension, RFC 3550.
1075 // 0 1 2 3
1076 // 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
1077 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1078 // | defined by profile | length |
1079 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1080 // | header extension |
1081 // | .... |
1082 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001083 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001084 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001085
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001086 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001087 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001088 kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001089
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001090 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001091 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001092
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001093 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001094 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001095 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001096 switch (type) {
1097 case kRtpExtensionTransmissionTimeOffset:
1098 block_length = BuildTransmissionTimeOffsetExtension(
1099 data_buffer + kHeaderLength + total_block_length);
1100 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001101 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001102 block_length = BuildAudioLevelExtension(
1103 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001104 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001105 case kRtpExtensionAbsoluteSendTime:
1106 block_length = BuildAbsoluteSendTimeExtension(
1107 data_buffer + kHeaderLength + total_block_length);
1108 break;
1109 default:
1110 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001111 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001112 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001113 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001114 }
1115 if (total_block_length == 0) {
1116 // No extension added.
1117 return 0;
1118 }
1119 // Set header length (in number of Word32, header excluded).
1120 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001121 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001122 total_block_length / 4);
1123 // Total added length.
1124 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001125}
1126
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001127uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1128 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001129 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1130 //
1131 // The transmission time is signaled to the receiver in-band using the
1132 // general mechanism for RTP header extensions [RFC5285]. The payload
1133 // of this extension (the transmitted value) is a 24-bit signed integer.
1134 // When added to the RTP timestamp of the packet, it represents the
1135 // "effective" RTP transmission time of the packet, on the RTP
1136 // timescale.
1137 //
1138 // The form of the transmission offset extension block:
1139 //
1140 // 0 1 2 3
1141 // 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
1142 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1143 // | ID | len=2 | transmission offset |
1144 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001145
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001146 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001147 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001148 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1149 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001150 // Not registered.
1151 return 0;
1152 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001153 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001154 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001155 data_buffer[pos++] = (id << 4) + len;
1156 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1157 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001158 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001159 assert(pos == kTransmissionTimeOffsetLength);
1160 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001161}
1162
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001163uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1164 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1165 //
1166 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1167 //
1168 // The form of the audio level extension block:
1169 //
1170 // 0 1 2 3
1171 // 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
1172 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1173 // | ID | len=0 |V| level | 0x00 | 0x00 |
1174 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1175 //
1176 // Note that we always include 2 pad bytes, which will result in legal and
1177 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1178 // are implemented. Right now the pad bytes would anyway be required at end
1179 // of the extension block, so it makes no difference.
1180
1181 // Get id defined by user.
1182 uint8_t id;
1183 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1184 // Not registered.
1185 return 0;
1186 }
1187 size_t pos = 0;
1188 const uint8_t len = 0;
1189 data_buffer[pos++] = (id << 4) + len;
1190 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1191 data_buffer[pos++] = 0; // Padding.
1192 data_buffer[pos++] = 0; // Padding.
1193 // kAudioLevelLength is including pad bytes.
1194 assert(pos == kAudioLevelLength);
1195 return kAudioLevelLength;
1196}
1197
1198uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001199 // Absolute send time in RTP streams.
1200 //
1201 // The absolute send time is signaled to the receiver in-band using the
1202 // general mechanism for RTP header extensions [RFC5285]. The payload
1203 // of this extension (the transmitted value) is a 24-bit unsigned integer
1204 // containing the sender's current time in seconds as a fixed point number
1205 // with 18 bits fractional part.
1206 //
1207 // The form of the absolute send time extension block:
1208 //
1209 // 0 1 2 3
1210 // 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
1211 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1212 // | ID | len=2 | absolute send time |
1213 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1214
1215 // Get id defined by user.
1216 uint8_t id;
1217 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1218 &id) != 0) {
1219 // Not registered.
1220 return 0;
1221 }
1222 size_t pos = 0;
1223 const uint8_t len = 2;
1224 data_buffer[pos++] = (id << 4) + len;
1225 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
1226 absolute_send_time_);
1227 pos += 3;
1228 assert(pos == kAbsoluteSendTimeLength);
1229 return kAbsoluteSendTimeLength;
1230}
1231
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001232void RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001233 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001234 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001235 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001236 // Get id.
1237 uint8_t id = 0;
1238 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1239 &id) != 0) {
1240 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001241 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001242 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001243 // Get length until start of header extension block.
1244 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001245 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001246 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001247 if (extension_block_pos < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001248 LOG(LS_WARNING)
1249 << "Failed to update transmission time offset, not registered.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001250 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001251 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001252 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001253 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001254 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001255 block_pos + kTransmissionTimeOffsetLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001256 LOG(LS_WARNING)
1257 << "Failed to update transmission time offset, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001258 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001259 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001260 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001261 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1262 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001263 LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1264 "extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001265 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001266 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001267 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001268 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001269 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001270 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001271 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001272 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001273 // Update transmission offset field (converting to a 90 kHz timestamp).
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001274 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +00001275 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001276}
1277
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001278bool RTPSender::UpdateAudioLevel(uint8_t *rtp_packet,
1279 const uint16_t rtp_packet_length,
1280 const RTPHeader &rtp_header,
1281 const bool is_voiced,
1282 const uint8_t dBov) const {
1283 CriticalSectionScoped cs(send_critsect_);
1284
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001285 // Get id.
1286 uint8_t id = 0;
1287 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1288 // Not registered.
1289 return false;
1290 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001291 // Get length until start of header extension block.
1292 int extension_block_pos =
1293 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1294 kRtpExtensionAudioLevel);
1295 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001296 // The feature is not enabled.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001297 return false;
1298 }
1299 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1300 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1301 rtp_header.headerLength < block_pos + kAudioLevelLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001302 LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001303 return false;
1304 }
1305 // Verify that header contains extension.
1306 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1307 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001308 LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001309 return false;
1310 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001311 // Verify first byte in block.
1312 const uint8_t first_block_byte = (id << 4) + 0;
1313 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001314 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001315 return false;
1316 }
1317 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1318 return true;
1319}
1320
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001321void RTPSender::UpdateAbsoluteSendTime(
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001322 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001323 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001324 CriticalSectionScoped cs(send_critsect_);
1325
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001326 // Get id.
1327 uint8_t id = 0;
1328 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1329 &id) != 0) {
1330 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001331 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001332 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001333 // Get length until start of header extension block.
1334 int extension_block_pos =
1335 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1336 kRtpExtensionAbsoluteSendTime);
1337 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001338 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001339 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001340 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001341 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001342 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001343 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001344 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001345 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001346 }
1347 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001348 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1349 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001350 LOG(LS_WARNING)
1351 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001352 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001353 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001354 // Verify first byte in block.
1355 const uint8_t first_block_byte = (id << 4) + 2;
1356 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001357 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001358 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001359 }
1360 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1361 // fractional part).
1362 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1363 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001364}
1365
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001366void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001367 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001368 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001369 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001370
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001371 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001372 SetStartTimestamp(RTPtime, false);
1373 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001374 if (!ssrc_forced_) {
1375 // Generate a new SSRC.
1376 ssrc_db_.ReturnSSRC(ssrc_);
1377 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001378 }
1379 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001380 if (!sequence_number_forced_ && !ssrc_forced_) {
1381 // Generate a new sequence number.
1382 sequence_number_ =
1383 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001384 }
1385 }
1386}
1387
1388void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001389 CriticalSectionScoped cs(send_critsect_);
1390 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001391}
1392
1393bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001394 CriticalSectionScoped cs(send_critsect_);
1395 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001396}
1397
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001398uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001399 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001400 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001401}
1402
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001403void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001404 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001405 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001406 start_time_stamp_forced_ = force;
1407 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001408 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001409 if (!start_time_stamp_forced_) {
1410 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001411 }
1412 }
1413}
1414
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001415uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001416 CriticalSectionScoped cs(send_critsect_);
1417 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001418}
1419
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001420uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001421 // If configured via API, return 0.
1422 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001423
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001424 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001425 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001426 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001427 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1428 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001429}
1430
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001431void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001432 // This is configured via the API.
1433 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001434
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001435 if (ssrc_ == ssrc && ssrc_forced_) {
1436 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001437 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001438 ssrc_forced_ = true;
1439 ssrc_db_.ReturnSSRC(ssrc_);
1440 ssrc_db_.RegisterSSRC(ssrc);
1441 ssrc_ = ssrc;
1442 if (!sequence_number_forced_) {
1443 sequence_number_ =
1444 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001445 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001446}
1447
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001448uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001449 CriticalSectionScoped cs(send_critsect_);
1450 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001451}
1452
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001453void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001454 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001455}
1456
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001457void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1458 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001459 assert(arr_length <= kRtpCsrcSize);
1460 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001461
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001462 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001463 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001464 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001465 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001466}
1467
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001468int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001469 assert(arr_of_csrc);
1470 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001471 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1472 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001473 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001474 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001475}
1476
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001477void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001478 CriticalSectionScoped cs(send_critsect_);
1479 sequence_number_forced_ = true;
1480 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001481}
1482
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001483uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001484 CriticalSectionScoped cs(send_critsect_);
1485 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001486}
1487
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001488// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001489int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1490 const uint16_t time_ms,
1491 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001492 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001493 return -1;
1494 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001495 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001496}
1497
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001498bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001499 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001500 return false;
1501 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001502 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001503}
1504
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001505int32_t RTPSender::SetAudioPacketSize(
1506 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001507 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001508 return -1;
1509 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001510 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001511}
1512
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001513int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001514 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001515}
1516
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001517int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001518 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001519 return -1;
1520 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001521 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001522}
1523
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001524int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001525 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001526 return -1;
1527 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001528 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001529}
1530
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001531// Video
1532VideoCodecInformation *RTPSender::CodecInformationVideo() {
1533 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001534 return NULL;
1535 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001536 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001537}
1538
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001539RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001540 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001541 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001542}
1543
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001544uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001545 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001546 return 0;
1547 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001548 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001549}
1550
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001551int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001552 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001553 return -1;
1554 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001555 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001556}
1557
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001558int32_t RTPSender::SetGenericFECStatus(
1559 const bool enable, const uint8_t payload_type_red,
1560 const uint8_t payload_type_fec) {
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_->SetGenericFECStatus(enable, payload_type_red,
1565 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001566}
1567
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001568int32_t RTPSender::GenericFECStatus(
1569 bool *enable, uint8_t *payload_type_red,
1570 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001571 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001572 return -1;
1573 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001574 return video_->GenericFECStatus(
1575 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001576}
1577
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001578int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001579 const FecProtectionParams *delta_params,
1580 const FecProtectionParams *key_params) {
1581 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001582 return -1;
1583 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001584 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001585}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001586
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001587void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1588 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001589 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001590 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001591 // Add RTX header.
1592 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001593 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001594
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001595 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001596 rtp_parser.Parse(rtp_header);
1597
1598 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001599 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001600
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001601 // Replace payload type, if a specific type is set for RTX.
1602 if (payload_type_rtx_ != -1) {
1603 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001604 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001605 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1606 }
1607
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001608 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001609 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001610 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1611
1612 // Replace SSRC.
1613 ptr += 6;
1614 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1615
1616 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001617 ptr = data_buffer_rtx + rtp_header.headerLength;
1618 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001619 ptr += 2;
1620
1621 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001622 memcpy(ptr, buffer + rtp_header.headerLength,
1623 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001624 *length += 2;
1625}
1626
sprang@webrtc.org71f055f2013-12-04 15:09:27 +00001627void RTPSender::RegisterFrameCountObserver(FrameCountObserver* observer) {
1628 CriticalSectionScoped cs(statistics_crit_.get());
1629 if (observer != NULL)
1630 assert(frame_count_observer_ == NULL);
1631 frame_count_observer_ = observer;
1632}
1633
1634FrameCountObserver* RTPSender::GetFrameCountObserver() const {
1635 CriticalSectionScoped cs(statistics_crit_.get());
1636 return frame_count_observer_;
1637}
1638
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001639void RTPSender::RegisterRtpStatisticsCallback(
1640 StreamDataCountersCallback* callback) {
1641 CriticalSectionScoped cs(statistics_crit_.get());
1642 if (callback != NULL)
1643 assert(rtp_stats_callback_ == NULL);
1644 rtp_stats_callback_ = callback;
1645}
1646
1647StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1648 CriticalSectionScoped cs(statistics_crit_.get());
1649 return rtp_stats_callback_;
1650}
1651
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001652void RTPSender::RegisterBitrateObserver(BitrateStatisticsObserver* observer) {
1653 CriticalSectionScoped cs(statistics_crit_.get());
1654 if (observer != NULL)
1655 assert(bitrate_callback_ == NULL);
1656 bitrate_callback_ = observer;
1657}
1658
1659BitrateStatisticsObserver* RTPSender::GetBitrateObserver() const {
1660 CriticalSectionScoped cs(statistics_crit_.get());
1661 return bitrate_callback_;
1662}
1663
1664uint32_t RTPSender::BitrateSent() const { return bitrate_sent_.BitrateLast(); }
1665
1666void RTPSender::BitrateUpdated(const BitrateStatistics& stats) {
1667 CriticalSectionScoped cs(statistics_crit_.get());
1668 if (bitrate_callback_) {
1669 bitrate_callback_->Notify(stats, ssrc_);
1670 }
1671}
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001672} // namespace webrtc