blob: 0e481c752f1c1de2294fdd79b884377040d38d64 [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),
61 rtx_(kRtxOff) {
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
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000258void RTPSender::SetRTXStatus(const RtxMode mode, const bool set_ssrc,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000259 const uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000260 CriticalSectionScoped cs(send_critsect_);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000261 rtx_ = mode;
262 if (rtx_ != kRtxOff) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000263 if (set_ssrc) {
264 ssrc_rtx_ = ssrc;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000265 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000266 ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000267 }
268 }
269}
270
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000271void RTPSender::RTXStatus(RtxMode* mode, uint32_t *SSRC) 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_;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000274 *SSRC = ssrc_rtx_;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000275}
276
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000277int32_t RTPSender::CheckPayloadType(const int8_t payload_type,
278 RtpVideoCodecTypes *video_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000279 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000280
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000281 if (payload_type < 0) {
282 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)",
283 payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000284 return -1;
285 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000286 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000287 int8_t red_pl_type = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000288 if (audio_->RED(red_pl_type) == 0) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000289 // We have configured RED.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000290 if (red_pl_type == payload_type) {
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000291 // And it's a match...
292 return 0;
293 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000294 }
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000295 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000296 if (payload_type_ == payload_type) {
297 if (!audio_configured_) {
298 *video_type = video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +0000299 }
300 return 0;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000301 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000302 std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000303 payload_type_map_.find(payload_type);
304 if (it == payload_type_map_.end()) {
305 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
306 "\tpayloadType:%d not registered", payload_type);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000307 return -1;
308 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000309 payload_type_ = payload_type;
310 ModuleRTPUtility::Payload *payload = it->second;
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000311 assert(payload);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000312 if (!payload->audio && !audio_configured_) {
313 video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType);
314 *video_type = payload->typeSpecific.Video.videoCodecType;
315 video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate);
pwestin@webrtc.org00741872012-01-19 15:56:10 +0000316 }
317 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000318}
319
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000320int32_t RTPSender::SendOutgoingData(
321 const FrameType frame_type, const int8_t payload_type,
322 const uint32_t capture_timestamp, int64_t capture_time_ms,
323 const uint8_t *payload_data, const uint32_t payload_size,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000324 const RTPFragmentationHeader *fragmentation,
325 VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000326 TRACE_EVENT2("webrtc_rtp", "RTPSender::SendOutgoingData",
327 "timestsamp", capture_timestamp,
328 "frame_type", FrameTypeToString(frame_type));
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000329 {
330 // Drop this packet if we're not sending media packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000331 CriticalSectionScoped cs(send_critsect_);
332 if (!sending_media_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000333 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000334 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000335 }
pbos@webrtc.org8911ce42013-03-18 16:39:03 +0000336 RtpVideoCodecTypes video_type = kRtpGenericVideo;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000337 if (CheckPayloadType(payload_type, &video_type) != 0) {
338 WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_,
339 "%s invalid argument failed to find payload_type:%d",
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000340 __FUNCTION__, payload_type);
341 return -1;
342 }
343
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000344 if (audio_configured_) {
345 assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN ||
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000346 frame_type == kFrameEmpty);
347
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000348 return audio_->SendAudio(frame_type, payload_type, capture_timestamp,
349 payload_data, payload_size, fragmentation);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000350 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000351 assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000352
353 if (frame_type == kFrameEmpty) {
354 return SendPaddingAccordingToBitrate(payload_type, capture_timestamp,
355 capture_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000356 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000357 return video_->SendVideo(video_type, frame_type, payload_type,
358 capture_timestamp, capture_time_ms, payload_data,
359 payload_size, fragmentation, codec_info,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000360 rtp_type_hdr);
361 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000362}
363
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000364int32_t RTPSender::SendPaddingAccordingToBitrate(
365 int8_t payload_type, uint32_t capture_timestamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000366 int64_t capture_time_ms) {
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000367 // Current bitrate since last estimate(1 second) averaged with the
368 // estimate since then, to get the most up to date bitrate.
369 uint32_t current_bitrate = BitrateNow();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000370 int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000371 if (bitrate_diff <= 0) {
372 return 0;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000373 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000374 int bytes = 0;
375 if (current_bitrate == 0) {
376 // Start up phase. Send one 33.3 ms batch to start with.
377 bytes = (bitrate_diff / 8) / 30;
378 } else {
379 bytes = (bitrate_diff / 8);
380 // Cap at 200 ms of target send data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000381 int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000382 if (bytes > bytes_cap) {
383 bytes = bytes_cap;
384 }
385 }
386 return SendPadData(payload_type, capture_timestamp, capture_time_ms, bytes);
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000387}
388
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000389int32_t RTPSender::SendPadData(
390 int8_t payload_type, uint32_t capture_timestamp,
391 int64_t capture_time_ms, int32_t bytes) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000392 // Drop this packet if we're not sending media packets.
393 if (!sending_media_) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000394 return 0;
395 }
396 // Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
397 int max_length = 224;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000398 uint8_t data_buffer[IP_PACKET_SIZE];
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000399
400 for (; bytes > 0; bytes -= max_length) {
asapersson@webrtc.org63a34f42012-04-20 13:20:27 +0000401 int padding_bytes_in_packet = max_length;
402 if (bytes < max_length) {
403 padding_bytes_in_packet = (bytes + 16) & 0xffe0; // Keep our modulus 32.
404 }
405 if (padding_bytes_in_packet < 32) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000406 // Sanity don't send empty packets.
407 break;
asapersson@webrtc.org63a34f42012-04-20 13:20:27 +0000408 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000409 // Correct seq num, timestamp and payload type.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000410 int header_length = BuildRTPheader(
411 data_buffer, payload_type, false, // No markerbit.
412 capture_timestamp, true, // Timestamp provided.
413 true); // Increment sequence number.
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000414 data_buffer[0] |= 0x20; // Set padding bit.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000415 int32_t *data =
416 reinterpret_cast<int32_t *>(&(data_buffer[header_length]));
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000417
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000418 // Fill data buffer with random data.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000419 for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) {
420 data[j] = rand(); // NOLINT
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000421 }
422 // Set number of padding bytes in the last byte of the packet.
423 data_buffer[header_length + padding_bytes_in_packet - 1] =
424 padding_bytes_in_packet;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000425 // Send the packet.
426 if (0 > SendToNetwork(data_buffer, padding_bytes_in_packet, header_length,
427 capture_time_ms, kDontRetransmit)) {
pwestin@webrtc.org12d97f62012-01-05 10:54:44 +0000428 // Error sending the packet.
429 break;
430 }
431 }
432 if (bytes > 31) { // 31 due to our modulus 32.
433 // We did not manage to send all bytes.
434 return -1;
435 }
436 return 0;
437}
438
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000439void RTPSender::SetStorePacketsStatus(const bool enable,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000440 const uint16_t number_to_store) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000441 packet_history_->SetStorePacketsStatus(enable, number_to_store);
niklase@google.com470e71d2011-07-07 08:21:25 +0000442}
443
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000444bool RTPSender::StorePackets() const { return packet_history_->StorePackets(); }
niklase@google.com470e71d2011-07-07 08:21:25 +0000445
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000446int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) {
447 uint16_t length = IP_PACKET_SIZE;
448 uint8_t data_buffer[IP_PACKET_SIZE];
449 uint8_t *buffer_to_send_ptr = data_buffer;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000450
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000451 int64_t stored_time_in_ms;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000452 StorageType type;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000453 bool found = packet_history_->GetRTPPacket(packet_id, min_resend_time,
454 data_buffer, &length,
455 &stored_time_in_ms, &type);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000456 if (!found) {
457 // Packet not found.
asapersson@webrtc.org83ed0a42012-04-23 12:43:05 +0000458 return 0;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000459 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000460 if (length == 0 || type == kDontRetransmit) {
461 // No bytes copied (packet recently resent, skip resending) or
462 // packet should not be retransmitted.
463 return 0;
464 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000465 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000466 if (rtx_ != kRtxOff) {
467 BuildRtxPacket(data_buffer, &length, data_buffer_rtx);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000468 buffer_to_send_ptr = data_buffer_rtx;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000469 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000470
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000471 int32_t bytes_sent = ReSendToNetwork(buffer_to_send_ptr, length);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000472 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
473 WebRtcRTPHeader rtp_header;
474 rtp_parser.Parse(rtp_header);
475 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket",
476 "timestamp", rtp_header.header.timestamp,
477 "seqnum", rtp_header.header.sequenceNumber);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000478 if (bytes_sent <= 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000479 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000480 "Transport failed to resend packet_id %u", packet_id);
481 return -1;
482 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000483 // Store the time when the packet was last resent.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000484 packet_history_->UpdateResendTime(packet_id);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000485 return bytes_sent;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000486}
487
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000488int32_t RTPSender::ReSendToNetwork(const uint8_t *packet, const uint32_t size) {
489 int32_t bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000490 if (transport_) {
491 bytes_sent = transport_->SendPacket(id_, packet, size);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000492 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000493 if (bytes_sent <= 0) {
494 return -1;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000495 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000496 // Update send statistics.
497 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000498 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000499 packets_sent_++;
500 // We on purpose don't add to payload_bytes_sent_ since this is a
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000501 // re-transmit and not new payload data.
502 return bytes_sent;
niklase@google.com470e71d2011-07-07 08:21:25 +0000503}
504
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000505int RTPSender::SelectiveRetransmissions() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000506 if (!video_)
507 return -1;
508 return video_->SelectiveRetransmissions();
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000509}
510
511int RTPSender::SetSelectiveRetransmissions(uint8_t settings) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000512 if (!video_)
513 return -1;
514 return video_->SetSelectiveRetransmissions(settings);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000515}
516
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000517void RTPSender::OnReceivedNACK(
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000518 const std::list<uint16_t>& nack_sequence_numbers,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000519 const uint16_t avg_rtt) {
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000520 TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK",
521 "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000522 const int64_t now = clock_->TimeInMilliseconds();
523 uint32_t bytes_re_sent = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000524
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000525 // Enough bandwidth to send NACK?
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000526 if (!ProcessNACKBitRate(now)) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000527 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000528 "NACK bitrate reached. Skip sending NACK response. Target %d",
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000529 target_send_bitrate_);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000530 return;
531 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000532
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000533 for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin();
534 it != nack_sequence_numbers.end(); ++it) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000535 const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000536 if (bytes_sent > 0) {
537 bytes_re_sent += bytes_sent;
538 } else if (bytes_sent == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000539 // The packet has previously been resent.
540 // Try resending next packet in the list.
541 continue;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000542 } else if (bytes_sent < 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000543 // Failed to send one Sequence number. Give up the rest in this nack.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000544 WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_,
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000545 "Failed resending RTP packet %d, Discard rest of packets",
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000546 *it);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000547 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000548 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000549 // Delay bandwidth estimate (RTT * BW).
550 if (target_send_bitrate_ != 0 && avg_rtt) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000551 // kbits/s * ms = bits => bits/8 = bytes
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000552 uint32_t target_bytes =
553 (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000554 if (bytes_re_sent > target_bytes) {
555 break; // Ignore the rest of the packets in the list.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000556 }
557 }
558 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000559 if (bytes_re_sent > 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000560 // TODO(pwestin) consolidate these two methods.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000561 UpdateNACKBitRate(bytes_re_sent, now);
562 nack_bitrate_.Update(bytes_re_sent);
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000563 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000564}
565
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000566bool RTPSender::ProcessNACKBitRate(const uint32_t now) {
567 uint32_t num = 0;
568 int32_t byte_count = 0;
569 const uint32_t avg_interval = 1000;
niklase@google.com470e71d2011-07-07 08:21:25 +0000570
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000571 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000572
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000573 if (target_send_bitrate_ == 0) {
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000574 return true;
575 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000576 for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) {
577 if ((now - nack_byte_count_times_[num]) > avg_interval) {
578 // Don't use data older than 1sec.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000579 break;
580 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000581 byte_count += nack_byte_count_[num];
niklase@google.com470e71d2011-07-07 08:21:25 +0000582 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000583 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000584 int32_t time_interval = avg_interval;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000585 if (num == NACK_BYTECOUNT_SIZE) {
586 // More than NACK_BYTECOUNT_SIZE nack messages has been received
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000587 // during the last msg_interval.
588 time_interval = now - nack_byte_count_times_[num - 1];
589 if (time_interval < 0) {
590 time_interval = avg_interval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000591 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000592 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000593 return (byte_count * 8) < (target_send_bitrate_ * time_interval);
niklase@google.com470e71d2011-07-07 08:21:25 +0000594}
595
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000596void RTPSender::UpdateNACKBitRate(const uint32_t bytes,
597 const uint32_t now) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000598 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000599
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000600 // Save bitrate statistics.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000601 if (bytes > 0) {
602 if (now == 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000603 // Add padding length.
604 nack_byte_count_[0] += bytes;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000605 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000606 if (nack_byte_count_times_[0] == 0) {
607 // First no shift.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000608 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000609 // Shift.
610 for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) {
611 nack_byte_count_[i + 1] = nack_byte_count_[i];
612 nack_byte_count_times_[i + 1] = nack_byte_count_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000613 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000614 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000615 nack_byte_count_[0] = bytes;
616 nack_byte_count_times_[0] = now;
niklase@google.com470e71d2011-07-07 08:21:25 +0000617 }
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000618 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000619}
620
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000621void RTPSender::TimeToSendPacket(uint16_t sequence_number,
622 int64_t capture_time_ms) {
623 StorageType type;
624 uint16_t length = IP_PACKET_SIZE;
625 uint8_t data_buffer[IP_PACKET_SIZE];
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000626 int64_t stored_time_ms; // TODO(pwestin) can we deprecate this?
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000627
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000628 if (packet_history_ == NULL) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000629 return;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000630 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000631 if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length,
632 &stored_time_ms, &type)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000633 return;
634 }
635 assert(length > 0);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000636
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000637 ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000638 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000639 rtp_parser.Parse(rtp_header);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000640 TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket",
641 "timestamp", rtp_header.header.timestamp,
642 "seqnum", sequence_number);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000643
stefan@webrtc.org7da34592013-04-09 14:56:29 +0000644 int64_t diff_ms = clock_->TimeInMilliseconds() - capture_time_ms;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000645 if (UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms)) {
646 // Update stored packet in case of receiving a re-transmission request.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000647 packet_history_->ReplaceRTPHeader(data_buffer,
648 rtp_header.header.sequenceNumber,
649 rtp_header.header.headerLength);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000650 }
651 int bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000652 if (transport_) {
653 bytes_sent = transport_->SendPacket(id_, data_buffer, length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000654 }
655 if (bytes_sent <= 0) {
656 return;
657 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000658 // Update send statistics.
659 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000660 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000661 packets_sent_++;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000662 if (bytes_sent > rtp_header.header.headerLength) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000663 payload_bytes_sent_ += bytes_sent - rtp_header.header.headerLength;
phoglund@webrtc.orgbaaf2432012-05-31 10:47:35 +0000664 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000665}
666
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000667// TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000668int32_t RTPSender::SendToNetwork(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000669 uint8_t *buffer, int payload_length, int rtp_header_length,
670 int64_t capture_time_ms, StorageType storage) {
671 ModuleRTPUtility::RTPHeaderParser rtp_parser(
672 buffer, payload_length + rtp_header_length);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000673 WebRtcRTPHeader rtp_header;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000674 rtp_parser.Parse(rtp_header);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000675
stefan@webrtc.org715faaf2012-08-28 15:20:39 +0000676 // |capture_time_ms| <= 0 is considered invalid.
677 // TODO(holmer): This should be changed all over Video Engine so that negative
678 // time is consider invalid, while 0 is considered a valid time.
679 if (capture_time_ms > 0) {
stefan@webrtc.org7da34592013-04-09 14:56:29 +0000680 int64_t time_now = clock_->TimeInMilliseconds();
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000681 UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length,
stefan@webrtc.org7da34592013-04-09 14:56:29 +0000682 rtp_header, time_now - capture_time_ms);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000683 }
684 // Used for NACK and to spread out the transmission of packets.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000685 if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length,
686 max_payload_length_, capture_time_ms,
687 storage) != 0) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000688 return -1;
689 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000690
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000691 int32_t bytes_sent = -1;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000692 // Create and send RTX Packet.
693 if (rtx_ == kRtxAll && storage == kAllowRetransmission) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000694 uint16_t length_rtx = payload_length + rtp_header_length;
695 uint8_t data_buffer_rtx[IP_PACKET_SIZE];
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000696 BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx);
697 if (transport_) {
698 bytes_sent += transport_->SendPacket(id_, data_buffer_rtx, length_rtx);
699 if (bytes_sent <= 0) {
700 return -1;
701 }
702 }
703 }
704
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000705 if (paced_sender_ && storage != kDontStore) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000706 if (!paced_sender_->SendPacket(
707 PacedSender::kNormalPriority, rtp_header.header.ssrc,
708 rtp_header.header.sequenceNumber, capture_time_ms,
709 payload_length + rtp_header_length)) {
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000710 // We can't send the packet right now.
711 // We will be called when it is time.
712 return payload_length + rtp_header_length;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000713 }
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000714 }
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000715 // Send data packet.
716 bytes_sent = -1;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000717 if (transport_) {
718 bytes_sent = transport_->SendPacket(id_, buffer,
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000719 payload_length + rtp_header_length);
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000720 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000721 if (bytes_sent <= 0) {
722 return -1;
723 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000724 // Update send statistics.
725 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000726 Bitrate::Update(bytes_sent);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000727 packets_sent_++;
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000728 if (bytes_sent > rtp_header_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000729 payload_bytes_sent_ += bytes_sent - rtp_header_length;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000730 }
731 return 0;
stefan@webrtc.org6a4bef42011-12-22 12:52:41 +0000732}
733
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000734void RTPSender::ProcessBitrate() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000735 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000736 Bitrate::Process();
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000737 nack_bitrate_.Process();
738 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000739 return;
740 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000741 video_->ProcessBitrate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000742}
743
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000744uint16_t RTPSender::RTPHeaderLength() const {
745 uint16_t rtp_header_length = 12;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000746 if (include_csrcs_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000747 rtp_header_length += sizeof(uint32_t) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000748 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000749 rtp_header_length += RtpHeaderExtensionTotalLength();
750 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000751}
752
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000753uint16_t RTPSender::IncrementSequenceNumber() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000754 CriticalSectionScoped cs(send_critsect_);
755 return sequence_number_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000756}
757
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000758void RTPSender::ResetDataCounters() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000759 packets_sent_ = 0;
760 payload_bytes_sent_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000761}
762
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000763uint32_t RTPSender::Packets() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000764 // Don't use critsect to avoid potential deadlock.
765 return packets_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000766}
767
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000768// Number of sent RTP bytes.
769// Don't use critsect to avoid potental deadlock.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000770uint32_t RTPSender::Bytes() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000771 return payload_bytes_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000772}
773
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000774int32_t RTPSender::BuildRTPheader(
775 uint8_t *data_buffer, const int8_t payload_type,
776 const bool marker_bit, const uint32_t capture_time_stamp,
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000777 const bool time_stamp_provided, const bool inc_sequence_number) {
778 assert(payload_type >= 0);
779 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000780
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000781 data_buffer[0] = static_cast<uint8_t>(0x80); // version 2.
782 data_buffer[1] = static_cast<uint8_t>(payload_type);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000783 if (marker_bit) {
784 data_buffer[1] |= kRtpMarkerBitMask; // Marker bit is set.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000785 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000786 if (time_stamp_provided) {
787 time_stamp_ = start_time_stamp_ + capture_time_stamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000788 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000789 // Make a unique time stamp.
790 // We can't inc by the actual time, since then we increase the risk of back
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000791 // timing.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000792 time_stamp_++;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000793 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000794 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + 2, sequence_number_);
795 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 4, time_stamp_);
796 ModuleRTPUtility::AssignUWord32ToBuffer(data_buffer + 8, ssrc_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000797 int32_t rtp_header_length = 12;
niklase@google.com470e71d2011-07-07 08:21:25 +0000798
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000799 // Add the CSRCs if any.
800 if (include_csrcs_ && csrcs_ > 0) {
801 if (csrcs_ > kRtpCsrcSize) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000802 // error
803 assert(false);
804 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000805 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000806 uint8_t *ptr = &data_buffer[rtp_header_length];
807 for (uint32_t i = 0; i < csrcs_; ++i) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000808 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrc_[i]);
809 ptr += 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000810 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000811 data_buffer[0] = (data_buffer[0] & 0xf0) | csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000812
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000813 // Update length of header.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000814 rtp_header_length += sizeof(uint32_t) * csrcs_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000815 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000816 sequence_number_++; // Prepare for next packet.
niklase@google.com470e71d2011-07-07 08:21:25 +0000817
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000818 uint16_t len = BuildRTPHeaderExtension(data_buffer + rtp_header_length);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000819 if (len) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000820 data_buffer[0] |= 0x10; // Set extension bit.
821 rtp_header_length += len;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000822 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000823 return rtp_header_length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000824}
825
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000826uint16_t RTPSender::BuildRTPHeaderExtension(
827 uint8_t *data_buffer) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000828 if (rtp_header_extension_map_.Size() <= 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000829 return 0;
830 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000831 // RTP header extension, RFC 3550.
832 // 0 1 2 3
833 // 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
834 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
835 // | defined by profile | length |
836 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
837 // | header extension |
838 // | .... |
839 //
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000840 const uint32_t kPosLength = 2;
841 const uint32_t kHeaderLength = RTP_ONE_BYTE_HEADER_LENGTH_IN_BYTES;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000842
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000843 // Add extension ID (0xBEDE).
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000844 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000845 RTP_ONE_BYTE_HEADER_EXTENSION);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000846
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000847 // Add extensions.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000848 uint16_t total_block_length = 0;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000849
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000850 RTPExtensionType type = rtp_header_extension_map_.First();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000851 while (type != kRtpExtensionNone) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000852 uint8_t block_length = 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000853 if (type == kRtpExtensionTransmissionTimeOffset) {
854 block_length = BuildTransmissionTimeOffsetExtension(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000855 data_buffer + kHeaderLength + total_block_length);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000856 }
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000857 total_block_length += block_length;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000858 type = rtp_header_extension_map_.Next(type);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000859 }
860 if (total_block_length == 0) {
861 // No extension added.
862 return 0;
863 }
864 // Set header length (in number of Word32, header excluded).
865 assert(total_block_length % 4 == 0);
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000866 ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000867 total_block_length / 4);
868 // Total added length.
869 return kHeaderLength + total_block_length;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000870}
871
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000872uint8_t RTPSender::BuildTransmissionTimeOffsetExtension(
873 uint8_t* data_buffer) const {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000874 // From RFC 5450: Transmission Time Offsets in RTP Streams.
875 //
876 // The transmission time is signaled to the receiver in-band using the
877 // general mechanism for RTP header extensions [RFC5285]. The payload
878 // of this extension (the transmitted value) is a 24-bit signed integer.
879 // When added to the RTP timestamp of the packet, it represents the
880 // "effective" RTP transmission time of the packet, on the RTP
881 // timescale.
882 //
883 // The form of the transmission offset extension block:
884 //
885 // 0 1 2 3
886 // 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
887 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
888 // | ID | len=2 | transmission offset |
889 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000890
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000891 // Get id defined by user.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000892 uint8_t id;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000893 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
894 &id) != 0) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000895 // Not registered.
896 return 0;
897 }
898 int pos = 0;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000899 const uint8_t len = 2;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000900 data_buffer[pos++] = (id << 4) + len;
901 ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos,
902 transmission_time_offset_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000903 pos += 3;
904 assert(pos == TRANSMISSION_TIME_OFFSET_LENGTH_IN_BYTES);
905 return TRANSMISSION_TIME_OFFSET_LENGTH_IN_BYTES;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000906}
907
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000908bool RTPSender::UpdateTransmissionTimeOffset(
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000909 uint8_t *rtp_packet, const uint16_t rtp_packet_length,
910 const WebRtcRTPHeader &rtp_header, const int64_t time_diff_ms) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000911 CriticalSectionScoped cs(send_critsect_);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000912
913 // Get length until start of transmission block.
914 int transmission_block_pos =
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000915 rtp_header_extension_map_.GetLengthUntilBlockStartInBytes(
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000916 kRtpExtensionTransmissionTimeOffset);
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000917 if (transmission_block_pos < 0) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000918 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000919 "Failed to update transmission time offset, not registered.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000920 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000921 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000922 int block_pos = 12 + rtp_header.header.numCSRCs + transmission_block_pos;
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +0000923 if (rtp_packet_length < block_pos + 4 ||
924 rtp_header.header.headerLength < block_pos + 4) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000925 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000926 "Failed to update transmission time offset, invalid length.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000927 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000928 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000929 // Verify that header contains extension.
930 if (!((rtp_packet[12 + rtp_header.header.numCSRCs] == 0xBE) &&
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000931 (rtp_packet[12 + rtp_header.header.numCSRCs + 1] == 0xDE))) {
932 WEBRTC_TRACE(
933 kTraceStream, kTraceRtpRtcp, id_,
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000934 "Failed to update transmission time offset, hdr extension not found.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000935 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000936 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000937 // Get id.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000938 uint8_t id = 0;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000939 if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset,
940 &id) != 0) {
941 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000942 "Failed to update transmission time offset, no id.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000943 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000944 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000945 // Verify first byte in block.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000946 const uint8_t first_block_byte = (id << 4) + 2;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000947 if (rtp_packet[block_pos] != first_block_byte) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000948 WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_,
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000949 "Failed to update transmission time offset.");
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000950 return false;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000951 }
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000952 // Update transmission offset field.
953 ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1,
mflodman@webrtc.orgba853c92012-08-10 14:30:53 +0000954 time_diff_ms * 90); // RTP timestamp.
asapersson@webrtc.orge5b49a02012-11-06 13:09:39 +0000955 return true;
asapersson@webrtc.org0b3c35a2012-01-16 11:06:31 +0000956}
957
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000958void RTPSender::SetSendingStatus(const bool enabled) {
959 if (enabled) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000960 uint32_t frequency_hz;
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000961 if (audio_configured_) {
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000962 uint32_t frequency = audio_->AudioFrequency();
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000963
964 // sanity
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000965 switch (frequency) {
966 case 8000:
967 case 12000:
968 case 16000:
969 case 24000:
970 case 32000:
971 break;
972 default:
973 assert(false);
974 return;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000975 }
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000976 frequency_hz = frequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000977 } else {
phoglund@webrtc.orgc38eef82013-01-07 10:18:30 +0000978 frequency_hz = kDefaultVideoFrequency;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000979 }
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000980 uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000981
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000982 // Will be ignored if it's already configured via API.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000983 SetStartTimestamp(RTPtime, false);
984 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000985 if (!ssrc_forced_) {
986 // Generate a new SSRC.
987 ssrc_db_.ReturnSSRC(ssrc_);
988 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000989 }
990 // Don't initialize seq number if SSRC passed externally.
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +0000991 if (!sequence_number_forced_ && !ssrc_forced_) {
992 // Generate a new sequence number.
993 sequence_number_ =
994 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +0000995 }
996 }
997}
998
999void RTPSender::SetSendingMediaStatus(const bool enabled) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001000 CriticalSectionScoped cs(send_critsect_);
1001 sending_media_ = enabled;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001002}
1003
1004bool RTPSender::SendingMedia() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001005 CriticalSectionScoped cs(send_critsect_);
1006 return sending_media_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001007}
1008
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001009uint32_t RTPSender::Timestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001010 CriticalSectionScoped cs(send_critsect_);
1011 return time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001012}
1013
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001014void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001015 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001016 if (force) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001017 start_time_stamp_forced_ = force;
1018 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001019 } else {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001020 if (!start_time_stamp_forced_) {
1021 start_time_stamp_ = timestamp;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001022 }
1023 }
1024}
1025
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001026uint32_t RTPSender::StartTimestamp() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001027 CriticalSectionScoped cs(send_critsect_);
1028 return start_time_stamp_;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001029}
1030
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001031uint32_t RTPSender::GenerateNewSSRC() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001032 // If configured via API, return 0.
1033 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001034
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001035 if (ssrc_forced_) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001036 return 0;
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001037 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001038 ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0.
1039 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001040}
1041
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001042void RTPSender::SetSSRC(uint32_t ssrc) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001043 // This is configured via the API.
1044 CriticalSectionScoped cs(send_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001045
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001046 if (ssrc_ == ssrc && ssrc_forced_) {
1047 return; // Since it's same ssrc, don't reset anything.
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001048 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001049 ssrc_forced_ = true;
1050 ssrc_db_.ReturnSSRC(ssrc_);
1051 ssrc_db_.RegisterSSRC(ssrc);
1052 ssrc_ = ssrc;
1053 if (!sequence_number_forced_) {
1054 sequence_number_ =
1055 rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001056 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001057}
1058
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001059uint32_t RTPSender::SSRC() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001060 CriticalSectionScoped cs(send_critsect_);
1061 return ssrc_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001062}
1063
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001064void RTPSender::SetCSRCStatus(const bool include) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001065 include_csrcs_ = include;
niklase@google.com470e71d2011-07-07 08:21:25 +00001066}
1067
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001068void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
1069 const uint8_t arr_length) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001070 assert(arr_length <= kRtpCsrcSize);
1071 CriticalSectionScoped cs(send_critsect_);
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001072
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001073 for (int i = 0; i < arr_length; i++) {
1074 csrc_[i] = arr_of_csrc[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001075 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001076 csrcs_ = arr_length;
niklase@google.com470e71d2011-07-07 08:21:25 +00001077}
1078
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001079int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001080 assert(arr_of_csrc);
1081 CriticalSectionScoped cs(send_critsect_);
1082 for (int i = 0; i < csrcs_ && i < kRtpCsrcSize; i++) {
1083 arr_of_csrc[i] = csrc_[i];
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001084 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001085 return csrcs_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001086}
1087
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001088void RTPSender::SetSequenceNumber(uint16_t seq) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001089 CriticalSectionScoped cs(send_critsect_);
1090 sequence_number_forced_ = true;
1091 sequence_number_ = seq;
niklase@google.com470e71d2011-07-07 08:21:25 +00001092}
1093
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001094uint16_t RTPSender::SequenceNumber() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001095 CriticalSectionScoped cs(send_critsect_);
1096 return sequence_number_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001097}
1098
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001099// Audio.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001100int32_t RTPSender::SendTelephoneEvent(const uint8_t key,
1101 const uint16_t time_ms,
1102 const uint8_t level) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001103 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001104 return -1;
1105 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001106 return audio_->SendTelephoneEvent(key, time_ms, level);
niklase@google.com470e71d2011-07-07 08:21:25 +00001107}
1108
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001109bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001110 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001111 return false;
1112 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001113 return audio_->SendTelephoneEventActive(*telephone_event);
niklase@google.com470e71d2011-07-07 08:21:25 +00001114}
1115
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001116int32_t RTPSender::SetAudioPacketSize(
1117 const uint16_t packet_size_samples) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001118 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001119 return -1;
1120 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001121 return audio_->SetAudioPacketSize(packet_size_samples);
niklase@google.com470e71d2011-07-07 08:21:25 +00001122}
1123
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001124int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable,
1125 const uint8_t ID) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001126 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001127 return -1;
1128 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001129 return audio_->SetAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00001130}
1131
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001132int32_t RTPSender::AudioLevelIndicationStatus(bool *enable,
1133 uint8_t* id) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001134 return audio_->AudioLevelIndicationStatus(*enable, *id);
niklase@google.com470e71d2011-07-07 08:21:25 +00001135}
1136
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001137int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001138 return audio_->SetAudioLevel(level_d_bov);
niklase@google.com470e71d2011-07-07 08:21:25 +00001139}
1140
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001141int32_t RTPSender::SetRED(const int8_t payload_type) {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001142 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001143 return -1;
1144 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001145 return audio_->SetRED(payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001146}
1147
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001148int32_t RTPSender::RED(int8_t *payload_type) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001149 if (!audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001150 return -1;
1151 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001152 return audio_->RED(*payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +00001153}
1154
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001155// Video
1156VideoCodecInformation *RTPSender::CodecInformationVideo() {
1157 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001158 return NULL;
1159 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001160 return video_->CodecInformationVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001161}
1162
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001163RtpVideoCodecTypes RTPSender::VideoCodecType() const {
pbos@webrtc.org8911ce42013-03-18 16:39:03 +00001164 assert(!audio_configured_ && "Sender is an audio stream!");
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001165 return video_->VideoCodecType();
niklase@google.com470e71d2011-07-07 08:21:25 +00001166}
1167
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001168uint32_t RTPSender::MaxConfiguredBitrateVideo() const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001169 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001170 return 0;
1171 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001172 return video_->MaxConfiguredBitrateVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +00001173}
1174
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001175int32_t RTPSender::SendRTPIntraRequest() {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001176 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001177 return -1;
1178 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001179 return video_->SendRTPIntraRequest();
niklase@google.com470e71d2011-07-07 08:21:25 +00001180}
1181
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001182int32_t RTPSender::SetGenericFECStatus(
1183 const bool enable, const uint8_t payload_type_red,
1184 const uint8_t payload_type_fec) {
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_->SetGenericFECStatus(enable, payload_type_red,
1189 payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001190}
1191
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001192int32_t RTPSender::GenericFECStatus(
1193 bool *enable, uint8_t *payload_type_red,
1194 uint8_t *payload_type_fec) const {
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001195 if (audio_configured_) {
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001196 return -1;
1197 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001198 return video_->GenericFECStatus(
1199 *enable, *payload_type_red, *payload_type_fec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001200}
1201
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001202int32_t RTPSender::SetFecParameters(
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001203 const FecProtectionParams *delta_params,
1204 const FecProtectionParams *key_params) {
1205 if (audio_configured_) {
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001206 return -1;
1207 }
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001208 return video_->SetFecParameters(delta_params, key_params);
marpan@google.com80c5d7a2011-07-15 21:32:40 +00001209}
phoglund@webrtc.org43da54a2013-01-25 10:53:38 +00001210
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001211void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length,
1212 uint8_t* buffer_rtx) {
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001213 CriticalSectionScoped cs(send_critsect_);
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001214 uint8_t* data_buffer_rtx = buffer_rtx;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001215 // Add RTX header.
1216 ModuleRTPUtility::RTPHeaderParser rtp_parser(
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001217 reinterpret_cast<const uint8_t *>(buffer), *length);
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001218
1219 WebRtcRTPHeader rtp_header;
1220 rtp_parser.Parse(rtp_header);
1221
1222 // Add original RTP header.
1223 memcpy(data_buffer_rtx, buffer, rtp_header.header.headerLength);
1224
1225 // Replace sequence number.
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001226 uint8_t *ptr = data_buffer_rtx + 2;
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +00001227 ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++);
1228
1229 // Replace SSRC.
1230 ptr += 6;
1231 ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_);
1232
1233 // Add OSN (original sequence number).
1234 ptr = data_buffer_rtx + rtp_header.header.headerLength;
1235 ModuleRTPUtility::AssignUWord16ToBuffer(ptr,
1236 rtp_header.header.sequenceNumber);
1237 ptr += 2;
1238
1239 // Add original payload data.
1240 memcpy(ptr, buffer + rtp_header.header.headerLength,
1241 *length - rtp_header.header.headerLength);
1242 *length += 2;
1243}
1244
pwestin@webrtc.orgc66e8b32012-11-07 17:01:04 +00001245} // namespace webrtc