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