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