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