niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +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/tmmbr_help.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 13 | #include <assert.h> |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +0000 | [diff] [blame] | 14 | #include <limits> |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 15 | #include <string.h> |
pbos@webrtc.org | a048d7c | 2013-05-29 14:27:38 +0000 | [diff] [blame] | 16 | #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | TMMBRSet::TMMBRSet() : |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 20 | _sizeOfSet(0), |
| 21 | _lengthOfSet(0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | { |
| 23 | } |
| 24 | |
| 25 | TMMBRSet::~TMMBRSet() |
| 26 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 27 | _sizeOfSet = 0; |
| 28 | _lengthOfSet = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 32 | TMMBRSet::VerifyAndAllocateSet(uint32_t minimumSize) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 34 | if(minimumSize > _sizeOfSet) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | { |
| 36 | // make sure that our buffers are big enough |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 37 | _data.resize(minimumSize); |
| 38 | _sizeOfSet = minimumSize; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | } |
| 40 | // reset memory |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 41 | for(uint32_t i = 0; i < _sizeOfSet; i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 43 | _data.at(i).tmmbr = 0; |
| 44 | _data.at(i).packet_oh = 0; |
| 45 | _data.at(i).ssrc = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | } |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 47 | _lengthOfSet = 0; |
| 48 | } |
| 49 | |
| 50 | void |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 51 | TMMBRSet::VerifyAndAllocateSetKeepingData(uint32_t minimumSize) |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 52 | { |
| 53 | if(minimumSize > _sizeOfSet) |
| 54 | { |
| 55 | { |
| 56 | _data.resize(minimumSize); |
| 57 | } |
| 58 | _sizeOfSet = minimumSize; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void TMMBRSet::SetEntry(unsigned int i, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 63 | uint32_t tmmbrSet, |
| 64 | uint32_t packetOHSet, |
| 65 | uint32_t ssrcSet) { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 66 | assert(i < _sizeOfSet); |
| 67 | _data.at(i).tmmbr = tmmbrSet; |
| 68 | _data.at(i).packet_oh = packetOHSet; |
| 69 | _data.at(i).ssrc = ssrcSet; |
| 70 | if (i >= _lengthOfSet) { |
| 71 | _lengthOfSet = i + 1; |
| 72 | } |
| 73 | } |
| 74 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 75 | void TMMBRSet::AddEntry(uint32_t tmmbrSet, |
| 76 | uint32_t packetOHSet, |
| 77 | uint32_t ssrcSet) { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 78 | assert(_lengthOfSet < _sizeOfSet); |
| 79 | SetEntry(_lengthOfSet, tmmbrSet, packetOHSet, ssrcSet); |
| 80 | } |
| 81 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 82 | void TMMBRSet::RemoveEntry(uint32_t sourceIdx) { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 83 | assert(sourceIdx < _lengthOfSet); |
| 84 | _data.erase(_data.begin() + sourceIdx); |
| 85 | _lengthOfSet--; |
| 86 | _data.resize(_sizeOfSet); // Ensure that size remains the same. |
| 87 | } |
| 88 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 89 | void TMMBRSet::SwapEntries(uint32_t i, uint32_t j) { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 90 | SetElement temp; |
| 91 | temp = _data[i]; |
| 92 | _data[i] = _data[j]; |
| 93 | _data[j] = temp; |
| 94 | } |
| 95 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 96 | void TMMBRSet::ClearEntry(uint32_t idx) { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 97 | SetEntry(idx, 0, 0, 0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | } |
| 99 | |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +0000 | [diff] [blame] | 100 | TMMBRHelp::TMMBRHelp() |
| 101 | : _criticalSection(CriticalSectionWrapper::CreateCriticalSection()), |
| 102 | _candidateSet(), |
| 103 | _boundingSet(), |
| 104 | _boundingSetToSend(), |
| 105 | _ptrIntersectionBoundingSet(NULL), |
| 106 | _ptrMaxPRBoundingSet(NULL) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | } |
| 108 | |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +0000 | [diff] [blame] | 109 | TMMBRHelp::~TMMBRHelp() { |
| 110 | delete [] _ptrIntersectionBoundingSet; |
| 111 | delete [] _ptrMaxPRBoundingSet; |
| 112 | _ptrIntersectionBoundingSet = 0; |
| 113 | _ptrMaxPRBoundingSet = 0; |
| 114 | delete _criticalSection; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TMMBRSet* |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 118 | TMMBRHelp::VerifyAndAllocateBoundingSet(uint32_t minimumSize) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | { |
| 120 | CriticalSectionScoped lock(_criticalSection); |
| 121 | |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 122 | if(minimumSize > _boundingSet.sizeOfSet()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | { |
| 124 | // make sure that our buffers are big enough |
| 125 | if(_ptrIntersectionBoundingSet) |
| 126 | { |
| 127 | delete [] _ptrIntersectionBoundingSet; |
| 128 | delete [] _ptrMaxPRBoundingSet; |
| 129 | } |
| 130 | _ptrIntersectionBoundingSet = new float[minimumSize]; |
| 131 | _ptrMaxPRBoundingSet = new float[minimumSize]; |
| 132 | } |
| 133 | _boundingSet.VerifyAndAllocateSet(minimumSize); |
| 134 | return &_boundingSet; |
| 135 | } |
| 136 | |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +0000 | [diff] [blame] | 137 | TMMBRSet* TMMBRHelp::BoundingSet() { |
| 138 | return &_boundingSet; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | } |
| 140 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 141 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | TMMBRHelp::SetTMMBRBoundingSetToSend(const TMMBRSet* boundingSetToSend, |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 143 | const uint32_t maxBitrateKbit) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | { |
| 145 | CriticalSectionScoped lock(_criticalSection); |
| 146 | |
| 147 | if (boundingSetToSend == NULL) |
| 148 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 149 | _boundingSetToSend.clearSet(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 153 | VerifyAndAllocateBoundingSetToSend(boundingSetToSend->lengthOfSet()); |
| 154 | _boundingSetToSend.clearSet(); |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 155 | for (uint32_t i = 0; i < boundingSetToSend->lengthOfSet(); i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | { |
| 157 | // cap at our configured max bitrate |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 158 | uint32_t bitrate = boundingSetToSend->Tmmbr(i); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | if(maxBitrateKbit) |
| 160 | { |
| 161 | // do we have a configured max bitrate? |
| 162 | if(bitrate > maxBitrateKbit) |
| 163 | { |
| 164 | bitrate = maxBitrateKbit; |
| 165 | } |
| 166 | } |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 167 | _boundingSetToSend.SetEntry(i, bitrate, |
| 168 | boundingSetToSend->PacketOH(i), |
| 169 | boundingSetToSend->Ssrc(i)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 174 | int32_t |
| 175 | TMMBRHelp::VerifyAndAllocateBoundingSetToSend(uint32_t minimumSize) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | { |
| 177 | CriticalSectionScoped lock(_criticalSection); |
| 178 | |
| 179 | _boundingSetToSend.VerifyAndAllocateSet(minimumSize); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | TMMBRSet* |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 184 | TMMBRHelp::VerifyAndAllocateCandidateSet(uint32_t minimumSize) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 185 | { |
| 186 | CriticalSectionScoped lock(_criticalSection); |
| 187 | |
| 188 | _candidateSet.VerifyAndAllocateSet(minimumSize); |
| 189 | return &_candidateSet; |
| 190 | } |
| 191 | |
| 192 | TMMBRSet* |
| 193 | TMMBRHelp::CandidateSet() |
| 194 | { |
| 195 | return &_candidateSet; |
| 196 | } |
| 197 | |
| 198 | TMMBRSet* |
| 199 | TMMBRHelp::BoundingSetToSend() |
| 200 | { |
| 201 | return &_boundingSetToSend; |
| 202 | } |
| 203 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 204 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | TMMBRHelp::FindTMMBRBoundingSet(TMMBRSet*& boundingSet) |
| 206 | { |
| 207 | CriticalSectionScoped lock(_criticalSection); |
| 208 | |
| 209 | // Work on local variable, will be modified |
| 210 | TMMBRSet candidateSet; |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 211 | candidateSet.VerifyAndAllocateSet(_candidateSet.sizeOfSet()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 212 | |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 213 | // TODO(hta) Figure out if this should be lengthOfSet instead. |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 214 | for (uint32_t i = 0; i < _candidateSet.sizeOfSet(); i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 215 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 216 | if(_candidateSet.Tmmbr(i)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 217 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 218 | candidateSet.AddEntry(_candidateSet.Tmmbr(i), |
| 219 | _candidateSet.PacketOH(i), |
| 220 | _candidateSet.Ssrc(i)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 221 | } |
| 222 | else |
| 223 | { |
| 224 | // make sure this is zero if tmmbr = 0 |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 225 | assert(_candidateSet.PacketOH(i) == 0); |
| 226 | // Old code: |
| 227 | // _candidateSet.ptrPacketOHSet[i] = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 230 | |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 231 | // Number of set candidates |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 232 | int32_t numSetCandidates = candidateSet.lengthOfSet(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 233 | // Find bounding set |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 234 | uint32_t numBoundingSet = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 235 | if (numSetCandidates > 0) |
| 236 | { |
| 237 | numBoundingSet = FindTMMBRBoundingSet(numSetCandidates, candidateSet); |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 238 | if(numBoundingSet < 1 || (numBoundingSet > _candidateSet.sizeOfSet())) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 239 | { |
| 240 | return -1; |
| 241 | } |
| 242 | boundingSet = &_boundingSet; |
| 243 | } |
| 244 | return numBoundingSet; |
| 245 | } |
| 246 | |
| 247 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 248 | int32_t |
| 249 | TMMBRHelp::FindTMMBRBoundingSet(int32_t numCandidates, TMMBRSet& candidateSet) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 250 | { |
| 251 | CriticalSectionScoped lock(_criticalSection); |
| 252 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 253 | uint32_t numBoundingSet = 0; |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 254 | VerifyAndAllocateBoundingSet(candidateSet.sizeOfSet()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 255 | |
| 256 | if (numCandidates == 1) |
| 257 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 258 | // TODO(hta): lengthOfSet instead of sizeOfSet? |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 259 | for (uint32_t i = 0; i < candidateSet.sizeOfSet(); i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 260 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 261 | if (candidateSet.Tmmbr(i) > 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 262 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 263 | _boundingSet.AddEntry(candidateSet.Tmmbr(i), |
| 264 | candidateSet.PacketOH(i), |
| 265 | candidateSet.Ssrc(i)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 266 | numBoundingSet++; |
| 267 | } |
| 268 | } |
| 269 | if (numBoundingSet != 1) |
| 270 | { |
| 271 | numBoundingSet = -1; |
| 272 | } |
| 273 | } else |
| 274 | { |
| 275 | // 1. Sort by increasing packetOH |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 276 | for (int i = candidateSet.sizeOfSet() - 1; i >= 0; i--) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 277 | { |
| 278 | for (int j = 1; j <= i; j++) |
| 279 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 280 | if (candidateSet.PacketOH(j-1) > candidateSet.PacketOH(j)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 281 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 282 | candidateSet.SwapEntries(j-1, j); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | } |
| 286 | // 2. For tuples with same OH, keep the one w/ the lowest bitrate |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 287 | for (uint32_t i = 0; i < candidateSet.sizeOfSet(); i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 288 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 289 | if (candidateSet.Tmmbr(i) > 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 290 | { |
| 291 | // get min bitrate for packets w/ same OH |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 292 | uint32_t currentPacketOH = candidateSet.PacketOH(i); |
| 293 | uint32_t currentMinTMMBR = candidateSet.Tmmbr(i); |
| 294 | uint32_t currentMinIndexTMMBR = i; |
| 295 | for (uint32_t j = i+1; j < candidateSet.sizeOfSet(); j++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 296 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 297 | if(candidateSet.PacketOH(j) == currentPacketOH) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 298 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 299 | if(candidateSet.Tmmbr(j) < currentMinTMMBR) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 300 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 301 | currentMinTMMBR = candidateSet.Tmmbr(j); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 302 | currentMinIndexTMMBR = j; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | // keep lowest bitrate |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 307 | for (uint32_t j = 0; j < candidateSet.sizeOfSet(); j++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 308 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 309 | if(candidateSet.PacketOH(j) == currentPacketOH |
| 310 | && j != currentMinIndexTMMBR) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 311 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 312 | candidateSet.ClearEntry(j); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | } |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 317 | // 3. Select and remove tuple w/ lowest tmmbr. |
| 318 | // (If more than 1, choose the one w/ highest OH). |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 319 | uint32_t minTMMBR = 0; |
| 320 | uint32_t minIndexTMMBR = 0; |
| 321 | for (uint32_t i = 0; i < candidateSet.sizeOfSet(); i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 322 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 323 | if (candidateSet.Tmmbr(i) > 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 324 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 325 | minTMMBR = candidateSet.Tmmbr(i); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 326 | minIndexTMMBR = i; |
| 327 | break; |
| 328 | } |
| 329 | } |
| 330 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 331 | for (uint32_t i = 0; i < candidateSet.sizeOfSet(); i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 332 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 333 | if (candidateSet.Tmmbr(i) > 0 && candidateSet.Tmmbr(i) <= minTMMBR) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 334 | { |
| 335 | // get min bitrate |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 336 | minTMMBR = candidateSet.Tmmbr(i); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 337 | minIndexTMMBR = i; |
| 338 | } |
| 339 | } |
| 340 | // first member of selected list |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 341 | _boundingSet.SetEntry(numBoundingSet, |
| 342 | candidateSet.Tmmbr(minIndexTMMBR), |
| 343 | candidateSet.PacketOH(minIndexTMMBR), |
| 344 | candidateSet.Ssrc(minIndexTMMBR)); |
| 345 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 346 | // set intersection value |
| 347 | _ptrIntersectionBoundingSet[numBoundingSet] = 0; |
| 348 | // calculate its maximum packet rate (where its line crosses x-axis) |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 349 | _ptrMaxPRBoundingSet[numBoundingSet] |
| 350 | = _boundingSet.Tmmbr(numBoundingSet) * 1000 |
| 351 | / float(8 * _boundingSet.PacketOH(numBoundingSet)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 352 | numBoundingSet++; |
| 353 | // remove from candidate list |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 354 | candidateSet.ClearEntry(minIndexTMMBR); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 355 | numCandidates--; |
| 356 | |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 357 | // 4. Discard from candidate list all tuple w/ lower OH |
| 358 | // (next tuple must be steeper) |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 359 | for (uint32_t i = 0; i < candidateSet.sizeOfSet(); i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 360 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 361 | if(candidateSet.Tmmbr(i) > 0 |
| 362 | && candidateSet.PacketOH(i) < _boundingSet.PacketOH(0)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 363 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 364 | candidateSet.ClearEntry(i); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 365 | numCandidates--; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | if (numCandidates == 0) |
| 370 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 371 | // Should be true already:_boundingSet.lengthOfSet = numBoundingSet; |
| 372 | assert(_boundingSet.lengthOfSet() == numBoundingSet); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 373 | return numBoundingSet; |
| 374 | } |
| 375 | |
| 376 | bool getNewCandidate = true; |
| 377 | int curCandidateTMMBR = 0; |
| 378 | int curCandidateIndex = 0; |
| 379 | int curCandidatePacketOH = 0; |
| 380 | int curCandidateSSRC = 0; |
| 381 | do |
| 382 | { |
| 383 | if (getNewCandidate) |
| 384 | { |
| 385 | // 5. Remove first remaining tuple from candidate list |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 386 | for (uint32_t i = 0; i < candidateSet.sizeOfSet(); i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 387 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 388 | if (candidateSet.Tmmbr(i) > 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 389 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 390 | curCandidateTMMBR = candidateSet.Tmmbr(i); |
| 391 | curCandidatePacketOH = candidateSet.PacketOH(i); |
| 392 | curCandidateSSRC = candidateSet.Ssrc(i); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 393 | curCandidateIndex = i; |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 394 | candidateSet.ClearEntry(curCandidateIndex); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 395 | break; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 400 | // 6. Calculate packet rate and intersection of the current |
| 401 | // line with line of last tuple in selected list |
| 402 | float packetRate |
| 403 | = float(curCandidateTMMBR |
| 404 | - _boundingSet.Tmmbr(numBoundingSet-1))*1000 |
| 405 | / (8*(curCandidatePacketOH |
| 406 | - _boundingSet.PacketOH(numBoundingSet-1))); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 407 | |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 408 | // 7. If the packet rate is equal or lower than intersection of |
| 409 | // last tuple in selected list, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 410 | // remove last tuple in selected list & go back to step 6 |
| 411 | if(packetRate <= _ptrIntersectionBoundingSet[numBoundingSet-1]) |
| 412 | { |
| 413 | // remove last tuple and goto step 6 |
| 414 | numBoundingSet--; |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 415 | _boundingSet.ClearEntry(numBoundingSet); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 416 | _ptrIntersectionBoundingSet[numBoundingSet] = 0; |
| 417 | _ptrMaxPRBoundingSet[numBoundingSet] = 0; |
| 418 | getNewCandidate = false; |
| 419 | } else |
| 420 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 421 | // 8. If packet rate is lower than maximum packet rate of |
| 422 | // last tuple in selected list, add current tuple to selected |
| 423 | // list |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 424 | if (packetRate < _ptrMaxPRBoundingSet[numBoundingSet-1]) |
| 425 | { |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 426 | _boundingSet.SetEntry(numBoundingSet, |
| 427 | curCandidateTMMBR, |
| 428 | curCandidatePacketOH, |
| 429 | curCandidateSSRC); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 430 | _ptrIntersectionBoundingSet[numBoundingSet] = packetRate; |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 431 | _ptrMaxPRBoundingSet[numBoundingSet] |
| 432 | = _boundingSet.Tmmbr(numBoundingSet)*1000 |
| 433 | / float(8*_boundingSet.PacketOH(numBoundingSet)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 434 | numBoundingSet++; |
| 435 | } |
| 436 | numCandidates--; |
| 437 | getNewCandidate = true; |
| 438 | } |
| 439 | |
| 440 | // 9. Go back to step 5 if any tuple remains in candidate list |
| 441 | } while (numCandidates > 0); |
| 442 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 443 | return numBoundingSet; |
| 444 | } |
| 445 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 446 | bool TMMBRHelp::IsOwner(const uint32_t ssrc, |
| 447 | const uint32_t length) const { |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +0000 | [diff] [blame] | 448 | CriticalSectionScoped lock(_criticalSection); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 449 | |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +0000 | [diff] [blame] | 450 | if (length == 0) { |
| 451 | // Empty bounding set. |
henrike@webrtc.org | 0ad5186 | 2012-03-30 16:54:13 +0000 | [diff] [blame] | 452 | return false; |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +0000 | [diff] [blame] | 453 | } |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 454 | for(uint32_t i = 0; |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 455 | (i < length) && (i < _boundingSet.sizeOfSet()); ++i) { |
| 456 | if(_boundingSet.Ssrc(i) == ssrc) { |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +0000 | [diff] [blame] | 457 | return true; |
| 458 | } |
| 459 | } |
| 460 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 461 | } |
| 462 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 463 | bool TMMBRHelp::CalcMinBitRate( uint32_t* minBitrateKbit) const { |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +0000 | [diff] [blame] | 464 | CriticalSectionScoped lock(_criticalSection); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 465 | |
hta@webrtc.org | 54536bb | 2012-05-03 14:07:23 +0000 | [diff] [blame] | 466 | if (_candidateSet.sizeOfSet() == 0) { |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +0000 | [diff] [blame] | 467 | // Empty bounding set. |
| 468 | return false; |
| 469 | } |
| 470 | *minBitrateKbit = std::numeric_limits<uint32_t>::max(); |
| 471 | |
pbos@webrtc.org | 2f44673 | 2013-04-08 11:08:41 +0000 | [diff] [blame] | 472 | for (uint32_t i = 0; i < _candidateSet.lengthOfSet(); ++i) { |
| 473 | uint32_t curNetBitRateKbit = _candidateSet.Tmmbr(i); |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +0000 | [diff] [blame] | 474 | if (curNetBitRateKbit < MIN_VIDEO_BW_MANAGEMENT_BITRATE) { |
| 475 | curNetBitRateKbit = MIN_VIDEO_BW_MANAGEMENT_BITRATE; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 476 | } |
pwestin@webrtc.org | cac7878 | 2012-04-05 08:30:10 +0000 | [diff] [blame] | 477 | *minBitrateKbit = curNetBitRateKbit < *minBitrateKbit ? |
| 478 | curNetBitRateKbit : *minBitrateKbit; |
| 479 | } |
| 480 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 481 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 482 | } // namespace webrtc |