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