blob: 677f3fc4986bc35910dd5b81a87f69be47c1f3d0 [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.
25const int 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000275uint16_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(
358 const uint16_t max_payload_length,
359 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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000371uint16_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
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000386uint16_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,
464 const uint8_t *payload_data, const uint32_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
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000516int RTPSender::TrySendRedundantPayloads(int bytes_to_send) {
517 {
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];
524 int bytes_left = bytes_to_send;
525 while (bytes_left > 0) {
526 uint16_t length = bytes_left;
527 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))
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000533 return -1;
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);
537 bytes_left -= length - rtp_header.headerLength;
538 }
539 return bytes_to_send - bytes_left;
540}
541
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000542int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length,
543 int32_t bytes) {
544 int padding_bytes_in_packet = kMaxPaddingLength;
545 if (bytes < kMaxPaddingLength) {
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000546 padding_bytes_in_packet = bytes;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000547 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +0000548 packet[0] |= 0x20; // Set padding bit.
549 int32_t *data =
550 reinterpret_cast<int32_t *>(&(packet[header_length]));
551
552 // Fill data buffer with random data.
553 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
554 data[j] = rand(); // NOLINT
555 }
556 // Set number of padding bytes in the last byte of the packet.
557 packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet;
558 return padding_bytes_in_packet;
559}
560
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000561int RTPSender::TrySendPadData(int bytes) {
562 int64_t capture_time_ms;
563 uint32_t timestamp;
564 {
565 CriticalSectionScoped cs(send_critsect_);
566 timestamp = timestamp_;
567 capture_time_ms = capture_time_ms_;
568 if (last_timestamp_time_ms_ > 0) {
569 timestamp +=
570 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_) * 90;
571 capture_time_ms +=
572 (clock_->TimeInMilliseconds() - last_timestamp_time_ms_);
573 }
574 }
575 return SendPadData(timestamp, capture_time_ms, bytes);
576}
577
578int RTPSender::SendPadData(uint32_t timestamp,
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000579 int64_t capture_time_ms,
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000580 int32_t bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000581 int padding_bytes_in_packet = 0;
582 int bytes_sent = 0;
583 for (; bytes > 0; bytes -= padding_bytes_in_packet) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000584 // Always send full padding packets.
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000585 if (bytes < kMaxPaddingLength)
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000586 bytes = kMaxPaddingLength;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000587
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000588 uint32_t ssrc;
589 uint16_t sequence_number;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000590 int payload_type;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000591 bool over_rtx;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000592 {
593 CriticalSectionScoped cs(send_critsect_);
594 // Only send padding packets following the last packet of a frame,
595 // indicated by the marker bit.
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000596 if (rtx_ == kRtxOff) {
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000597 // Without RTX we can't send padding in the middle of frames.
598 if (!last_packet_marker_bit_)
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000599 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000600 ssrc = ssrc_;
601 sequence_number = sequence_number_;
602 ++sequence_number_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000603 payload_type = payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000604 over_rtx = false;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000605 } else {
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000606 // Without abs-send-time a media packet must be sent before padding so
607 // that the timestamps used for estimation are correct.
608 if (!media_has_been_sent_ && !rtp_header_extension_map_.IsRegistered(
609 kRtpExtensionAbsoluteSendTime))
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000610 return 0;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000611 ssrc = ssrc_rtx_;
612 sequence_number = sequence_number_rtx_;
613 ++sequence_number_rtx_;
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000614 payload_type = ((rtx_ & kRtxRedundantPayloads) > 0) ? payload_type_rtx_
615 : payload_type_;
pbos@webrtc.org63c60ed2014-07-16 09:37:29 +0000616 over_rtx = true;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000617 }
618 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000619
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000620 uint8_t padding_packet[IP_PACKET_SIZE];
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000621 int header_length = CreateRTPHeader(padding_packet,
622 payload_type,
623 ssrc,
624 false,
625 timestamp,
626 sequence_number,
627 NULL,
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +0000628 0);
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000629 padding_bytes_in_packet =
630 BuildPaddingPacket(padding_packet, header_length, bytes);
631 int length = padding_bytes_in_packet + header_length;
632 int64_t now_ms = clock_->TimeInMilliseconds();
633
634 RtpUtility::RtpHeaderParser rtp_parser(padding_packet, length);
635 RTPHeader rtp_header;
636 rtp_parser.Parse(rtp_header);
637
638 if (capture_time_ms > 0) {
639 UpdateTransmissionTimeOffset(
640 padding_packet, length, rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000641 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000642
643 UpdateAbsoluteSendTime(padding_packet, length, rtp_header, now_ms);
644 if (!SendPacketToNetwork(padding_packet, length))
645 break;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000646 bytes_sent += padding_bytes_in_packet;
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000647 UpdateRtpStats(padding_packet, length, rtp_header, over_rtx, false);
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000648 }
pbos@webrtc.org72491b92014-07-10 16:24:54 +0000649
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000650 return bytes_sent;
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000651}
652
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000653void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000654 const uint16_t number_to_store) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000655 packet_history_.SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000656}
657
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000658bool RTPSender::StorePackets() const {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000659 return packet_history_.StorePackets();
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000660}
niklase@google.com470e71d2011-07-07 08:21:25 +0000661
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000662int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
663 uint16_t length = IP_PACKET_SIZE;
664 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000665 int64_t capture_time_ms;
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000666 if (!packet_history_.GetPacketAndSetSendTime(packet_id, min_resend_time, true,
667 data_buffer, &length,
668 &capture_time_ms)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000669 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000670 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000671 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000672
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000673 if (paced_sender_) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000674 RtpUtility::RtpHeaderParser rtp_parser(data_buffer, length);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000675 RTPHeader header;
676 if (!rtp_parser.Parse(header)) {
677 assert(false);
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000678 return -1;
679 }
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +0000680 // Convert from TickTime to Clock since capture_time_ms is based on
681 // TickTime.
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000682 int64_t corrected_capture_tims_ms = capture_time_ms + clock_delta_ms_;
683 if (!paced_sender_->SendPacket(
684 PacedSender::kHighPriority, header.ssrc, header.sequenceNumber,
685 corrected_capture_tims_ms, length - header.headerLength, true)) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000686 // We can't send the packet right now.
687 // We will be called when it is time.
stefan@webrtc.org5c58f632013-05-23 13:36:55 +0000688 return length;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000689 }
690 }
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000691 int rtx = kRtxOff;
692 {
693 CriticalSectionScoped lock(send_critsect_);
694 rtx = rtx_;
695 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000696 return PrepareAndSendPacket(data_buffer, length, capture_time_ms,
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000697 (rtx & kRtxRetransmitted) > 0, true) ?
stefan@webrtc.org16395222014-03-19 19:34:07 +0000698 length : -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000699}
700
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000701bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
702 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000703 if (transport_) {
704 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000705 }
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000706 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork",
707 "size", size, "sent", bytes_sent);
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000708 // TODO(pwestin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000709 if (bytes_sent <= 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000710 LOG(LS_WARNING) << "Transport failed to send packet";
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000711 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000712 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000713 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000714}
715
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000716int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000717 if (!video_)
718 return -1;
719 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000720}
721
722int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000723 if (!video_)
724 return -1;
725 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000726}
727
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000728void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000729 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000730 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000731 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
732 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000733 const int64_t now = clock_->TimeInMilliseconds();
734 uint32_t bytes_re_sent = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000735 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000736
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000737 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000738 if (!ProcessNACKBitRate(now)) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000739 LOG(LS_INFO) << "NACK bitrate reached. Skip sending NACK response. Target "
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000740 << target_bitrate;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000741 return;
742 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000743
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000744 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
745 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000746 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000747 if (bytes_sent > 0) {
748 bytes_re_sent += bytes_sent;
749 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000750 // The packet has previously been resent.
751 // Try resending next packet in the list.
752 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000753 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000754 // Failed to send one Sequence number. Give up the rest in this nack.
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000755 LOG(LS_WARNING) << "Failed resending RTP packet " << *it
756 << ", Discard rest of packets";
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000757 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000758 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000759 // Delay bandwidth estimate (RTT * BW).
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000760 if (target_bitrate != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000761 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000762 uint32_t target_bytes =
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000763 (static_cast<uint32_t>(target_bitrate / 1000) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000764 if (bytes_re_sent > target_bytes) {
765 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000766 }
767 }
768 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000769 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000770 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000771 UpdateNACKBitRate(bytes_re_sent, now);
772 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000773 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000774}
775
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000776bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
777 uint32_t num = 0;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000778 int byte_count = 0;
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000779 const uint32_t kAvgIntervalMs = 1000;
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000780 uint32_t target_bitrate = GetTargetBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000781
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000782 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000783
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000784 if (target_bitrate == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000785 return true;
786 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000787 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000788 if ((now - nack_byte_count_times_[num]) > kAvgIntervalMs) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000789 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000790 break;
791 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000792 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000793 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000794 }
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000795 uint32_t time_interval = kAvgIntervalMs;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000796 if (num == NACK_BYTECOUNT_SIZE) {
797 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000798 // during the last msg_interval.
henrike@webrtc.orgfe526ff2014-06-25 20:59:51 +0000799 if (nack_byte_count_times_[num - 1] <= now) {
800 time_interval = now - nack_byte_count_times_[num - 1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000801 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000802 }
stefan@webrtc.orga15fbfd2014-06-17 17:32:05 +0000803 return (byte_count * 8) <
804 static_cast<int>(target_bitrate / 1000 * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000805}
806
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000807void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
808 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000809 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000810
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000811 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000812 if (bytes > 0) {
813 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000814 // Add padding length.
815 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000816 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000817 if (nack_byte_count_times_[0] == 0) {
818 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000819 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000820 // Shift.
821 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
822 nack_byte_count_[i + 1] = nack_byte_count_[i];
823 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000824 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000825 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000826 nack_byte_count_[0] = bytes;
827 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000828 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000829 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000830}
831
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000832// Called from pacer when we can send the packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000833bool RTPSender::TimeToSendPacket(uint16_t sequence_number,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000834 int64_t capture_time_ms,
835 bool retransmission) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000836 uint16_t length = IP_PACKET_SIZE;
837 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000838 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000839
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000840 if (!packet_history_.GetPacketAndSetSendTime(sequence_number,
841 0,
842 retransmission,
843 data_buffer,
844 &length,
845 &stored_time_ms)) {
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000846 // Packet cannot be found. Allow sending to continue.
847 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000848 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +0000849 if (!retransmission && capture_time_ms > 0) {
850 UpdateDelayStatistics(capture_time_ms, clock_->TimeInMilliseconds());
851 }
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000852 int rtx;
853 {
854 CriticalSectionScoped lock(send_critsect_);
855 rtx = rtx_;
856 }
857 return PrepareAndSendPacket(data_buffer,
858 length,
859 capture_time_ms,
860 retransmission && (rtx & kRtxRetransmitted) > 0,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000861 retransmission);
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000862}
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000863
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000864bool RTPSender::PrepareAndSendPacket(uint8_t* buffer,
865 uint16_t length,
866 int64_t capture_time_ms,
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000867 bool send_over_rtx,
868 bool is_retransmit) {
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000869 uint8_t *buffer_to_send_ptr = buffer;
870
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000871 RtpUtility::RtpHeaderParser rtp_parser(buffer, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000872 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000873 rtp_parser.Parse(rtp_header);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000874 if (!is_retransmit && rtp_header.markerBit) {
875 TRACE_EVENT_ASYNC_END0("webrtc_rtp", "PacedSend", capture_time_ms);
876 }
877
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000878 TRACE_EVENT_INSTANT2("webrtc_rtp", "PrepareAndSendPacket",
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000879 "timestamp", rtp_header.timestamp,
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000880 "seqnum", rtp_header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000881
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000882 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000883 if (send_over_rtx) {
884 BuildRtxPacket(buffer, &length, data_buffer_rtx);
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000885 buffer_to_send_ptr = data_buffer_rtx;
886 }
887
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000888 int64_t now_ms = clock_->TimeInMilliseconds();
889 int64_t diff_ms = now_ms - capture_time_ms;
stefan@webrtc.org420b2562014-05-30 12:17:15 +0000890 UpdateTransmissionTimeOffset(buffer_to_send_ptr, length, rtp_header,
891 diff_ms);
892 UpdateAbsoluteSendTime(buffer_to_send_ptr, length, rtp_header, now_ms);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000893 bool ret = SendPacketToNetwork(buffer_to_send_ptr, length);
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +0000894 if (ret) {
895 CriticalSectionScoped lock(send_critsect_);
896 media_has_been_sent_ = true;
897 }
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000898 UpdateRtpStats(buffer_to_send_ptr, length, rtp_header, send_over_rtx,
899 is_retransmit);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000900 return ret;
901}
902
903void RTPSender::UpdateRtpStats(const uint8_t* buffer,
904 uint32_t size,
905 const RTPHeader& header,
906 bool is_rtx,
907 bool is_retransmit) {
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000908 StreamDataCounters* counters;
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000909 // Get ssrc before taking statistics_crit_ to avoid possible deadlock.
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000910 uint32_t ssrc = is_rtx ? RtxSsrc() : SSRC();
sprang@webrtc.org5314e852014-01-27 13:20:36 +0000911
912 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000913 if (is_rtx) {
914 counters = &rtx_rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000915 } else {
916 counters = &rtp_stats_;
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000917 }
918
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +0000919 total_bitrate_sent_.Update(size);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +0000920 ++counters->packets;
921 if (IsFecPacket(buffer, header)) {
922 ++counters->fec_packets;
923 }
924
925 if (is_retransmit) {
926 ++counters->retransmitted_packets;
927 } else {
928 counters->bytes += size - (header.headerLength + header.paddingLength);
929 counters->header_bytes += header.headerLength;
930 counters->padding_bytes += header.paddingLength;
931 }
932
933 if (rtp_stats_callback_) {
934 rtp_stats_callback_->DataCountersUpdated(*counters, ssrc);
935 }
936}
937
938bool RTPSender::IsFecPacket(const uint8_t* buffer,
939 const RTPHeader& header) const {
940 if (!video_) {
941 return false;
942 }
943 bool fec_enabled;
944 uint8_t pt_red;
945 uint8_t pt_fec;
946 video_->GenericFECStatus(fec_enabled, pt_red, pt_fec);
947 return fec_enabled &&
948 header.payloadType == pt_red &&
949 buffer[header.headerLength] == pt_fec;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000950}
951
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000952int RTPSender::TimeToSendPadding(int bytes) {
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000953 {
954 CriticalSectionScoped cs(send_critsect_);
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000955 if (!sending_media_) return 0;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000956 }
andresp@webrtc.org817a0342014-08-14 08:24:47 +0000957 int available_bytes = bytes;
958 if (available_bytes > 0)
959 available_bytes -= TrySendRedundantPayloads(available_bytes);
960 if (available_bytes > 0)
961 available_bytes -= TrySendPadData(available_bytes);
962 return bytes - available_bytes;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000963}
964
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000965// TODO(pwestin): send in the RtpHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000966int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000967 uint8_t *buffer, int payload_length, int rtp_header_length,
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000968 int64_t capture_time_ms, StorageType storage,
969 PacedSender::Priority priority) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000970 RtpUtility::RtpHeaderParser rtp_parser(buffer,
971 payload_length + rtp_header_length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000972 RTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000973 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000974
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000975 int64_t now_ms = clock_->TimeInMilliseconds();
976
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000977 // |capture_time_ms| <= 0 is considered invalid.
978 // TODO(holmer): This should be changed all over Video Engine so that negative
979 // time is consider invalid, while 0 is considered a valid time.
980 if (capture_time_ms > 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000981 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000982 rtp_header, now_ms - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000983 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +0000984
985 UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length,
986 rtp_header, now_ms);
987
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000988 // Used for NACK and to spread out the transmission of packets.
stefan@webrtc.org7e9315b2013-12-04 10:24:26 +0000989 if (packet_history_.PutRTPPacket(buffer, rtp_header_length + payload_length,
990 max_payload_length_, capture_time_ms,
991 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000992 return -1;
993 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000994
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000995 if (paced_sender_ && storage != kDontStore) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000996 // Correct offset between implementations of millisecond time stamps in
997 // TickTime and Clock.
998 int64_t corrected_time_ms = capture_time_ms + clock_delta_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000999 if (!paced_sender_->SendPacket(priority, rtp_header.ssrc,
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +00001000 rtp_header.sequenceNumber, corrected_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +00001001 payload_length, false)) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +00001002 if (last_capture_time_ms_sent_ == 0 ||
1003 corrected_time_ms > last_capture_time_ms_sent_) {
1004 last_capture_time_ms_sent_ = corrected_time_ms;
1005 TRACE_EVENT_ASYNC_BEGIN1("webrtc_rtp", "PacedSend", corrected_time_ms,
1006 "capture_time_ms", corrected_time_ms);
1007 }
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +00001008 // We can't send the packet right now.
1009 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +00001010 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +00001011 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +00001012 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001013 if (capture_time_ms > 0) {
1014 UpdateDelayStatistics(capture_time_ms, now_ms);
1015 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001016 uint32_t length = payload_length + rtp_header_length;
1017 if (!SendPacketToNetwork(buffer, length))
1018 return -1;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001019 {
1020 CriticalSectionScoped lock(send_critsect_);
1021 media_has_been_sent_ = true;
1022 }
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001023 UpdateRtpStats(buffer, length, rtp_header, false, false);
1024 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +00001025}
1026
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001027void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
stefan@webrtc.org168f23f2014-07-11 13:44:02 +00001028 uint32_t ssrc;
1029 int avg_delay_ms = 0;
1030 int max_delay_ms = 0;
1031 {
1032 CriticalSectionScoped lock(send_critsect_);
1033 ssrc = ssrc_;
1034 }
1035 {
1036 CriticalSectionScoped cs(statistics_crit_.get());
1037 // TODO(holmer): Compute this iteratively instead.
1038 send_delays_[now_ms] = now_ms - capture_time_ms;
1039 send_delays_.erase(send_delays_.begin(),
1040 send_delays_.lower_bound(now_ms -
1041 kSendSideDelayWindowMs));
1042 }
1043 if (send_side_delay_observer_ &&
1044 GetSendSideDelay(&avg_delay_ms, &max_delay_ms)) {
1045 send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms,
1046 max_delay_ms, ssrc);
1047 }
stefan@webrtc.org0a3c1472013-12-05 14:05:07 +00001048}
1049
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001050void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001051 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001052 total_bitrate_sent_.Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001053 nack_bitrate_.Process();
1054 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001055 return;
1056 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001057 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +00001058}
1059
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001060uint16_t RTPSender::RTPHeaderLength() const {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001061 CriticalSectionScoped lock(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001062 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001063 if (include_csrcs_) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001064 rtp_header_length += sizeof(uint32_t) * num_csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001065 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001066 rtp_header_length += RtpHeaderExtensionTotalLength();
1067 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001068}
1069
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001070uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001071 CriticalSectionScoped cs(send_critsect_);
1072 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +00001073}
1074
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001075void RTPSender::ResetDataCounters() {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001076 uint32_t ssrc;
1077 uint32_t ssrc_rtx;
1078 {
1079 CriticalSectionScoped ssrc_lock(send_critsect_);
1080 ssrc = ssrc_;
1081 ssrc_rtx = ssrc_rtx_;
1082 }
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001083 CriticalSectionScoped lock(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001084 rtp_stats_ = StreamDataCounters();
1085 rtx_rtp_stats_ = StreamDataCounters();
1086 if (rtp_stats_callback_) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001087 rtp_stats_callback_->DataCountersUpdated(rtp_stats_, ssrc);
1088 rtp_stats_callback_->DataCountersUpdated(rtx_rtp_stats_, ssrc_rtx);
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001089 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001090}
1091
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001092void RTPSender::GetDataCounters(StreamDataCounters* rtp_stats,
1093 StreamDataCounters* rtx_stats) const {
pbos@webrtc.orge07049f2013-09-10 11:29:17 +00001094 CriticalSectionScoped lock(statistics_crit_.get());
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +00001095 *rtp_stats = rtp_stats_;
1096 *rtx_stats = rtx_rtp_stats_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001097}
1098
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001099int RTPSender::CreateRTPHeader(
1100 uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit,
1101 uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs,
1102 uint8_t num_csrcs) const {
1103 header[0] = 0x80; // version 2.
1104 header[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001105 if (marker_bit) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001106 header[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001107 }
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001108 RtpUtility::AssignUWord16ToBuffer(header + 2, sequence_number);
1109 RtpUtility::AssignUWord32ToBuffer(header + 4, timestamp);
1110 RtpUtility::AssignUWord32ToBuffer(header + 8, ssrc);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001111 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +00001112
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001113 // Add the CSRCs if any.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001114 if (num_csrcs > 0) {
1115 if (num_csrcs > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001116 // error
1117 assert(false);
1118 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001119 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001120 uint8_t *ptr = &header[rtp_header_length];
1121 for (int i = 0; i < num_csrcs; ++i) {
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001122 RtpUtility::AssignUWord32ToBuffer(ptr, csrcs[i]);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001123 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +00001124 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001125 header[0] = (header[0] & 0xf0) | num_csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001126
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001127 // Update length of header.
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001128 rtp_header_length += sizeof(uint32_t) * num_csrcs;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001129 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001130
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001131 uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length);
1132 if (len > 0) {
1133 header[0] |= 0x10; // Set extension bit.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001134 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001135 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001136 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001137}
1138
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001139int32_t RTPSender::BuildRTPheader(uint8_t* data_buffer,
1140 const int8_t payload_type,
1141 const bool marker_bit,
1142 const uint32_t capture_timestamp,
1143 int64_t capture_time_ms,
1144 const bool timestamp_provided,
1145 const bool inc_sequence_number) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001146 assert(payload_type >= 0);
1147 CriticalSectionScoped cs(send_critsect_);
1148
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001149 if (timestamp_provided) {
1150 timestamp_ = start_timestamp_ + capture_timestamp;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001151 } else {
1152 // Make a unique time stamp.
1153 // We can't inc by the actual time, since then we increase the risk of back
1154 // timing.
1155 timestamp_++;
1156 }
henrik.lundin@webrtc.org6e95d7a2013-11-15 08:59:19 +00001157 last_timestamp_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001158 uint32_t sequence_number = sequence_number_++;
stefan@webrtc.org8ccb9f92013-06-19 14:13:42 +00001159 capture_time_ms_ = capture_time_ms;
1160 last_packet_marker_bit_ = marker_bit;
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001161 int csrcs_length = 0;
1162 if (include_csrcs_)
1163 csrcs_length = num_csrcs_;
1164 return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit,
1165 timestamp_, sequence_number, csrcs_, csrcs_length);
1166}
1167
1168uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001169 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001170 return 0;
1171 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001172 // RTP header extension, RFC 3550.
1173 // 0 1 2 3
1174 // 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
1175 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1176 // | defined by profile | length |
1177 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1178 // | header extension |
1179 // | .... |
1180 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001181 const uint32_t kPosLength = 2;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001182 const uint32_t kHeaderLength = kRtpOneByteHeaderLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001183
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001184 // Add extension ID (0xBEDE).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001185 RtpUtility::AssignUWord16ToBuffer(data_buffer, kRtpOneByteHeaderExtensionId);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001186
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001187 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001188 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001189
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001190 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001191 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001192 uint8_t block_length = 0;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001193 switch (type) {
1194 case kRtpExtensionTransmissionTimeOffset:
1195 block_length = BuildTransmissionTimeOffsetExtension(
1196 data_buffer + kHeaderLength + total_block_length);
1197 break;
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001198 case kRtpExtensionAudioLevel:
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001199 block_length = BuildAudioLevelExtension(
1200 data_buffer + kHeaderLength + total_block_length);
solenberg@webrtc.orgc0352d52013-05-20 20:55:07 +00001201 break;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001202 case kRtpExtensionAbsoluteSendTime:
1203 block_length = BuildAbsoluteSendTimeExtension(
1204 data_buffer + kHeaderLength + total_block_length);
1205 break;
1206 default:
1207 assert(false);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001208 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001209 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001210 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001211 }
1212 if (total_block_length == 0) {
1213 // No extension added.
1214 return 0;
1215 }
1216 // Set header length (in number of Word32, header excluded).
1217 assert(total_block_length % 4 == 0);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001218 RtpUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
1219 total_block_length / 4);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001220 // Total added length.
1221 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001222}
1223
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001224uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
1225 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001226 // From RFC 5450: Transmission Time Offsets in RTP Streams.
1227 //
1228 // The transmission time is signaled to the receiver in-band using the
1229 // general mechanism for RTP header extensions [RFC5285]. The payload
1230 // of this extension (the transmitted value) is a 24-bit signed integer.
1231 // When added to the RTP timestamp of the packet, it represents the
1232 // "effective" RTP transmission time of the packet, on the RTP
1233 // timescale.
1234 //
1235 // The form of the transmission offset extension block:
1236 //
1237 // 0 1 2 3
1238 // 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
1239 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1240 // | ID | len=2 | transmission offset |
1241 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001242
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001243 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001244 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001245 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1246 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001247 // Not registered.
1248 return 0;
1249 }
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001250 size_t pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001251 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001252 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001253 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos,
1254 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001255 pos += 3;
pbos@webrtc.org3004c792013-05-07 12:36:21 +00001256 assert(pos == kTransmissionTimeOffsetLength);
1257 return kTransmissionTimeOffsetLength;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +00001258}
1259
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001260uint8_t RTPSender::BuildAudioLevelExtension(uint8_t* data_buffer) const {
1261 // An RTP Header Extension for Client-to-Mixer Audio Level Indication
1262 //
1263 // https://datatracker.ietf.org/doc/draft-lennox-avt-rtp-audio-level-exthdr/
1264 //
1265 // The form of the audio level extension block:
1266 //
1267 // 0 1 2 3
1268 // 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
1269 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1270 // | ID | len=0 |V| level | 0x00 | 0x00 |
1271 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1272 //
1273 // Note that we always include 2 pad bytes, which will result in legal and
1274 // correctly parsed RTP, but may be a bit wasteful if more short extensions
1275 // are implemented. Right now the pad bytes would anyway be required at end
1276 // of the extension block, so it makes no difference.
1277
1278 // Get id defined by user.
1279 uint8_t id;
1280 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1281 // Not registered.
1282 return 0;
1283 }
1284 size_t pos = 0;
1285 const uint8_t len = 0;
1286 data_buffer[pos++] = (id << 4) + len;
1287 data_buffer[pos++] = (1 << 7) + 0; // Voice, 0 dBov.
1288 data_buffer[pos++] = 0; // Padding.
1289 data_buffer[pos++] = 0; // Padding.
1290 // kAudioLevelLength is including pad bytes.
1291 assert(pos == kAudioLevelLength);
1292 return kAudioLevelLength;
1293}
1294
1295uint8_t RTPSender::BuildAbsoluteSendTimeExtension(uint8_t* data_buffer) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001296 // Absolute send time in RTP streams.
1297 //
1298 // The absolute send time is signaled to the receiver in-band using the
1299 // general mechanism for RTP header extensions [RFC5285]. The payload
1300 // of this extension (the transmitted value) is a 24-bit unsigned integer
1301 // containing the sender's current time in seconds as a fixed point number
1302 // with 18 bits fractional part.
1303 //
1304 // The form of the absolute send time extension block:
1305 //
1306 // 0 1 2 3
1307 // 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
1308 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1309 // | ID | len=2 | absolute send time |
1310 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1311
1312 // Get id defined by user.
1313 uint8_t id;
1314 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1315 &id) != 0) {
1316 // Not registered.
1317 return 0;
1318 }
1319 size_t pos = 0;
1320 const uint8_t len = 2;
1321 data_buffer[pos++] = (id << 4) + len;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001322 RtpUtility::AssignUWord24ToBuffer(data_buffer + pos, absolute_send_time_);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001323 pos += 3;
1324 assert(pos == kAbsoluteSendTimeLength);
1325 return kAbsoluteSendTimeLength;
1326}
1327
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001328void RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001329 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001330 const RTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001331 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001332 // Get id.
1333 uint8_t id = 0;
1334 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
1335 &id) != 0) {
1336 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001337 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001338 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001339 // Get length until start of header extension block.
1340 int extension_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001341 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001342 kRtpExtensionTransmissionTimeOffset);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001343 if (extension_block_pos < 0) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001344 LOG(LS_WARNING)
1345 << "Failed to update transmission time offset, not registered.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001346 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001347 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001348 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001349 if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001350 rtp_header.headerLength <
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001351 block_pos + kTransmissionTimeOffsetLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001352 LOG(LS_WARNING)
1353 << "Failed to update transmission time offset, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001354 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001355 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001356 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001357 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1358 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001359 LOG(LS_WARNING) << "Failed to update transmission time offset, hdr "
1360 "extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001361 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001362 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001363 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001364 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001365 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001366 LOG(LS_WARNING) << "Failed to update transmission time offset.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001367 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001368 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001369 // Update transmission offset field (converting to a 90 kHz timestamp).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001370 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1371 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +00001372}
1373
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001374bool RTPSender::UpdateAudioLevel(uint8_t *rtp_packet,
1375 const uint16_t rtp_packet_length,
1376 const RTPHeader &rtp_header,
1377 const bool is_voiced,
1378 const uint8_t dBov) const {
1379 CriticalSectionScoped cs(send_critsect_);
1380
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001381 // Get id.
1382 uint8_t id = 0;
1383 if (rtp_header_extension_map_.GetId(kRtpExtensionAudioLevel, &id) != 0) {
1384 // Not registered.
1385 return false;
1386 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001387 // Get length until start of header extension block.
1388 int extension_block_pos =
1389 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1390 kRtpExtensionAudioLevel);
1391 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001392 // The feature is not enabled.
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001393 return false;
1394 }
1395 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
1396 if (rtp_packet_length < block_pos + kAudioLevelLength ||
1397 rtp_header.headerLength < block_pos + kAudioLevelLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001398 LOG(LS_WARNING) << "Failed to update audio level, invalid length.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001399 return false;
1400 }
1401 // Verify that header contains extension.
1402 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1403 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001404 LOG(LS_WARNING) << "Failed to update audio level, hdr extension not found.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001405 return false;
1406 }
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001407 // Verify first byte in block.
1408 const uint8_t first_block_byte = (id << 4) + 0;
1409 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001410 LOG(LS_WARNING) << "Failed to update audio level.";
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00001411 return false;
1412 }
1413 rtp_packet[block_pos + 1] = (is_voiced ? 0x80 : 0x00) + (dBov & 0x7f);
1414 return true;
1415}
1416
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001417void RTPSender::UpdateAbsoluteSendTime(
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001418 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001419 const RTPHeader &rtp_header, const int64_t now_ms) const {
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001420 CriticalSectionScoped cs(send_critsect_);
1421
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001422 // Get id.
1423 uint8_t id = 0;
1424 if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime,
1425 &id) != 0) {
1426 // Not registered.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001427 return;
stefan@webrtc.org2f8d5f32014-04-15 12:28:46 +00001428 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001429 // Get length until start of header extension block.
1430 int extension_block_pos =
1431 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
1432 kRtpExtensionAbsoluteSendTime);
1433 if (extension_block_pos < 0) {
andrew@webrtc.org2c3f1ab2014-04-15 21:26:34 +00001434 // The feature is not enabled.
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001435 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001436 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001437 int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001438 if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength ||
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001439 rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001440 LOG(LS_WARNING) << "Failed to update absolute send time, invalid length.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001441 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001442 }
1443 // Verify that header contains extension.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001444 if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) &&
1445 (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001446 LOG(LS_WARNING)
1447 << "Failed to update absolute send time, hdr extension not found.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001448 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001449 }
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001450 // Verify first byte in block.
1451 const uint8_t first_block_byte = (id << 4) + 2;
1452 if (rtp_packet[block_pos] != first_block_byte) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +00001453 LOG(LS_WARNING) << "Failed to update absolute send time.";
stefan@webrtc.org420b2562014-05-30 12:17:15 +00001454 return;
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001455 }
1456 // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit
1457 // fractional part).
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001458 RtpUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
1459 ((now_ms << 18) / 1000) & 0x00ffffff);
solenberg@webrtc.org7ebbea12013-05-16 11:10:31 +00001460}
1461
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001462void RTPSender::SetSendingStatus(bool enabled) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001463 if (enabled) {
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001464 uint32_t frequency_hz = SendPayloadFrequency();
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001465 uint32_t RTPtime = RtpUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001466
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001467 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001468 SetStartTimestamp(RTPtime, false);
1469 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001470 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001471 if (!ssrc_forced_) {
1472 // Generate a new SSRC.
1473 ssrc_db_.ReturnSSRC(ssrc_);
1474 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001475 bitrates_->set_ssrc(ssrc_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001476 }
1477 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001478 if (!sequence_number_forced_ && !ssrc_forced_) {
1479 // Generate a new sequence number.
1480 sequence_number_ =
1481 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001482 }
1483 }
1484}
1485
1486void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001487 CriticalSectionScoped cs(send_critsect_);
1488 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001489}
1490
1491bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001492 CriticalSectionScoped cs(send_critsect_);
1493 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001494}
1495
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001496uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001497 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001498 return timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001499}
1500
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001501void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001502 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001503 if (force) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001504 start_timestamp_forced_ = true;
1505 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001506 } else {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001507 if (!start_timestamp_forced_) {
1508 start_timestamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001509 }
1510 }
1511}
1512
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001513uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001514 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001515 return start_timestamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001516}
1517
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001518uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001519 // If configured via API, return 0.
1520 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001521
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001522 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001523 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001524 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001525 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001526 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001527 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001528}
1529
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001530void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001531 // This is configured via the API.
1532 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001533
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001534 if (ssrc_ == ssrc && ssrc_forced_) {
1535 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001536 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001537 ssrc_forced_ = true;
1538 ssrc_db_.ReturnSSRC(ssrc_);
1539 ssrc_db_.RegisterSSRC(ssrc);
1540 ssrc_ = ssrc;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001541 bitrates_->set_ssrc(ssrc_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001542 if (!sequence_number_forced_) {
1543 sequence_number_ =
1544 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001545 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001546}
1547
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001548uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001549 CriticalSectionScoped cs(send_critsect_);
1550 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001551}
1552
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001553void RTPSender::SetCSRCStatus(const bool include) {
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001554 CriticalSectionScoped lock(send_critsect_);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001555 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001556}
1557
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001558void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1559 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001560 assert(arr_length <= kRtpCsrcSize);
1561 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001562
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001563 for (int i = 0; i < arr_length; i++) {
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001564 csrcs_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001565 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001566 num_csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001567}
1568
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001569int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001570 assert(arr_of_csrc);
1571 CriticalSectionScoped cs(send_critsect_);
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001572 for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) {
1573 arr_of_csrc[i] = csrcs_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001574 }
stefan@webrtc.orga8179622013-06-04 13:47:36 +00001575 return num_csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001576}
1577
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001578void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001579 CriticalSectionScoped cs(send_critsect_);
1580 sequence_number_forced_ = true;
1581 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001582}
1583
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001584uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001585 CriticalSectionScoped cs(send_critsect_);
1586 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001587}
1588
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001589// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001590int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1591 const uint16_t time_ms,
1592 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001593 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001594 return -1;
1595 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001596 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001597}
1598
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001599bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001600 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001601 return false;
1602 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001603 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001604}
1605
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001606int32_t RTPSender::SetAudioPacketSize(
1607 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001608 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001609 return -1;
1610 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001611 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001612}
1613
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001614int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001615 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001616}
1617
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001618int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001619 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001620 return -1;
1621 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001622 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001623}
1624
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001625int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001626 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001627 return -1;
1628 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001629 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001630}
1631
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001632// Video
1633VideoCodecInformation *RTPSender::CodecInformationVideo() {
1634 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001635 return NULL;
1636 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001637 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001638}
1639
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001640RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001641 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001642 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001643}
1644
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001645uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001646 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001647 return 0;
1648 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001649 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001650}
1651
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001652int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001653 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001654 return -1;
1655 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001656 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001657}
1658
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001659int32_t RTPSender::SetGenericFECStatus(
1660 const bool enable, const uint8_t payload_type_red,
1661 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001662 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001663 return -1;
1664 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001665 return video_->SetGenericFECStatus(enable, payload_type_red,
1666 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001667}
1668
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001669int32_t RTPSender::GenericFECStatus(
1670 bool *enable, uint8_t *payload_type_red,
1671 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001672 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001673 return -1;
1674 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001675 return video_->GenericFECStatus(
1676 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001677}
1678
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001679int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001680 const FecProtectionParams *delta_params,
1681 const FecProtectionParams *key_params) {
1682 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001683 return -1;
1684 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001685 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001686}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001687
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001688void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1689 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001690 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001691 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001692 // Add RTX header.
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001693 RtpUtility::RtpHeaderParser rtp_parser(
1694 reinterpret_cast<const uint8_t*>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001695
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001696 RTPHeader rtp_header;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001697 rtp_parser.Parse(rtp_header);
1698
1699 // Add original RTP header.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001700 memcpy(data_buffer_rtx, buffer, rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001701
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001702 // Replace payload type, if a specific type is set for RTX.
1703 if (payload_type_rtx_ != -1) {
1704 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001705 if (rtp_header.markerBit)
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001706 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1707 }
1708
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001709 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001710 uint8_t *ptr = data_buffer_rtx + 2;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001711 RtpUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001712
1713 // Replace SSRC.
1714 ptr += 6;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001715 RtpUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001716
1717 // Add OSN (original sequence number).
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001718 ptr = data_buffer_rtx + rtp_header.headerLength;
pbos@webrtc.org62bafae2014-07-08 12:10:51 +00001719 RtpUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001720 ptr += 2;
1721
1722 // Add original payload data.
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001723 memcpy(ptr, buffer + rtp_header.headerLength,
1724 *length - rtp_header.headerLength);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001725 *length += 2;
1726}
1727
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001728void RTPSender::RegisterRtpStatisticsCallback(
1729 StreamDataCountersCallback* callback) {
1730 CriticalSectionScoped cs(statistics_crit_.get());
sprang@webrtc.orgebad7652013-12-05 14:29:02 +00001731 rtp_stats_callback_ = callback;
1732}
1733
1734StreamDataCountersCallback* RTPSender::GetRtpStatisticsCallback() const {
1735 CriticalSectionScoped cs(statistics_crit_.get());
1736 return rtp_stats_callback_;
1737}
1738
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001739uint32_t RTPSender::BitrateSent() const {
1740 return total_bitrate_sent_.BitrateLast();
sprang@webrtc.org6811b6e2013-12-13 09:46:59 +00001741}
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001742
1743void RTPSender::SetRtpState(const RtpState& rtp_state) {
1744 SetStartTimestamp(rtp_state.start_timestamp, true);
1745 CriticalSectionScoped lock(send_critsect_);
1746 sequence_number_ = rtp_state.sequence_number;
1747 sequence_number_forced_ = true;
1748 timestamp_ = rtp_state.timestamp;
1749 capture_time_ms_ = rtp_state.capture_time_ms;
1750 last_timestamp_time_ms_ = rtp_state.last_timestamp_time_ms;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001751 media_has_been_sent_ = rtp_state.media_has_been_sent;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001752}
1753
1754RtpState RTPSender::GetRtpState() const {
1755 CriticalSectionScoped lock(send_critsect_);
1756
1757 RtpState state;
1758 state.sequence_number = sequence_number_;
1759 state.start_timestamp = start_timestamp_;
1760 state.timestamp = timestamp_;
1761 state.capture_time_ms = capture_time_ms_;
1762 state.last_timestamp_time_ms = last_timestamp_time_ms_;
stefan@webrtc.org8b94e3d2014-07-17 16:10:14 +00001763 state.media_has_been_sent = media_has_been_sent_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +00001764
1765 return state;
1766}
1767
1768void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
1769 CriticalSectionScoped lock(send_critsect_);
1770 sequence_number_rtx_ = rtp_state.sequence_number;
1771}
1772
1773RtpState RTPSender::GetRtxRtpState() const {
1774 CriticalSectionScoped lock(send_critsect_);
1775
1776 RtpState state;
1777 state.sequence_number = sequence_number_rtx_;
1778 state.start_timestamp = start_timestamp_;
1779
1780 return state;
1781}
1782
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001783} // namespace webrtc