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