blob: e74465f89ac884ffc9a18b5948013dd759059c86 [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
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000013#include <cstdlib> // srand
niklase@google.com470e71d2011-07-07 08:21:25 +000014
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000015#include "webrtc/modules/pacing/include/paced_sender.h"
16#include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
17#include "webrtc/modules/rtp_rtcp/source/rtp_sender_audio.h"
18#include "webrtc/modules/rtp_rtcp/source/rtp_sender_video.h"
19#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
20#include "webrtc/system_wrappers/interface/trace.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000021#include "webrtc/system_wrappers/interface/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000025namespace {
26
27const char* FrameTypeToString(const FrameType frame_type) {
28 switch (frame_type) {
29 case kFrameEmpty: return "empty";
30 case kAudioFrameSpeech: return "audio_speech";
31 case kAudioFrameCN: return "audio_cn";
32 case kVideoFrameKey: return "video_key";
33 case kVideoFrameDelta: return "video_delta";
34 case kVideoFrameGolden: return "video_golden";
35 case kVideoFrameAltRef: return "video_altref";
36 }
37 return "";
38}
39
40} // namespace
41
pbos@webrtc.org2f446732013-04-08 11:08:41 +000042RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000043 Transport *transport, RtpAudioFeedback *audio_feedback,
44 PacedSender *paced_sender)
45 : Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL),
46 video_(NULL), paced_sender_(paced_sender),
47 send_critsect_(CriticalSectionWrapper::CreateCriticalSection()),
48 transport_(transport), sending_media_(true), // Default to sending media.
49 max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
50 target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1),
51 payload_type_map_(), rtp_header_extension_map_(),
52 transmission_time_offset_(0),
53 // NACK.
54 nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock),
55 packet_history_(new RTPPacketHistory(clock)),
56 // Statistics
57 packets_sent_(0), payload_bytes_sent_(0), start_time_stamp_forced_(false),
58 start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()),
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000059 remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false),
60 time_stamp_(0), csrcs_(0), csrc_(), include_csrcs_(true),
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +000061 rtx_(kRtxOff), payload_type_rtx_(-1) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000062 memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_));
63 memset(nack_byte_count_, 0, sizeof(nack_byte_count_));
64 memset(csrc_, 0, sizeof(csrc_));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000065 // We need to seed the random generator.
pbos@webrtc.org2f446732013-04-08 11:08:41 +000066 srand(static_cast<uint32_t>(clock_->TimeInMilliseconds()));
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000067 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000068 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
69 // Random start, 16 bits. Can't be 0.
70 sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
71 sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF;
niklase@google.com470e71d2011-07-07 08:21:25 +000072
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000073 if (audio) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000074 audio_ = new RTPSenderAudio(id, clock_, this);
75 audio_->RegisterAudioCallback(audio_feedback);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000076 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000077 video_ = new RTPSenderVideo(id, clock_, this);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +000078 }
79 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +000080}
81
pwestin@webrtc.org00741872012-01-19 15:56:10 +000082RTPSender::~RTPSender() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000083 if (remote_ssrc_ != 0) {
84 ssrc_db_.ReturnSSRC(remote_ssrc_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000085 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000086 ssrc_db_.ReturnSSRC(ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +000087
pwestin@webrtc.org00741872012-01-19 15:56:10 +000088 SSRCDatabase::ReturnSSRCDatabase();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000089 delete send_critsect_;
90 while (!payload_type_map_.empty()) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +000091 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000092 payload_type_map_.begin();
pwestin@webrtc.org00741872012-01-19 15:56:10 +000093 delete it->second;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000094 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +000095 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +000096 delete packet_history_;
97 delete audio_;
98 delete video_;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +000099
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000100 WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101}
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000103void RTPSender::SetTargetSendBitrate(const uint32_t bits) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000104 target_send_bitrate_ = static_cast<uint16_t>(bits / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105}
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000106
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000107uint16_t RTPSender::ActualSendBitrateKbit() const {
108 return (uint16_t)(Bitrate::BitrateNow() / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000109}
110
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000111uint32_t RTPSender::VideoBitrateSent() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000112 if (video_) {
113 return video_->VideoBitrateSent();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000114 }
115 return 0;
stefan@webrtc.orgfbea4e52011-10-27 16:08:29 +0000116}
117
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000118uint32_t RTPSender::FecOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000119 if (video_) {
120 return video_->FecOverheadRate();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000121 }
122 return 0;
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000123}
124
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000125uint32_t RTPSender::NackOverheadRate() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000126 return nack_bitrate_.BitrateLast();
stefan@webrtc.orgd0bdab02011-10-14 14:24:54 +0000127}
128
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000129int32_t RTPSender::SetTransmissionTimeOffset(
130 const int32_t transmission_time_offset) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000131 if (transmission_time_offset > (0x800000 - 1) ||
132 transmission_time_offset < -(0x800000 - 1)) { // Word24.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000133 return -1;
134 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000135 CriticalSectionScoped cs(send_critsect_);
136 transmission_time_offset_ = transmission_time_offset;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000137 return 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000138}
139
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000140int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type,
141 const uint8_t id) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000142 CriticalSectionScoped cs(send_critsect_);
143 return rtp_header_extension_map_.Register(type, id);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000144}
145
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000146int32_t RTPSender::DeregisterRtpHeaderExtension(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000147 const RTPExtensionType type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000148 CriticalSectionScoped cs(send_critsect_);
149 return rtp_header_extension_map_.Deregister(type);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000150}
151
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000152uint16_t RTPSender::RtpHeaderExtensionTotalLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000153 CriticalSectionScoped cs(send_critsect_);
154 return rtp_header_extension_map_.GetTotalLengthInBytes();
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000155}
156
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000157int32_t RTPSender::RegisterPayload(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000158 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000159 const int8_t payload_number, const uint32_t frequency,
160 const uint8_t channels, const uint32_t rate) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000161 assert(payload_name);
162 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000163
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000164 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000165 payload_type_map_.find(payload_number);
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000167 if (payload_type_map_.end() != it) {
168 // We already use this payload type.
169 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000170 assert(payload);
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000172 // Check if it's the same as we already have.
173 if (ModuleRTPUtility::StringCompare(payload->name, payload_name,
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000174 RTP_PAYLOAD_NAME_SIZE - 1)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000175 if (audio_configured_ && payload->audio &&
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000176 payload->typeSpecific.Audio.frequency == frequency &&
177 (payload->typeSpecific.Audio.rate == rate ||
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000178 payload->typeSpecific.Audio.rate == 0 || rate == 0)) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000179 payload->typeSpecific.Audio.rate = rate;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000180 // Ensure that we update the rate if new or old is zero.
niklase@google.com470e71d2011-07-07 08:21:25 +0000181 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000182 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000183 if (!audio_configured_ && !payload->audio) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000184 return 0;
185 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000186 }
187 return -1;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000188 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000189 int32_t ret_val = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000190 ModuleRTPUtility::Payload *payload = NULL;
191 if (audio_configured_) {
192 ret_val = audio_->RegisterAudioPayload(payload_name, payload_number,
193 frequency, channels, rate, payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000194 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000195 ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate,
196 payload);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000197 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000198 if (payload) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000199 payload_type_map_[payload_number] = payload;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000200 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000201 return ret_val;
niklase@google.com470e71d2011-07-07 08:21:25 +0000202}
203
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000204int32_t RTPSender::DeRegisterSendPayload(
205 const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000206 CriticalSectionScoped lock(send_critsect_);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000207
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000208 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000209 payload_type_map_.find(payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000210
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000211 if (payload_type_map_.end() == it) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000212 return -1;
213 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000214 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000215 delete payload;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000216 payload_type_map_.erase(it);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000217 return 0;
218}
niklase@google.com470e71d2011-07-07 08:21:25 +0000219
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000220int8_t RTPSender::SendPayloadType() const { return payload_type_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000221
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000222int RTPSender::SendPayloadFrequency() const { return audio_->AudioFrequency(); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000223
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000224int32_t RTPSender::SetMaxPayloadLength(
225 const uint16_t max_payload_length,
226 const uint16_t packet_over_head) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000227 // Sanity check.
228 if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) {
229 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument",
230 __FUNCTION__);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000231 return -1;
232 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000233 CriticalSectionScoped cs(send_critsect_);
234 max_payload_length_ = max_payload_length;
235 packet_over_head_ = packet_over_head;
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000237 WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.",
238 max_payload_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000239 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000240}
241
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000242uint16_t RTPSender::MaxDataPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000243 if (audio_configured_) {
244 return max_payload_length_ - RTPHeaderLength();
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000245 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000246 return max_payload_length_ - RTPHeaderLength() -
247 video_->FECPacketOverhead() - ((rtx_) ? 2 : 0);
248 // Include the FEC/ULP/RED overhead.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000249 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000250}
251
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000252uint16_t RTPSender::MaxPayloadLength() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000253 return max_payload_length_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000254}
255
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000256uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000257
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000258void RTPSender::SetRTXStatus(RtxMode mode, bool set_ssrc, uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000259 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000260 rtx_ = mode;
261 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000262 if (set_ssrc) {
263 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000264 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000265 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000266 }
267 }
268}
269
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000270void RTPSender::RTXStatus(RtxMode* mode, uint32_t* ssrc,
271 int* payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000272 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000273 *mode = rtx_;
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +0000274 *ssrc = ssrc_rtx_;
275 *payload_type = payload_type_rtx_;
276}
277
278
279void RTPSender::SetRtxPayloadType(int payload_type) {
280 CriticalSectionScoped cs(send_critsect_);
281 payload_type_rtx_ = payload_type;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000282}
283
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000284int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
285 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000286 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000287
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000288 if (payload_type < 0) {
289 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
290 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000291 return -1;
292 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000293 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000294 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000295 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000296 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000297 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000298 // And it's a match...
299 return 0;
300 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000301 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000302 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000303 if (payload_type_ == payload_type) {
304 if (!audio_configured_) {
305 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000306 }
307 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000308 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000309 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000310 payload_type_map_.find(payload_type);
311 if (it == payload_type_map_.end()) {
312 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
313 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000314 return -1;
315 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000316 payload_type_ = payload_type;
317 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000318 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000319 if (!payload->audio && !audio_configured_) {
320 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
321 *video_type = payload->typeSpecific.Video.videoCodecType;
322 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000323 }
324 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000325}
326
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000327int32_t RTPSender::SendOutgoingData(
328 const FrameType frame_type, const int8_t payload_type,
329 const uint32_t capture_timestamp, int64_t capture_time_ms,
330 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000331 const RTPFragmentationHeader *fragmentation,
332 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000333 TRACE_EVENT2("webrtc_rtp", "RTPSender::SendOutgoingData",
334 "timestsamp", capture_timestamp,
335 "frame_type", FrameTypeToString(frame_type));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000336 {
337 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000338 CriticalSectionScoped cs(send_critsect_);
339 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000340 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000341 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000342 }
pbos@webrtc.org8911ce42013-03-18 16:39:03 +0000343 RtpVideoCodecTypes video_type = kRtpGenericVideo;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000344 if (CheckPayloadType(payload_type, &video_type) != 0) {
345 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
346 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000347 __FUNCTION__, payload_type);
348 return -1;
349 }
350
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000351 if (audio_configured_) {
352 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000353 frame_type == kFrameEmpty);
354
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000355 return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
356 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000357 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000358 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000359
360 if (frame_type == kFrameEmpty) {
361 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
362 capture_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000363 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000364 return video_->SendVideo(video_type, frame_type, payload_type,
365 capture_timestamp, capture_time_ms, payload_data,
366 payload_size, fragmentation, codec_info,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000367 rtp_type_hdr);
368 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000369}
370
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000371int32_t RTPSender::SendPaddingAccordingToBitrate(
372 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000373 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000374 // Current bitrate since last estimate(1 second) averaged with the
375 // estimate since then, to get the most up to date bitrate.
376 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000377 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000378 if (bitrate_diff <= 0) {
379 return 0;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000380 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000381 int bytes = 0;
382 if (current_bitrate == 0) {
383 // Start up phase. Send one 33.3 ms batch to start with.
384 bytes = (bitrate_diff / 8) / 30;
385 } else {
386 bytes = (bitrate_diff / 8);
387 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000388 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000389 if (bytes > bytes_cap) {
390 bytes = bytes_cap;
391 }
392 }
393 return SendPadData(payload_type, capture_timestamp, capture_time_ms, bytes);
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000394}
395
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000396int32_t RTPSender::SendPadData(
397 int8_t payload_type, uint32_t capture_timestamp,
398 int64_t capture_time_ms, int32_t bytes) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000399 // Drop this packet if we're not sending media packets.
400 if (!sending_media_) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000401 return 0;
402 }
403 // Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
404 int max_length = 224;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000405 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000406
407 for (; bytes > 0; bytes -= max_length) {
asapersson@webrtc.org63a34f42012-04-20 13:20:27 +0000408 int padding_bytes_in_packet = max_length;
409 if (bytes < max_length) {
410 padding_bytes_in_packet = (bytes + 16) & 0xffe0; // Keep our modulus 32.
411 }
412 if (padding_bytes_in_packet < 32) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000413 // Sanity don't send empty packets.
414 break;
asapersson@webrtc.org63a34f42012-04-20 13:20:27 +0000415 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000416 // Correct seq num, timestamp and payload type.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000417 int header_length = BuildRTPheader(
418 data_buffer, payload_type, false, // No markerbit.
419 capture_timestamp, true, // Timestamp provided.
420 true); // Increment sequence number.
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000421 data_buffer[0] |= 0x20; // Set padding bit.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000422 int32_t *data =
423 reinterpret_cast<int32_t *>(&(data_buffer[header_length]));
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000424
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000425 // Fill data buffer with random data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000426 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
427 data[j] = rand(); // NOLINT
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000428 }
429 // Set number of padding bytes in the last byte of the packet.
430 data_buffer[header_length + padding_bytes_in_packet - 1] =
431 padding_bytes_in_packet;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000432 // Send the packet.
433 if (0 > SendToNetwork(data_buffer, padding_bytes_in_packet, header_length,
434 capture_time_ms, kDontRetransmit)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000435 // Error sending the packet.
436 break;
437 }
438 }
439 if (bytes > 31) { // 31 due to our modulus 32.
440 // We did not manage to send all bytes.
441 return -1;
442 }
443 return 0;
444}
445
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000446void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000447 const uint16_t number_to_store) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000448 packet_history_->SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000449}
450
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000451bool RTPSender::StorePackets() const {
452 return packet_history_->StorePackets();
453}
niklase@google.com470e71d2011-07-07 08:21:25 +0000454
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000455int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
456 uint16_t length = IP_PACKET_SIZE;
457 uint8_t data_buffer[IP_PACKET_SIZE];
458 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000459 int64_t capture_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000460 StorageType type;
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000461 if (!packet_history_->GetRTPPacket(packet_id, min_resend_time, data_buffer,
462 &length, &capture_time_ms, &type)) {
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000463 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000464 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000465 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000466 if (length == 0 || type == kDontRetransmit) {
467 // No bytes copied (packet recently resent, skip resending) or
468 // packet should not be retransmitted.
469 return 0;
470 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000471
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000472 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000473 if (rtx_ != kRtxOff) {
474 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000475 buffer_to_send_ptr = data_buffer_rtx;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000476 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000477
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000478 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
479 WebRtcRTPHeader rtp_header;
480 rtp_parser.Parse(rtp_header);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000481
482 // Store the time when the packet was last sent or added to pacer.
483 packet_history_->UpdateResendTime(packet_id);
484
485 {
486 // Update send statistics prior to pacer.
487 CriticalSectionScoped cs(send_critsect_);
488 Bitrate::Update(length);
489 packets_sent_++;
490 // We on purpose don't add to payload_bytes_sent_ since this is a
491 // re-transmit and not new payload data.
492 }
493
494 if (paced_sender_) {
495 if (!paced_sender_->SendPacket(PacedSender::kHighPriority,
496 rtp_header.header.ssrc,
497 rtp_header.header.sequenceNumber,
498 capture_time_ms,
499 length)) {
500 // We can't send the packet right now.
501 // We will be called when it is time.
502 return 0;
503 }
504 }
505
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000506 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
507 "timestamp", rtp_header.header.timestamp,
508 "seqnum", rtp_header.header.sequenceNumber);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000509
510 if (SendPacketToNetwork(buffer_to_send_ptr, length)) {
511 return 0;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000512 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000513 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000514}
515
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000516bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) {
517 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000518 if (transport_) {
519 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000520 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000521 // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer.
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000522 if (bytes_sent <= 0) {
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000523 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
524 "Transport failed to send packet");
525 return false;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000526 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000527 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000528}
529
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000530int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000531 if (!video_)
532 return -1;
533 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000534}
535
536int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000537 if (!video_)
538 return -1;
539 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000540}
541
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000542void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000543 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000544 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000545 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
546 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000547 const int64_t now = clock_->TimeInMilliseconds();
548 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000549
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000550 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000551 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000552 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000553 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000554 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000555 return;
556 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000557
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000558 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
559 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000560 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000561 if (bytes_sent > 0) {
562 bytes_re_sent += bytes_sent;
563 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000564 // The packet has previously been resent.
565 // Try resending next packet in the list.
566 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000567 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000568 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000569 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000570 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000571 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000572 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000573 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000574 // Delay bandwidth estimate (RTT * BW).
575 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000576 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000577 uint32_t target_bytes =
578 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000579 if (bytes_re_sent > target_bytes) {
580 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000581 }
582 }
583 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000584 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000585 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000586 UpdateNACKBitRate(bytes_re_sent, now);
587 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000588 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000589}
590
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000591bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
592 uint32_t num = 0;
593 int32_t byte_count = 0;
594 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000595
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000596 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000597
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000598 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000599 return true;
600 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000601 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
602 if ((now - nack_byte_count_times_[num]) > avg_interval) {
603 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000604 break;
605 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000606 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000607 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000608 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000609 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000610 if (num == NACK_BYTECOUNT_SIZE) {
611 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000612 // during the last msg_interval.
613 time_interval = now - nack_byte_count_times_[num - 1];
614 if (time_interval < 0) {
615 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000616 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000617 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000618 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000619}
620
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000621void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
622 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000623 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000624
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000625 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000626 if (bytes > 0) {
627 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000628 // Add padding length.
629 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000630 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000631 if (nack_byte_count_times_[0] == 0) {
632 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000633 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000634 // Shift.
635 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
636 nack_byte_count_[i + 1] = nack_byte_count_[i];
637 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000638 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000639 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000640 nack_byte_count_[0] = bytes;
641 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000642 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000643 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000644}
645
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000646// Called from pacer when we can send the packet.
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000647void RTPSender::TimeToSendPacket(uint16_t sequence_number,
648 int64_t capture_time_ms) {
649 StorageType type;
650 uint16_t length = IP_PACKET_SIZE;
651 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000652 int64_t stored_time_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000653
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000654 if (packet_history_ == NULL) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000655 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000656 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000657 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
658 &stored_time_ms, &type)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000659 return;
660 }
661 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000662
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000663 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000664 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000665 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000666 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
667 "timestamp", rtp_header.header.timestamp,
668 "seqnum", sequence_number);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000669
stefan@webrtc.org7da34592013-04-09 14:56:29 +0000670 int64_t diff_ms = clock_->TimeInMilliseconds() - capture_time_ms;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000671 if (UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms)) {
672 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000673 packet_history_->ReplaceRTPHeader(data_buffer,
674 rtp_header.header.sequenceNumber,
675 rtp_header.header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000676 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000677 SendPacketToNetwork(data_buffer, length);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000678}
679
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000680// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000681int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000682 uint8_t *buffer, int payload_length, int rtp_header_length,
683 int64_t capture_time_ms, StorageType storage) {
684 ModuleRTPUtility::RTPHeaderParser rtp_parser(
685 buffer, payload_length + rtp_header_length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000686 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000687 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000688
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000689 // |capture_time_ms| <= 0 is considered invalid.
690 // TODO(holmer): This should be changed all over Video Engine so that negative
691 // time is consider invalid, while 0 is considered a valid time.
692 if (capture_time_ms > 0) {
stefan@webrtc.org7da34592013-04-09 14:56:29 +0000693 int64_t time_now = clock_->TimeInMilliseconds();
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000694 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
stefan@webrtc.org7da34592013-04-09 14:56:29 +0000695 rtp_header, time_now - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000696 }
697 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000698 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
699 max_payload_length_, capture_time_ms,
700 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000701 return -1;
702 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000703
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000704 // Create and send RTX Packet.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000705 // TODO(pwesin): This should be moved to its own code path triggered by pacer.
706 bool rtx_sent = false;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000707 if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000708 uint16_t length_rtx = payload_length + rtp_header_length;
709 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000710 BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000711 if (!SendPacketToNetwork(data_buffer_rtx, length_rtx)) return -1;
712 rtx_sent = true;
713 }
714 {
715 // Update send statistics prior to pacer.
716 CriticalSectionScoped cs(send_critsect_);
717 Bitrate::Update(payload_length + rtp_header_length);
718 ++packets_sent_;
719 payload_bytes_sent_ += payload_length;
720 if (rtx_sent) {
721 // The RTX packet.
722 ++packets_sent_;
723 payload_bytes_sent_ += payload_length;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000724 }
725 }
726
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000727 if (paced_sender_ && storage != kDontStore) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000728 if (!paced_sender_->SendPacket(
729 PacedSender::kNormalPriority, rtp_header.header.ssrc,
730 rtp_header.header.sequenceNumber, capture_time_ms,
731 payload_length + rtp_header_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000732 // We can't send the packet right now.
733 // We will be called when it is time.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000734 return 0;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000735 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000736 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000737 if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) {
738 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000739 }
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000740 return -1;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000741}
742
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000743void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000744 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000745 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000746 nack_bitrate_.Process();
747 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000748 return;
749 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000750 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000751}
752
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000753uint16_t RTPSender::RTPHeaderLength() const {
754 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000755 if (include_csrcs_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000756 rtp_header_length += sizeof(uint32_t) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000757 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000758 rtp_header_length += RtpHeaderExtensionTotalLength();
759 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000760}
761
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000762uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000763 CriticalSectionScoped cs(send_critsect_);
764 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000765}
766
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000767void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000768 packets_sent_ = 0;
769 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000770}
771
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000772uint32_t RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000773 // Don't use critsect to avoid potential deadlock.
774 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000775}
776
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000777// Number of sent RTP bytes.
778// Don't use critsect to avoid potental deadlock.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000779uint32_t RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000780 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000781}
782
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000783int32_t RTPSender::BuildRTPheader(
784 uint8_t *data_buffer, const int8_t payload_type,
785 const bool marker_bit, const uint32_t capture_time_stamp,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000786 const bool time_stamp_provided, const bool inc_sequence_number) {
787 assert(payload_type >= 0);
788 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000789
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000790 data_buffer[0] = static_cast<uint8_t>(0x80); // version 2.
791 data_buffer[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000792 if (marker_bit) {
793 data_buffer[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000794 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000795 if (time_stamp_provided) {
796 time_stamp_ = start_time_stamp_ + capture_time_stamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000797 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000798 // Make a unique time stamp.
799 // We can't inc by the actual time, since then we increase the risk of back
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000800 // timing.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000801 time_stamp_++;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000802 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000803 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + 2, sequence_number_);
804 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 4, time_stamp_);
805 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 8, ssrc_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000806 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000807
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000808 // Add the CSRCs if any.
809 if (include_csrcs_ && csrcs_ > 0) {
810 if (csrcs_ > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000811 // error
812 assert(false);
813 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000814 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000815 uint8_t *ptr = &data_buffer[rtp_header_length];
816 for (uint32_t i = 0; i < csrcs_; ++i) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000817 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrc_[i]);
818 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000819 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000820 data_buffer[0] = (data_buffer[0] & 0xf0) | csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000821
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000822 // Update length of header.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000823 rtp_header_length += sizeof(uint32_t) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000824 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000825 sequence_number_++; // Prepare for next packet.
niklase@google.com470e71d2011-07-07 08:21:25 +0000826
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000827 uint16_t len = BuildRTPHeaderExtension(data_buffer + rtp_header_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000828 if (len) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000829 data_buffer[0] |= 0x10; // Set extension bit.
830 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000831 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000832 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000833}
834
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000835uint16_t RTPSender::BuildRTPHeaderExtension(
836 uint8_t *data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000837 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000838 return 0;
839 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000840 // RTP header extension, RFC 3550.
841 // 0 1 2 3
842 // 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
843 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
844 // | defined by profile | length |
845 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
846 // | header extension |
847 // | .... |
848 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000849 const uint32_t kPosLength = 2;
850 const uint32_t kHeaderLength = RTP_ONE_BYTE_HEADER_LENGTH_IN_BYTES;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000851
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000852 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000853 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000854 RTP_ONE_BYTE_HEADER_EXTENSION);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000855
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000856 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000857 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000858
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000859 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000860 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000861 uint8_t block_length = 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000862 if (type == kRtpExtensionTransmissionTimeOffset) {
863 block_length = BuildTransmissionTimeOffsetExtension(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000864 data_buffer + kHeaderLength + total_block_length);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000865 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000866 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000867 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000868 }
869 if (total_block_length == 0) {
870 // No extension added.
871 return 0;
872 }
873 // Set header length (in number of Word32, header excluded).
874 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000875 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000876 total_block_length / 4);
877 // Total added length.
878 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000879}
880
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000881uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
882 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000883 // From RFC 5450: Transmission Time Offsets in RTP Streams.
884 //
885 // The transmission time is signaled to the receiver in-band using the
886 // general mechanism for RTP header extensions [RFC5285]. The payload
887 // of this extension (the transmitted value) is a 24-bit signed integer.
888 // When added to the RTP timestamp of the packet, it represents the
889 // "effective" RTP transmission time of the packet, on the RTP
890 // timescale.
891 //
892 // The form of the transmission offset extension block:
893 //
894 // 0 1 2 3
895 // 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
896 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
897 // | ID | len=2 | transmission offset |
898 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000899
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000900 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000901 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000902 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
903 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000904 // Not registered.
905 return 0;
906 }
907 int pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000908 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000909 data_buffer[pos++] = (id << 4) + len;
910 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
911 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000912 pos += 3;
913 assert(pos == TRANSMISSION_TIME_OFFSET_LENGTH_IN_BYTES);
914 return TRANSMISSION_TIME_OFFSET_LENGTH_IN_BYTES;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000915}
916
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000917bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000918 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
919 const WebRtcRTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000920 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000921
922 // Get length until start of transmission block.
923 int transmission_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000924 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000925 kRtpExtensionTransmissionTimeOffset);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000926 if (transmission_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000927 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000928 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000929 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000930 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000931 int block_pos = 12 + rtp_header.header.numCSRCs + transmission_block_pos;
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +0000932 if (rtp_packet_length < block_pos + 4 ||
933 rtp_header.header.headerLength < block_pos + 4) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000934 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000935 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000936 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000937 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000938 // Verify that header contains extension.
939 if (!((rtp_packet[12 + rtp_header.header.numCSRCs] == 0xBE) &&
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000940 (rtp_packet[12 + rtp_header.header.numCSRCs + 1] == 0xDE))) {
941 WEBRTC_TRACE(
942 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000943 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000944 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000945 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000946 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000947 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000948 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
949 &id) != 0) {
950 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000951 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000952 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000953 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000954 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000955 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000956 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000957 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000958 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000959 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000960 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000961 // Update transmission offset field.
962 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +0000963 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000964 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000965}
966
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000967void RTPSender::SetSendingStatus(const bool enabled) {
968 if (enabled) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000969 uint32_t frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000970 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000971 uint32_t frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000972
973 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000974 switch (frequency) {
975 case 8000:
976 case 12000:
977 case 16000:
978 case 24000:
979 case 32000:
980 break;
981 default:
982 assert(false);
983 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000984 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000985 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000986 } else {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000987 frequency_hz = kDefaultVideoFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000988 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000989 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000990
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000991 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000992 SetStartTimestamp(RTPtime, false);
993 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000994 if (!ssrc_forced_) {
995 // Generate a new SSRC.
996 ssrc_db_.ReturnSSRC(ssrc_);
997 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000998 }
999 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001000 if (!sequence_number_forced_ && !ssrc_forced_) {
1001 // Generate a new sequence number.
1002 sequence_number_ =
1003 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001004 }
1005 }
1006}
1007
1008void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001009 CriticalSectionScoped cs(send_critsect_);
1010 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001011}
1012
1013bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001014 CriticalSectionScoped cs(send_critsect_);
1015 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001016}
1017
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001018uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001019 CriticalSectionScoped cs(send_critsect_);
1020 return time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001021}
1022
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001023void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001024 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001025 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001026 start_time_stamp_forced_ = force;
1027 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001028 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001029 if (!start_time_stamp_forced_) {
1030 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001031 }
1032 }
1033}
1034
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001035uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001036 CriticalSectionScoped cs(send_critsect_);
1037 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001038}
1039
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001040uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001041 // If configured via API, return 0.
1042 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001043
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001044 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001045 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001046 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001047 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1048 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001049}
1050
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001051void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001052 // This is configured via the API.
1053 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001054
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001055 if (ssrc_ == ssrc && ssrc_forced_) {
1056 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001057 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001058 ssrc_forced_ = true;
1059 ssrc_db_.ReturnSSRC(ssrc_);
1060 ssrc_db_.RegisterSSRC(ssrc);
1061 ssrc_ = ssrc;
1062 if (!sequence_number_forced_) {
1063 sequence_number_ =
1064 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001065 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001066}
1067
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001068uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001069 CriticalSectionScoped cs(send_critsect_);
1070 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001071}
1072
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001073void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001074 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001075}
1076
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001077void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1078 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001079 assert(arr_length <= kRtpCsrcSize);
1080 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001081
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001082 for (int i = 0; i < arr_length; i++) {
1083 csrc_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001084 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001085 csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001086}
1087
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001088int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001089 assert(arr_of_csrc);
1090 CriticalSectionScoped cs(send_critsect_);
1091 for (int i = 0; i < csrcs_ && i < kRtpCsrcSize; i++) {
1092 arr_of_csrc[i] = csrc_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001093 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001094 return csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001095}
1096
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001097void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001098 CriticalSectionScoped cs(send_critsect_);
1099 sequence_number_forced_ = true;
1100 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001101}
1102
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001103uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001104 CriticalSectionScoped cs(send_critsect_);
1105 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001106}
1107
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001108// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001109int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1110 const uint16_t time_ms,
1111 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001112 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001113 return -1;
1114 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001115 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001116}
1117
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001118bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001119 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001120 return false;
1121 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001122 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001123}
1124
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001125int32_t RTPSender::SetAudioPacketSize(
1126 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001127 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001128 return -1;
1129 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001130 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001131}
1132
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001133int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1134 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001135 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001136 return -1;
1137 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001138 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001139}
1140
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001141int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1142 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001143 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001144}
1145
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001146int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001147 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001148}
1149
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001150int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001151 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001152 return -1;
1153 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001154 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001155}
1156
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001157int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001158 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001159 return -1;
1160 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001161 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001162}
1163
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001164// Video
1165VideoCodecInformation *RTPSender::CodecInformationVideo() {
1166 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001167 return NULL;
1168 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001169 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001170}
1171
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001172RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001173 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001174 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001175}
1176
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001177uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001178 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001179 return 0;
1180 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001181 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001182}
1183
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001184int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001185 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001186 return -1;
1187 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001188 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001189}
1190
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001191int32_t RTPSender::SetGenericFECStatus(
1192 const bool enable, const uint8_t payload_type_red,
1193 const uint8_t payload_type_fec) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001194 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001195 return -1;
1196 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001197 return video_->SetGenericFECStatus(enable, payload_type_red,
1198 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001199}
1200
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001201int32_t RTPSender::GenericFECStatus(
1202 bool *enable, uint8_t *payload_type_red,
1203 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001204 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001205 return -1;
1206 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001207 return video_->GenericFECStatus(
1208 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001209}
1210
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001211int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001212 const FecProtectionParams *delta_params,
1213 const FecProtectionParams *key_params) {
1214 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001215 return -1;
1216 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001217 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001218}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001219
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001220void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1221 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001222 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001223 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001224 // Add RTX header.
1225 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001226 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001227
1228 WebRtcRTPHeader rtp_header;
1229 rtp_parser.Parse(rtp_header);
1230
1231 // Add original RTP header.
1232 memcpy(data_buffer_rtx, buffer, rtp_header.header.headerLength);
1233
mflodman@webrtc.org9f5ebb52013-04-12 14:55:46 +00001234 // Replace payload type, if a specific type is set for RTX.
1235 if (payload_type_rtx_ != -1) {
1236 data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_);
1237 if (rtp_header.header.markerBit)
1238 data_buffer_rtx[1] |= kRtpMarkerBitMask;
1239 }
1240
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001241 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001242 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001243 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1244
1245 // Replace SSRC.
1246 ptr += 6;
1247 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1248
1249 // Add OSN (original sequence number).
1250 ptr = data_buffer_rtx + rtp_header.header.headerLength;
1251 ModuleRTPUtility::AssignUWord16ToBuffer(ptr,
1252 rtp_header.header.sequenceNumber);
1253 ptr += 2;
1254
1255 // Add original payload data.
1256 memcpy(ptr, buffer + rtp_header.header.headerLength,
1257 *length - rtp_header.header.headerLength);
1258 *length += 2;
1259}
1260
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001261} // namespace webrtc