niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/rtp_rtcp/source/rtp_sender.h" |
| 12 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 13 | #include <cstdlib> // srand |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 15 | #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.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 21 | #include "webrtc/system_wrappers/interface/trace_event.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 25 | // Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP. |
| 26 | const int kMaxPaddingLength = 224; |
| 27 | |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | const char* FrameTypeToString(const FrameType frame_type) { |
| 31 | switch (frame_type) { |
| 32 | case kFrameEmpty: return "empty"; |
| 33 | case kAudioFrameSpeech: return "audio_speech"; |
| 34 | case kAudioFrameCN: return "audio_cn"; |
| 35 | case kVideoFrameKey: return "video_key"; |
| 36 | case kVideoFrameDelta: return "video_delta"; |
| 37 | case kVideoFrameGolden: return "video_golden"; |
| 38 | case kVideoFrameAltRef: return "video_altref"; |
| 39 | } |
| 40 | return ""; |
| 41 | } |
| 42 | |
| 43 | } // namespace |
| 44 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 45 | RTPSender::RTPSender(const int32_t id, const bool audio, Clock *clock, |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 46 | Transport *transport, RtpAudioFeedback *audio_feedback, |
| 47 | PacedSender *paced_sender) |
| 48 | : Bitrate(clock), id_(id), audio_configured_(audio), audio_(NULL), |
| 49 | video_(NULL), paced_sender_(paced_sender), |
| 50 | send_critsect_(CriticalSectionWrapper::CreateCriticalSection()), |
| 51 | transport_(transport), sending_media_(true), // Default to sending media. |
| 52 | max_payload_length_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP. |
| 53 | target_send_bitrate_(0), packet_over_head_(28), payload_type_(-1), |
| 54 | payload_type_map_(), rtp_header_extension_map_(), |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 55 | transmission_time_offset_(0), absolute_send_time_(0), |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 56 | // NACK. |
| 57 | nack_byte_count_times_(), nack_byte_count_(), nack_bitrate_(clock), |
| 58 | packet_history_(new RTPPacketHistory(clock)), |
| 59 | // Statistics |
| 60 | packets_sent_(0), payload_bytes_sent_(0), start_time_stamp_forced_(false), |
| 61 | start_time_stamp_(0), ssrc_db_(*SSRCDatabase::GetSSRCDatabase()), |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 62 | remote_ssrc_(0), sequence_number_forced_(false), ssrc_forced_(false), |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 63 | timestamp_(0), num_csrcs_(0), csrcs_(), include_csrcs_(true), |
mflodman@webrtc.org | 9f5ebb5 | 2013-04-12 14:55:46 +0000 | [diff] [blame] | 64 | rtx_(kRtxOff), payload_type_rtx_(-1) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 65 | memset(nack_byte_count_times_, 0, sizeof(nack_byte_count_times_)); |
| 66 | memset(nack_byte_count_, 0, sizeof(nack_byte_count_)); |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 67 | memset(csrcs_, 0, sizeof(csrcs_)); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 68 | // We need to seed the random generator. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 69 | srand(static_cast<uint32_t>(clock_->TimeInMilliseconds())); |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 70 | ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0. |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 71 | ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0. |
| 72 | // Random start, 16 bits. Can't be 0. |
| 73 | sequence_number_rtx_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF; |
| 74 | sequence_number_ = static_cast<uint16_t>(rand() + 1) & 0x7FFF; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 76 | if (audio) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 77 | audio_ = new RTPSenderAudio(id, clock_, this); |
| 78 | audio_->RegisterAudioCallback(audio_feedback); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 79 | } else { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 80 | video_ = new RTPSenderVideo(id, clock_, this); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 81 | } |
| 82 | WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | } |
| 84 | |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 85 | RTPSender::~RTPSender() { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 86 | if (remote_ssrc_ != 0) { |
| 87 | ssrc_db_.ReturnSSRC(remote_ssrc_); |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 88 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 89 | ssrc_db_.ReturnSSRC(ssrc_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 91 | SSRCDatabase::ReturnSSRCDatabase(); |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 92 | delete send_critsect_; |
| 93 | while (!payload_type_map_.empty()) { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 94 | std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it = |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 95 | payload_type_map_.begin(); |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 96 | delete it->second; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 97 | payload_type_map_.erase(it); |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 98 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 99 | delete packet_history_; |
| 100 | delete audio_; |
| 101 | delete video_; |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 102 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 103 | WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id_, "%s deleted", __FUNCTION__); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 106 | void RTPSender::SetTargetSendBitrate(const uint32_t bits) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 107 | target_send_bitrate_ = static_cast<uint16_t>(bits / 1000); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | } |
stefan@webrtc.org | d0bdab0 | 2011-10-14 14:24:54 +0000 | [diff] [blame] | 109 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 110 | uint16_t RTPSender::ActualSendBitrateKbit() const { |
| 111 | return (uint16_t)(Bitrate::BitrateNow() / 1000); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | } |
| 113 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 114 | uint32_t RTPSender::VideoBitrateSent() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 115 | if (video_) { |
| 116 | return video_->VideoBitrateSent(); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 117 | } |
| 118 | return 0; |
stefan@webrtc.org | fbea4e5 | 2011-10-27 16:08:29 +0000 | [diff] [blame] | 119 | } |
| 120 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 121 | uint32_t RTPSender::FecOverheadRate() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 122 | if (video_) { |
| 123 | return video_->FecOverheadRate(); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 124 | } |
| 125 | return 0; |
stefan@webrtc.org | d0bdab0 | 2011-10-14 14:24:54 +0000 | [diff] [blame] | 126 | } |
| 127 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 128 | uint32_t RTPSender::NackOverheadRate() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 129 | return nack_bitrate_.BitrateLast(); |
stefan@webrtc.org | d0bdab0 | 2011-10-14 14:24:54 +0000 | [diff] [blame] | 130 | } |
| 131 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 132 | int32_t RTPSender::SetTransmissionTimeOffset( |
| 133 | const int32_t transmission_time_offset) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 134 | if (transmission_time_offset > (0x800000 - 1) || |
| 135 | transmission_time_offset < -(0x800000 - 1)) { // Word24. |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 136 | return -1; |
| 137 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 138 | CriticalSectionScoped cs(send_critsect_); |
| 139 | transmission_time_offset_ = transmission_time_offset; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 140 | return 0; |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 141 | } |
| 142 | |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 143 | int32_t RTPSender::SetAbsoluteSendTime( |
| 144 | const uint32_t absolute_send_time) { |
| 145 | if (absolute_send_time > 0xffffff) { // UWord24. |
| 146 | return -1; |
| 147 | } |
| 148 | CriticalSectionScoped cs(send_critsect_); |
| 149 | absolute_send_time_ = absolute_send_time; |
| 150 | return 0; |
| 151 | } |
| 152 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 153 | int32_t RTPSender::RegisterRtpHeaderExtension(const RTPExtensionType type, |
| 154 | const uint8_t id) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 155 | CriticalSectionScoped cs(send_critsect_); |
| 156 | return rtp_header_extension_map_.Register(type, id); |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 157 | } |
| 158 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 159 | int32_t RTPSender::DeregisterRtpHeaderExtension( |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 160 | const RTPExtensionType type) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 161 | CriticalSectionScoped cs(send_critsect_); |
| 162 | return rtp_header_extension_map_.Deregister(type); |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 163 | } |
| 164 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 165 | uint16_t RTPSender::RtpHeaderExtensionTotalLength() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 166 | CriticalSectionScoped cs(send_critsect_); |
| 167 | return rtp_header_extension_map_.GetTotalLengthInBytes(); |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 168 | } |
| 169 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 170 | int32_t RTPSender::RegisterPayload( |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 171 | const char payload_name[RTP_PAYLOAD_NAME_SIZE], |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 172 | const int8_t payload_number, const uint32_t frequency, |
| 173 | const uint8_t channels, const uint32_t rate) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 174 | assert(payload_name); |
| 175 | CriticalSectionScoped cs(send_critsect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 177 | std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it = |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 178 | payload_type_map_.find(payload_number); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 180 | if (payload_type_map_.end() != it) { |
| 181 | // We already use this payload type. |
| 182 | ModuleRTPUtility::Payload *payload = it->second; |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 183 | assert(payload); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 185 | // Check if it's the same as we already have. |
| 186 | if (ModuleRTPUtility::StringCompare(payload->name, payload_name, |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 187 | RTP_PAYLOAD_NAME_SIZE - 1)) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 188 | if (audio_configured_ && payload->audio && |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 189 | payload->typeSpecific.Audio.frequency == frequency && |
| 190 | (payload->typeSpecific.Audio.rate == rate || |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 191 | payload->typeSpecific.Audio.rate == 0 || rate == 0)) { |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 192 | payload->typeSpecific.Audio.rate = rate; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 193 | // Ensure that we update the rate if new or old is zero. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 194 | return 0; |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 195 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 196 | if (!audio_configured_ && !payload->audio) { |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 197 | return 0; |
| 198 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 199 | } |
| 200 | return -1; |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 201 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 202 | int32_t ret_val = -1; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 203 | ModuleRTPUtility::Payload *payload = NULL; |
| 204 | if (audio_configured_) { |
| 205 | ret_val = audio_->RegisterAudioPayload(payload_name, payload_number, |
| 206 | frequency, channels, rate, payload); |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 207 | } else { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 208 | ret_val = video_->RegisterVideoPayload(payload_name, payload_number, rate, |
| 209 | payload); |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 210 | } |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 211 | if (payload) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 212 | payload_type_map_[payload_number] = payload; |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 213 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 214 | return ret_val; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 215 | } |
| 216 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 217 | int32_t RTPSender::DeRegisterSendPayload( |
| 218 | const int8_t payload_type) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 219 | CriticalSectionScoped lock(send_critsect_); |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 220 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 221 | std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it = |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 222 | payload_type_map_.find(payload_type); |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 223 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 224 | if (payload_type_map_.end() == it) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 225 | return -1; |
| 226 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 227 | ModuleRTPUtility::Payload *payload = it->second; |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 228 | delete payload; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 229 | payload_type_map_.erase(it); |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 230 | return 0; |
| 231 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 233 | int8_t RTPSender::SendPayloadType() const { return payload_type_; } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 234 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 235 | int RTPSender::SendPayloadFrequency() const { return audio_->AudioFrequency(); } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 236 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 237 | int32_t RTPSender::SetMaxPayloadLength( |
| 238 | const uint16_t max_payload_length, |
| 239 | const uint16_t packet_over_head) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 240 | // Sanity check. |
| 241 | if (max_payload_length < 100 || max_payload_length > IP_PACKET_SIZE) { |
| 242 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "%s invalid argument", |
| 243 | __FUNCTION__); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 244 | return -1; |
| 245 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 246 | CriticalSectionScoped cs(send_critsect_); |
| 247 | max_payload_length_ = max_payload_length; |
| 248 | packet_over_head_ = packet_over_head; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 249 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 250 | WEBRTC_TRACE(kTraceInfo, kTraceRtpRtcp, id_, "SetMaxPayloadLength to %d.", |
| 251 | max_payload_length); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 252 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 253 | } |
| 254 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 255 | uint16_t RTPSender::MaxDataPayloadLength() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 256 | if (audio_configured_) { |
| 257 | return max_payload_length_ - RTPHeaderLength(); |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 258 | } else { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 259 | return max_payload_length_ - RTPHeaderLength() - |
| 260 | video_->FECPacketOverhead() - ((rtx_) ? 2 : 0); |
| 261 | // Include the FEC/ULP/RED overhead. |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 262 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 263 | } |
| 264 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 265 | uint16_t RTPSender::MaxPayloadLength() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 266 | return max_payload_length_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 267 | } |
| 268 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 269 | uint16_t RTPSender::PacketOverHead() const { return packet_over_head_; } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 270 | |
mflodman@webrtc.org | 9f5ebb5 | 2013-04-12 14:55:46 +0000 | [diff] [blame] | 271 | void RTPSender::SetRTXStatus(RtxMode mode, bool set_ssrc, uint32_t ssrc) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 272 | CriticalSectionScoped cs(send_critsect_); |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 273 | rtx_ = mode; |
| 274 | if (rtx_ != kRtxOff) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 275 | if (set_ssrc) { |
| 276 | ssrc_rtx_ = ssrc; |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 277 | } else { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 278 | ssrc_rtx_ = ssrc_db_.CreateSSRC(); // Can't be 0. |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
mflodman@webrtc.org | 9f5ebb5 | 2013-04-12 14:55:46 +0000 | [diff] [blame] | 283 | void RTPSender::RTXStatus(RtxMode* mode, uint32_t* ssrc, |
| 284 | int* payload_type) const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 285 | CriticalSectionScoped cs(send_critsect_); |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 286 | *mode = rtx_; |
mflodman@webrtc.org | 9f5ebb5 | 2013-04-12 14:55:46 +0000 | [diff] [blame] | 287 | *ssrc = ssrc_rtx_; |
| 288 | *payload_type = payload_type_rtx_; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | void RTPSender::SetRtxPayloadType(int payload_type) { |
| 293 | CriticalSectionScoped cs(send_critsect_); |
| 294 | payload_type_rtx_ = payload_type; |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 295 | } |
| 296 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 297 | int32_t RTPSender::CheckPayloadType(const int8_t payload_type, |
| 298 | RtpVideoCodecTypes *video_type) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 299 | CriticalSectionScoped cs(send_critsect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 300 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 301 | if (payload_type < 0) { |
| 302 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, "\tinvalid payload_type (%d)", |
| 303 | payload_type); |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 304 | return -1; |
| 305 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 306 | if (audio_configured_) { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 307 | int8_t red_pl_type = -1; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 308 | if (audio_->RED(red_pl_type) == 0) { |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 309 | // We have configured RED. |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 310 | if (red_pl_type == payload_type) { |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 311 | // And it's a match... |
| 312 | return 0; |
| 313 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 314 | } |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 315 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 316 | if (payload_type_ == payload_type) { |
| 317 | if (!audio_configured_) { |
| 318 | *video_type = video_->VideoCodecType(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 319 | } |
| 320 | return 0; |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 321 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 322 | std::map<int8_t, ModuleRTPUtility::Payload *>::iterator it = |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 323 | payload_type_map_.find(payload_type); |
| 324 | if (it == payload_type_map_.end()) { |
| 325 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, |
| 326 | "\tpayloadType:%d not registered", payload_type); |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 327 | return -1; |
| 328 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 329 | payload_type_ = payload_type; |
| 330 | ModuleRTPUtility::Payload *payload = it->second; |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 331 | assert(payload); |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 332 | if (!payload->audio && !audio_configured_) { |
| 333 | video_->SetVideoCodecType(payload->typeSpecific.Video.videoCodecType); |
| 334 | *video_type = payload->typeSpecific.Video.videoCodecType; |
| 335 | video_->SetMaxConfiguredBitrateVideo(payload->typeSpecific.Video.maxRate); |
pwestin@webrtc.org | 0074187 | 2012-01-19 15:56:10 +0000 | [diff] [blame] | 336 | } |
| 337 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 338 | } |
| 339 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 340 | int32_t RTPSender::SendOutgoingData( |
| 341 | const FrameType frame_type, const int8_t payload_type, |
| 342 | const uint32_t capture_timestamp, int64_t capture_time_ms, |
| 343 | const uint8_t *payload_data, const uint32_t payload_size, |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 344 | const RTPFragmentationHeader *fragmentation, |
| 345 | VideoCodecInformation *codec_info, const RTPVideoTypeHeader *rtp_type_hdr) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 346 | { |
| 347 | // Drop this packet if we're not sending media packets. |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 348 | CriticalSectionScoped cs(send_critsect_); |
| 349 | if (!sending_media_) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 350 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 351 | } |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 352 | } |
pbos@webrtc.org | 8911ce4 | 2013-03-18 16:39:03 +0000 | [diff] [blame] | 353 | RtpVideoCodecTypes video_type = kRtpGenericVideo; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 354 | if (CheckPayloadType(payload_type, &video_type) != 0) { |
| 355 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, id_, |
| 356 | "%s invalid argument failed to find payload_type:%d", |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 357 | __FUNCTION__, payload_type); |
| 358 | return -1; |
| 359 | } |
| 360 | |
justinlin@chromium.org | 7bfb3a3 | 2013-05-13 22:59:00 +0000 | [diff] [blame] | 361 | if (frame_type == kVideoFrameKey) { |
| 362 | TRACE_EVENT_INSTANT1("webrtc_rtp", "SendKeyFrame", |
| 363 | "timestamp", capture_timestamp); |
| 364 | } else { |
| 365 | TRACE_EVENT_INSTANT2("webrtc_rtp", "SendFrame", |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 366 | "timestamp", capture_timestamp, |
justinlin@chromium.org | 7bfb3a3 | 2013-05-13 22:59:00 +0000 | [diff] [blame] | 367 | "frame_type", FrameTypeToString(frame_type)); |
| 368 | } |
| 369 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 370 | if (audio_configured_) { |
| 371 | assert(frame_type == kAudioFrameSpeech || frame_type == kAudioFrameCN || |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 372 | frame_type == kFrameEmpty); |
| 373 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 374 | return audio_->SendAudio(frame_type, payload_type, capture_timestamp, |
| 375 | payload_data, payload_size, fragmentation); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 376 | } else { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 377 | assert(frame_type != kAudioFrameSpeech && frame_type != kAudioFrameCN); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 378 | |
| 379 | if (frame_type == kFrameEmpty) { |
| 380 | return SendPaddingAccordingToBitrate(payload_type, capture_timestamp, |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 381 | capture_time_ms) ? 0 : -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 382 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 383 | return video_->SendVideo(video_type, frame_type, payload_type, |
| 384 | capture_timestamp, capture_time_ms, payload_data, |
| 385 | payload_size, fragmentation, codec_info, |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 386 | rtp_type_hdr); |
| 387 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 388 | } |
| 389 | |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 390 | bool RTPSender::SendPaddingAccordingToBitrate( |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 391 | int8_t payload_type, uint32_t capture_timestamp, |
stefan@webrtc.org | ddfdfed | 2012-07-03 13:21:22 +0000 | [diff] [blame] | 392 | int64_t capture_time_ms) { |
phoglund@webrtc.org | baaf243 | 2012-05-31 10:47:35 +0000 | [diff] [blame] | 393 | // Current bitrate since last estimate(1 second) averaged with the |
| 394 | // estimate since then, to get the most up to date bitrate. |
| 395 | uint32_t current_bitrate = BitrateNow(); |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 396 | int bitrate_diff = target_send_bitrate_ * 1000 - current_bitrate; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 397 | if (bitrate_diff <= 0) { |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 398 | return true; |
phoglund@webrtc.org | baaf243 | 2012-05-31 10:47:35 +0000 | [diff] [blame] | 399 | } |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 400 | int bytes = 0; |
| 401 | if (current_bitrate == 0) { |
| 402 | // Start up phase. Send one 33.3 ms batch to start with. |
| 403 | bytes = (bitrate_diff / 8) / 30; |
| 404 | } else { |
| 405 | bytes = (bitrate_diff / 8); |
| 406 | // Cap at 200 ms of target send data. |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 407 | int bytes_cap = target_send_bitrate_ * 25; // 1000 / 8 / 5. |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 408 | if (bytes > bytes_cap) { |
| 409 | bytes = bytes_cap; |
| 410 | } |
| 411 | } |
| 412 | return SendPadData(payload_type, capture_timestamp, capture_time_ms, bytes); |
phoglund@webrtc.org | baaf243 | 2012-05-31 10:47:35 +0000 | [diff] [blame] | 413 | } |
| 414 | |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 415 | int RTPSender::BuildPaddingPacket(uint8_t* packet, int header_length, |
| 416 | int32_t bytes) { |
| 417 | int padding_bytes_in_packet = kMaxPaddingLength; |
| 418 | if (bytes < kMaxPaddingLength) { |
| 419 | // Round to the nearest multiple of 32. |
| 420 | padding_bytes_in_packet = (bytes + 16) & 0xffe0; |
| 421 | } |
| 422 | if (padding_bytes_in_packet < 32) { |
| 423 | // Sanity don't send empty packets. |
pwestin@webrtc.org | 12d97f6 | 2012-01-05 10:54:44 +0000 | [diff] [blame] | 424 | return 0; |
| 425 | } |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 426 | packet[0] |= 0x20; // Set padding bit. |
| 427 | int32_t *data = |
| 428 | reinterpret_cast<int32_t *>(&(packet[header_length])); |
| 429 | |
| 430 | // Fill data buffer with random data. |
| 431 | for (int j = 0; j < (padding_bytes_in_packet >> 2); ++j) { |
| 432 | data[j] = rand(); // NOLINT |
| 433 | } |
| 434 | // Set number of padding bytes in the last byte of the packet. |
| 435 | packet[header_length + padding_bytes_in_packet - 1] = padding_bytes_in_packet; |
| 436 | return padding_bytes_in_packet; |
| 437 | } |
| 438 | |
| 439 | bool RTPSender::SendPadData(int8_t payload_type, uint32_t capture_timestamp, |
| 440 | int64_t capture_time_ms, int32_t bytes) { |
| 441 | // Drop this packet if we're not sending media packets. |
| 442 | if (!sending_media_) { |
| 443 | return true; |
| 444 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 445 | uint8_t data_buffer[IP_PACKET_SIZE]; |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 446 | for (; bytes > 0; bytes -= kMaxPaddingLength) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 447 | // Correct seq num, timestamp and payload type. |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 448 | int header_length = BuildRTPheader(data_buffer, |
| 449 | payload_type, |
| 450 | false, // No markerbit. |
| 451 | capture_timestamp, |
| 452 | true, // Timestamp provided. |
| 453 | true); // Increment sequence number. |
| 454 | int padding_bytes_in_packet = BuildPaddingPacket(data_buffer, header_length, |
| 455 | bytes); |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 456 | // Send the packet. |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 457 | if (SendToNetwork(data_buffer, padding_bytes_in_packet, header_length, |
| 458 | capture_time_ms, kDontRetransmit) < 0) { |
pwestin@webrtc.org | 12d97f6 | 2012-01-05 10:54:44 +0000 | [diff] [blame] | 459 | // Error sending the packet. |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | if (bytes > 31) { // 31 due to our modulus 32. |
| 464 | // We did not manage to send all bytes. |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 465 | return false; |
pwestin@webrtc.org | 12d97f6 | 2012-01-05 10:54:44 +0000 | [diff] [blame] | 466 | } |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 467 | return true; |
pwestin@webrtc.org | 12d97f6 | 2012-01-05 10:54:44 +0000 | [diff] [blame] | 468 | } |
| 469 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 470 | void RTPSender::SetStorePacketsStatus(const bool enable, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 471 | const uint16_t number_to_store) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 472 | packet_history_->SetStorePacketsStatus(enable, number_to_store); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 473 | } |
| 474 | |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 475 | bool RTPSender::StorePackets() const { |
| 476 | return packet_history_->StorePackets(); |
| 477 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 478 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 479 | int32_t RTPSender::ReSendPacket(uint16_t packet_id, uint32_t min_resend_time) { |
| 480 | uint16_t length = IP_PACKET_SIZE; |
| 481 | uint8_t data_buffer[IP_PACKET_SIZE]; |
| 482 | uint8_t *buffer_to_send_ptr = data_buffer; |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 483 | int64_t capture_time_ms; |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 484 | StorageType type; |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 485 | if (!packet_history_->GetRTPPacket(packet_id, min_resend_time, data_buffer, |
| 486 | &length, &capture_time_ms, &type)) { |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 487 | // Packet not found. |
asapersson@webrtc.org | 83ed0a4 | 2012-04-23 12:43:05 +0000 | [diff] [blame] | 488 | return 0; |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 489 | } |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 490 | if (length == 0 || type == kDontRetransmit) { |
| 491 | // No bytes copied (packet recently resent, skip resending) or |
| 492 | // packet should not be retransmitted. |
| 493 | return 0; |
| 494 | } |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 495 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 496 | uint8_t data_buffer_rtx[IP_PACKET_SIZE]; |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 497 | if (rtx_ != kRtxOff) { |
| 498 | BuildRtxPacket(data_buffer, &length, data_buffer_rtx); |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 499 | buffer_to_send_ptr = data_buffer_rtx; |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 500 | } |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 501 | |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 502 | ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length); |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 503 | RTPHeader header; |
| 504 | rtp_parser.Parse(header); |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 505 | |
| 506 | // Store the time when the packet was last sent or added to pacer. |
| 507 | packet_history_->UpdateResendTime(packet_id); |
| 508 | |
| 509 | { |
| 510 | // Update send statistics prior to pacer. |
| 511 | CriticalSectionScoped cs(send_critsect_); |
| 512 | Bitrate::Update(length); |
| 513 | packets_sent_++; |
| 514 | // We on purpose don't add to payload_bytes_sent_ since this is a |
| 515 | // re-transmit and not new payload data. |
| 516 | } |
| 517 | |
pwestin@webrtc.org | 52b4e88 | 2013-05-02 19:02:17 +0000 | [diff] [blame] | 518 | TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::ReSendPacket", |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 519 | "timestamp", header.timestamp, |
| 520 | "seqnum", header.sequenceNumber); |
| 521 | |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 522 | if (paced_sender_) { |
| 523 | if (!paced_sender_->SendPacket(PacedSender::kHighPriority, |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 524 | header.ssrc, |
| 525 | header.sequenceNumber, |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 526 | capture_time_ms, |
| 527 | length)) { |
| 528 | // We can't send the packet right now. |
| 529 | // We will be called when it is time. |
stefan@webrtc.org | 5c58f63 | 2013-05-23 13:36:55 +0000 | [diff] [blame] | 530 | return length; |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 534 | if (SendPacketToNetwork(buffer_to_send_ptr, length)) { |
stefan@webrtc.org | 5c58f63 | 2013-05-23 13:36:55 +0000 | [diff] [blame] | 535 | return length; |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 536 | } |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 537 | return -1; |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 538 | } |
| 539 | |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 540 | bool RTPSender::SendPacketToNetwork(const uint8_t *packet, uint32_t size) { |
| 541 | int bytes_sent = -1; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 542 | if (transport_) { |
| 543 | bytes_sent = transport_->SendPacket(id_, packet, size); |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 544 | } |
pwestin@webrtc.org | 52b4e88 | 2013-05-02 19:02:17 +0000 | [diff] [blame] | 545 | TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::SendPacketToNetwork", |
| 546 | "size", size, "sent", bytes_sent); |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 547 | // TODO(pwesin): Add a separate bitrate for sent bitrate after pacer. |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 548 | if (bytes_sent <= 0) { |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 549 | WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_, |
| 550 | "Transport failed to send packet"); |
| 551 | return false; |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 552 | } |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 553 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 554 | } |
| 555 | |
stefan@webrtc.org | 6a4bef4 | 2011-12-22 12:52:41 +0000 | [diff] [blame] | 556 | int RTPSender::SelectiveRetransmissions() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 557 | if (!video_) |
| 558 | return -1; |
| 559 | return video_->SelectiveRetransmissions(); |
stefan@webrtc.org | 6a4bef4 | 2011-12-22 12:52:41 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | int RTPSender::SetSelectiveRetransmissions(uint8_t settings) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 563 | if (!video_) |
| 564 | return -1; |
| 565 | return video_->SetSelectiveRetransmissions(settings); |
stefan@webrtc.org | 6a4bef4 | 2011-12-22 12:52:41 +0000 | [diff] [blame] | 566 | } |
| 567 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 568 | void RTPSender::OnReceivedNACK( |
stefan@webrtc.org | becf9c8 | 2013-02-01 15:09:57 +0000 | [diff] [blame] | 569 | const std::list<uint16_t>& nack_sequence_numbers, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 570 | const uint16_t avg_rtt) { |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 571 | TRACE_EVENT2("webrtc_rtp", "RTPSender::OnReceivedNACK", |
| 572 | "num_seqnum", nack_sequence_numbers.size(), "avg_rtt", avg_rtt); |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 573 | const int64_t now = clock_->TimeInMilliseconds(); |
| 574 | uint32_t bytes_re_sent = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 575 | |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 576 | // Enough bandwidth to send NACK? |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 577 | if (!ProcessNACKBitRate(now)) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 578 | WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_, |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 579 | "NACK bitrate reached. Skip sending NACK response. Target %d", |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 580 | target_send_bitrate_); |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 581 | return; |
| 582 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 583 | |
stefan@webrtc.org | becf9c8 | 2013-02-01 15:09:57 +0000 | [diff] [blame] | 584 | for (std::list<uint16_t>::const_iterator it = nack_sequence_numbers.begin(); |
| 585 | it != nack_sequence_numbers.end(); ++it) { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 586 | const int32_t bytes_sent = ReSendPacket(*it, 5 + avg_rtt); |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 587 | if (bytes_sent > 0) { |
| 588 | bytes_re_sent += bytes_sent; |
| 589 | } else if (bytes_sent == 0) { |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 590 | // The packet has previously been resent. |
| 591 | // Try resending next packet in the list. |
| 592 | continue; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 593 | } else if (bytes_sent < 0) { |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 594 | // Failed to send one Sequence number. Give up the rest in this nack. |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 595 | WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, id_, |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 596 | "Failed resending RTP packet %d, Discard rest of packets", |
stefan@webrtc.org | becf9c8 | 2013-02-01 15:09:57 +0000 | [diff] [blame] | 597 | *it); |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 598 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 599 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 600 | // Delay bandwidth estimate (RTT * BW). |
| 601 | if (target_send_bitrate_ != 0 && avg_rtt) { |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 602 | // kbits/s * ms = bits => bits/8 = bytes |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 603 | uint32_t target_bytes = |
| 604 | (static_cast<uint32_t>(target_send_bitrate_) * avg_rtt) >> 3; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 605 | if (bytes_re_sent > target_bytes) { |
| 606 | break; // Ignore the rest of the packets in the list. |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 610 | if (bytes_re_sent > 0) { |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 611 | // TODO(pwestin) consolidate these two methods. |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 612 | UpdateNACKBitRate(bytes_re_sent, now); |
| 613 | nack_bitrate_.Update(bytes_re_sent); |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 614 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 615 | } |
| 616 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 617 | bool RTPSender::ProcessNACKBitRate(const uint32_t now) { |
| 618 | uint32_t num = 0; |
| 619 | int32_t byte_count = 0; |
| 620 | const uint32_t avg_interval = 1000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 621 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 622 | CriticalSectionScoped cs(send_critsect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 623 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 624 | if (target_send_bitrate_ == 0) { |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 625 | return true; |
| 626 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 627 | for (num = 0; num < NACK_BYTECOUNT_SIZE; ++num) { |
| 628 | if ((now - nack_byte_count_times_[num]) > avg_interval) { |
| 629 | // Don't use data older than 1sec. |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 630 | break; |
| 631 | } else { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 632 | byte_count += nack_byte_count_[num]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 633 | } |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 634 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 635 | int32_t time_interval = avg_interval; |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 636 | if (num == NACK_BYTECOUNT_SIZE) { |
| 637 | // More than NACK_BYTECOUNT_SIZE nack messages has been received |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 638 | // during the last msg_interval. |
| 639 | time_interval = now - nack_byte_count_times_[num - 1]; |
| 640 | if (time_interval < 0) { |
| 641 | time_interval = avg_interval; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 642 | } |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 643 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 644 | return (byte_count * 8) < (target_send_bitrate_ * time_interval); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 645 | } |
| 646 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 647 | void RTPSender::UpdateNACKBitRate(const uint32_t bytes, |
| 648 | const uint32_t now) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 649 | CriticalSectionScoped cs(send_critsect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 650 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 651 | // Save bitrate statistics. |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 652 | if (bytes > 0) { |
| 653 | if (now == 0) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 654 | // Add padding length. |
| 655 | nack_byte_count_[0] += bytes; |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 656 | } else { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 657 | if (nack_byte_count_times_[0] == 0) { |
| 658 | // First no shift. |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 659 | } else { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 660 | // Shift. |
| 661 | for (int i = (NACK_BYTECOUNT_SIZE - 2); i >= 0; i--) { |
| 662 | nack_byte_count_[i + 1] = nack_byte_count_[i]; |
| 663 | nack_byte_count_times_[i + 1] = nack_byte_count_times_[i]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 664 | } |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 665 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 666 | nack_byte_count_[0] = bytes; |
| 667 | nack_byte_count_times_[0] = now; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 668 | } |
pwestin@webrtc.org | 8281e7d | 2012-01-10 14:09:18 +0000 | [diff] [blame] | 669 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 670 | } |
| 671 | |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 672 | // Called from pacer when we can send the packet. |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 673 | void RTPSender::TimeToSendPacket(uint16_t sequence_number, |
| 674 | int64_t capture_time_ms) { |
| 675 | StorageType type; |
| 676 | uint16_t length = IP_PACKET_SIZE; |
| 677 | uint8_t data_buffer[IP_PACKET_SIZE]; |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 678 | int64_t stored_time_ms; |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 679 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 680 | if (packet_history_ == NULL) { |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 681 | return; |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 682 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 683 | if (!packet_history_->GetRTPPacket(sequence_number, 0, data_buffer, &length, |
| 684 | &stored_time_ms, &type)) { |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 685 | return; |
| 686 | } |
| 687 | assert(length > 0); |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 688 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 689 | ModuleRTPUtility::RTPHeaderParser rtp_parser(data_buffer, length); |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 690 | RTPHeader rtp_header; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 691 | rtp_parser.Parse(rtp_header); |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 692 | TRACE_EVENT_INSTANT2("webrtc_rtp", "RTPSender::TimeToSendPacket", |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 693 | "timestamp", rtp_header.timestamp, |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 694 | "seqnum", sequence_number); |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 695 | |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 696 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 697 | int64_t diff_ms = now_ms - capture_time_ms; |
| 698 | bool updated_transmission_time_offset = |
| 699 | UpdateTransmissionTimeOffset(data_buffer, length, rtp_header, diff_ms); |
| 700 | bool updated_abs_send_time = |
| 701 | UpdateAbsoluteSendTime(data_buffer, length, rtp_header, now_ms); |
| 702 | if (updated_transmission_time_offset || updated_abs_send_time) { |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 703 | // Update stored packet in case of receiving a re-transmission request. |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 704 | packet_history_->ReplaceRTPHeader(data_buffer, |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 705 | rtp_header.sequenceNumber, |
| 706 | rtp_header.headerLength); |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 707 | } |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 708 | SendPacketToNetwork(data_buffer, length); |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 709 | } |
| 710 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 711 | // TODO(pwestin): send in the RTPHeaderParser to avoid parsing it again. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 712 | int32_t RTPSender::SendToNetwork( |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 713 | uint8_t *buffer, int payload_length, int rtp_header_length, |
| 714 | int64_t capture_time_ms, StorageType storage) { |
| 715 | ModuleRTPUtility::RTPHeaderParser rtp_parser( |
| 716 | buffer, payload_length + rtp_header_length); |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 717 | RTPHeader rtp_header; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 718 | rtp_parser.Parse(rtp_header); |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 719 | |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 720 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 721 | |
stefan@webrtc.org | 715faaf | 2012-08-28 15:20:39 +0000 | [diff] [blame] | 722 | // |capture_time_ms| <= 0 is considered invalid. |
| 723 | // TODO(holmer): This should be changed all over Video Engine so that negative |
| 724 | // time is consider invalid, while 0 is considered a valid time. |
| 725 | if (capture_time_ms > 0) { |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 726 | UpdateTransmissionTimeOffset(buffer, payload_length + rtp_header_length, |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 727 | rtp_header, now_ms - capture_time_ms); |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 728 | } |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 729 | |
| 730 | UpdateAbsoluteSendTime(buffer, payload_length + rtp_header_length, |
| 731 | rtp_header, now_ms); |
| 732 | |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 733 | // Used for NACK and to spread out the transmission of packets. |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 734 | if (packet_history_->PutRTPPacket(buffer, rtp_header_length + payload_length, |
| 735 | max_payload_length_, capture_time_ms, |
| 736 | storage) != 0) { |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 737 | return -1; |
| 738 | } |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 739 | |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 740 | // Create and send RTX Packet. |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 741 | // TODO(pwesin): This should be moved to its own code path triggered by pacer. |
| 742 | bool rtx_sent = false; |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 743 | if (rtx_ == kRtxAll && storage == kAllowRetransmission) { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 744 | uint16_t length_rtx = payload_length + rtp_header_length; |
| 745 | uint8_t data_buffer_rtx[IP_PACKET_SIZE]; |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 746 | BuildRtxPacket(buffer, &length_rtx, data_buffer_rtx); |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 747 | if (!SendPacketToNetwork(data_buffer_rtx, length_rtx)) return -1; |
| 748 | rtx_sent = true; |
| 749 | } |
| 750 | { |
| 751 | // Update send statistics prior to pacer. |
| 752 | CriticalSectionScoped cs(send_critsect_); |
| 753 | Bitrate::Update(payload_length + rtp_header_length); |
| 754 | ++packets_sent_; |
| 755 | payload_bytes_sent_ += payload_length; |
| 756 | if (rtx_sent) { |
| 757 | // The RTX packet. |
| 758 | ++packets_sent_; |
| 759 | payload_bytes_sent_ += payload_length; |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 763 | if (paced_sender_ && storage != kDontStore) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 764 | if (!paced_sender_->SendPacket( |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 765 | PacedSender::kNormalPriority, rtp_header.ssrc, |
| 766 | rtp_header.sequenceNumber, capture_time_ms, |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 767 | payload_length + rtp_header_length)) { |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 768 | // We can't send the packet right now. |
| 769 | // We will be called when it is time. |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 770 | return 0; |
asapersson@webrtc.org | e5b49a0 | 2012-11-06 13:09:39 +0000 | [diff] [blame] | 771 | } |
stefan@webrtc.org | ddfdfed | 2012-07-03 13:21:22 +0000 | [diff] [blame] | 772 | } |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 773 | if (SendPacketToNetwork(buffer, payload_length + rtp_header_length)) { |
| 774 | return 0; |
stefan@webrtc.org | 6a4bef4 | 2011-12-22 12:52:41 +0000 | [diff] [blame] | 775 | } |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 776 | return -1; |
stefan@webrtc.org | 6a4bef4 | 2011-12-22 12:52:41 +0000 | [diff] [blame] | 777 | } |
| 778 | |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 779 | void RTPSender::ProcessBitrate() { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 780 | CriticalSectionScoped cs(send_critsect_); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 781 | Bitrate::Process(); |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 782 | nack_bitrate_.Process(); |
| 783 | if (audio_configured_) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 784 | return; |
| 785 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 786 | video_->ProcessBitrate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 787 | } |
| 788 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 789 | uint16_t RTPSender::RTPHeaderLength() const { |
| 790 | uint16_t rtp_header_length = 12; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 791 | if (include_csrcs_) { |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 792 | rtp_header_length += sizeof(uint32_t) * num_csrcs_; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 793 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 794 | rtp_header_length += RtpHeaderExtensionTotalLength(); |
| 795 | return rtp_header_length; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 796 | } |
| 797 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 798 | uint16_t RTPSender::IncrementSequenceNumber() { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 799 | CriticalSectionScoped cs(send_critsect_); |
| 800 | return sequence_number_++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 801 | } |
| 802 | |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 803 | void RTPSender::ResetDataCounters() { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 804 | packets_sent_ = 0; |
| 805 | payload_bytes_sent_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 806 | } |
| 807 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 808 | uint32_t RTPSender::Packets() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 809 | // Don't use critsect to avoid potential deadlock. |
| 810 | return packets_sent_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 811 | } |
| 812 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 813 | // Number of sent RTP bytes. |
| 814 | // Don't use critsect to avoid potental deadlock. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 815 | uint32_t RTPSender::Bytes() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 816 | return payload_bytes_sent_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 817 | } |
| 818 | |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 819 | int RTPSender::CreateRTPHeader( |
| 820 | uint8_t* header, int8_t payload_type, uint32_t ssrc, bool marker_bit, |
| 821 | uint32_t timestamp, uint16_t sequence_number, const uint32_t* csrcs, |
| 822 | uint8_t num_csrcs) const { |
| 823 | header[0] = 0x80; // version 2. |
| 824 | header[1] = static_cast<uint8_t>(payload_type); |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 825 | if (marker_bit) { |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 826 | header[1] |= kRtpMarkerBitMask; // Marker bit is set. |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 827 | } |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 828 | ModuleRTPUtility::AssignUWord16ToBuffer(header + 2, sequence_number); |
| 829 | ModuleRTPUtility::AssignUWord32ToBuffer(header + 4, timestamp); |
| 830 | ModuleRTPUtility::AssignUWord32ToBuffer(header + 8, ssrc); |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 831 | int32_t rtp_header_length = 12; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 832 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 833 | // Add the CSRCs if any. |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 834 | if (num_csrcs > 0) { |
| 835 | if (num_csrcs > kRtpCsrcSize) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 836 | // error |
| 837 | assert(false); |
| 838 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 839 | } |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 840 | uint8_t *ptr = &header[rtp_header_length]; |
| 841 | for (int i = 0; i < num_csrcs; ++i) { |
| 842 | ModuleRTPUtility::AssignUWord32ToBuffer(ptr, csrcs[i]); |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 843 | ptr += 4; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 844 | } |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 845 | header[0] = (header[0] & 0xf0) | num_csrcs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 846 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 847 | // Update length of header. |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 848 | rtp_header_length += sizeof(uint32_t) * num_csrcs; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 849 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 850 | |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 851 | uint16_t len = BuildRTPHeaderExtension(header + rtp_header_length); |
| 852 | if (len > 0) { |
| 853 | header[0] |= 0x10; // Set extension bit. |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 854 | rtp_header_length += len; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 855 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 856 | return rtp_header_length; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 857 | } |
| 858 | |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 859 | int32_t RTPSender::BuildRTPheader( |
| 860 | uint8_t *data_buffer, const int8_t payload_type, |
| 861 | const bool marker_bit, const uint32_t capture_timestamp, |
| 862 | const bool time_stamp_provided, const bool inc_sequence_number) { |
| 863 | assert(payload_type >= 0); |
| 864 | CriticalSectionScoped cs(send_critsect_); |
| 865 | |
| 866 | if (time_stamp_provided) { |
| 867 | timestamp_ = start_time_stamp_ + capture_timestamp; |
| 868 | capture_timestamp_ = capture_timestamp; |
| 869 | } else { |
| 870 | // Make a unique time stamp. |
| 871 | // We can't inc by the actual time, since then we increase the risk of back |
| 872 | // timing. |
| 873 | timestamp_++; |
| 874 | } |
| 875 | uint32_t sequence_number = sequence_number_++; |
| 876 | int csrcs_length = 0; |
| 877 | if (include_csrcs_) |
| 878 | csrcs_length = num_csrcs_; |
| 879 | return CreateRTPHeader(data_buffer, payload_type, ssrc_, marker_bit, |
| 880 | timestamp_, sequence_number, csrcs_, csrcs_length); |
| 881 | } |
| 882 | |
| 883 | uint16_t RTPSender::BuildRTPHeaderExtension(uint8_t* data_buffer) const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 884 | if (rtp_header_extension_map_.Size() <= 0) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 885 | return 0; |
| 886 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 887 | // RTP header extension, RFC 3550. |
| 888 | // 0 1 2 3 |
| 889 | // 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 |
| 890 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 891 | // | defined by profile | length | |
| 892 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 893 | // | header extension | |
| 894 | // | .... | |
| 895 | // |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 896 | const uint32_t kPosLength = 2; |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 +0000 | [diff] [blame] | 897 | const uint32_t kHeaderLength = kRtpOneByteHeaderLength; |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 898 | |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 899 | // Add extension ID (0xBEDE). |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 900 | ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer, |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 +0000 | [diff] [blame] | 901 | kRtpOneByteHeaderExtensionId); |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 902 | |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 903 | // Add extensions. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 904 | uint16_t total_block_length = 0; |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 905 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 906 | RTPExtensionType type = rtp_header_extension_map_.First(); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 907 | while (type != kRtpExtensionNone) { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 908 | uint8_t block_length = 0; |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 909 | switch (type) { |
| 910 | case kRtpExtensionTransmissionTimeOffset: |
| 911 | block_length = BuildTransmissionTimeOffsetExtension( |
| 912 | data_buffer + kHeaderLength + total_block_length); |
| 913 | break; |
solenberg@webrtc.org | c0352d5 | 2013-05-20 20:55:07 +0000 | [diff] [blame] | 914 | case kRtpExtensionAudioLevel: |
| 915 | // Because AudioLevel is handled specially by RTPSenderAudio, we pretend |
| 916 | // we don't have to care about it here, which is true until we wan't to |
| 917 | // use it together with any of the other extensions we support. |
| 918 | break; |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 919 | case kRtpExtensionAbsoluteSendTime: |
| 920 | block_length = BuildAbsoluteSendTimeExtension( |
| 921 | data_buffer + kHeaderLength + total_block_length); |
| 922 | break; |
| 923 | default: |
| 924 | assert(false); |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 925 | } |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 926 | total_block_length += block_length; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 927 | type = rtp_header_extension_map_.Next(type); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 928 | } |
| 929 | if (total_block_length == 0) { |
| 930 | // No extension added. |
| 931 | return 0; |
| 932 | } |
| 933 | // Set header length (in number of Word32, header excluded). |
| 934 | assert(total_block_length % 4 == 0); |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 935 | ModuleRTPUtility::AssignUWord16ToBuffer(data_buffer + kPosLength, |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 936 | total_block_length / 4); |
| 937 | // Total added length. |
| 938 | return kHeaderLength + total_block_length; |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 939 | } |
| 940 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 941 | uint8_t RTPSender::BuildTransmissionTimeOffsetExtension( |
| 942 | uint8_t* data_buffer) const { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 943 | // From RFC 5450: Transmission Time Offsets in RTP Streams. |
| 944 | // |
| 945 | // The transmission time is signaled to the receiver in-band using the |
| 946 | // general mechanism for RTP header extensions [RFC5285]. The payload |
| 947 | // of this extension (the transmitted value) is a 24-bit signed integer. |
| 948 | // When added to the RTP timestamp of the packet, it represents the |
| 949 | // "effective" RTP transmission time of the packet, on the RTP |
| 950 | // timescale. |
| 951 | // |
| 952 | // The form of the transmission offset extension block: |
| 953 | // |
| 954 | // 0 1 2 3 |
| 955 | // 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 |
| 956 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 957 | // | ID | len=2 | transmission offset | |
| 958 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 959 | |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 960 | // Get id defined by user. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 961 | uint8_t id; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 962 | if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset, |
| 963 | &id) != 0) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 964 | // Not registered. |
| 965 | return 0; |
| 966 | } |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 +0000 | [diff] [blame] | 967 | size_t pos = 0; |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 968 | const uint8_t len = 2; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 969 | data_buffer[pos++] = (id << 4) + len; |
| 970 | ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos, |
| 971 | transmission_time_offset_); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 972 | pos += 3; |
pbos@webrtc.org | 3004c79 | 2013-05-07 12:36:21 +0000 | [diff] [blame] | 973 | assert(pos == kTransmissionTimeOffsetLength); |
| 974 | return kTransmissionTimeOffsetLength; |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 975 | } |
| 976 | |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 977 | uint8_t RTPSender::BuildAbsoluteSendTimeExtension( |
| 978 | uint8_t* data_buffer) const { |
| 979 | // Absolute send time in RTP streams. |
| 980 | // |
| 981 | // The absolute send time is signaled to the receiver in-band using the |
| 982 | // general mechanism for RTP header extensions [RFC5285]. The payload |
| 983 | // of this extension (the transmitted value) is a 24-bit unsigned integer |
| 984 | // containing the sender's current time in seconds as a fixed point number |
| 985 | // with 18 bits fractional part. |
| 986 | // |
| 987 | // The form of the absolute send time extension block: |
| 988 | // |
| 989 | // 0 1 2 3 |
| 990 | // 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 |
| 991 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 992 | // | ID | len=2 | absolute send time | |
| 993 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 994 | |
| 995 | // Get id defined by user. |
| 996 | uint8_t id; |
| 997 | if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime, |
| 998 | &id) != 0) { |
| 999 | // Not registered. |
| 1000 | return 0; |
| 1001 | } |
| 1002 | size_t pos = 0; |
| 1003 | const uint8_t len = 2; |
| 1004 | data_buffer[pos++] = (id << 4) + len; |
| 1005 | ModuleRTPUtility::AssignUWord24ToBuffer(data_buffer + pos, |
| 1006 | absolute_send_time_); |
| 1007 | pos += 3; |
| 1008 | assert(pos == kAbsoluteSendTimeLength); |
| 1009 | return kAbsoluteSendTimeLength; |
| 1010 | } |
| 1011 | |
asapersson@webrtc.org | e5b49a0 | 2012-11-06 13:09:39 +0000 | [diff] [blame] | 1012 | bool RTPSender::UpdateTransmissionTimeOffset( |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1013 | uint8_t *rtp_packet, const uint16_t rtp_packet_length, |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1014 | const RTPHeader &rtp_header, const int64_t time_diff_ms) const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1015 | CriticalSectionScoped cs(send_critsect_); |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 1016 | |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 1017 | // Get length until start of header extension block. |
| 1018 | int extension_block_pos = |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1019 | rtp_header_extension_map_.GetLengthUntilBlockStartInBytes( |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1020 | kRtpExtensionTransmissionTimeOffset); |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 1021 | if (extension_block_pos < 0) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1022 | WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_, |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1023 | "Failed to update transmission time offset, not registered."); |
asapersson@webrtc.org | e5b49a0 | 2012-11-06 13:09:39 +0000 | [diff] [blame] | 1024 | return false; |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 1025 | } |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1026 | int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos; |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 1027 | if (rtp_packet_length < block_pos + kTransmissionTimeOffsetLength || |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1028 | rtp_header.headerLength < |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 1029 | block_pos + kTransmissionTimeOffsetLength) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1030 | WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_, |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1031 | "Failed to update transmission time offset, invalid length."); |
asapersson@webrtc.org | e5b49a0 | 2012-11-06 13:09:39 +0000 | [diff] [blame] | 1032 | return false; |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 1033 | } |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 1034 | // Verify that header contains extension. |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1035 | if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) && |
| 1036 | (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1037 | WEBRTC_TRACE( |
| 1038 | kTraceStream, kTraceRtpRtcp, id_, |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 1039 | "Failed to update transmission time offset, hdr extension not found."); |
asapersson@webrtc.org | e5b49a0 | 2012-11-06 13:09:39 +0000 | [diff] [blame] | 1040 | return false; |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 1041 | } |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 1042 | // Get id. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1043 | uint8_t id = 0; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1044 | if (rtp_header_extension_map_.GetId(kRtpExtensionTransmissionTimeOffset, |
| 1045 | &id) != 0) { |
| 1046 | WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_, |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1047 | "Failed to update transmission time offset, no id."); |
asapersson@webrtc.org | e5b49a0 | 2012-11-06 13:09:39 +0000 | [diff] [blame] | 1048 | return false; |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 1049 | } |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 1050 | // Verify first byte in block. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1051 | const uint8_t first_block_byte = (id << 4) + 2; |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 1052 | if (rtp_packet[block_pos] != first_block_byte) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1053 | WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_, |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1054 | "Failed to update transmission time offset."); |
asapersson@webrtc.org | e5b49a0 | 2012-11-06 13:09:39 +0000 | [diff] [blame] | 1055 | return false; |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 1056 | } |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 1057 | // Update transmission offset field (converting to a 90 kHz timestamp). |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 1058 | ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1, |
mflodman@webrtc.org | ba853c9 | 2012-08-10 14:30:53 +0000 | [diff] [blame] | 1059 | time_diff_ms * 90); // RTP timestamp. |
asapersson@webrtc.org | e5b49a0 | 2012-11-06 13:09:39 +0000 | [diff] [blame] | 1060 | return true; |
asapersson@webrtc.org | 0b3c35a | 2012-01-16 11:06:31 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 1063 | bool RTPSender::UpdateAbsoluteSendTime( |
| 1064 | uint8_t *rtp_packet, const uint16_t rtp_packet_length, |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1065 | const RTPHeader &rtp_header, const int64_t now_ms) const { |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 1066 | CriticalSectionScoped cs(send_critsect_); |
| 1067 | |
| 1068 | // Get length until start of header extension block. |
| 1069 | int extension_block_pos = |
| 1070 | rtp_header_extension_map_.GetLengthUntilBlockStartInBytes( |
| 1071 | kRtpExtensionAbsoluteSendTime); |
| 1072 | if (extension_block_pos < 0) { |
| 1073 | WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_, |
| 1074 | "Failed to update absolute send time, not registered."); |
| 1075 | return false; |
| 1076 | } |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1077 | int block_pos = 12 + rtp_header.numCSRCs + extension_block_pos; |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 1078 | if (rtp_packet_length < block_pos + kAbsoluteSendTimeLength || |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1079 | rtp_header.headerLength < block_pos + kAbsoluteSendTimeLength) { |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 1080 | WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_, |
| 1081 | "Failed to update absolute send time, invalid length."); |
| 1082 | return false; |
| 1083 | } |
| 1084 | // Verify that header contains extension. |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1085 | if (!((rtp_packet[12 + rtp_header.numCSRCs] == 0xBE) && |
| 1086 | (rtp_packet[12 + rtp_header.numCSRCs + 1] == 0xDE))) { |
solenberg@webrtc.org | 7ebbea1 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 1087 | WEBRTC_TRACE( |
| 1088 | kTraceStream, kTraceRtpRtcp, id_, |
| 1089 | "Failed to update absolute send time, hdr extension not found."); |
| 1090 | return false; |
| 1091 | } |
| 1092 | // Get id. |
| 1093 | uint8_t id = 0; |
| 1094 | if (rtp_header_extension_map_.GetId(kRtpExtensionAbsoluteSendTime, |
| 1095 | &id) != 0) { |
| 1096 | WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_, |
| 1097 | "Failed to update absolute send time, no id."); |
| 1098 | return false; |
| 1099 | } |
| 1100 | // Verify first byte in block. |
| 1101 | const uint8_t first_block_byte = (id << 4) + 2; |
| 1102 | if (rtp_packet[block_pos] != first_block_byte) { |
| 1103 | WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, id_, |
| 1104 | "Failed to update absolute send time."); |
| 1105 | return false; |
| 1106 | } |
| 1107 | // Update absolute send time field (convert ms to 24-bit unsigned with 18 bit |
| 1108 | // fractional part). |
| 1109 | ModuleRTPUtility::AssignUWord24ToBuffer(rtp_packet + block_pos + 1, |
| 1110 | ((now_ms << 18) / 1000) & 0x00ffffff); |
| 1111 | return true; |
| 1112 | } |
| 1113 | |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1114 | void RTPSender::SetSendingStatus(const bool enabled) { |
| 1115 | if (enabled) { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1116 | uint32_t frequency_hz; |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1117 | if (audio_configured_) { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1118 | uint32_t frequency = audio_->AudioFrequency(); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1119 | |
| 1120 | // sanity |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1121 | switch (frequency) { |
| 1122 | case 8000: |
| 1123 | case 12000: |
| 1124 | case 16000: |
| 1125 | case 24000: |
| 1126 | case 32000: |
| 1127 | break; |
| 1128 | default: |
| 1129 | assert(false); |
| 1130 | return; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1131 | } |
phoglund@webrtc.org | c38eef8 | 2013-01-07 10:18:30 +0000 | [diff] [blame] | 1132 | frequency_hz = frequency; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1133 | } else { |
phoglund@webrtc.org | c38eef8 | 2013-01-07 10:18:30 +0000 | [diff] [blame] | 1134 | frequency_hz = kDefaultVideoFrequency; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1135 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1136 | uint32_t RTPtime = ModuleRTPUtility::GetCurrentRTP(clock_, frequency_hz); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1137 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1138 | // Will be ignored if it's already configured via API. |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1139 | SetStartTimestamp(RTPtime, false); |
| 1140 | } else { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1141 | if (!ssrc_forced_) { |
| 1142 | // Generate a new SSRC. |
| 1143 | ssrc_db_.ReturnSSRC(ssrc_); |
| 1144 | ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0. |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1145 | } |
| 1146 | // Don't initialize seq number if SSRC passed externally. |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1147 | if (!sequence_number_forced_ && !ssrc_forced_) { |
| 1148 | // Generate a new sequence number. |
| 1149 | sequence_number_ = |
| 1150 | rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | void RTPSender::SetSendingMediaStatus(const bool enabled) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1156 | CriticalSectionScoped cs(send_critsect_); |
| 1157 | sending_media_ = enabled; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | bool RTPSender::SendingMedia() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1161 | CriticalSectionScoped cs(send_critsect_); |
| 1162 | return sending_media_; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1165 | uint32_t RTPSender::Timestamp() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1166 | CriticalSectionScoped cs(send_critsect_); |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 1167 | return timestamp_; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1170 | void RTPSender::SetStartTimestamp(uint32_t timestamp, bool force) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1171 | CriticalSectionScoped cs(send_critsect_); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1172 | if (force) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1173 | start_time_stamp_forced_ = force; |
| 1174 | start_time_stamp_ = timestamp; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1175 | } else { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1176 | if (!start_time_stamp_forced_) { |
| 1177 | start_time_stamp_ = timestamp; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1182 | uint32_t RTPSender::StartTimestamp() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1183 | CriticalSectionScoped cs(send_critsect_); |
| 1184 | return start_time_stamp_; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1187 | uint32_t RTPSender::GenerateNewSSRC() { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1188 | // If configured via API, return 0. |
| 1189 | CriticalSectionScoped cs(send_critsect_); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1190 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1191 | if (ssrc_forced_) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1192 | return 0; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1193 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1194 | ssrc_ = ssrc_db_.CreateSSRC(); // Can't be 0. |
| 1195 | return ssrc_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1198 | void RTPSender::SetSSRC(uint32_t ssrc) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1199 | // This is configured via the API. |
| 1200 | CriticalSectionScoped cs(send_critsect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1201 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1202 | if (ssrc_ == ssrc && ssrc_forced_) { |
| 1203 | return; // Since it's same ssrc, don't reset anything. |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1204 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1205 | ssrc_forced_ = true; |
| 1206 | ssrc_db_.ReturnSSRC(ssrc_); |
| 1207 | ssrc_db_.RegisterSSRC(ssrc); |
| 1208 | ssrc_ = ssrc; |
| 1209 | if (!sequence_number_forced_) { |
| 1210 | sequence_number_ = |
| 1211 | rand() / (RAND_MAX / MAX_INIT_RTP_SEQ_NUMBER); // NOLINT |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1212 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1215 | uint32_t RTPSender::SSRC() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1216 | CriticalSectionScoped cs(send_critsect_); |
| 1217 | return ssrc_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1220 | void RTPSender::SetCSRCStatus(const bool include) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1221 | include_csrcs_ = include; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1224 | void RTPSender::SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize], |
| 1225 | const uint8_t arr_length) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1226 | assert(arr_length <= kRtpCsrcSize); |
| 1227 | CriticalSectionScoped cs(send_critsect_); |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1228 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1229 | for (int i = 0; i < arr_length; i++) { |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 1230 | csrcs_[i] = arr_of_csrc[i]; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1231 | } |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 1232 | num_csrcs_ = arr_length; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1235 | int32_t RTPSender::CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1236 | assert(arr_of_csrc); |
| 1237 | CriticalSectionScoped cs(send_critsect_); |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 1238 | for (int i = 0; i < num_csrcs_ && i < kRtpCsrcSize; i++) { |
| 1239 | arr_of_csrc[i] = csrcs_[i]; |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1240 | } |
stefan@webrtc.org | a817962 | 2013-06-04 13:47:36 +0000 | [diff] [blame^] | 1241 | return num_csrcs_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1244 | void RTPSender::SetSequenceNumber(uint16_t seq) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1245 | CriticalSectionScoped cs(send_critsect_); |
| 1246 | sequence_number_forced_ = true; |
| 1247 | sequence_number_ = seq; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1250 | uint16_t RTPSender::SequenceNumber() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1251 | CriticalSectionScoped cs(send_critsect_); |
| 1252 | return sequence_number_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1255 | // Audio. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1256 | int32_t RTPSender::SendTelephoneEvent(const uint8_t key, |
| 1257 | const uint16_t time_ms, |
| 1258 | const uint8_t level) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1259 | if (!audio_configured_) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1260 | return -1; |
| 1261 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1262 | return audio_->SendTelephoneEvent(key, time_ms, level); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1265 | bool RTPSender::SendTelephoneEventActive(int8_t *telephone_event) const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1266 | if (!audio_configured_) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1267 | return false; |
| 1268 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1269 | return audio_->SendTelephoneEventActive(*telephone_event); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1272 | int32_t RTPSender::SetAudioPacketSize( |
| 1273 | const uint16_t packet_size_samples) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1274 | if (!audio_configured_) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1275 | return -1; |
| 1276 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1277 | return audio_->SetAudioPacketSize(packet_size_samples); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1280 | int32_t RTPSender::SetAudioLevelIndicationStatus(const bool enable, |
| 1281 | const uint8_t ID) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1282 | if (!audio_configured_) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1283 | return -1; |
| 1284 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1285 | return audio_->SetAudioLevelIndicationStatus(enable, ID); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1288 | int32_t RTPSender::AudioLevelIndicationStatus(bool *enable, |
| 1289 | uint8_t* id) const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1290 | return audio_->AudioLevelIndicationStatus(*enable, *id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1293 | int32_t RTPSender::SetAudioLevel(const uint8_t level_d_bov) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1294 | return audio_->SetAudioLevel(level_d_bov); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1297 | int32_t RTPSender::SetRED(const int8_t payload_type) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1298 | if (!audio_configured_) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1299 | return -1; |
| 1300 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1301 | return audio_->SetRED(payload_type); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1304 | int32_t RTPSender::RED(int8_t *payload_type) const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1305 | if (!audio_configured_) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1306 | return -1; |
| 1307 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1308 | return audio_->RED(*payload_type); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1311 | // Video |
| 1312 | VideoCodecInformation *RTPSender::CodecInformationVideo() { |
| 1313 | if (audio_configured_) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1314 | return NULL; |
| 1315 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1316 | return video_->CodecInformationVideo(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1319 | RtpVideoCodecTypes RTPSender::VideoCodecType() const { |
pbos@webrtc.org | 8911ce4 | 2013-03-18 16:39:03 +0000 | [diff] [blame] | 1320 | assert(!audio_configured_ && "Sender is an audio stream!"); |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1321 | return video_->VideoCodecType(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1324 | uint32_t RTPSender::MaxConfiguredBitrateVideo() const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1325 | if (audio_configured_) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1326 | return 0; |
| 1327 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1328 | return video_->MaxConfiguredBitrateVideo(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1331 | int32_t RTPSender::SendRTPIntraRequest() { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1332 | if (audio_configured_) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1333 | return -1; |
| 1334 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1335 | return video_->SendRTPIntraRequest(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1338 | int32_t RTPSender::SetGenericFECStatus( |
| 1339 | const bool enable, const uint8_t payload_type_red, |
| 1340 | const uint8_t payload_type_fec) { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1341 | if (audio_configured_) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1342 | return -1; |
| 1343 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1344 | return video_->SetGenericFECStatus(enable, payload_type_red, |
| 1345 | payload_type_fec); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1348 | int32_t RTPSender::GenericFECStatus( |
| 1349 | bool *enable, uint8_t *payload_type_red, |
| 1350 | uint8_t *payload_type_fec) const { |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1351 | if (audio_configured_) { |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1352 | return -1; |
| 1353 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1354 | return video_->GenericFECStatus( |
| 1355 | *enable, *payload_type_red, *payload_type_fec); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1358 | int32_t RTPSender::SetFecParameters( |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1359 | const FecProtectionParams *delta_params, |
| 1360 | const FecProtectionParams *key_params) { |
| 1361 | if (audio_configured_) { |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 1362 | return -1; |
| 1363 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1364 | return video_->SetFecParameters(delta_params, key_params); |
marpan@google.com | 80c5d7a | 2011-07-15 21:32:40 +0000 | [diff] [blame] | 1365 | } |
phoglund@webrtc.org | 43da54a | 2013-01-25 10:53:38 +0000 | [diff] [blame] | 1366 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1367 | void RTPSender::BuildRtxPacket(uint8_t* buffer, uint16_t* length, |
| 1368 | uint8_t* buffer_rtx) { |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 1369 | CriticalSectionScoped cs(send_critsect_); |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1370 | uint8_t* data_buffer_rtx = buffer_rtx; |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 1371 | // Add RTX header. |
| 1372 | ModuleRTPUtility::RTPHeaderParser rtp_parser( |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1373 | reinterpret_cast<const uint8_t *>(buffer), *length); |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 1374 | |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1375 | RTPHeader rtp_header; |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 1376 | rtp_parser.Parse(rtp_header); |
| 1377 | |
| 1378 | // Add original RTP header. |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1379 | memcpy(data_buffer_rtx, buffer, rtp_header.headerLength); |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 1380 | |
mflodman@webrtc.org | 9f5ebb5 | 2013-04-12 14:55:46 +0000 | [diff] [blame] | 1381 | // Replace payload type, if a specific type is set for RTX. |
| 1382 | if (payload_type_rtx_ != -1) { |
| 1383 | data_buffer_rtx[1] = static_cast<uint8_t>(payload_type_rtx_); |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1384 | if (rtp_header.markerBit) |
mflodman@webrtc.org | 9f5ebb5 | 2013-04-12 14:55:46 +0000 | [diff] [blame] | 1385 | data_buffer_rtx[1] |= kRtpMarkerBitMask; |
| 1386 | } |
| 1387 | |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 1388 | // Replace sequence number. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1389 | uint8_t *ptr = data_buffer_rtx + 2; |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 1390 | ModuleRTPUtility::AssignUWord16ToBuffer(ptr, sequence_number_rtx_++); |
| 1391 | |
| 1392 | // Replace SSRC. |
| 1393 | ptr += 6; |
| 1394 | ModuleRTPUtility::AssignUWord32ToBuffer(ptr, ssrc_rtx_); |
| 1395 | |
| 1396 | // Add OSN (original sequence number). |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1397 | ptr = data_buffer_rtx + rtp_header.headerLength; |
| 1398 | ModuleRTPUtility::AssignUWord16ToBuffer(ptr, rtp_header.sequenceNumber); |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 1399 | ptr += 2; |
| 1400 | |
| 1401 | // Add original payload data. |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1402 | memcpy(ptr, buffer + rtp_header.headerLength, |
| 1403 | *length - rtp_header.headerLength); |
mikhal@webrtc.org | bda7f30 | 2013-03-15 23:21:52 +0000 | [diff] [blame] | 1404 | *length += 2; |
| 1405 | } |
| 1406 | |
pwestin@webrtc.org | c66e8b3 | 2012-11-07 17:01:04 +0000 | [diff] [blame] | 1407 | } // namespace webrtc |