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