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 | |
pbos@webrtc.org | a048d7c | 2013-05-29 14:27:38 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/rtp_rtcp/source/rtcp_sender.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> // assert |
| 14 | #include <stdlib.h> // rand |
stefan@webrtc.org | 9354cc9 | 2012-06-07 08:10:14 +0000 | [diff] [blame] | 15 | #include <string.h> // memcpy |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 17 | #include <algorithm> // min |
| 18 | |
pbos@webrtc.org | a048d7c | 2013-05-29 14:27:38 +0000 | [diff] [blame] | 19 | #include "webrtc/common_types.h" |
| 20 | #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h" |
| 21 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 22 | #include "webrtc/system_wrappers/interface/trace.h" |
| 23 | #include "webrtc/system_wrappers/interface/trace_event.h" |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 24 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | namespace webrtc { |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 26 | |
| 27 | using RTCPUtility::RTCPCnameInformation; |
| 28 | |
edjee@google.com | 79b0289 | 2013-04-04 19:43:34 +0000 | [diff] [blame] | 29 | NACKStringBuilder::NACKStringBuilder() : |
| 30 | _stream(""), _count(0), _consecutive(false) |
| 31 | { |
| 32 | // Empty. |
| 33 | } |
| 34 | |
pbos@webrtc.org | f3e4cee | 2013-07-31 15:17:19 +0000 | [diff] [blame] | 35 | NACKStringBuilder::~NACKStringBuilder() {} |
| 36 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 37 | void NACKStringBuilder::PushNACK(uint16_t nack) |
edjee@google.com | 79b0289 | 2013-04-04 19:43:34 +0000 | [diff] [blame] | 38 | { |
| 39 | if (_count == 0) |
| 40 | { |
| 41 | _stream << nack; |
| 42 | } else if (nack == _prevNack + 1) |
| 43 | { |
| 44 | _consecutive = true; |
| 45 | } else |
| 46 | { |
| 47 | if (_consecutive) |
| 48 | { |
| 49 | _stream << "-" << _prevNack; |
| 50 | _consecutive = false; |
| 51 | } |
| 52 | _stream << "," << nack; |
| 53 | } |
| 54 | _count++; |
| 55 | _prevNack = nack; |
| 56 | } |
| 57 | |
| 58 | std::string NACKStringBuilder::GetResult() |
| 59 | { |
| 60 | if (_consecutive) |
| 61 | { |
| 62 | _stream << "-" << _prevNack; |
| 63 | _consecutive = false; |
| 64 | } |
| 65 | return _stream.str(); |
| 66 | } |
| 67 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 68 | RTCPSender::RTCPSender(const int32_t id, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | const bool audio, |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 70 | Clock* clock, |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 71 | ModuleRtpRtcpImpl* owner, |
| 72 | ReceiveStatistics* receive_statistics) : |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | _id(id), |
| 74 | _audio(audio), |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 75 | _clock(clock), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | _method(kRtcpOff), |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 77 | _rtpRtcp(*owner), |
henrike@webrtc.org | 65573f2 | 2011-12-13 19:17:27 +0000 | [diff] [blame] | 78 | _criticalSectionTransport(CriticalSectionWrapper::CreateCriticalSection()), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | _cbTransport(NULL), |
| 80 | |
henrike@webrtc.org | 65573f2 | 2011-12-13 19:17:27 +0000 | [diff] [blame] | 81 | _criticalSectionRTCPSender(CriticalSectionWrapper::CreateCriticalSection()), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | _usingNack(false), |
| 83 | _sending(false), |
| 84 | _sendTMMBN(false), |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 85 | _REMB(false), |
| 86 | _sendREMB(false), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | _TMMBR(false), |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 88 | _IJ(false), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | _nextTimeToSendRTCP(0), |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 90 | start_timestamp_(0), |
| 91 | last_rtp_timestamp_(0), |
| 92 | last_frame_capture_time_ms_(-1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | _SSRC(0), |
| 94 | _remoteSSRC(0), |
| 95 | _CNAME(), |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 96 | receive_statistics_(receive_statistics), |
| 97 | internal_report_blocks_(), |
| 98 | external_report_blocks_(), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | _csrcCNAMEs(), |
| 100 | |
stefan@webrtc.org | 7da3459 | 2013-04-09 14:56:29 +0000 | [diff] [blame] | 101 | _cameraDelayMS(0), |
| 102 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | _lastSendReport(), |
| 104 | _lastRTCPTime(), |
| 105 | |
| 106 | _CSRCs(0), |
| 107 | _CSRC(), |
| 108 | _includeCSRCs(true), |
| 109 | |
| 110 | _sequenceNumberFIR(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 112 | _lengthRembSSRC(0), |
| 113 | _sizeRembSSRC(0), |
| 114 | _rembSSRC(NULL), |
| 115 | _rembBitrate(0), |
| 116 | |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +0000 | [diff] [blame] | 117 | _tmmbrHelp(), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | _tmmbr_Send(0), |
| 119 | _packetOH_Send(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | |
| 121 | _appSend(false), |
| 122 | _appSubType(0), |
| 123 | _appName(), |
| 124 | _appData(NULL), |
| 125 | _appLength(0), |
| 126 | _xrSendVoIPMetric(false), |
edjee@google.com | 79b0289 | 2013-04-04 19:43:34 +0000 | [diff] [blame] | 127 | _xrVoIPMetric(), |
| 128 | _nackCount(0), |
| 129 | _pliCount(0), |
| 130 | _fullIntraRequestCount(0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 131 | { |
| 132 | memset(_CNAME, 0, sizeof(_CNAME)); |
| 133 | memset(_lastSendReport, 0, sizeof(_lastSendReport)); |
| 134 | memset(_lastRTCPTime, 0, sizeof(_lastRTCPTime)); |
| 135 | |
| 136 | WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, id, "%s created", __FUNCTION__); |
| 137 | } |
| 138 | |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 139 | RTCPSender::~RTCPSender() { |
| 140 | delete [] _rembSSRC; |
| 141 | delete [] _appData; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 143 | while (!internal_report_blocks_.empty()) { |
| 144 | delete internal_report_blocks_.begin()->second; |
| 145 | internal_report_blocks_.erase(internal_report_blocks_.begin()); |
| 146 | } |
| 147 | while (!external_report_blocks_.empty()) { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 148 | std::map<uint32_t, RTCPReportBlock*>::iterator it = |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 149 | external_report_blocks_.begin(); |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 150 | delete it->second; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 151 | external_report_blocks_.erase(it); |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 152 | } |
| 153 | while (!_csrcCNAMEs.empty()) { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 154 | std::map<uint32_t, RTCPCnameInformation*>::iterator it = |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 155 | _csrcCNAMEs.begin(); |
| 156 | delete it->second; |
| 157 | _csrcCNAMEs.erase(it); |
| 158 | } |
| 159 | delete _criticalSectionTransport; |
| 160 | delete _criticalSectionRTCPSender; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 161 | |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 162 | WEBRTC_TRACE(kTraceMemory, kTraceRtpRtcp, _id, "%s deleted", __FUNCTION__); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | } |
| 164 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 165 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | RTCPSender::Init() |
| 167 | { |
| 168 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 169 | |
| 170 | _method = kRtcpOff; |
| 171 | _cbTransport = NULL; |
| 172 | _usingNack = false; |
| 173 | _sending = false; |
| 174 | _sendTMMBN = false; |
| 175 | _TMMBR = false; |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 176 | _IJ = false; |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 177 | _REMB = false; |
| 178 | _sendREMB = false; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 179 | last_rtp_timestamp_ = 0; |
| 180 | last_frame_capture_time_ms_ = -1; |
| 181 | start_timestamp_ = -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | _SSRC = 0; |
| 183 | _remoteSSRC = 0; |
stefan@webrtc.org | 7da3459 | 2013-04-09 14:56:29 +0000 | [diff] [blame] | 184 | _cameraDelayMS = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 185 | _sequenceNumberFIR = 0; |
| 186 | _tmmbr_Send = 0; |
| 187 | _packetOH_Send = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | _nextTimeToSendRTCP = 0; |
| 189 | _CSRCs = 0; |
| 190 | _appSend = false; |
| 191 | _appSubType = 0; |
| 192 | |
| 193 | if(_appData) |
| 194 | { |
| 195 | delete [] _appData; |
| 196 | _appData = NULL; |
| 197 | } |
| 198 | _appLength = 0; |
| 199 | |
| 200 | _xrSendVoIPMetric = false; |
| 201 | |
| 202 | memset(&_xrVoIPMetric, 0, sizeof(_xrVoIPMetric)); |
| 203 | memset(_CNAME, 0, sizeof(_CNAME)); |
| 204 | memset(_lastSendReport, 0, sizeof(_lastSendReport)); |
| 205 | memset(_lastRTCPTime, 0, sizeof(_lastRTCPTime)); |
edjee@google.com | 79b0289 | 2013-04-04 19:43:34 +0000 | [diff] [blame] | 206 | |
| 207 | _nackCount = 0; |
| 208 | _pliCount = 0; |
| 209 | _fullIntraRequestCount = 0; |
| 210 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | void |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 215 | RTCPSender::ChangeUniqueId(const int32_t id) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | { |
| 217 | _id = id; |
| 218 | } |
| 219 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 220 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 221 | RTCPSender::RegisterSendTransport(Transport* outgoingTransport) |
| 222 | { |
| 223 | CriticalSectionScoped lock(_criticalSectionTransport); |
| 224 | _cbTransport = outgoingTransport; |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | RTCPMethod |
| 229 | RTCPSender::Status() const |
| 230 | { |
| 231 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 232 | return _method; |
| 233 | } |
| 234 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 235 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 236 | RTCPSender::SetRTCPStatus(const RTCPMethod method) |
| 237 | { |
| 238 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 239 | if(method != kRtcpOff) |
| 240 | { |
| 241 | if(_audio) |
| 242 | { |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 243 | _nextTimeToSendRTCP = _clock->TimeInMilliseconds() + |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 244 | (RTCP_INTERVAL_AUDIO_MS/2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 245 | } else |
| 246 | { |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 247 | _nextTimeToSendRTCP = _clock->TimeInMilliseconds() + |
stefan@webrtc.org | 20ed36d | 2013-01-17 14:01:20 +0000 | [diff] [blame] | 248 | (RTCP_INTERVAL_VIDEO_MS/2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | _method = method; |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | bool |
| 256 | RTCPSender::Sending() const |
| 257 | { |
| 258 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 259 | return _sending; |
| 260 | } |
| 261 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 262 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 263 | RTCPSender::SetSendingStatus(const bool sending) |
| 264 | { |
| 265 | bool sendRTCPBye = false; |
| 266 | { |
| 267 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 268 | |
| 269 | if(_method != kRtcpOff) |
| 270 | { |
| 271 | if(sending == false && _sending == true) |
| 272 | { |
| 273 | // Trigger RTCP bye |
| 274 | sendRTCPBye = true; |
| 275 | } |
| 276 | } |
| 277 | _sending = sending; |
| 278 | } |
| 279 | if(sendRTCPBye) |
| 280 | { |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 281 | return SendRTCP(kRtcpBye); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 282 | } |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | bool |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 287 | RTCPSender::REMB() const |
| 288 | { |
| 289 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 290 | return _REMB; |
| 291 | } |
| 292 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 293 | int32_t |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 294 | RTCPSender::SetREMBStatus(const bool enable) |
| 295 | { |
| 296 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 297 | _REMB = enable; |
| 298 | return 0; |
| 299 | } |
| 300 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 301 | int32_t |
| 302 | RTCPSender::SetREMBData(const uint32_t bitrate, |
| 303 | const uint8_t numberOfSSRC, |
| 304 | const uint32_t* SSRC) |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 305 | { |
| 306 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 307 | _rembBitrate = bitrate; |
| 308 | |
| 309 | if(_sizeRembSSRC < numberOfSSRC) |
| 310 | { |
| 311 | delete [] _rembSSRC; |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 312 | _rembSSRC = new uint32_t[numberOfSSRC]; |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 313 | _sizeRembSSRC = numberOfSSRC; |
| 314 | } |
| 315 | |
| 316 | _lengthRembSSRC = numberOfSSRC; |
| 317 | for (int i = 0; i < numberOfSSRC; i++) |
| 318 | { |
| 319 | _rembSSRC[i] = SSRC[i]; |
| 320 | } |
mflodman@webrtc.org | 84dc3d1 | 2011-12-22 10:26:13 +0000 | [diff] [blame] | 321 | _sendREMB = true; |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | bool |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 326 | RTCPSender::TMMBR() const |
| 327 | { |
| 328 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 329 | return _TMMBR; |
| 330 | } |
| 331 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 332 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 333 | RTCPSender::SetTMMBRStatus(const bool enable) |
| 334 | { |
| 335 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 336 | _TMMBR = enable; |
| 337 | return 0; |
| 338 | } |
| 339 | |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 340 | bool |
| 341 | RTCPSender::IJ() const |
| 342 | { |
| 343 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 344 | return _IJ; |
| 345 | } |
| 346 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 347 | int32_t |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 348 | RTCPSender::SetIJStatus(const bool enable) |
| 349 | { |
| 350 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 351 | _IJ = enable; |
| 352 | return 0; |
| 353 | } |
| 354 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 355 | void RTCPSender::SetStartTimestamp(uint32_t start_timestamp) { |
| 356 | start_timestamp_ = start_timestamp; |
| 357 | } |
| 358 | |
| 359 | void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp, |
| 360 | int64_t capture_time_ms) { |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame] | 361 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 362 | last_rtp_timestamp_ = rtp_timestamp; |
| 363 | if (capture_time_ms < 0) { |
| 364 | // We don't currently get a capture time from VoiceEngine. |
stefan@webrtc.org | 7da3459 | 2013-04-09 14:56:29 +0000 | [diff] [blame] | 365 | last_frame_capture_time_ms_ = _clock->TimeInMilliseconds(); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 366 | } else { |
| 367 | last_frame_capture_time_ms_ = capture_time_ms; |
| 368 | } |
| 369 | } |
| 370 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 371 | void |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 372 | RTCPSender::SetSSRC( const uint32_t ssrc) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 373 | { |
| 374 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 375 | |
| 376 | if(_SSRC != 0) |
| 377 | { |
| 378 | // not first SetSSRC, probably due to a collision |
| 379 | // schedule a new RTCP report |
pwestin@webrtc.org | 0644b1d | 2011-12-01 15:42:31 +0000 | [diff] [blame] | 380 | // make sure that we send a RTP packet |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 381 | _nextTimeToSendRTCP = _clock->TimeInMilliseconds() + 100; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 382 | } |
| 383 | _SSRC = ssrc; |
| 384 | } |
| 385 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 386 | void RTCPSender::SetRemoteSSRC(uint32_t ssrc) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 387 | { |
| 388 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 389 | _remoteSSRC = ssrc; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 390 | } |
| 391 | |
stefan@webrtc.org | 7da3459 | 2013-04-09 14:56:29 +0000 | [diff] [blame] | 392 | int32_t |
| 393 | RTCPSender::SetCameraDelay(const int32_t delayMS) |
| 394 | { |
| 395 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 396 | if(delayMS > 1000 || delayMS < -1000) |
| 397 | { |
| 398 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id, "%s invalid argument, delay can't be larger than 1 sec", __FUNCTION__); |
| 399 | return -1; |
| 400 | } |
| 401 | _cameraDelayMS = delayMS; |
| 402 | return 0; |
| 403 | } |
| 404 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 405 | int32_t RTCPSender::CNAME(char cName[RTCP_CNAME_SIZE]) { |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 406 | assert(cName); |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 407 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 408 | cName[RTCP_CNAME_SIZE - 1] = 0; |
| 409 | strncpy(cName, _CNAME, RTCP_CNAME_SIZE - 1); |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 413 | int32_t RTCPSender::SetCNAME(const char cName[RTCP_CNAME_SIZE]) { |
tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 414 | if (!cName) |
| 415 | return -1; |
| 416 | |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 417 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 418 | _CNAME[RTCP_CNAME_SIZE - 1] = 0; |
| 419 | strncpy(_CNAME, cName, RTCP_CNAME_SIZE - 1); |
| 420 | return 0; |
| 421 | } |
| 422 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 423 | int32_t RTCPSender::AddMixedCNAME(const uint32_t SSRC, |
| 424 | const char cName[RTCP_CNAME_SIZE]) { |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 425 | assert(cName); |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 426 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 427 | if (_csrcCNAMEs.size() >= kRtpCsrcSize) { |
| 428 | return -1; |
| 429 | } |
| 430 | RTCPCnameInformation* ptr = new RTCPCnameInformation(); |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 431 | ptr->name[RTCP_CNAME_SIZE - 1] = 0; |
| 432 | strncpy(ptr->name, cName, RTCP_CNAME_SIZE - 1); |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 433 | _csrcCNAMEs[SSRC] = ptr; |
| 434 | return 0; |
| 435 | } |
| 436 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 437 | int32_t RTCPSender::RemoveMixedCNAME(const uint32_t SSRC) { |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 438 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 439 | std::map<uint32_t, RTCPCnameInformation*>::iterator it = |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 440 | _csrcCNAMEs.find(SSRC); |
| 441 | |
| 442 | if (it == _csrcCNAMEs.end()) { |
| 443 | return -1; |
| 444 | } |
| 445 | delete it->second; |
| 446 | _csrcCNAMEs.erase(it); |
| 447 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | bool |
| 451 | RTCPSender::TimeToSendRTCPReport(const bool sendKeyframeBeforeRTP) const |
| 452 | { |
| 453 | /* |
| 454 | For audio we use a fix 5 sec interval |
| 455 | |
| 456 | For video we use 1 sec interval fo a BW smaller than 360 kbit/s, |
| 457 | technicaly we break the max 5% RTCP BW for video below 10 kbit/s but that should be extreamly rare |
| 458 | |
| 459 | |
| 460 | From RFC 3550 |
| 461 | |
| 462 | MAX RTCP BW is 5% if the session BW |
| 463 | A send report is approximately 65 bytes inc CNAME |
| 464 | A report report is approximately 28 bytes |
| 465 | |
| 466 | The RECOMMENDED value for the reduced minimum in seconds is 360 |
| 467 | divided by the session bandwidth in kilobits/second. This minimum |
| 468 | is smaller than 5 seconds for bandwidths greater than 72 kb/s. |
| 469 | |
| 470 | If the participant has not yet sent an RTCP packet (the variable |
| 471 | initial is true), the constant Tmin is set to 2.5 seconds, else it |
| 472 | is set to 5 seconds. |
| 473 | |
| 474 | The interval between RTCP packets is varied randomly over the |
| 475 | range [0.5,1.5] times the calculated interval to avoid unintended |
| 476 | synchronization of all participants |
| 477 | |
| 478 | if we send |
| 479 | If the participant is a sender (we_sent true), the constant C is |
| 480 | set to the average RTCP packet size (avg_rtcp_size) divided by 25% |
| 481 | of the RTCP bandwidth (rtcp_bw), and the constant n is set to the |
| 482 | number of senders. |
| 483 | |
| 484 | if we receive only |
| 485 | If we_sent is not true, the constant C is set |
| 486 | to the average RTCP packet size divided by 75% of the RTCP |
| 487 | bandwidth. The constant n is set to the number of receivers |
| 488 | (members - senders). If the number of senders is greater than |
| 489 | 25%, senders and receivers are treated together. |
| 490 | |
| 491 | reconsideration NOT required for peer-to-peer |
| 492 | "timer reconsideration" is |
| 493 | employed. This algorithm implements a simple back-off mechanism |
| 494 | which causes users to hold back RTCP packet transmission if the |
| 495 | group sizes are increasing. |
| 496 | |
| 497 | n = number of members |
| 498 | C = avg_size/(rtcpBW/4) |
| 499 | |
| 500 | 3. The deterministic calculated interval Td is set to max(Tmin, n*C). |
| 501 | |
| 502 | 4. The calculated interval T is set to a number uniformly distributed |
| 503 | between 0.5 and 1.5 times the deterministic calculated interval. |
| 504 | |
| 505 | 5. The resulting value of T is divided by e-3/2=1.21828 to compensate |
| 506 | for the fact that the timer reconsideration algorithm converges to |
| 507 | a value of the RTCP bandwidth below the intended average |
| 508 | */ |
| 509 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 510 | int64_t now = _clock->TimeInMilliseconds(); |
xians@webrtc.org | 8738d27 | 2011-11-25 13:43:53 +0000 | [diff] [blame] | 511 | |
| 512 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 513 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 514 | if(_method == kRtcpOff) |
| 515 | { |
| 516 | return false; |
| 517 | } |
| 518 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 519 | if(!_audio && sendKeyframeBeforeRTP) |
| 520 | { |
| 521 | // for video key-frames we want to send the RTCP before the large key-frame |
| 522 | // if we have a 100 ms margin |
| 523 | now += RTCP_SEND_BEFORE_KEY_FRAME_MS; |
| 524 | } |
| 525 | |
| 526 | if(now > _nextTimeToSendRTCP) |
| 527 | { |
| 528 | return true; |
| 529 | |
| 530 | } else if(now < 0x0000ffff && _nextTimeToSendRTCP > 0xffff0000) // 65 sec margin |
| 531 | { |
| 532 | // wrap |
| 533 | return true; |
| 534 | } |
| 535 | return false; |
| 536 | } |
| 537 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 538 | uint32_t |
| 539 | RTCPSender::LastSendReport( uint32_t& lastRTCPTime) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 540 | { |
| 541 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 542 | |
| 543 | lastRTCPTime = _lastRTCPTime[0]; |
| 544 | return _lastSendReport[0]; |
| 545 | } |
| 546 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 547 | uint32_t |
| 548 | RTCPSender::SendTimeOfSendReport(const uint32_t sendReport) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 549 | { |
| 550 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 551 | |
| 552 | // This is only saved when we are the sender |
| 553 | if((_lastSendReport[0] == 0) || (sendReport == 0)) |
| 554 | { |
| 555 | return 0; // will be ignored |
| 556 | } else |
| 557 | { |
| 558 | for(int i = 0; i < RTCP_NUMBER_OF_SR; ++i) |
| 559 | { |
| 560 | if( _lastSendReport[i] == sendReport) |
| 561 | { |
| 562 | return _lastRTCPTime[i]; |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | return 0; |
| 567 | } |
| 568 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 569 | int32_t RTCPSender::AddExternalReportBlock( |
| 570 | uint32_t SSRC, |
| 571 | const RTCPReportBlock* reportBlock) { |
| 572 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 573 | return AddReportBlock(SSRC, &external_report_blocks_, reportBlock); |
| 574 | } |
| 575 | |
| 576 | int32_t RTCPSender::AddReportBlock( |
| 577 | uint32_t SSRC, |
| 578 | std::map<uint32_t, RTCPReportBlock*>* report_blocks, |
| 579 | const RTCPReportBlock* reportBlock) { |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 580 | if (reportBlock == NULL) { |
| 581 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id, |
| 582 | "%s invalid argument", __FUNCTION__); |
| 583 | return -1; |
| 584 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 585 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 586 | if (report_blocks->size() >= RTCP_MAX_REPORT_BLOCKS) { |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 587 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id, |
| 588 | "%s invalid argument", __FUNCTION__); |
| 589 | return -1; |
| 590 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 591 | std::map<uint32_t, RTCPReportBlock*>::iterator it = |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 592 | report_blocks->find(SSRC); |
| 593 | if (it != report_blocks->end()) { |
stefan@webrtc.org | 8d0cd07 | 2012-12-03 14:01:46 +0000 | [diff] [blame] | 594 | delete it->second; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 595 | report_blocks->erase(it); |
stefan@webrtc.org | 8d0cd07 | 2012-12-03 14:01:46 +0000 | [diff] [blame] | 596 | } |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 597 | RTCPReportBlock* copyReportBlock = new RTCPReportBlock(); |
| 598 | memcpy(copyReportBlock, reportBlock, sizeof(RTCPReportBlock)); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 599 | (*report_blocks)[SSRC] = copyReportBlock; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 600 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 601 | } |
| 602 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 603 | int32_t RTCPSender::RemoveExternalReportBlock(uint32_t SSRC) { |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 604 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 605 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 606 | std::map<uint32_t, RTCPReportBlock*>::iterator it = |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 607 | external_report_blocks_.find(SSRC); |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 608 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 609 | if (it == external_report_blocks_.end()) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 610 | return -1; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 611 | } |
| 612 | delete it->second; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 613 | external_report_blocks_.erase(it); |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 614 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 615 | } |
| 616 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 617 | int32_t |
| 618 | RTCPSender::BuildSR(uint8_t* rtcpbuffer, |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 619 | int& pos, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 620 | const uint32_t NTPsec, |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 621 | const uint32_t NTPfrac) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 622 | { |
| 623 | // sanity |
| 624 | if(pos + 52 >= IP_PACKET_SIZE) |
| 625 | { |
| 626 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id, "%s invalid argument", __FUNCTION__); |
| 627 | return -2; |
| 628 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 629 | uint32_t RTPtime; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 630 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 631 | uint32_t posNumberOfReportBlocks = pos; |
| 632 | rtcpbuffer[pos++]=(uint8_t)0x80; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 633 | |
| 634 | // Sender report |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 635 | rtcpbuffer[pos++]=(uint8_t)200; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 636 | |
| 637 | for(int i = (RTCP_NUMBER_OF_SR-2); i >= 0; i--) |
| 638 | { |
| 639 | // shift old |
| 640 | _lastSendReport[i+1] = _lastSendReport[i]; |
| 641 | _lastRTCPTime[i+1] =_lastRTCPTime[i]; |
| 642 | } |
| 643 | |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 644 | _lastRTCPTime[0] = Clock::NtpToMs(NTPsec, NTPfrac); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 645 | _lastSendReport[0] = (NTPsec << 16) + (NTPfrac >> 16); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 646 | |
stefan@webrtc.org | 7da3459 | 2013-04-09 14:56:29 +0000 | [diff] [blame] | 647 | uint32_t freqHz = 90000; // For video |
| 648 | if(_audio) { |
| 649 | freqHz = _rtpRtcp.CurrentSendFrequencyHz(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 650 | } |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame] | 651 | |
stefan@webrtc.org | 7da3459 | 2013-04-09 14:56:29 +0000 | [diff] [blame] | 652 | // The timestamp of this RTCP packet should be estimated as the timestamp of |
| 653 | // the frame being captured at this moment. We are calculating that |
| 654 | // timestamp as the last frame's timestamp + the time since the last frame |
| 655 | // was captured. |
| 656 | { |
| 657 | // Needs protection since this method is called on the process thread. |
| 658 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 659 | RTPtime = start_timestamp_ + last_rtp_timestamp_ + ( |
| 660 | _clock->TimeInMilliseconds() - last_frame_capture_time_ms_) * |
| 661 | (freqHz / 1000); |
| 662 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 663 | |
| 664 | // Add sender data |
| 665 | // Save for our length field |
| 666 | pos++; |
| 667 | pos++; |
| 668 | |
| 669 | // Add our own SSRC |
| 670 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 671 | pos += 4; |
| 672 | // NTP |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 673 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, NTPsec); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 674 | pos += 4; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 675 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, NTPfrac); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 676 | pos += 4; |
| 677 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, RTPtime); |
| 678 | pos += 4; |
| 679 | |
| 680 | //sender's packet count |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 681 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _rtpRtcp.PacketCountSent()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 682 | pos += 4; |
| 683 | |
| 684 | //sender's octet count |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 685 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _rtpRtcp.ByteCountSent()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 686 | pos += 4; |
| 687 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 688 | uint8_t numberOfReportBlocks = 0; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 689 | int32_t retVal = WriteAllReportBlocksToBuffer(rtcpbuffer, pos, |
| 690 | numberOfReportBlocks, |
| 691 | NTPsec, NTPfrac); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 692 | if(retVal < 0) |
| 693 | { |
| 694 | // |
| 695 | return retVal ; |
| 696 | } |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 697 | pos = retVal; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 698 | rtcpbuffer[posNumberOfReportBlocks] += numberOfReportBlocks; |
| 699 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 700 | uint16_t len = uint16_t((pos/4) -1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 701 | ModuleRTPUtility::AssignUWord16ToBuffer(rtcpbuffer+2, len); |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 706 | int32_t RTCPSender::BuildSDEC(uint8_t* rtcpbuffer, int& pos) { |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 707 | size_t lengthCname = strlen(_CNAME); |
| 708 | assert(lengthCname < RTCP_CNAME_SIZE); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 709 | |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 710 | // sanity |
| 711 | if(pos + 12 + lengthCname >= IP_PACKET_SIZE) { |
| 712 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id, |
| 713 | "%s invalid argument", __FUNCTION__); |
| 714 | return -2; |
| 715 | } |
| 716 | // SDEC Source Description |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 717 | |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 718 | // We always need to add SDES CNAME |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 719 | rtcpbuffer[pos++] = static_cast<uint8_t>(0x80 + 1 + _csrcCNAMEs.size()); |
| 720 | rtcpbuffer[pos++] = static_cast<uint8_t>(202); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 721 | |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 722 | // handle SDES length later on |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 723 | uint32_t SDESLengthPos = pos; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 724 | pos++; |
| 725 | pos++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 726 | |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 727 | // Add our own SSRC |
| 728 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 729 | pos += 4; |
| 730 | |
| 731 | // CNAME = 1 |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 732 | rtcpbuffer[pos++] = static_cast<uint8_t>(1); |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 733 | |
| 734 | // |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 735 | rtcpbuffer[pos++] = static_cast<uint8_t>(lengthCname); |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 736 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 737 | uint16_t SDESLength = 10; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 738 | |
| 739 | memcpy(&rtcpbuffer[pos], _CNAME, lengthCname); |
| 740 | pos += lengthCname; |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 741 | SDESLength += (uint16_t)lengthCname; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 742 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 743 | uint16_t padding = 0; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 744 | // We must have a zero field even if we have an even multiple of 4 bytes |
| 745 | if ((pos % 4) == 0) { |
| 746 | padding++; |
| 747 | rtcpbuffer[pos++]=0; |
| 748 | } |
| 749 | while ((pos % 4) != 0) { |
| 750 | padding++; |
| 751 | rtcpbuffer[pos++]=0; |
| 752 | } |
| 753 | SDESLength += padding; |
| 754 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 755 | std::map<uint32_t, RTCPUtility::RTCPCnameInformation*>::iterator it = |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 756 | _csrcCNAMEs.begin(); |
| 757 | |
| 758 | for(; it != _csrcCNAMEs.end(); it++) { |
| 759 | RTCPCnameInformation* cname = it->second; |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 760 | uint32_t SSRC = it->first; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 761 | |
| 762 | // Add SSRC |
| 763 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, SSRC); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 764 | pos += 4; |
| 765 | |
| 766 | // CNAME = 1 |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 767 | rtcpbuffer[pos++] = static_cast<uint8_t>(1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 768 | |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 769 | size_t length = strlen(cname->name); |
| 770 | assert(length < RTCP_CNAME_SIZE); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 771 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 772 | rtcpbuffer[pos++]= static_cast<uint8_t>(length); |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 773 | SDESLength += 6; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 774 | |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 775 | memcpy(&rtcpbuffer[pos],cname->name, length); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 776 | |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 777 | pos += length; |
| 778 | SDESLength += length; |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 779 | uint16_t padding = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 780 | |
| 781 | // We must have a zero field even if we have an even multiple of 4 bytes |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 782 | if((pos % 4) == 0){ |
| 783 | padding++; |
| 784 | rtcpbuffer[pos++]=0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 785 | } |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 786 | while((pos % 4) != 0){ |
| 787 | padding++; |
| 788 | rtcpbuffer[pos++] = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 789 | } |
| 790 | SDESLength += padding; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 791 | } |
| 792 | // in 32-bit words minus one and we don't count the header |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 793 | uint16_t buffer_length = (SDESLength / 4) - 1; |
pwestin@webrtc.org | f6bb77a | 2012-01-24 17:16:59 +0000 | [diff] [blame] | 794 | ModuleRTPUtility::AssignUWord16ToBuffer(rtcpbuffer + SDESLengthPos, |
| 795 | buffer_length); |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 796 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 797 | } |
| 798 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 799 | int32_t |
| 800 | RTCPSender::BuildRR(uint8_t* rtcpbuffer, |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 801 | int& pos, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 802 | const uint32_t NTPsec, |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 803 | const uint32_t NTPfrac) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 804 | { |
| 805 | // sanity one block |
| 806 | if(pos + 32 >= IP_PACKET_SIZE) |
| 807 | { |
| 808 | return -2; |
| 809 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 810 | uint32_t posNumberOfReportBlocks = pos; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 811 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 812 | rtcpbuffer[pos++]=(uint8_t)0x80; |
| 813 | rtcpbuffer[pos++]=(uint8_t)201; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 814 | |
| 815 | // Save for our length field |
| 816 | pos++; |
| 817 | pos++; |
| 818 | |
| 819 | // Add our own SSRC |
| 820 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 821 | pos += 4; |
| 822 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 823 | uint8_t numberOfReportBlocks = 0; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 824 | int retVal = WriteAllReportBlocksToBuffer(rtcpbuffer, pos, |
| 825 | numberOfReportBlocks, |
| 826 | NTPsec, NTPfrac); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 827 | if(retVal < 0) |
| 828 | { |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 829 | return pos; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 830 | } |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 831 | pos = retVal; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 832 | rtcpbuffer[posNumberOfReportBlocks] += numberOfReportBlocks; |
| 833 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 834 | uint16_t len = uint16_t((pos)/4 -1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 835 | ModuleRTPUtility::AssignUWord16ToBuffer(rtcpbuffer+2, len); |
| 836 | return 0; |
| 837 | } |
| 838 | |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 839 | // From RFC 5450: Transmission Time Offsets in RTP Streams. |
| 840 | // 0 1 2 3 |
| 841 | // 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 |
| 842 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 843 | // hdr |V=2|P| RC | PT=IJ=195 | length | |
| 844 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 845 | // | inter-arrival jitter | |
| 846 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 847 | // . . |
| 848 | // . . |
| 849 | // . . |
| 850 | // | inter-arrival jitter | |
| 851 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 852 | // |
| 853 | // If present, this RTCP packet must be placed after a receiver report |
| 854 | // (inside a compound RTCP packet), and MUST have the same value for RC |
| 855 | // (reception report count) as the receiver report. |
| 856 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 857 | int32_t |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 858 | RTCPSender::BuildExtendedJitterReport( |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 859 | uint8_t* rtcpbuffer, |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 860 | int& pos, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 861 | const uint32_t jitterTransmissionTimeOffset) |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 862 | { |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 863 | if (external_report_blocks_.size() > 0) |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 864 | { |
| 865 | WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, _id, "Not implemented."); |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | // sanity |
| 870 | if(pos + 8 >= IP_PACKET_SIZE) |
| 871 | { |
| 872 | return -2; |
| 873 | } |
| 874 | // add picture loss indicator |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 875 | uint8_t RC = 1; |
| 876 | rtcpbuffer[pos++]=(uint8_t)0x80 + RC; |
| 877 | rtcpbuffer[pos++]=(uint8_t)195; |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 878 | |
| 879 | // Used fixed length of 2 |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 880 | rtcpbuffer[pos++]=(uint8_t)0; |
| 881 | rtcpbuffer[pos++]=(uint8_t)(1); |
asapersson@webrtc.org | 5249cc8 | 2011-12-16 14:31:37 +0000 | [diff] [blame] | 882 | |
| 883 | // Add inter-arrival jitter |
| 884 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, |
| 885 | jitterTransmissionTimeOffset); |
| 886 | pos += 4; |
| 887 | return 0; |
| 888 | } |
| 889 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 890 | int32_t |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 891 | RTCPSender::BuildPLI(uint8_t* rtcpbuffer, int& pos) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 892 | { |
| 893 | // sanity |
| 894 | if(pos + 12 >= IP_PACKET_SIZE) |
| 895 | { |
| 896 | return -2; |
| 897 | } |
| 898 | // add picture loss indicator |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 899 | uint8_t FMT = 1; |
| 900 | rtcpbuffer[pos++]=(uint8_t)0x80 + FMT; |
| 901 | rtcpbuffer[pos++]=(uint8_t)206; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 902 | |
| 903 | //Used fixed length of 2 |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 904 | rtcpbuffer[pos++]=(uint8_t)0; |
| 905 | rtcpbuffer[pos++]=(uint8_t)(2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 906 | |
| 907 | // Add our own SSRC |
| 908 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 909 | pos += 4; |
| 910 | |
| 911 | // Add the remote SSRC |
| 912 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _remoteSSRC); |
| 913 | pos += 4; |
| 914 | return 0; |
| 915 | } |
| 916 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 917 | int32_t RTCPSender::BuildFIR(uint8_t* rtcpbuffer, |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 918 | int& pos, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 919 | bool repeat) { |
pwestin@webrtc.org | 5e95481 | 2012-02-10 12:13:12 +0000 | [diff] [blame] | 920 | // sanity |
| 921 | if(pos + 20 >= IP_PACKET_SIZE) { |
| 922 | return -2; |
| 923 | } |
| 924 | if (!repeat) { |
| 925 | _sequenceNumberFIR++; // do not increase if repetition |
| 926 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 927 | |
pwestin@webrtc.org | 5e95481 | 2012-02-10 12:13:12 +0000 | [diff] [blame] | 928 | // add full intra request indicator |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 929 | uint8_t FMT = 4; |
| 930 | rtcpbuffer[pos++] = (uint8_t)0x80 + FMT; |
| 931 | rtcpbuffer[pos++] = (uint8_t)206; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 932 | |
pwestin@webrtc.org | 5e95481 | 2012-02-10 12:13:12 +0000 | [diff] [blame] | 933 | //Length of 4 |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 934 | rtcpbuffer[pos++] = (uint8_t)0; |
| 935 | rtcpbuffer[pos++] = (uint8_t)(4); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 936 | |
pwestin@webrtc.org | 5e95481 | 2012-02-10 12:13:12 +0000 | [diff] [blame] | 937 | // Add our own SSRC |
| 938 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _SSRC); |
| 939 | pos += 4; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 940 | |
pwestin@webrtc.org | 5e95481 | 2012-02-10 12:13:12 +0000 | [diff] [blame] | 941 | // RFC 5104 4.3.1.2. Semantics |
| 942 | // SSRC of media source |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 943 | rtcpbuffer[pos++] = (uint8_t)0; |
| 944 | rtcpbuffer[pos++] = (uint8_t)0; |
| 945 | rtcpbuffer[pos++] = (uint8_t)0; |
| 946 | rtcpbuffer[pos++] = (uint8_t)0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 947 | |
pwestin@webrtc.org | 5e95481 | 2012-02-10 12:13:12 +0000 | [diff] [blame] | 948 | // Additional Feedback Control Information (FCI) |
| 949 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer + pos, _remoteSSRC); |
| 950 | pos += 4; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 951 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 952 | rtcpbuffer[pos++] = (uint8_t)(_sequenceNumberFIR); |
| 953 | rtcpbuffer[pos++] = (uint8_t)0; |
| 954 | rtcpbuffer[pos++] = (uint8_t)0; |
| 955 | rtcpbuffer[pos++] = (uint8_t)0; |
pwestin@webrtc.org | 5e95481 | 2012-02-10 12:13:12 +0000 | [diff] [blame] | 956 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | /* |
| 960 | 0 1 2 3 |
| 961 | 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 |
| 962 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 963 | | First | Number | PictureID | |
| 964 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 965 | */ |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 966 | int32_t |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 967 | RTCPSender::BuildSLI(uint8_t* rtcpbuffer, int& pos, const uint8_t pictureID) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 968 | { |
| 969 | // sanity |
| 970 | if(pos + 16 >= IP_PACKET_SIZE) |
| 971 | { |
| 972 | return -2; |
| 973 | } |
| 974 | // add slice loss indicator |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 975 | uint8_t FMT = 2; |
| 976 | rtcpbuffer[pos++]=(uint8_t)0x80 + FMT; |
| 977 | rtcpbuffer[pos++]=(uint8_t)206; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 978 | |
| 979 | //Used fixed length of 3 |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 980 | rtcpbuffer[pos++]=(uint8_t)0; |
| 981 | rtcpbuffer[pos++]=(uint8_t)(3); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 982 | |
| 983 | // Add our own SSRC |
| 984 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 985 | pos += 4; |
| 986 | |
| 987 | // Add the remote SSRC |
| 988 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _remoteSSRC); |
| 989 | pos += 4; |
| 990 | |
| 991 | // Add first, number & picture ID 6 bits |
| 992 | // first = 0, 13 - bits |
| 993 | // number = 0x1fff, 13 - bits only ones for now |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 994 | uint32_t sliField = (0x1fff << 6)+ (0x3f & pictureID); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 995 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, sliField); |
| 996 | pos += 4; |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | 0 1 2 3 |
| 1002 | 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 |
| 1003 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1004 | | PB |0| Payload Type| Native RPSI bit string | |
| 1005 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1006 | | defined per codec ... | Padding (0) | |
| 1007 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1008 | */ |
| 1009 | /* |
| 1010 | * Note: not generic made for VP8 |
| 1011 | */ |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1012 | int32_t |
| 1013 | RTCPSender::BuildRPSI(uint8_t* rtcpbuffer, |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 1014 | int& pos, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1015 | const uint64_t pictureID, |
| 1016 | const uint8_t payloadType) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1017 | { |
| 1018 | // sanity |
| 1019 | if(pos + 24 >= IP_PACKET_SIZE) |
| 1020 | { |
| 1021 | return -2; |
| 1022 | } |
| 1023 | // add Reference Picture Selection Indication |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1024 | uint8_t FMT = 3; |
| 1025 | rtcpbuffer[pos++]=(uint8_t)0x80 + FMT; |
| 1026 | rtcpbuffer[pos++]=(uint8_t)206; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1027 | |
| 1028 | // calc length |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1029 | uint32_t bitsRequired = 7; |
| 1030 | uint8_t bytesRequired = 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1031 | while((pictureID>>bitsRequired) > 0) |
| 1032 | { |
| 1033 | bitsRequired += 7; |
| 1034 | bytesRequired++; |
| 1035 | } |
| 1036 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1037 | uint8_t size = 3; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1038 | if(bytesRequired > 6) |
| 1039 | { |
| 1040 | size = 5; |
| 1041 | } else if(bytesRequired > 2) |
| 1042 | { |
| 1043 | size = 4; |
| 1044 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1045 | rtcpbuffer[pos++]=(uint8_t)0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1046 | rtcpbuffer[pos++]=size; |
| 1047 | |
| 1048 | // Add our own SSRC |
| 1049 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 1050 | pos += 4; |
| 1051 | |
| 1052 | // Add the remote SSRC |
| 1053 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _remoteSSRC); |
| 1054 | pos += 4; |
| 1055 | |
| 1056 | // calc padding length |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1057 | uint8_t paddingBytes = 4-((2+bytesRequired)%4); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1058 | if(paddingBytes == 4) |
| 1059 | { |
| 1060 | paddingBytes = 0; |
| 1061 | } |
| 1062 | // add padding length in bits |
| 1063 | rtcpbuffer[pos] = paddingBytes*8; // padding can be 0, 8, 16 or 24 |
| 1064 | pos++; |
| 1065 | |
| 1066 | // add payload type |
| 1067 | rtcpbuffer[pos] = payloadType; |
| 1068 | pos++; |
| 1069 | |
| 1070 | // add picture ID |
| 1071 | for(int i = bytesRequired-1; i > 0; i--) |
| 1072 | { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1073 | rtcpbuffer[pos] = 0x80 | uint8_t(pictureID >> (i*7)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1074 | pos++; |
| 1075 | } |
| 1076 | // add last byte of picture ID |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1077 | rtcpbuffer[pos] = uint8_t(pictureID & 0x7f); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1078 | pos++; |
| 1079 | |
| 1080 | // add padding |
| 1081 | for(int j = 0; j <paddingBytes; j++) |
| 1082 | { |
| 1083 | rtcpbuffer[pos] = 0; |
| 1084 | pos++; |
| 1085 | } |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1089 | int32_t |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 1090 | RTCPSender::BuildREMB(uint8_t* rtcpbuffer, int& pos) |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 1091 | { |
| 1092 | // sanity |
| 1093 | if(pos + 20 + 4 * _lengthRembSSRC >= IP_PACKET_SIZE) |
| 1094 | { |
| 1095 | return -2; |
| 1096 | } |
| 1097 | // add application layer feedback |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1098 | uint8_t FMT = 15; |
| 1099 | rtcpbuffer[pos++]=(uint8_t)0x80 + FMT; |
| 1100 | rtcpbuffer[pos++]=(uint8_t)206; |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 1101 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1102 | rtcpbuffer[pos++]=(uint8_t)0; |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 1103 | rtcpbuffer[pos++]=_lengthRembSSRC + 4; |
| 1104 | |
| 1105 | // Add our own SSRC |
| 1106 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 1107 | pos += 4; |
| 1108 | |
| 1109 | // Remote SSRC must be 0 |
| 1110 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, 0); |
| 1111 | pos += 4; |
| 1112 | |
| 1113 | rtcpbuffer[pos++]='R'; |
| 1114 | rtcpbuffer[pos++]='E'; |
| 1115 | rtcpbuffer[pos++]='M'; |
| 1116 | rtcpbuffer[pos++]='B'; |
| 1117 | |
| 1118 | rtcpbuffer[pos++] = _lengthRembSSRC; |
| 1119 | // 6 bit Exp |
| 1120 | // 18 bit mantissa |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1121 | uint8_t brExp = 0; |
| 1122 | for(uint32_t i=0; i<64; i++) |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 1123 | { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1124 | if(_rembBitrate <= ((uint32_t)262143 << i)) |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 1125 | { |
| 1126 | brExp = i; |
| 1127 | break; |
| 1128 | } |
| 1129 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1130 | const uint32_t brMantissa = (_rembBitrate >> brExp); |
| 1131 | rtcpbuffer[pos++]=(uint8_t)((brExp << 2) + ((brMantissa >> 16) & 0x03)); |
| 1132 | rtcpbuffer[pos++]=(uint8_t)(brMantissa >> 8); |
| 1133 | rtcpbuffer[pos++]=(uint8_t)(brMantissa); |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 1134 | |
| 1135 | for (int i = 0; i < _lengthRembSSRC; i++) |
| 1136 | { |
| 1137 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _rembSSRC[i]); |
| 1138 | pos += 4; |
| 1139 | } |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
stefan@webrtc.org | 9354cc9 | 2012-06-07 08:10:14 +0000 | [diff] [blame] | 1143 | void |
| 1144 | RTCPSender::SetTargetBitrate(unsigned int target_bitrate) |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 1145 | { |
mflodman@webrtc.org | 117c119 | 2012-01-13 08:52:58 +0000 | [diff] [blame] | 1146 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
pwestin@webrtc.org | 741da94 | 2011-09-20 13:52:04 +0000 | [diff] [blame] | 1147 | _tmmbr_Send = target_bitrate / 1000; |
mflodman@webrtc.org | 117c119 | 2012-01-13 08:52:58 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1150 | int32_t |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 1151 | RTCPSender::BuildTMMBR(uint8_t* rtcpbuffer, int& pos) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1152 | { |
| 1153 | // Before sending the TMMBR check the received TMMBN, only an owner is allowed to raise the bitrate |
| 1154 | // If the sender is an owner of the TMMBN -> send TMMBR |
| 1155 | // If not an owner but the TMMBR would enter the TMMBN -> send TMMBR |
| 1156 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1157 | // get current bounding set from RTCP receiver |
| 1158 | bool tmmbrOwner = false; |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 1159 | // store in candidateSet, allocates one extra slot |
| 1160 | TMMBRSet* candidateSet = _tmmbrHelp.CandidateSet(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1161 | |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 1162 | // holding _criticalSectionRTCPSender while calling RTCPreceiver which |
| 1163 | // will accuire _criticalSectionRTCPReceiver is a potental deadlock but |
| 1164 | // since RTCPreceiver is not doing the reverse we should be fine |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1165 | int32_t lengthOfBoundingSet |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 1166 | = _rtpRtcp.BoundingSet(tmmbrOwner, candidateSet); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1167 | |
| 1168 | if(lengthOfBoundingSet > 0) |
| 1169 | { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1170 | for (int32_t i = 0; i < lengthOfBoundingSet; i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1171 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 1172 | if( candidateSet->Tmmbr(i) == _tmmbr_Send && |
| 1173 | candidateSet->PacketOH(i) == _packetOH_Send) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1174 | { |
| 1175 | // do not send the same tuple |
| 1176 | return 0; |
| 1177 | } |
| 1178 | } |
| 1179 | if(!tmmbrOwner) |
| 1180 | { |
| 1181 | // use received bounding set as candidate set |
| 1182 | // add current tuple |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 1183 | candidateSet->SetEntry(lengthOfBoundingSet, |
| 1184 | _tmmbr_Send, |
| 1185 | _packetOH_Send, |
| 1186 | _SSRC); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1187 | int numCandidates = lengthOfBoundingSet+ 1; |
| 1188 | |
| 1189 | // find bounding set |
| 1190 | TMMBRSet* boundingSet = NULL; |
| 1191 | int numBoundingSet = _tmmbrHelp.FindTMMBRBoundingSet(boundingSet); |
| 1192 | if(numBoundingSet > 0 || numBoundingSet <= numCandidates) |
| 1193 | { |
| 1194 | tmmbrOwner = _tmmbrHelp.IsOwner(_SSRC, numBoundingSet); |
| 1195 | } |
| 1196 | if(!tmmbrOwner) |
| 1197 | { |
| 1198 | // did not enter bounding set, no meaning to send this request |
| 1199 | return 0; |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | if(_tmmbr_Send) |
| 1205 | { |
| 1206 | // sanity |
| 1207 | if(pos + 20 >= IP_PACKET_SIZE) |
| 1208 | { |
| 1209 | return -2; |
| 1210 | } |
| 1211 | // add TMMBR indicator |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1212 | uint8_t FMT = 3; |
| 1213 | rtcpbuffer[pos++]=(uint8_t)0x80 + FMT; |
| 1214 | rtcpbuffer[pos++]=(uint8_t)205; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1215 | |
| 1216 | //Length of 4 |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1217 | rtcpbuffer[pos++]=(uint8_t)0; |
| 1218 | rtcpbuffer[pos++]=(uint8_t)(4); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1219 | |
| 1220 | // Add our own SSRC |
| 1221 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 1222 | pos += 4; |
| 1223 | |
| 1224 | // RFC 5104 4.2.1.2. Semantics |
| 1225 | |
| 1226 | // SSRC of media source |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1227 | rtcpbuffer[pos++]=(uint8_t)0; |
| 1228 | rtcpbuffer[pos++]=(uint8_t)0; |
| 1229 | rtcpbuffer[pos++]=(uint8_t)0; |
| 1230 | rtcpbuffer[pos++]=(uint8_t)0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1231 | |
| 1232 | // Additional Feedback Control Information (FCI) |
| 1233 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _remoteSSRC); |
| 1234 | pos += 4; |
| 1235 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1236 | uint32_t bitRate = _tmmbr_Send*1000; |
| 1237 | uint32_t mmbrExp = 0; |
| 1238 | for(uint32_t i=0;i<64;i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1239 | { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1240 | if(bitRate <= ((uint32_t)131071 << i)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1241 | { |
| 1242 | mmbrExp = i; |
| 1243 | break; |
| 1244 | } |
| 1245 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1246 | uint32_t mmbrMantissa = (bitRate >> mmbrExp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1247 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1248 | rtcpbuffer[pos++]=(uint8_t)((mmbrExp << 2) + ((mmbrMantissa >> 15) & 0x03)); |
| 1249 | rtcpbuffer[pos++]=(uint8_t)(mmbrMantissa >> 7); |
| 1250 | rtcpbuffer[pos++]=(uint8_t)((mmbrMantissa << 1) + ((_packetOH_Send >> 8)& 0x01)); |
| 1251 | rtcpbuffer[pos++]=(uint8_t)(_packetOH_Send); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1252 | } |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1256 | int32_t |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 1257 | RTCPSender::BuildTMMBN(uint8_t* rtcpbuffer, int& pos) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1258 | { |
| 1259 | TMMBRSet* boundingSet = _tmmbrHelp.BoundingSetToSend(); |
| 1260 | if(boundingSet == NULL) |
| 1261 | { |
| 1262 | return -1; |
| 1263 | } |
| 1264 | // sanity |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 1265 | if(pos + 12 + boundingSet->lengthOfSet()*8 >= IP_PACKET_SIZE) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1266 | { |
| 1267 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id, "%s invalid argument", __FUNCTION__); |
| 1268 | return -2; |
| 1269 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1270 | uint8_t FMT = 4; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1271 | // add TMMBN indicator |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1272 | rtcpbuffer[pos++]=(uint8_t)0x80 + FMT; |
| 1273 | rtcpbuffer[pos++]=(uint8_t)205; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1274 | |
| 1275 | //Add length later |
| 1276 | int posLength = pos; |
| 1277 | pos++; |
| 1278 | pos++; |
| 1279 | |
| 1280 | // Add our own SSRC |
| 1281 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 1282 | pos += 4; |
| 1283 | |
| 1284 | // RFC 5104 4.2.2.2. Semantics |
| 1285 | |
| 1286 | // SSRC of media source |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1287 | rtcpbuffer[pos++]=(uint8_t)0; |
| 1288 | rtcpbuffer[pos++]=(uint8_t)0; |
| 1289 | rtcpbuffer[pos++]=(uint8_t)0; |
| 1290 | rtcpbuffer[pos++]=(uint8_t)0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1291 | |
| 1292 | // Additional Feedback Control Information (FCI) |
| 1293 | int numBoundingSet = 0; |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1294 | for(uint32_t n=0; n< boundingSet->lengthOfSet(); n++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1295 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 1296 | if (boundingSet->Tmmbr(n) > 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1297 | { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1298 | uint32_t tmmbrSSRC = boundingSet->Ssrc(n); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1299 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, tmmbrSSRC); |
| 1300 | pos += 4; |
| 1301 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1302 | uint32_t bitRate = boundingSet->Tmmbr(n) * 1000; |
| 1303 | uint32_t mmbrExp = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1304 | for(int i=0; i<64; i++) |
| 1305 | { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1306 | if(bitRate <= ((uint32_t)131071 << i)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1307 | { |
| 1308 | mmbrExp = i; |
| 1309 | break; |
| 1310 | } |
| 1311 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1312 | uint32_t mmbrMantissa = (bitRate >> mmbrExp); |
| 1313 | uint32_t measuredOH = boundingSet->PacketOH(n); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1314 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1315 | rtcpbuffer[pos++]=(uint8_t)((mmbrExp << 2) + ((mmbrMantissa >> 15) & 0x03)); |
| 1316 | rtcpbuffer[pos++]=(uint8_t)(mmbrMantissa >> 7); |
| 1317 | rtcpbuffer[pos++]=(uint8_t)((mmbrMantissa << 1) + ((measuredOH >> 8)& 0x01)); |
| 1318 | rtcpbuffer[pos++]=(uint8_t)(measuredOH); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1319 | numBoundingSet++; |
| 1320 | } |
| 1321 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1322 | uint16_t length= (uint16_t)(2+2*numBoundingSet); |
| 1323 | rtcpbuffer[posLength++]=(uint8_t)(length>>8); |
| 1324 | rtcpbuffer[posLength]=(uint8_t)(length); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1325 | return 0; |
| 1326 | } |
| 1327 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1328 | int32_t |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 1329 | RTCPSender::BuildAPP(uint8_t* rtcpbuffer, int& pos) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1330 | { |
| 1331 | // sanity |
| 1332 | if(_appData == NULL) |
| 1333 | { |
| 1334 | WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, _id, "%s invalid state", __FUNCTION__); |
| 1335 | return -1; |
| 1336 | } |
| 1337 | if(pos + 12 + _appLength >= IP_PACKET_SIZE) |
| 1338 | { |
| 1339 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id, "%s invalid argument", __FUNCTION__); |
| 1340 | return -2; |
| 1341 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1342 | rtcpbuffer[pos++]=(uint8_t)0x80 + _appSubType; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1343 | |
| 1344 | // Add APP ID |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1345 | rtcpbuffer[pos++]=(uint8_t)204; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1346 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1347 | uint16_t length = (_appLength>>2) + 2; // include SSRC and name |
| 1348 | rtcpbuffer[pos++]=(uint8_t)(length>>8); |
| 1349 | rtcpbuffer[pos++]=(uint8_t)(length); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1350 | |
| 1351 | // Add our own SSRC |
| 1352 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 1353 | pos += 4; |
| 1354 | |
| 1355 | // Add our application name |
| 1356 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _appName); |
| 1357 | pos += 4; |
| 1358 | |
| 1359 | // Add the data |
| 1360 | memcpy(rtcpbuffer +pos, _appData,_appLength); |
| 1361 | pos += _appLength; |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1365 | int32_t |
| 1366 | RTCPSender::BuildNACK(uint8_t* rtcpbuffer, |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 1367 | int& pos, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1368 | const int32_t nackSize, |
| 1369 | const uint16_t* nackList, |
edjee@google.com | 79b0289 | 2013-04-04 19:43:34 +0000 | [diff] [blame] | 1370 | std::string* nackString) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1371 | { |
| 1372 | // sanity |
| 1373 | if(pos + 16 >= IP_PACKET_SIZE) |
| 1374 | { |
| 1375 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id, "%s invalid argument", __FUNCTION__); |
| 1376 | return -2; |
| 1377 | } |
| 1378 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1379 | // int size, uint16_t* nackList |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1380 | // add nack list |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1381 | uint8_t FMT = 1; |
| 1382 | rtcpbuffer[pos++]=(uint8_t)0x80 + FMT; |
| 1383 | rtcpbuffer[pos++]=(uint8_t)205; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1384 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1385 | rtcpbuffer[pos++]=(uint8_t) 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1386 | int nackSizePos = pos; |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1387 | rtcpbuffer[pos++]=(uint8_t)(3); //setting it to one kNACK signal as default |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1388 | |
| 1389 | // Add our own SSRC |
| 1390 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 1391 | pos += 4; |
| 1392 | |
| 1393 | // Add the remote SSRC |
| 1394 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _remoteSSRC); |
| 1395 | pos += 4; |
| 1396 | |
edjee@google.com | 79b0289 | 2013-04-04 19:43:34 +0000 | [diff] [blame] | 1397 | NACKStringBuilder stringBuilder; |
andresp@webrtc.org | 523f937 | 2013-04-11 11:30:39 +0000 | [diff] [blame] | 1398 | // Build NACK bitmasks and write them to the RTCP message. |
| 1399 | // The nack list should be sorted and not contain duplicates if one |
| 1400 | // wants to build the smallest rtcp nack packet. |
| 1401 | int numOfNackFields = 0; |
| 1402 | int maxNackFields = std::min<int>(kRtcpMaxNackFields, |
| 1403 | (IP_PACKET_SIZE - pos) / 4); |
| 1404 | int i = 0; |
| 1405 | while (i < nackSize && numOfNackFields < maxNackFields) { |
| 1406 | stringBuilder.PushNACK(nackList[i]); |
| 1407 | uint16_t nack = nackList[i++]; |
| 1408 | uint16_t bitmask = 0; |
| 1409 | while (i < nackSize) { |
| 1410 | int shift = static_cast<uint16_t>(nackList[i] - nack) - 1; |
| 1411 | if (shift >= 0 && shift <= 15) { |
| 1412 | stringBuilder.PushNACK(nackList[i]); |
| 1413 | bitmask |= (1 << shift); |
| 1414 | ++i; |
| 1415 | } else { |
| 1416 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1417 | } |
andresp@webrtc.org | 523f937 | 2013-04-11 11:30:39 +0000 | [diff] [blame] | 1418 | } |
| 1419 | // Write the sequence number and the bitmask to the packet. |
| 1420 | assert(pos + 4 < IP_PACKET_SIZE); |
| 1421 | ModuleRTPUtility::AssignUWord16ToBuffer(rtcpbuffer + pos, nack); |
| 1422 | pos += 2; |
| 1423 | ModuleRTPUtility::AssignUWord16ToBuffer(rtcpbuffer + pos, bitmask); |
| 1424 | pos += 2; |
| 1425 | numOfNackFields++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1426 | } |
andresp@webrtc.org | 523f937 | 2013-04-11 11:30:39 +0000 | [diff] [blame] | 1427 | if (i != nackSize) { |
| 1428 | WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, _id, |
| 1429 | "Nack list to large for one packet."); |
| 1430 | } |
| 1431 | rtcpbuffer[nackSizePos] = static_cast<uint8_t>(2 + numOfNackFields); |
edjee@google.com | 79b0289 | 2013-04-04 19:43:34 +0000 | [diff] [blame] | 1432 | *nackString = stringBuilder.GetResult(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1433 | return 0; |
| 1434 | } |
| 1435 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1436 | int32_t |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 1437 | RTCPSender::BuildBYE(uint8_t* rtcpbuffer, int& pos) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1438 | { |
| 1439 | // sanity |
| 1440 | if(pos + 8 >= IP_PACKET_SIZE) |
| 1441 | { |
| 1442 | return -2; |
| 1443 | } |
| 1444 | if(_includeCSRCs) |
| 1445 | { |
| 1446 | // Add a bye packet |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1447 | rtcpbuffer[pos++]=(uint8_t)0x80 + 1 + _CSRCs; // number of SSRC+CSRCs |
| 1448 | rtcpbuffer[pos++]=(uint8_t)203; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1449 | |
| 1450 | // length |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1451 | rtcpbuffer[pos++]=(uint8_t)0; |
| 1452 | rtcpbuffer[pos++]=(uint8_t)(1 + _CSRCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1453 | |
| 1454 | // Add our own SSRC |
| 1455 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 1456 | pos += 4; |
| 1457 | |
| 1458 | // add CSRCs |
| 1459 | for(int i = 0; i < _CSRCs; i++) |
| 1460 | { |
| 1461 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _CSRC[i]); |
| 1462 | pos += 4; |
| 1463 | } |
| 1464 | } else |
| 1465 | { |
| 1466 | // Add a bye packet |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1467 | rtcpbuffer[pos++]=(uint8_t)0x80 + 1; // number of SSRC+CSRCs |
| 1468 | rtcpbuffer[pos++]=(uint8_t)203; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1469 | |
| 1470 | // length |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1471 | rtcpbuffer[pos++]=(uint8_t)0; |
| 1472 | rtcpbuffer[pos++]=(uint8_t)1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1473 | |
| 1474 | // Add our own SSRC |
| 1475 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 1476 | pos += 4; |
| 1477 | } |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1481 | int32_t |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 1482 | RTCPSender::BuildVoIPMetric(uint8_t* rtcpbuffer, int& pos) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1483 | { |
| 1484 | // sanity |
| 1485 | if(pos + 44 >= IP_PACKET_SIZE) |
| 1486 | { |
| 1487 | return -2; |
| 1488 | } |
| 1489 | |
| 1490 | // Add XR header |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1491 | rtcpbuffer[pos++]=(uint8_t)0x80; |
| 1492 | rtcpbuffer[pos++]=(uint8_t)207; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1493 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1494 | uint32_t XRLengthPos = pos; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1495 | |
| 1496 | // handle length later on |
| 1497 | pos++; |
| 1498 | pos++; |
| 1499 | |
| 1500 | // Add our own SSRC |
| 1501 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _SSRC); |
| 1502 | pos += 4; |
| 1503 | |
| 1504 | // Add a VoIP metrics block |
| 1505 | rtcpbuffer[pos++]=7; |
| 1506 | rtcpbuffer[pos++]=0; |
| 1507 | rtcpbuffer[pos++]=0; |
| 1508 | rtcpbuffer[pos++]=8; |
| 1509 | |
| 1510 | // Add the remote SSRC |
| 1511 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+pos, _remoteSSRC); |
| 1512 | pos += 4; |
| 1513 | |
| 1514 | rtcpbuffer[pos++] = _xrVoIPMetric.lossRate; |
| 1515 | rtcpbuffer[pos++] = _xrVoIPMetric.discardRate; |
| 1516 | rtcpbuffer[pos++] = _xrVoIPMetric.burstDensity; |
| 1517 | rtcpbuffer[pos++] = _xrVoIPMetric.gapDensity; |
| 1518 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1519 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.burstDuration >> 8); |
| 1520 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.burstDuration); |
| 1521 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.gapDuration >> 8); |
| 1522 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.gapDuration); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1523 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1524 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.roundTripDelay >> 8); |
| 1525 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.roundTripDelay); |
| 1526 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.endSystemDelay >> 8); |
| 1527 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.endSystemDelay); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1528 | |
| 1529 | rtcpbuffer[pos++] = _xrVoIPMetric.signalLevel; |
| 1530 | rtcpbuffer[pos++] = _xrVoIPMetric.noiseLevel; |
| 1531 | rtcpbuffer[pos++] = _xrVoIPMetric.RERL; |
| 1532 | rtcpbuffer[pos++] = _xrVoIPMetric.Gmin; |
| 1533 | |
| 1534 | rtcpbuffer[pos++] = _xrVoIPMetric.Rfactor; |
| 1535 | rtcpbuffer[pos++] = _xrVoIPMetric.extRfactor; |
| 1536 | rtcpbuffer[pos++] = _xrVoIPMetric.MOSLQ; |
| 1537 | rtcpbuffer[pos++] = _xrVoIPMetric.MOSCQ; |
| 1538 | |
| 1539 | rtcpbuffer[pos++] = _xrVoIPMetric.RXconfig; |
| 1540 | rtcpbuffer[pos++] = 0; // reserved |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1541 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.JBnominal >> 8); |
| 1542 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.JBnominal); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1543 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1544 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.JBmax >> 8); |
| 1545 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.JBmax); |
| 1546 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.JBabsMax >> 8); |
| 1547 | rtcpbuffer[pos++] = (uint8_t)(_xrVoIPMetric.JBabsMax); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1548 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1549 | rtcpbuffer[XRLengthPos]=(uint8_t)(0); |
| 1550 | rtcpbuffer[XRLengthPos+1]=(uint8_t)(10); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1551 | return 0; |
| 1552 | } |
| 1553 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1554 | int32_t |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 1555 | RTCPSender::SendRTCP( |
| 1556 | uint32_t packetTypeFlags, |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 1557 | int32_t nackSize, |
| 1558 | const uint16_t* nackList, |
| 1559 | bool repeat, |
| 1560 | uint64_t pictureID) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1561 | { |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 1562 | { |
| 1563 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 1564 | if(_method == kRtcpOff) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1565 | { |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 1566 | WEBRTC_TRACE(kTraceWarning, kTraceRtpRtcp, _id, |
| 1567 | "%s invalid state", __FUNCTION__); |
pwestin@webrtc.org | 8edb39d | 2011-12-22 07:40:33 +0000 | [diff] [blame] | 1568 | return -1; |
| 1569 | } |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 1570 | } |
| 1571 | uint8_t rtcp_buffer[IP_PACKET_SIZE]; |
| 1572 | int rtcp_length = PrepareRTCP(packetTypeFlags, nackSize, nackList, repeat, |
| 1573 | pictureID, rtcp_buffer, IP_PACKET_SIZE); |
| 1574 | if (rtcp_length < 0) { |
| 1575 | return -1; |
| 1576 | } |
| 1577 | // Sanity don't send empty packets. |
| 1578 | if (rtcp_length == 0) |
| 1579 | { |
| 1580 | return -1; |
| 1581 | } |
| 1582 | return SendToNetwork(rtcp_buffer, static_cast<uint16_t>(rtcp_length)); |
| 1583 | } |
| 1584 | |
| 1585 | int RTCPSender::PrepareRTCP( |
| 1586 | uint32_t packetTypeFlags, |
| 1587 | int32_t nackSize, |
| 1588 | const uint16_t* nackList, |
| 1589 | bool repeat, |
| 1590 | uint64_t pictureID, |
| 1591 | uint8_t* rtcp_buffer, |
| 1592 | int buffer_size) { |
| 1593 | uint32_t rtcpPacketTypeFlags = packetTypeFlags; |
| 1594 | // Collect the received information. |
| 1595 | uint32_t NTPsec = 0; |
| 1596 | uint32_t NTPfrac = 0; |
| 1597 | uint32_t jitterTransmissionOffset = 0; |
| 1598 | int position = 0; |
| 1599 | |
| 1600 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 1601 | |
| 1602 | if(_TMMBR ) // Attach TMMBR to send and receive reports. |
| 1603 | { |
| 1604 | rtcpPacketTypeFlags |= kRtcpTmmbr; |
| 1605 | } |
| 1606 | if(_appSend) |
| 1607 | { |
| 1608 | rtcpPacketTypeFlags |= kRtcpApp; |
| 1609 | _appSend = false; |
| 1610 | } |
| 1611 | if(_REMB && _sendREMB) |
| 1612 | { |
| 1613 | // Always attach REMB to SR if that is configured. Note that REMB is |
| 1614 | // only sent on one of the RTP modules in the REMB group. |
| 1615 | rtcpPacketTypeFlags |= kRtcpRemb; |
| 1616 | } |
| 1617 | if(_xrSendVoIPMetric) |
| 1618 | { |
| 1619 | rtcpPacketTypeFlags |= kRtcpXrVoipMetric; |
| 1620 | _xrSendVoIPMetric = false; |
| 1621 | } |
| 1622 | if(_sendTMMBN) // Set when having received a TMMBR. |
| 1623 | { |
| 1624 | rtcpPacketTypeFlags |= kRtcpTmmbn; |
| 1625 | _sendTMMBN = false; |
| 1626 | } |
| 1627 | |
| 1628 | if(_method == kRtcpCompound) |
| 1629 | { |
| 1630 | if(_sending) |
| 1631 | { |
| 1632 | rtcpPacketTypeFlags |= kRtcpSr; |
| 1633 | } else |
| 1634 | { |
| 1635 | rtcpPacketTypeFlags |= kRtcpRr; |
| 1636 | } |
| 1637 | } else if(_method == kRtcpNonCompound) |
| 1638 | { |
| 1639 | if(rtcpPacketTypeFlags & kRtcpReport) |
| 1640 | { |
| 1641 | if(_sending) |
| 1642 | { |
| 1643 | rtcpPacketTypeFlags |= kRtcpSr; |
| 1644 | } else |
| 1645 | { |
| 1646 | rtcpPacketTypeFlags |= kRtcpRr; |
| 1647 | } |
| 1648 | } |
| 1649 | } |
| 1650 | if( rtcpPacketTypeFlags & kRtcpRr || |
| 1651 | rtcpPacketTypeFlags & kRtcpSr) |
| 1652 | { |
| 1653 | // generate next time to send a RTCP report |
| 1654 | // seeded from RTP constructor |
| 1655 | int32_t random = rand() % 1000; |
| 1656 | int32_t timeToNext = RTCP_INTERVAL_AUDIO_MS; |
| 1657 | |
| 1658 | if(_audio) |
| 1659 | { |
| 1660 | timeToNext = (RTCP_INTERVAL_AUDIO_MS/2) + |
| 1661 | (RTCP_INTERVAL_AUDIO_MS*random/1000); |
| 1662 | }else |
| 1663 | { |
| 1664 | uint32_t minIntervalMs = RTCP_INTERVAL_AUDIO_MS; |
| 1665 | if(_sending) |
| 1666 | { |
| 1667 | // Calculate bandwidth for video; 360 / send bandwidth in kbit/s. |
| 1668 | uint32_t sendBitrateKbit = 0; |
| 1669 | uint32_t videoRate = 0; |
| 1670 | uint32_t fecRate = 0; |
| 1671 | uint32_t nackRate = 0; |
| 1672 | _rtpRtcp.BitrateSent(&sendBitrateKbit, |
| 1673 | &videoRate, |
| 1674 | &fecRate, |
| 1675 | &nackRate); |
| 1676 | sendBitrateKbit /= 1000; |
| 1677 | if(sendBitrateKbit != 0) |
| 1678 | { |
| 1679 | minIntervalMs = 360000/sendBitrateKbit; |
| 1680 | } |
| 1681 | } |
| 1682 | if(minIntervalMs > RTCP_INTERVAL_VIDEO_MS) |
| 1683 | { |
| 1684 | minIntervalMs = RTCP_INTERVAL_VIDEO_MS; |
| 1685 | } |
| 1686 | timeToNext = (minIntervalMs/2) + (minIntervalMs*random/1000); |
| 1687 | } |
| 1688 | _nextTimeToSendRTCP = _clock->TimeInMilliseconds() + timeToNext; |
| 1689 | } |
| 1690 | |
| 1691 | // If the data does not fit in the packet we fill it as much as possible. |
| 1692 | int32_t buildVal = 0; |
| 1693 | |
| 1694 | // We need to send our NTP even if we haven't received any reports. |
| 1695 | _clock->CurrentNtp(NTPsec, NTPfrac); |
| 1696 | if (ShouldSendReportBlocks(rtcpPacketTypeFlags)) { |
| 1697 | StatisticianMap statisticians = |
| 1698 | receive_statistics_->GetActiveStatisticians(); |
| 1699 | if (!statisticians.empty()) { |
| 1700 | StatisticianMap::const_iterator it; |
| 1701 | int i; |
| 1702 | for (it = statisticians.begin(), i = 0; it != statisticians.end(); |
| 1703 | ++it, ++i) { |
| 1704 | RTCPReportBlock report_block; |
| 1705 | if (PrepareReport(it->second, &report_block, &NTPsec, &NTPfrac)) |
| 1706 | AddReportBlock(it->first, &internal_report_blocks_, &report_block); |
| 1707 | } |
| 1708 | if (_IJ && !statisticians.empty()) { |
| 1709 | rtcpPacketTypeFlags |= kRtcpTransmissionTimeOffset; |
| 1710 | } |
| 1711 | _lastRTCPTime[0] = Clock::NtpToMs(NTPsec, NTPfrac); |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | if(rtcpPacketTypeFlags & kRtcpSr) |
| 1716 | { |
| 1717 | buildVal = BuildSR(rtcp_buffer, position, NTPsec, NTPfrac); |
| 1718 | if (buildVal == -1) { |
| 1719 | return -1; |
| 1720 | } else if (buildVal == -2) { |
| 1721 | return position; |
| 1722 | } |
| 1723 | buildVal = BuildSDEC(rtcp_buffer, position); |
| 1724 | if (buildVal == -1) { |
| 1725 | return -1; |
| 1726 | } else if (buildVal == -2) { |
| 1727 | return position; |
| 1728 | } |
| 1729 | }else if(rtcpPacketTypeFlags & kRtcpRr) |
| 1730 | { |
| 1731 | buildVal = BuildRR(rtcp_buffer, position, NTPsec, NTPfrac); |
| 1732 | if (buildVal == -1) { |
| 1733 | return -1; |
| 1734 | } else if (buildVal == -2) { |
| 1735 | return position; |
| 1736 | } |
| 1737 | // only of set |
| 1738 | if(_CNAME[0] != 0) |
| 1739 | { |
| 1740 | buildVal = BuildSDEC(rtcp_buffer, position); |
| 1741 | if (buildVal == -1) { |
| 1742 | return -1; |
| 1743 | } |
| 1744 | } |
| 1745 | } |
| 1746 | if(rtcpPacketTypeFlags & kRtcpTransmissionTimeOffset) |
| 1747 | { |
| 1748 | // If present, this RTCP packet must be placed after a |
| 1749 | // receiver report. |
| 1750 | buildVal = BuildExtendedJitterReport(rtcp_buffer, |
| 1751 | position, |
| 1752 | jitterTransmissionOffset); |
| 1753 | if (buildVal == -1) { |
| 1754 | return -1; |
| 1755 | } else if (buildVal == -2) { |
| 1756 | return position; |
| 1757 | } |
| 1758 | } |
| 1759 | if(rtcpPacketTypeFlags & kRtcpPli) |
| 1760 | { |
| 1761 | buildVal = BuildPLI(rtcp_buffer, position); |
| 1762 | if (buildVal == -1) { |
| 1763 | return -1; |
| 1764 | } else if (buildVal == -2) { |
| 1765 | return position; |
| 1766 | } |
| 1767 | TRACE_EVENT_INSTANT0("webrtc_rtp", "RTCPSender::PLI"); |
| 1768 | _pliCount++; |
| 1769 | TRACE_COUNTER_ID1("webrtc_rtp", "RTCP_PLICount", _SSRC, _pliCount); |
| 1770 | } |
| 1771 | if(rtcpPacketTypeFlags & kRtcpFir) |
| 1772 | { |
| 1773 | buildVal = BuildFIR(rtcp_buffer, position, repeat); |
| 1774 | if (buildVal == -1) { |
| 1775 | return -1; |
| 1776 | } else if (buildVal == -2) { |
| 1777 | return position; |
| 1778 | } |
| 1779 | TRACE_EVENT_INSTANT0("webrtc_rtp", "RTCPSender::FIR"); |
| 1780 | _fullIntraRequestCount++; |
| 1781 | TRACE_COUNTER_ID1("webrtc_rtp", "RTCP_FIRCount", _SSRC, |
| 1782 | _fullIntraRequestCount); |
| 1783 | } |
| 1784 | if(rtcpPacketTypeFlags & kRtcpSli) |
| 1785 | { |
| 1786 | buildVal = BuildSLI(rtcp_buffer, position, (uint8_t)pictureID); |
| 1787 | if (buildVal == -1) { |
| 1788 | return -1; |
| 1789 | } else if (buildVal == -2) { |
| 1790 | return position; |
| 1791 | } |
| 1792 | } |
| 1793 | if(rtcpPacketTypeFlags & kRtcpRpsi) |
| 1794 | { |
| 1795 | const int8_t payloadType = _rtpRtcp.SendPayloadType(); |
| 1796 | if (payloadType == -1) { |
| 1797 | return -1; |
| 1798 | } |
| 1799 | buildVal = BuildRPSI(rtcp_buffer, position, pictureID, |
| 1800 | (uint8_t)payloadType); |
| 1801 | if (buildVal == -1) { |
| 1802 | return -1; |
| 1803 | } else if (buildVal == -2) { |
| 1804 | return position; |
| 1805 | } |
| 1806 | } |
| 1807 | if(rtcpPacketTypeFlags & kRtcpRemb) |
| 1808 | { |
| 1809 | buildVal = BuildREMB(rtcp_buffer, position); |
| 1810 | if (buildVal == -1) { |
| 1811 | return -1; |
| 1812 | } else if (buildVal == -2) { |
| 1813 | return position; |
| 1814 | } |
| 1815 | TRACE_EVENT_INSTANT0("webrtc_rtp", "RTCPSender::REMB"); |
| 1816 | } |
| 1817 | if(rtcpPacketTypeFlags & kRtcpBye) |
| 1818 | { |
| 1819 | buildVal = BuildBYE(rtcp_buffer, position); |
| 1820 | if (buildVal == -1) { |
| 1821 | return -1; |
| 1822 | } else if (buildVal == -2) { |
| 1823 | return position; |
| 1824 | } |
| 1825 | } |
| 1826 | if(rtcpPacketTypeFlags & kRtcpApp) |
| 1827 | { |
| 1828 | buildVal = BuildAPP(rtcp_buffer, position); |
| 1829 | if (buildVal == -1) { |
| 1830 | return -1; |
| 1831 | } else if (buildVal == -2) { |
| 1832 | return position; |
| 1833 | } |
| 1834 | } |
| 1835 | if(rtcpPacketTypeFlags & kRtcpTmmbr) |
| 1836 | { |
| 1837 | buildVal = BuildTMMBR(rtcp_buffer, position); |
| 1838 | if (buildVal == -1) { |
| 1839 | return -1; |
| 1840 | } else if (buildVal == -2) { |
| 1841 | return position; |
| 1842 | } |
| 1843 | } |
| 1844 | if(rtcpPacketTypeFlags & kRtcpTmmbn) |
| 1845 | { |
| 1846 | buildVal = BuildTMMBN(rtcp_buffer, position); |
| 1847 | if (buildVal == -1) { |
| 1848 | return -1; |
| 1849 | } else if (buildVal == -2) { |
| 1850 | return position; |
| 1851 | } |
| 1852 | } |
| 1853 | if(rtcpPacketTypeFlags & kRtcpNack) |
| 1854 | { |
| 1855 | std::string nackString; |
| 1856 | buildVal = BuildNACK(rtcp_buffer, position, nackSize, nackList, |
| 1857 | &nackString); |
| 1858 | if (buildVal == -1) { |
| 1859 | return -1; |
| 1860 | } else if (buildVal == -2) { |
| 1861 | return position; |
| 1862 | } |
| 1863 | TRACE_EVENT_INSTANT1("webrtc_rtp", "RTCPSender::NACK", |
| 1864 | "nacks", TRACE_STR_COPY(nackString.c_str())); |
| 1865 | _nackCount++; |
| 1866 | TRACE_COUNTER_ID1("webrtc_rtp", "RTCP_NACKCount", _SSRC, _nackCount); |
| 1867 | } |
| 1868 | if(rtcpPacketTypeFlags & kRtcpXrVoipMetric) |
| 1869 | { |
| 1870 | buildVal = BuildVoIPMetric(rtcp_buffer, position); |
| 1871 | if (buildVal == -1) { |
| 1872 | return -1; |
| 1873 | } else if (buildVal == -2) { |
| 1874 | return position; |
| 1875 | } |
| 1876 | } |
| 1877 | return position; |
| 1878 | } |
| 1879 | |
| 1880 | bool RTCPSender::ShouldSendReportBlocks(uint32_t rtcp_packet_type) const { |
| 1881 | return Status() == kRtcpCompound || |
| 1882 | (rtcp_packet_type & kRtcpReport) || |
| 1883 | (rtcp_packet_type & kRtcpSr) || |
| 1884 | (rtcp_packet_type & kRtcpRr); |
| 1885 | } |
| 1886 | |
| 1887 | bool RTCPSender::PrepareReport(StreamStatistician* statistician, |
| 1888 | RTCPReportBlock* report_block, |
| 1889 | uint32_t* ntp_secs, uint32_t* ntp_frac) { |
| 1890 | // Do we have receive statistics to send? |
| 1891 | StreamStatistician::Statistics stats; |
| 1892 | if (!statistician->GetStatistics(&stats, true)) |
| 1893 | return false; |
| 1894 | report_block->fractionLost = stats.fraction_lost; |
| 1895 | report_block->cumulativeLost = stats.cumulative_lost; |
| 1896 | report_block->extendedHighSeqNum = |
| 1897 | stats.extended_max_sequence_number; |
| 1898 | report_block->jitter = stats.jitter; |
| 1899 | |
| 1900 | uint32_t lastReceivedRRNTPsecs = 0; |
| 1901 | uint32_t lastReceivedRRNTPfrac = 0; |
| 1902 | uint32_t remoteSR = 0; |
| 1903 | |
| 1904 | // ok even if we have not received a SR, we will send 0 in that case |
| 1905 | _rtpRtcp.LastReceivedNTP(lastReceivedRRNTPsecs, |
| 1906 | lastReceivedRRNTPfrac, |
| 1907 | remoteSR); |
| 1908 | |
| 1909 | // get our NTP as late as possible to avoid a race |
| 1910 | _clock->CurrentNtp(*ntp_secs, *ntp_frac); |
| 1911 | |
| 1912 | // Delay since last received report |
| 1913 | uint32_t delaySinceLastReceivedSR = 0; |
| 1914 | if((lastReceivedRRNTPsecs !=0) || (lastReceivedRRNTPfrac !=0)) { |
| 1915 | // get the 16 lowest bits of seconds and the 16 higest bits of fractions |
| 1916 | uint32_t now=*ntp_secs&0x0000FFFF; |
| 1917 | now <<=16; |
| 1918 | now += (*ntp_frac&0xffff0000)>>16; |
| 1919 | |
| 1920 | uint32_t receiveTime = lastReceivedRRNTPsecs&0x0000FFFF; |
| 1921 | receiveTime <<=16; |
| 1922 | receiveTime += (lastReceivedRRNTPfrac&0xffff0000)>>16; |
| 1923 | |
| 1924 | delaySinceLastReceivedSR = now-receiveTime; |
| 1925 | } |
| 1926 | report_block->delaySinceLastSR = delaySinceLastReceivedSR; |
| 1927 | report_block->lastSR = remoteSR; |
| 1928 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1931 | int32_t |
| 1932 | RTCPSender::SendToNetwork(const uint8_t* dataBuffer, |
| 1933 | const uint16_t length) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1934 | { |
| 1935 | CriticalSectionScoped lock(_criticalSectionTransport); |
| 1936 | if(_cbTransport) |
| 1937 | { |
| 1938 | if(_cbTransport->SendRTCPPacket(_id, dataBuffer, length) > 0) |
| 1939 | { |
| 1940 | return 0; |
| 1941 | } |
| 1942 | } |
| 1943 | return -1; |
| 1944 | } |
| 1945 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1946 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1947 | RTCPSender::SetCSRCStatus(const bool include) |
| 1948 | { |
| 1949 | _includeCSRCs = include; |
| 1950 | return 0; |
| 1951 | } |
| 1952 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1953 | int32_t |
| 1954 | RTCPSender::SetCSRCs(const uint32_t arrOfCSRC[kRtpCsrcSize], |
| 1955 | const uint8_t arrLength) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1956 | { |
| 1957 | if(arrLength > kRtpCsrcSize) |
| 1958 | { |
| 1959 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id, "%s invalid argument", __FUNCTION__); |
| 1960 | assert(false); |
| 1961 | return -1; |
| 1962 | } |
| 1963 | |
| 1964 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 1965 | |
| 1966 | for(int i = 0; i < arrLength;i++) |
| 1967 | { |
| 1968 | _CSRC[i] = arrOfCSRC[i]; |
| 1969 | } |
| 1970 | _CSRCs = arrLength; |
| 1971 | return 0; |
| 1972 | } |
| 1973 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1974 | int32_t |
| 1975 | RTCPSender::SetApplicationSpecificData(const uint8_t subType, |
| 1976 | const uint32_t name, |
| 1977 | const uint8_t* data, |
| 1978 | const uint16_t length) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1979 | { |
| 1980 | if(length %4 != 0) |
| 1981 | { |
| 1982 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id, "%s invalid argument", __FUNCTION__); |
| 1983 | return -1; |
| 1984 | } |
| 1985 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 1986 | |
| 1987 | if(_appData) |
| 1988 | { |
| 1989 | delete [] _appData; |
| 1990 | } |
| 1991 | |
| 1992 | _appSend = true; |
| 1993 | _appSubType = subType; |
| 1994 | _appName = name; |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 1995 | _appData = new uint8_t[length]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1996 | _appLength = length; |
| 1997 | memcpy(_appData, data, length); |
| 1998 | return 0; |
| 1999 | } |
| 2000 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 2001 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2002 | RTCPSender::SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric) |
| 2003 | { |
| 2004 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 2005 | memcpy(&_xrVoIPMetric, VoIPMetric, sizeof(RTCPVoIPMetric)); |
| 2006 | |
| 2007 | _xrSendVoIPMetric = true; |
| 2008 | return 0; |
| 2009 | } |
| 2010 | |
| 2011 | // called under critsect _criticalSectionRTCPSender |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2012 | int32_t RTCPSender::WriteAllReportBlocksToBuffer( |
| 2013 | uint8_t* rtcpbuffer, |
| 2014 | int pos, |
| 2015 | uint8_t& numberOfReportBlocks, |
| 2016 | const uint32_t NTPsec, |
| 2017 | const uint32_t NTPfrac) { |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2018 | // sanity one block |
| 2019 | if(pos + 24 >= IP_PACKET_SIZE) { |
| 2020 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id, |
| 2021 | "%s invalid argument", __FUNCTION__); |
| 2022 | return -1; |
| 2023 | } |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2024 | numberOfReportBlocks = external_report_blocks_.size(); |
| 2025 | numberOfReportBlocks += internal_report_blocks_.size(); |
| 2026 | if ((pos + numberOfReportBlocks * 24) >= IP_PACKET_SIZE) { |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2027 | WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id, |
| 2028 | "%s invalid argument", __FUNCTION__); |
| 2029 | return -1; |
| 2030 | } |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2031 | pos = WriteReportBlocksToBuffer(rtcpbuffer, pos, internal_report_blocks_); |
| 2032 | while (!internal_report_blocks_.empty()) { |
| 2033 | delete internal_report_blocks_.begin()->second; |
| 2034 | internal_report_blocks_.erase(internal_report_blocks_.begin()); |
| 2035 | } |
| 2036 | pos = WriteReportBlocksToBuffer(rtcpbuffer, pos, external_report_blocks_); |
| 2037 | return pos; |
| 2038 | } |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2039 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2040 | int32_t RTCPSender::WriteReportBlocksToBuffer( |
| 2041 | uint8_t* rtcpbuffer, |
| 2042 | int32_t position, |
| 2043 | const std::map<uint32_t, RTCPReportBlock*>& report_blocks) { |
| 2044 | std::map<uint32_t, RTCPReportBlock*>::const_iterator it = |
| 2045 | report_blocks.begin(); |
| 2046 | for (; it != report_blocks.end(); it++) { |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 2047 | uint32_t remoteSSRC = it->first; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2048 | RTCPReportBlock* reportBlock = it->second; |
| 2049 | if (reportBlock) { |
| 2050 | // Remote SSRC |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2051 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+position, remoteSSRC); |
| 2052 | position += 4; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2053 | |
| 2054 | // fraction lost |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2055 | rtcpbuffer[position++] = reportBlock->fractionLost; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2056 | |
| 2057 | // cumulative loss |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2058 | ModuleRTPUtility::AssignUWord24ToBuffer(rtcpbuffer+position, |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2059 | reportBlock->cumulativeLost); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2060 | position += 3; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2061 | |
| 2062 | // extended highest seq_no, contain the highest sequence number received |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2063 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+position, |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2064 | reportBlock->extendedHighSeqNum); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2065 | position += 4; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2066 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2067 | // Jitter |
| 2068 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+position, |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2069 | reportBlock->jitter); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2070 | position += 4; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2071 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2072 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+position, |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2073 | reportBlock->lastSR); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2074 | position += 4; |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2075 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2076 | ModuleRTPUtility::AssignUWord32ToBuffer(rtcpbuffer+position, |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2077 | reportBlock->delaySinceLastSR); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2078 | position += 4; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2079 | } |
pwestin@webrtc.org | 26f8d9c | 2012-01-19 15:53:09 +0000 | [diff] [blame] | 2080 | } |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame^] | 2081 | return position; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | // no callbacks allowed inside this function |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 2085 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2086 | RTCPSender::SetTMMBN(const TMMBRSet* boundingSet, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 2087 | const uint32_t maxBitrateKbit) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2088 | { |
| 2089 | CriticalSectionScoped lock(_criticalSectionRTCPSender); |
| 2090 | |
| 2091 | if (0 == _tmmbrHelp.SetTMMBRBoundingSetToSend(boundingSet, maxBitrateKbit)) |
| 2092 | { |
| 2093 | _sendTMMBN = true; |
| 2094 | return 0; |
| 2095 | } |
| 2096 | return -1; |
| 2097 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 2098 | } // namespace webrtc |