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