blob: 38ac859fd749f4e6de4db3d974d058e33be461da [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"
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000019#include "webrtc/system_wrappers/interface/tick_util.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000020#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
stefan@webrtc.orga8179622013-06-04 13:47:36 +000024// Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000025const size_t kMaxPaddingLength = 224;
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +000026const int kSendSideDelayWindowMs = 1000;
stefan@webrtc.orga8179622013-06-04 13:47:36 +000027
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000028namespace {
29
30const char* FrameTypeToString(const FrameType frame_type) {
31 switch (frame_type) {
32 case kFrameEmpty: return "empty";
33 case kAudioFrameSpeech: return "audio_speech";
34 case kAudioFrameCN: return "audio_cn";
35 case kVideoFrameKey: return "video_key";
36 case kVideoFrameDelta: return "video_delta";
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000037 }
38 return "";
39}
40
41} // namespace
42
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +000043class BitrateAggregator {
44 public:
45 explicit BitrateAggregator(BitrateStatisticsObserver* bitrate_callback)
46 : callback_(bitrate_callback),
47 total_bitrate_observer_(*this),
48 retransmit_bitrate_observer_(*this),
49 ssrc_(0) {}
50
51 void OnStatsUpdated() const {
52 if (callback_)
53 callback_->Notify(total_bitrate_observer_.statistics(),
54 retransmit_bitrate_observer_.statistics(),
55 ssrc_);
56 }
57
58 Bitrate::Observer* total_bitrate_observer() {
59 return &total_bitrate_observer_;
60 }
61 Bitrate::Observer* retransmit_bitrate_observer() {
62 return &retransmit_bitrate_observer_;
63 }
64
65 void set_ssrc(uint32_t ssrc) { ssrc_ = ssrc; }
66
67 private:
68 // We assume that these observers are called on the same thread, which is
69 // true for RtpSender as they are called on the Process thread.
70 class BitrateObserver : public Bitrate::Observer {
71 public:
72 explicit BitrateObserver(const BitrateAggregator& aggregator)
73 : aggregator_(aggregator) {}
74
75 // Implements Bitrate::Observer.
76 virtual void BitrateUpdated(const BitrateStatistics& stats) OVERRIDE {
77 statistics_ = stats;
78 aggregator_.OnStatsUpdated();
79 }
80
81 BitrateStatistics statistics() const { return statistics_; }
82
83 private:
84 BitrateStatistics statistics_;
85 const BitrateAggregator& aggregator_;
86 };
87
88 BitrateStatisticsObserver* const callback_;
89 BitrateObserver total_bitrate_observer_;
90 BitrateObserver retransmit_bitrate_observer_;
91 uint32_t ssrc_;
92};
93
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +000094RTPSender::RTPSender(const int32_t id,
95 const bool audio,
96 Clock* clock,
97 Transport* transport,
98 RtpAudioFeedback* audio_feedback,
andresp@webrtc.orgd11bec42014-07-08 14:32:58 +000099 PacedSender* paced_sender,
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000100 BitrateStatisticsObserver* bitrate_callback,
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000101 FrameCountObserver* frame_count_observer,
102 SendSideDelayObserver* send_side_delay_observer)
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000103 : clock_(clock),
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000104 // TODO(holmer): Remove this conversion when we remove the use of
105 // TickTime.
106 clock_delta_ms_(clock_->TimeInMilliseconds() -
107 TickTime::MillisecondTimestamp()),
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000108 bitrates_(new BitrateAggregator(bitrate_callback)),
109 total_bitrate_sent_(clock, bitrates_->total_bitrate_observer()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000110 id_(id),
111 audio_configured_(audio),
112 audio_(NULL),
113 video_(NULL),
114 paced_sender_(paced_sender),
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000115 last_capture_time_ms_sent_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000116 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000117 transport_(transport),
118 sending_media_(true), // Default to sending media.
119 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000120 packet_over_head_(28),
121 payload_type_(-1),
122 payload_type_map_(),
123 rtp_header_extension_map_(),
124 transmission_time_offset_(0),
125 absolute_send_time_(0),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000126 // NACK.
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000127 nack_byte_count_times_(),
128 nack_byte_count_(),
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000129 nack_bitrate_(clock, bitrates_->retransmit_bitrate_observer()),
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000130 packet_history_(clock),
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000131 // Statistics
pbos@webrtc.orge07049f2013-09-10 11:29:17 +0000132 statistics_crit_(CriticalSectionWrapper::CreateCriticalSection()),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000133 rtp_stats_callback_(NULL),
andresp@webrtc.org8f151212014-07-10 09:39:23 +0000134 frame_count_observer_(frame_count_observer),
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000135 send_side_delay_observer_(send_side_delay_observer),
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000136 // RTP variables
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000137 start_timestamp_forced_(false),
138 start_timestamp_(0),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000139 ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
140 remote_ssrc_(0),
141 sequence_number_forced_(false),
142 ssrc_forced_(false),
143 timestamp_(0),
144 capture_time_ms_(0),
145 last_timestamp_time_ms_(0),
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000146 media_has_been_sent_(false),
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +0000147 last_packet_marker_bit_(false),
148 num_csrcs_(0),
149 csrcs_(),
150 include_csrcs_(true),
151 rtx_(kRtxOff),
andresp@webrtc.orgd09d0742014-03-26 14:27:34 +0000152 payload_type_rtx_(-1),
153 target_bitrate_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000154 target_bitrate_(0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000155 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
156 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000157 memset(csrcs_, 0, sizeof(csrcs_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000158 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000159 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000160 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000161 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000162 bitrates_->set_ssrc(ssrc_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000163 // Random start, 16 bits. Can't be 0.
164 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
165 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000167 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000168 audio_ = new RTPSenderAudio(id, clock_, this);
169 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000170 } else {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000171 video_ = new RTPSenderVideo(clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000172 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000173}
174
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000175RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000176 if (remote_ssrc_ != 0) {
177 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000178 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000179 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000180
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000181 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000182 delete send_critsect_;
183 while (!payload_type_map_.empty()) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000184 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000185 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000186 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000187 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000188 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000189 delete audio_;
190 delete video_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191}
niklase@google.com470e71d2011-07-07 08:21:25 +0000192
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000193void RTPSender::SetTargetBitrate(uint32_t bitrate) {
194 CriticalSectionScoped cs(target_bitrate_critsect_.get());
195 target_bitrate_ = bitrate;
196}
197
198uint32_t RTPSender::GetTargetBitrate() {
199 CriticalSectionScoped cs(target_bitrate_critsect_.get());
200 return target_bitrate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000201}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000202
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000203uint16_t RTPSender::ActualSendBitrateKbit() const {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000204 return (uint16_t)(total_bitrate_sent_.BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000205}
206
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000207uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000208 if (video_) {
209 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000210 }
211 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000212}
213
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000214uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000215 if (video_) {
216 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000217 }
218 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000219}
220
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000221uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000222 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000223}
224
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000225bool RTPSender::GetSendSideDelay(int* avg_send_delay_ms,
226 int* max_send_delay_ms) const {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000227 CriticalSectionScoped lock(statistics_crit_.get());
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000228 SendDelayMap::const_iterator it = send_delays_.upper_bound(
229 clock_->TimeInMilliseconds() - kSendSideDelayWindowMs);
sprang@webrtc.org5a320fb2014-03-13 15:12:37 +0000230 if (it == send_delays_.end())
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000231 return false;
232 int num_delays = 0;
233 for (; it != send_delays_.end(); ++it) {
234 *max_send_delay_ms = std::max(*max_send_delay_ms, it->second);
235 *avg_send_delay_ms += it->second;
236 ++num_delays;
237 }
238 *avg_send_delay_ms = (*avg_send_delay_ms + num_delays / 2) / num_delays;
239 return true;
240}
241
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000242int32_t RTPSender::SetTransmissionTimeOffset(
243 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000244 if (transmission_time_offset > (0x800000 - 1) ||
245 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000246 return -1;
247 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000248 CriticalSectionScoped cs(send_critsect_);
249 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000250 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000251}
252
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000253int32_t RTPSender::SetAbsoluteSendTime(
254 const uint32_t absolute_send_time) {
255 if (absolute_send_time > 0xffffff) { // UWord24.
256 return -1;
257 }
258 CriticalSectionScoped cs(send_critsect_);
259 absolute_send_time_ = absolute_send_time;
260 return 0;
261}
262
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000263int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
264 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000265 CriticalSectionScoped cs(send_critsect_);
266 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000267}
268
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000269int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000270 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000271 CriticalSectionScoped cs(send_critsect_);
272 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000273}
274
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000275size_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000276 CriticalSectionScoped cs(send_critsect_);
277 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000278}
279
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000280int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000281 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000282 const int8_t payload_number, const uint32_t frequency,
283 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000284 assert(payload_name);
285 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000286
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000287 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000288 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000289
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000290 if (payload_type_map_.end() != it) {
291 // We already use this payload type.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000292 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000293 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000294
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000295 // Check if it's the same as we already have.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000296 if (RtpUtility::StringCompare(
297 payload->name, payload_name, RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000298 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000299 payload->typeSpecific.Audio.frequency == frequency &&
300 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000301 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000302 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000303 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000304 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000305 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000306 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000307 return 0;
308 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000309 }
310 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000311 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000312 int32_t ret_val = -1;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000313 RtpUtility::Payload* payload = NULL;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000314 if (audio_configured_) {
315 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
316 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000317 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000318 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
319 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000320 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000321 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000322 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000323 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000324 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000325}
326
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000327int32_t RTPSender::DeRegisterSendPayload(
328 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000329 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000330
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000331 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000332 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000333
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000334 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000335 return -1;
336 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000337 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000338 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000339 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000340 return 0;
341}
niklase@google.com470e71d2011-07-07 08:21:25 +0000342
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000343void RTPSender::SetSendPayloadType(int8_t payload_type) {
344 CriticalSectionScoped cs(send_critsect_);
345 payload_type_ = payload_type;
346}
347
sprang@webrtc.orgefcad392014-03-25 16:51:35 +0000348int8_t RTPSender::SendPayloadType() const {
349 CriticalSectionScoped cs(send_critsect_);
350 return payload_type_;
351}
niklase@google.com470e71d2011-07-07 08:21:25 +0000352
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000353int RTPSender::SendPayloadFrequency() const {
354 return audio_ != NULL ? audio_->AudioFrequency() : kVideoPayloadTypeFrequency;
355}
niklase@google.com470e71d2011-07-07 08:21:25 +0000356
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000357int32_t RTPSender::SetMaxPayloadLength(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000358 const size_t max_payload_length,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000359 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000360 // Sanity check.
361 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000362 LOG(LS_ERROR) << "Invalid max payload length: " << max_payload_length;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000363 return -1;
364 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000365 CriticalSectionScoped cs(send_critsect_);
366 max_payload_length_ = max_payload_length;
367 packet_over_head_ = packet_over_head;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000368 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000369}
370
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000371size_t RTPSender::MaxDataPayloadLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000372 int rtx;
373 {
374 CriticalSectionScoped rtx_lock(send_critsect_);
375 rtx = rtx_;
376 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000377 if (audio_configured_) {
378 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000379 } else {
sprang@webrtc.org346094c2014-02-18 08:40:33 +0000380 return max_payload_length_ - RTPHeaderLength() // RTP overhead.
381 - video_->FECPacketOverhead() // FEC/ULP/RED overhead.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000382 - ((rtx) ? 2 : 0); // RTX overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000383 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000384}
385
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000386size_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000387 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000388}
389
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000390uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000391
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000392void RTPSender::SetRTXStatus(int mode) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000393 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000394 rtx_ = mode;
stefan@webrtc.orgef927552014-06-05 08:25:29 +0000395}
396
397void RTPSender::SetRtxSsrc(uint32_t ssrc) {
398 CriticalSectionScoped cs(send_critsect_);
399 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000400}
401
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000402uint32_t RTPSender::RtxSsrc() const {
403 CriticalSectionScoped cs(send_critsect_);
404 return ssrc_rtx_;
405}
406
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000407void RTPSender::RTXStatus(int* mode, uint32_t* ssrc,
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000408 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000409 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000410 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000411 *ssrc = ssrc_rtx_;
412 *payload_type = payload_type_rtx_;
413}
414
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000415void RTPSender::SetRtxPayloadType(int payload_type) {
416 CriticalSectionScoped cs(send_critsect_);
417 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000418}
419
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000420int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
421 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000422 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000423
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000424 if (payload_type < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000425 LOG(LS_ERROR) << "Invalid payload_type " << payload_type;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000426 return -1;
427 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000428 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000429 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000430 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000431 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000432 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000433 // And it's a match...
434 return 0;
435 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000436 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000437 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000438 if (payload_type_ == payload_type) {
439 if (!audio_configured_) {
440 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000441 }
442 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000443 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000444 std::map<int8_t, RtpUtility::Payload*>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000445 payload_type_map_.find(payload_type);
446 if (it == payload_type_map_.end()) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000447 LOG(LS_WARNING) << "Payload type " << payload_type << " not registered.";
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000448 return -1;
449 }
andresp@webrtc.orgc3c29112014-08-27 09:39:43 +0000450 SetSendPayloadType(payload_type);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000451 RtpUtility::Payload* payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000452 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000453 if (!payload->audio && !audio_configured_) {
454 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
455 *video_type = payload->typeSpecific.Video.videoCodecType;
456 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000457 }
458 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000459}
460
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000461int32_t RTPSender::SendOutgoingData(
462 const FrameType frame_type, const int8_t payload_type,
463 const uint32_t capture_timestamp, int64_t capture_time_ms,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000464 const uint8_t *payload_data, const size_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000465 const RTPFragmentationHeader *fragmentation,
466 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000467 uint32_t ssrc;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000468 {
469 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000470 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000471 ssrc = ssrc_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000472 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000473 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000474 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000475 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000476 RtpVideoCodecTypes video_type = kRtpVideoGeneric;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000477 if (CheckPayloadType(payload_type, &video_type) != 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000478 LOG(LS_ERROR) << "Don't send data with unknown payload type.";
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000479 return -1;
480 }
481
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000482 uint32_t ret_val;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000483 if (audio_configured_) {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000484 TRACE_EVENT_ASYNC_STEP1("webrtc", "Audio", capture_timestamp,
485 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000486 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000487 frame_type == kFrameEmpty);
488
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000489 ret_val = audio_->SendAudio(frame_type, payload_type, capture_timestamp,
490 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000491 } else {
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000492 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", capture_time_ms,
493 "Send", "type", FrameTypeToString(frame_type));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000494 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000495
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000496 if (frame_type == kFrameEmpty)
497 return 0;
498
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000499 ret_val = video_->SendVideo(video_type, frame_type, payload_type,
500 capture_timestamp, capture_time_ms,
501 payload_data, payload_size,
502 fragmentation, codec_info,
503 rtp_type_hdr);
504
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000505 }
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000506
507 CriticalSectionScoped cs(statistics_crit_.get());
508 uint32_t frame_count = ++frame_counts_[frame_type];
509 if (frame_count_observer_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000510 frame_count_observer_->FrameCountUpdated(frame_type, frame_count, ssrc);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000511 }
512
513 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000514}
515
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000516size_t RTPSender::TrySendRedundantPayloads(size_t bytes_to_send) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000517 {
518 CriticalSectionScoped cs(send_critsect_);
519 if ((rtx_ & kRtxRedundantPayloads) == 0)
520 return 0;
521 }
522
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000523 uint8_t buffer[IP_PACKET_SIZE];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000524 int bytes_left = static_cast<int>(bytes_to_send);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000525 while (bytes_left > 0) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000526 size_t length = bytes_left;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000527 int64_t capture_time_ms;
528 if (!packet_history_.GetBestFittingPacket(buffer, &length,
529 &capture_time_ms)) {
530 break;
531 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000532 if (!PrepareAndSendPacket(buffer, length, capture_time_ms, true, false))
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000533 break;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000534 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000535 RTPHeader rtp_header;
536 rtp_parser.Parse(rtp_header);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000537 bytes_left -= static_cast<int>(length - rtp_header.headerLength);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000538 }
539 return bytes_to_send - bytes_left;
540}
541
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000542size_t RTPSender::BuildPaddingPacket(uint8_t* packet, size_t header_length) {
543 size_t padding_bytes_in_packet = kMaxPaddingLength;
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000544 packet[0] |= 0x20; // Set padding bit.
545 int32_t *data =
546 reinterpret_cast<int32_t *>(&(packet[header_length]));
547
548 // Fill data buffer with random data.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000549 for (size_t j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000550 data[j] = rand(); // NOLINT
551 }
552 // Set number of padding bytes in the last byte of the packet.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000553 packet[header_length + padding_bytes_in_packet - 1] =
554 static_cast<uint8_t>(padding_bytes_in_packet);
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000555 return padding_bytes_in_packet;
556}
557
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000558size_t RTPSender::TrySendPadData(size_t bytes) {
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000559 int64_t capture_time_ms;
560 uint32_t timestamp;
561 {
562 CriticalSectionScoped cs(send_critsect_);
563 timestamp = timestamp_;
564 capture_time_ms = capture_time_ms_;
565 if (last_timestamp_time_ms_ > 0) {
566 timestamp +=
567 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
568 capture_time_ms +=
569 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
570 }
571 }
572 return SendPadData(timestamp, capture_time_ms, bytes);
573}
574
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000575size_t RTPSender::SendPadData(uint32_t timestamp,
576 int64_t capture_time_ms,
577 size_t bytes) {
578 size_t padding_bytes_in_packet = 0;
579 size_t bytes_sent = 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000580 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000581 // Always send full padding packets.
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000582 if (bytes < kMaxPaddingLength)
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000583 bytes = kMaxPaddingLength;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000584
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000585 uint32_t ssrc;
586 uint16_t sequence_number;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000587 int payload_type;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000588 bool over_rtx;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000589 {
590 CriticalSectionScoped cs(send_critsect_);
591 // Only send padding packets following the last packet of a frame,
592 // indicated by the marker bit.
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000593 if (rtx_ == kRtxOff) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000594 // Without RTX we can't send padding in the middle of frames.
595 if (!last_packet_marker_bit_)
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000596 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000597 ssrc = ssrc_;
598 sequence_number = sequence_number_;
599 ++sequence_number_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000600 payload_type = payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000601 over_rtx = false;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000602 } else {
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000603 // Without abs-send-time a media packet must be sent before padding so
604 // that the timestamps used for estimation are correct.
605 if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
606 kRtpExtensionAbsoluteSendTime))
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000607 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000608 ssrc = ssrc_rtx_;
609 sequence_number = sequence_number_rtx_;
610 ++sequence_number_rtx_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000611 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_
612 : payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000613 over_rtx = true;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000614 }
615 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000616
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000617 uint8_t padding_packet[IP_PACKET_SIZE];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000618 size_t header_length = CreateRTPHeader(padding_packet,
619 payload_type,
620 ssrc,
621 false,
622 timestamp,
623 sequence_number,
624 NULL,
625 0);
626 assert(header_length != static_cast<size_t>(-1));
627 padding_bytes_in_packet = BuildPaddingPacket(padding_packet, header_length);
628 assert(padding_bytes_in_packet <= bytes);
629 size_t length = padding_bytes_in_packet + header_length;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000630 int64_t now_ms = clock_->TimeInMilliseconds();
631
632 RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
633 RTPHeader rtp_header;
634 rtp_parser.Parse(rtp_header);
635
636 if (capture_time_ms > 0) {
637 UpdateTransmissionTimeOffset(
638 padding_packet, length, rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000639 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000640
641 UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
642 if (!SendPacketToNetwork(padding_packet, length))
643 break;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000644 bytes_sent += padding_bytes_in_packet;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000645 UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000646 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000647
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000648 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000649}
650
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000651void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000652 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000653 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000654}
655
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000656bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000657 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000658}
niklase@google.com470e71d2011-07-07 08:21:25 +0000659
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000660int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000661 size_t length = IP_PACKET_SIZE;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000662 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000663 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000664 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
665 data_buffer, &length,
666 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000667 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000668 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000669 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000670
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000671 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000672 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000673 RTPHeader header;
674 if (!rtp_parser.Parse(header)) {
675 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000676 return -1;
677 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000678 // Convert from TickTime to Clock since capture_time_ms is based on
679 // TickTime.
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000680 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_;
681 if (!paced_sender_->SendPacket(
682 PacedSender::kHighPriority, header.ssrc, header.sequenceNumber,
683 corrected_capture_tims_ms, length - header.headerLength, true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000684 // We can't send the packet right now.
685 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000686 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000687 }
688 }
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000689 int rtx = kRtxOff;
690 {
691 CriticalSectionScoped lock(send_critsect_);
692 rtx = rtx_;
693 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000694 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000695 (rtx & kRtxRetransmitted) > 0, true) ?
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000696 static_cast<int32_t>(length) : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000697}
698
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000699bool RTPSender::SendPacketToNetwork(const uint8_t *packet, size_t size) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000700 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000701 if (transport_) {
702 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000703 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000704 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
705 "size", size, "sent", bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000706 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000707 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000708 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000709 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000710 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000711 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000712}
713
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000714int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000715 if (!video_)
716 return -1;
717 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000718}
719
720int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000721 if (!video_)
722 return -1;
723 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000724}
725
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000726void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000727 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000728 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000729 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
730 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000731 const int64_t now = clock_->TimeInMilliseconds();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000732 size_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000733 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000734
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000735 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000736 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000737 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000738 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000739 return;
740 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000741
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000742 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
743 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000744 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000745 if (bytes_sent > 0) {
746 bytes_re_sent += bytes_sent;
747 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000748 // The packet has previously been resent.
749 // Try resending next packet in the list.
750 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000751 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000752 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000753 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
754 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000755 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000756 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000757 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000758 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000759 // kbits/s * ms = bits => bits/8 = bytes
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000760 size_t target_bytes =
761 (static_cast<size_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000762 if (bytes_re_sent > target_bytes) {
763 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000764 }
765 }
766 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000767 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000768 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000769 UpdateNACKBitRate(bytes_re_sent, now);
770 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000771 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000772}
773
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000774bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
775 uint32_t num = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000776 size_t byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000777 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000778 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000779
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000780 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000781
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000782 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000783 return true;
784 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000785 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000786 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000787 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000788 break;
789 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000790 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000791 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000792 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000793 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000794 if (num == NACK_BYTECOUNT_SIZE) {
795 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000796 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000797 if (nack_byte_count_times_[num - 1] <= now) {
798 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000799 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000800 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000801 return (byte_count * 8) < (target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000802}
803
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000804void RTPSender::UpdateNACKBitRate(const size_t bytes,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000805 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000806 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000807
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000808 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000809 if (bytes > 0) {
810 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000811 // Add padding length.
812 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000813 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000814 if (nack_byte_count_times_[0] == 0) {
815 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000816 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000817 // Shift.
818 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
819 nack_byte_count_[i + 1] = nack_byte_count_[i];
820 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000821 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000822 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000823 nack_byte_count_[0] = bytes;
824 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000825 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000826 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000827}
828
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000829// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000830bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000831 int64_t capture_time_ms,
832 bool retransmission) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000833 size_t length = IP_PACKET_SIZE;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000834 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000835 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000836
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000837 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
838 0,
839 retransmission,
840 data_buffer,
841 &length,
842 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000843 // Packet cannot be found. Allow sending to continue.
844 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000845 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000846 if (!retransmission && capture_time_ms > 0) {
847 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
848 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000849 int rtx;
850 {
851 CriticalSectionScoped lock(send_critsect_);
852 rtx = rtx_;
853 }
854 return PrepareAndSendPacket(data_buffer,
855 length,
856 capture_time_ms,
857 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000858 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000859}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000860
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000861bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000862 size_t length,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000863 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000864 bool send_over_rtx,
865 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000866 uint8_t *buffer_to_send_ptr = buffer;
867
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000868 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000869 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000870 rtp_parser.Parse(rtp_header);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000871 if (!is_retransmit && rtp_header.markerBit) {
872 TRACE_EVENT_ASYNC_END0("webrtc_rtp", "PacedSend", capture_time_ms);
873 }
874
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000875 TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000876 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000877 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000878
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000879 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000880 if (send_over_rtx) {
881 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000882 buffer_to_send_ptr = data_buffer_rtx;
883 }
884
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000885 int64_t now_ms = clock_->TimeInMilliseconds();
886 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000887 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
888 diff_ms);
889 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000890 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000891 if (ret) {
892 CriticalSectionScoped lock(send_critsect_);
893 media_has_been_sent_ = true;
894 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000895 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
896 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000897 return ret;
898}
899
900void RTPSender::UpdateRtpStats(const uint8_t* buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000901 size_t size,
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000902 const RTPHeader& header,
903 bool is_rtx,
904 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000905 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000906 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000907 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000908
909 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000910 if (is_rtx) {
911 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000912 } else {
913 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000914 }
915
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000916 total_bitrate_sent_.Update(size);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000917 ++counters->packets;
918 if (IsFecPacket(buffer, header)) {
919 ++counters->fec_packets;
920 }
921
922 if (is_retransmit) {
923 ++counters->retransmitted_packets;
924 } else {
925 counters->bytes += size - (header.headerLength + header.paddingLength);
926 counters->header_bytes += header.headerLength;
927 counters->padding_bytes += header.paddingLength;
928 }
929
930 if (rtp_stats_callback_) {
931 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
932 }
933}
934
935bool RTPSender::IsFecPacket(const uint8_t* buffer,
936 const RTPHeader& header) const {
937 if (!video_) {
938 return false;
939 }
940 bool fec_enabled;
941 uint8_t pt_red;
942 uint8_t pt_fec;
943 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
944 return fec_enabled &&
945 header.payloadType == pt_red &&
946 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000947}
948
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000949size_t RTPSender::TimeToSendPadding(size_t bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000950 {
951 CriticalSectionScoped cs(send_critsect_);
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000952 if (!sending_media_) return 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000953 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000954 if (bytes == 0)
955 return 0;
956 size_t bytes_sent = TrySendRedundantPayloads(bytes);
957 if (bytes_sent < bytes)
958 bytes_sent += TrySendPadData(bytes - bytes_sent);
959 return bytes_sent;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000960}
961
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000962// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000963int32_t RTPSender::SendToNetwork(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000964 uint8_t *buffer, size_t payload_length, size_t rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000965 int64_t capture_time_ms, StorageType storage,
966 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000967 RtpUtility::RtpHeaderParser rtp_parser(buffer,
968 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000969 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000970 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000971
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000972 int64_t now_ms = clock_->TimeInMilliseconds();
973
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000974 // |capture_time_ms| <= 0 is considered invalid.
975 // TODO(holmer): This should be changed all over Video Engine so that negative
976 // time is consider invalid, while 0 is considered a valid time.
977 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000978 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000979 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000980 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000981
982 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
983 rtp_header, now_ms);
984
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000985 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000986 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
987 max_payload_length_, capture_time_ms,
988 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000989 return -1;
990 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000991
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000992 if (paced_sender_ && storage != kDontStore) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000993 // Correct offset between implementations of millisecond time stamps in
994 // TickTime and Clock.
995 int64_t corrected_time_ms = capture_time_ms + clock_delta_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000996 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000997 rtp_header.sequenceNumber, corrected_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000998 payload_length, false)) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000999 if (last_capture_time_ms_sent_ == 0 ||
1000 corrected_time_ms > last_capture_time_ms_sent_) {
1001 last_capture_time_ms_sent_ = corrected_time_ms;
1002 TRACE_EVENT_ASYNC_BEGIN1("webrtc_rtp", "PacedSend", corrected_time_ms,
1003 "capture_time_ms", corrected_time_ms);
1004 }
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +00001005 // We can't send the packet right now.
1006 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +00001007 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001008 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +00001009 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001010 if (capture_time_ms > 0) {
1011 UpdateDelayStatistics(capture_time_ms, now_ms);
1012 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001013 size_t length = payload_length + rtp_header_length;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001014 if (!SendPacketToNetwork(buffer, length))
1015 return -1;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001016 {
1017 CriticalSectionScoped lock(send_critsect_);
1018 media_has_been_sent_ = true;
1019 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001020 UpdateRtpStats(buffer, length, rtp_header, false, false);
1021 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +00001022}
1023
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001024void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001025 uint32_t ssrc;
1026 int avg_delay_ms = 0;
1027 int max_delay_ms = 0;
1028 {
1029 CriticalSectionScoped lock(send_critsect_);
1030 ssrc = ssrc_;
1031 }
1032 {
1033 CriticalSectionScoped cs(statistics_crit_.get());
1034 // TODO(holmer): Compute this iteratively instead.
1035 send_delays_[now_ms] = now_ms - capture_time_ms;
1036 send_delays_.erase(send_delays_.begin(),
1037 send_delays_.lower_bound(now_ms -
1038 kSendSideDelayWindowMs));
1039 }
1040 if (send_side_delay_observer_ &&
1041 GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
1042 send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
1043 max_delay_ms, ssrc);
1044 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001045}
1046
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001047void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001048 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001049 total_bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001050 nack_bitrate_.Process();
1051 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001052 return;
1053 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001054 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +00001055}
1056
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001057size_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001058 CriticalSectionScoped lock(send_critsect_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001059 size_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001060 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001061 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001062 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001063 rtp_header_length += RtpHeaderExtensionTotalLength();
1064 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001065}
1066
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001067uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001068 CriticalSectionScoped cs(send_critsect_);
1069 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001070}
1071
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001072void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001073 uint32_t ssrc;
1074 uint32_t ssrc_rtx;
1075 {
1076 CriticalSectionScoped ssrc_lock(send_critsect_);
1077 ssrc = ssrc_;
1078 ssrc_rtx = ssrc_rtx_;
1079 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001080 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001081 rtp_stats_ = StreamDataCounters();
1082 rtx_rtp_stats_ = StreamDataCounters();
1083 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001084 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
1085 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001086 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001087}
1088
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001089void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1090 StreamDataCounters* rtx_stats) const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001091 CriticalSectionScoped lock(statistics_crit_.get());
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001092 *rtp_stats = rtp_stats_;
1093 *rtx_stats = rtx_rtp_stats_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001094}
1095
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001096int RTPSender::CreateRTPHeader(
1097 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
1098 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
1099 uint8_t num_csrcs) const {
1100 header[0] = 0x80; // version 2.
1101 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001102 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001103 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001104 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001105 RtpUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1106 RtpUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1107 RtpUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001108 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001109
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001110 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001111 if (num_csrcs > 0) {
1112 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001113 // error
1114 assert(false);
1115 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001116 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001117 uint8_t *ptr = &header[rtp_header_length];
1118 for (int i = 0; i < num_csrcs; ++i) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001119 RtpUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001120 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001121 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001122 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001123
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001124 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001125 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001126 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001127
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001128 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1129 if (len > 0) {
1130 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001131 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001132 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001133 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001134}
1135
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001136int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
1137 const int8_t payload_type,
1138 const bool marker_bit,
1139 const uint32_t capture_timestamp,
1140 int64_t capture_time_ms,
1141 const bool timestamp_provided,
1142 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001143 assert(payload_type >= 0);
1144 CriticalSectionScoped cs(send_critsect_);
1145
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001146 if (timestamp_provided) {
1147 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001148 } else {
1149 // Make a unique time stamp.
1150 // We can't inc by the actual time, since then we increase the risk of back
1151 // timing.
1152 timestamp_++;
1153 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001154 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001155 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001156 capture_time_ms_ = capture_time_ms;
1157 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001158 int csrcs_length = 0;
1159 if (include_csrcs_)
1160 csrcs_length = num_csrcs_;
1161 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1162 timestamp_, sequence_number, csrcs_, csrcs_length);
1163}
1164
1165uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001166 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001167 return 0;
1168 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001169 // RTP header extension, RFC 3550.
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 // | defined by profile | length |
1174 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1175 // | header extension |
1176 // | .... |
1177 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001178 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001179 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001180
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001181 // Add extension ID (0xBEDE).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001182 RtpUtility::AssignUWord16ToBuffer(data_buffer, kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001183
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001184 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001185 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001186
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001187 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001188 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001189 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001190 switch (type) {
1191 case kRtpExtensionTransmissionTimeOffset:
1192 block_length = BuildTransmissionTimeOffsetExtension(
1193 data_buffer + kHeaderLength + total_block_length);
1194 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001195 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001196 block_length = BuildAudioLevelExtension(
1197 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001198 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001199 case kRtpExtensionAbsoluteSendTime:
1200 block_length = BuildAbsoluteSendTimeExtension(
1201 data_buffer + kHeaderLength + total_block_length);
1202 break;
1203 default:
1204 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001205 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001206 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001207 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001208 }
1209 if (total_block_length == 0) {
1210 // No extension added.
1211 return 0;
1212 }
1213 // Set header length (in number of Word32, header excluded).
1214 assert(total_block_length % 4 == 0);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001215 RtpUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
1216 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001217 // Total added length.
1218 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001219}
1220
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001221uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1222 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001223 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1224 //
1225 // The transmission time is signaled to the receiver in-band using the
1226 // general mechanism for RTP header extensions [RFC5285]. The payload
1227 // of this extension (the transmitted value) is a 24-bit signed integer.
1228 // When added to the RTP timestamp of the packet, it represents the
1229 // "effective" RTP transmission time of the packet, on the RTP
1230 // timescale.
1231 //
1232 // The form of the transmission offset extension block:
1233 //
1234 // 0 1 2 3
1235 // 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
1236 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1237 // | ID | len=2 | transmission offset |
1238 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001239
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001240 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001241 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001242 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1243 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001244 // Not registered.
1245 return 0;
1246 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001247 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001248 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001249 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001250 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos,
1251 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001252 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001253 assert(pos == kTransmissionTimeOffsetLength);
1254 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001255}
1256
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001257uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1258 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1259 //
1260 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1261 //
1262 // The form of the audio level extension block:
1263 //
1264 // 0 1 2 3
1265 // 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
1266 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1267 // | ID | len=0 |V| level | 0x00 | 0x00 |
1268 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1269 //
1270 // Note that we always include 2 pad bytes, which will result in legal and
1271 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1272 // are implemented. Right now the pad bytes would anyway be required at end
1273 // of the extension block, so it makes no difference.
1274
1275 // Get id defined by user.
1276 uint8_t id;
1277 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1278 // Not registered.
1279 return 0;
1280 }
1281 size_t pos = 0;
1282 const uint8_t len = 0;
1283 data_buffer[pos++] = (id << 4) + len;
1284 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1285 data_buffer[pos++] = 0; // Padding.
1286 data_buffer[pos++] = 0; // Padding.
1287 // kAudioLevelLength is including pad bytes.
1288 assert(pos == kAudioLevelLength);
1289 return kAudioLevelLength;
1290}
1291
1292uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001293 // Absolute send time in RTP streams.
1294 //
1295 // The absolute send time is signaled to the receiver in-band using the
1296 // general mechanism for RTP header extensions [RFC5285]. The payload
1297 // of this extension (the transmitted value) is a 24-bit unsigned integer
1298 // containing the sender's current time in seconds as a fixed point number
1299 // with 18 bits fractional part.
1300 //
1301 // The form of the absolute send time extension block:
1302 //
1303 // 0 1 2 3
1304 // 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
1305 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1306 // | ID | len=2 | absolute send time |
1307 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1308
1309 // Get id defined by user.
1310 uint8_t id;
1311 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1312 &id) != 0) {
1313 // Not registered.
1314 return 0;
1315 }
1316 size_t pos = 0;
1317 const uint8_t len = 2;
1318 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001319 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos, absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001320 pos += 3;
1321 assert(pos == kAbsoluteSendTimeLength);
1322 return kAbsoluteSendTimeLength;
1323}
1324
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001325void RTPSender::UpdateTransmissionTimeOffset(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001326 uint8_t *rtp_packet, const size_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001327 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001328 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001329 // Get id.
1330 uint8_t id = 0;
1331 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1332 &id) != 0) {
1333 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001334 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001335 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001336 // Get length until start of header extension block.
1337 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001338 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001339 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001340 if (extension_block_pos < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001341 LOG(LS_WARNING)
1342 << "Failed to update transmission time offset, not registered.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001343 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001344 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001345 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001346 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001347 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001348 block_pos + kTransmissionTimeOffsetLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001349 LOG(LS_WARNING)
1350 << "Failed to update transmission time offset, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001351 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001352 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001353 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001354 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1355 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001356 LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1357 "extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001358 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001359 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001360 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001361 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001362 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001363 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001364 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001365 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001366 // Update transmission offset field (converting to a 90 kHz timestamp).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001367 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1368 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001369}
1370
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001371bool RTPSender::UpdateAudioLevel(uint8_t *rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001372 const size_t rtp_packet_length,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001373 const RTPHeader &rtp_header,
1374 const bool is_voiced,
1375 const uint8_t dBov) const {
1376 CriticalSectionScoped cs(send_critsect_);
1377
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001378 // Get id.
1379 uint8_t id = 0;
1380 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1381 // Not registered.
1382 return false;
1383 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001384 // Get length until start of header extension block.
1385 int extension_block_pos =
1386 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1387 kRtpExtensionAudioLevel);
1388 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001389 // The feature is not enabled.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001390 return false;
1391 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001392 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001393 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1394 rtp_header.headerLength < block_pos + kAudioLevelLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001395 LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001396 return false;
1397 }
1398 // Verify that header contains extension.
1399 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1400 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001401 LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001402 return false;
1403 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001404 // Verify first byte in block.
1405 const uint8_t first_block_byte = (id << 4) + 0;
1406 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001407 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001408 return false;
1409 }
1410 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1411 return true;
1412}
1413
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001414void RTPSender::UpdateAbsoluteSendTime(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001415 uint8_t *rtp_packet, const size_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001416 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001417 CriticalSectionScoped cs(send_critsect_);
1418
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001419 // Get id.
1420 uint8_t id = 0;
1421 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1422 &id) != 0) {
1423 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001424 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001425 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001426 // Get length until start of header extension block.
1427 int extension_block_pos =
1428 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1429 kRtpExtensionAbsoluteSendTime);
1430 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001431 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001432 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001433 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001434 size_t block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001435 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001436 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001437 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001438 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001439 }
1440 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001441 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1442 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001443 LOG(LS_WARNING)
1444 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001445 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001446 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001447 // Verify first byte in block.
1448 const uint8_t first_block_byte = (id << 4) + 2;
1449 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001450 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001451 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001452 }
1453 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1454 // fractional part).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001455 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1456 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001457}
1458
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001459void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001460 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001461 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001462 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001463
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001464 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001465 SetStartTimestamp(RTPtime, false);
1466 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001467 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001468 if (!ssrc_forced_) {
1469 // Generate a new SSRC.
1470 ssrc_db_.ReturnSSRC(ssrc_);
1471 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001472 bitrates_->set_ssrc(ssrc_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001473 }
1474 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001475 if (!sequence_number_forced_ && !ssrc_forced_) {
1476 // Generate a new sequence number.
1477 sequence_number_ =
1478 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001479 }
1480 }
1481}
1482
1483void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001484 CriticalSectionScoped cs(send_critsect_);
1485 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001486}
1487
1488bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001489 CriticalSectionScoped cs(send_critsect_);
1490 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001491}
1492
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001493uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001494 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001495 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001496}
1497
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001498void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001499 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001500 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001501 start_timestamp_forced_ = true;
1502 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001503 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001504 if (!start_timestamp_forced_) {
1505 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001506 }
1507 }
1508}
1509
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001510uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001511 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001512 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001513}
1514
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001515uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001516 // If configured via API, return 0.
1517 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001518
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001519 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001520 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001521 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001522 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001523 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001524 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001525}
1526
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001527void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001528 // This is configured via the API.
1529 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001530
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001531 if (ssrc_ == ssrc && ssrc_forced_) {
1532 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001533 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001534 ssrc_forced_ = true;
1535 ssrc_db_.ReturnSSRC(ssrc_);
1536 ssrc_db_.RegisterSSRC(ssrc);
1537 ssrc_ = ssrc;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001538 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001539 if (!sequence_number_forced_) {
1540 sequence_number_ =
1541 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001542 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001543}
1544
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001545uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001546 CriticalSectionScoped cs(send_critsect_);
1547 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001548}
1549
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001550void RTPSender::SetCSRCStatus(const bool include) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001551 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001552 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001553}
1554
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001555void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1556 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001557 assert(arr_length <= kRtpCsrcSize);
1558 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001559
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001560 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001561 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001562 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001563 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001564}
1565
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001566int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001567 assert(arr_of_csrc);
1568 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001569 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1570 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001571 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001572 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001573}
1574
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001575void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001576 CriticalSectionScoped cs(send_critsect_);
1577 sequence_number_forced_ = true;
1578 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001579}
1580
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001581uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001582 CriticalSectionScoped cs(send_critsect_);
1583 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001584}
1585
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001586// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001587int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1588 const uint16_t time_ms,
1589 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001590 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001591 return -1;
1592 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001593 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001594}
1595
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001596bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001597 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001598 return false;
1599 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001600 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001601}
1602
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001603int32_t RTPSender::SetAudioPacketSize(
1604 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001605 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001606 return -1;
1607 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001608 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001609}
1610
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001611int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001612 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001613}
1614
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001615int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001616 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001617 return -1;
1618 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001619 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001620}
1621
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001622int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001623 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001624 return -1;
1625 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001626 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001627}
1628
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001629// Video
1630VideoCodecInformation *RTPSender::CodecInformationVideo() {
1631 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001632 return NULL;
1633 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001634 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001635}
1636
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001637RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001638 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001639 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001640}
1641
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001642uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001643 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001644 return 0;
1645 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001646 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001647}
1648
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001649int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001650 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001651 return -1;
1652 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001653 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001654}
1655
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001656int32_t RTPSender::SetGenericFECStatus(
1657 const bool enable, const uint8_t payload_type_red,
1658 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001659 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001660 return -1;
1661 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001662 return video_->SetGenericFECStatus(enable, payload_type_red,
1663 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001664}
1665
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001666int32_t RTPSender::GenericFECStatus(
1667 bool *enable, uint8_t *payload_type_red,
1668 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001669 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001670 return -1;
1671 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001672 return video_->GenericFECStatus(
1673 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001674}
1675
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001676int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001677 const FecProtectionParams *delta_params,
1678 const FecProtectionParams *key_params) {
1679 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001680 return -1;
1681 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001682 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001683}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001684
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001685void RTPSender::BuildRtxPacket(uint8_t* buffer, size_t* length,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001686 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001687 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001688 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001689 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001690 RtpUtility::RtpHeaderParser rtp_parser(
1691 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001692
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001693 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001694 rtp_parser.Parse(rtp_header);
1695
1696 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001697 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001698
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001699 // Replace payload type, if a specific type is set for RTX.
1700 if (payload_type_rtx_ != -1) {
1701 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001702 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001703 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1704 }
1705
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001706 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001707 uint8_t *ptr = data_buffer_rtx + 2;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001708 RtpUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001709
1710 // Replace SSRC.
1711 ptr += 6;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001712 RtpUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001713
1714 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001715 ptr = data_buffer_rtx + rtp_header.headerLength;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001716 RtpUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001717 ptr += 2;
1718
1719 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001720 memcpy(ptr, buffer + rtp_header.headerLength,
1721 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001722 *length += 2;
1723}
1724
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001725void RTPSender::RegisterRtpStatisticsCallback(
1726 StreamDataCountersCallback* callback) {
1727 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001728 rtp_stats_callback_ = callback;
1729}
1730
1731StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1732 CriticalSectionScoped cs(statistics_crit_.get());
1733 return rtp_stats_callback_;
1734}
1735
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001736uint32_t RTPSender::BitrateSent() const {
1737 return total_bitrate_sent_.BitrateLast();
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001738}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001739
1740void RTPSender::SetRtpState(const RtpState& rtp_state) {
1741 SetStartTimestamp(rtp_state.start_timestamp, true);
1742 CriticalSectionScoped lock(send_critsect_);
1743 sequence_number_ = rtp_state.sequence_number;
1744 sequence_number_forced_ = true;
1745 timestamp_ = rtp_state.timestamp;
1746 capture_time_ms_ = rtp_state.capture_time_ms;
1747 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001748 media_has_been_sent_ = rtp_state.media_has_been_sent;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001749}
1750
1751RtpState RTPSender::GetRtpState() const {
1752 CriticalSectionScoped lock(send_critsect_);
1753
1754 RtpState state;
1755 state.sequence_number = sequence_number_;
1756 state.start_timestamp = start_timestamp_;
1757 state.timestamp = timestamp_;
1758 state.capture_time_ms = capture_time_ms_;
1759 state.last_timestamp_time_ms = last_timestamp_time_ms_;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001760 state.media_has_been_sent = media_has_been_sent_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001761
1762 return state;
1763}
1764
1765void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
1766 CriticalSectionScoped lock(send_critsect_);
1767 sequence_number_rtx_ = rtp_state.sequence_number;
1768}
1769
1770RtpState RTPSender::GetRtxRtpState() const {
1771 CriticalSectionScoped lock(send_critsect_);
1772
1773 RtpState state;
1774 state.sequence_number = sequence_number_rtx_;
1775 state.start_timestamp = start_timestamp_;
1776
1777 return state;
1778}
1779
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001780} // namespace webrtc