OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h" | 11 #include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <limits> | 14 #include <limits> |
15 #include <utility> | |
16 | 15 |
17 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
18 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h" | |
19 | 17 |
20 namespace webrtc { | 18 namespace webrtc { |
21 void TMMBRSet::VerifyAndAllocateSet(uint32_t minimumSize) { | 19 void TMMBRSet::VerifyAndAllocateSet(uint32_t minimumSize) { |
22 clear(); | 20 clear(); |
23 reserve(minimumSize); | 21 reserve(minimumSize); |
24 } | 22 } |
25 | 23 |
26 void TMMBRSet::VerifyAndAllocateSetKeepingData(uint32_t minimumSize) { | 24 void TMMBRSet::VerifyAndAllocateSetKeepingData(uint32_t minimumSize) { |
27 reserve(minimumSize); | 25 reserve(minimumSize); |
28 } | 26 } |
(...skipping 16 matching lines...) Expand all Loading... |
45 uint32_t ssrcSet) { | 43 uint32_t ssrcSet) { |
46 RTC_DCHECK_LT(size(), capacity()); | 44 RTC_DCHECK_LT(size(), capacity()); |
47 SetEntry(size(), tmmbrSet, packetOHSet, ssrcSet); | 45 SetEntry(size(), tmmbrSet, packetOHSet, ssrcSet); |
48 } | 46 } |
49 | 47 |
50 void TMMBRSet::RemoveEntry(uint32_t sourceIdx) { | 48 void TMMBRSet::RemoveEntry(uint32_t sourceIdx) { |
51 RTC_DCHECK_LT(sourceIdx, size()); | 49 RTC_DCHECK_LT(sourceIdx, size()); |
52 erase(begin() + sourceIdx); | 50 erase(begin() + sourceIdx); |
53 } | 51 } |
54 | 52 |
55 TMMBRSet* TMMBRHelp::VerifyAndAllocateCandidateSet(uint32_t minimumSize) { | 53 std::vector<rtcp::TmmbItem> TMMBRHelp::FindBoundingSet( |
56 _candidateSet.VerifyAndAllocateSet(minimumSize); | 54 std::vector<rtcp::TmmbItem> candidates) { |
57 return &_candidateSet; | 55 // Filter out candidates with 0 bitrate. |
58 } | 56 for (auto it = candidates.begin(); it != candidates.end();) { |
59 | 57 if (!it->bitrate_bps()) |
60 TMMBRSet* TMMBRHelp::CandidateSet() { | 58 it = candidates.erase(it); |
61 return &_candidateSet; | 59 else |
62 } | 60 ++it; |
63 | |
64 std::vector<rtcp::TmmbItem> TMMBRHelp::FindTMMBRBoundingSet() { | |
65 // Work on local variable, will be modified | |
66 TMMBRSet candidateSet; | |
67 candidateSet.VerifyAndAllocateSet(_candidateSet.capacity()); | |
68 | |
69 for (size_t i = 0; i < _candidateSet.size(); i++) { | |
70 if (_candidateSet.Tmmbr(i)) { | |
71 candidateSet.AddEntry(_candidateSet.Tmmbr(i), _candidateSet.PacketOH(i), | |
72 _candidateSet.Ssrc(i)); | |
73 } else { | |
74 // make sure this is zero if tmmbr = 0 | |
75 RTC_DCHECK_EQ(_candidateSet.PacketOH(i), 0u); | |
76 // Old code: | |
77 // _candidateSet.ptrPacketOHSet[i] = 0; | |
78 } | |
79 } | 61 } |
80 | 62 |
81 // Number of set candidates | 63 if (candidates.size() <= 1) |
82 int32_t numSetCandidates = candidateSet.lengthOfSet(); | 64 return candidates; |
83 // Find bounding set | |
84 std::vector<rtcp::TmmbItem> bounding; | |
85 if (numSetCandidates > 0) { | |
86 FindBoundingSet(std::move(candidateSet), &bounding); | |
87 size_t numBoundingSet = bounding.size(); | |
88 RTC_DCHECK_GE(numBoundingSet, 1u); | |
89 RTC_DCHECK_LE(numBoundingSet, _candidateSet.size()); | |
90 } | |
91 return bounding; | |
92 } | |
93 | 65 |
94 void TMMBRHelp::FindBoundingSet(std::vector<rtcp::TmmbItem> candidates, | |
95 std::vector<rtcp::TmmbItem>* bounding_set) { | |
96 RTC_DCHECK(bounding_set); | |
97 RTC_DCHECK(!candidates.empty()); | |
98 size_t num_candidates = candidates.size(); | 66 size_t num_candidates = candidates.size(); |
99 | 67 |
100 if (num_candidates == 1) { | |
101 RTC_DCHECK(candidates[0].bitrate_bps()); | |
102 *bounding_set = std::move(candidates); | |
103 return; | |
104 } | |
105 | |
106 // 1. Sort by increasing packet overhead. | 68 // 1. Sort by increasing packet overhead. |
107 std::sort(candidates.begin(), candidates.end(), | 69 std::sort(candidates.begin(), candidates.end(), |
108 [](const rtcp::TmmbItem& lhs, const rtcp::TmmbItem& rhs) { | 70 [](const rtcp::TmmbItem& lhs, const rtcp::TmmbItem& rhs) { |
109 return lhs.packet_overhead() < rhs.packet_overhead(); | 71 return lhs.packet_overhead() < rhs.packet_overhead(); |
110 }); | 72 }); |
111 | 73 |
112 // 2. For tuples with same overhead, keep the one with the lowest bitrate. | 74 // 2. For tuples with same overhead, keep the one with the lowest bitrate. |
113 for (auto it = candidates.begin(); it != candidates.end();) { | 75 for (auto it = candidates.begin(); it != candidates.end();) { |
114 RTC_DCHECK(it->bitrate_bps()); | 76 RTC_DCHECK(it->bitrate_bps()); |
115 auto current_min = it; | 77 auto current_min = it; |
116 auto next_it = it + 1; | 78 auto next_it = it + 1; |
117 // Use fact candidates are sorted by overhead, so candidates with same | 79 // Use fact candidates are sorted by overhead, so candidates with same |
118 // overhead are adjusted. | 80 // overhead are adjusted. |
119 while (next_it != candidates.end() && | 81 while (next_it != candidates.end() && |
120 next_it->packet_overhead() == current_min->packet_overhead()) { | 82 next_it->packet_overhead() == current_min->packet_overhead()) { |
121 if (next_it->bitrate_bps() < current_min->bitrate_bps()) { | 83 if (next_it->bitrate_bps() < current_min->bitrate_bps()) { |
122 current_min->set_bitrate_bps(0); | 84 current_min->set_bitrate_bps(0); |
123 current_min = next_it; | 85 current_min = next_it; |
124 } else { | 86 } else { |
125 next_it->set_bitrate_bps(0); | 87 next_it->set_bitrate_bps(0); |
126 } | 88 } |
127 ++next_it; | 89 ++next_it; |
128 --num_candidates; | 90 --num_candidates; |
129 } | 91 } |
130 it = next_it; | 92 it = next_it; |
131 } | 93 } |
132 | 94 |
133 // 3. Select and remove tuple with lowest tmmbr. | 95 // 3. Select and remove tuple with lowest bitrate. |
134 // (If more than 1, choose the one with highest overhead). | 96 // (If more than 1, choose the one with highest overhead). |
135 auto min_bitrate_it = candidates.end(); | 97 auto min_bitrate_it = candidates.end(); |
136 for (auto it = candidates.begin(); it != candidates.end(); ++it) { | 98 for (auto it = candidates.begin(); it != candidates.end(); ++it) { |
137 if (it->bitrate_bps()) { | 99 if (it->bitrate_bps()) { |
138 min_bitrate_it = it; | 100 min_bitrate_it = it; |
139 break; | 101 break; |
140 } | 102 } |
141 } | 103 } |
142 | 104 |
143 for (auto it = min_bitrate_it; it != candidates.end(); ++it) { | 105 for (auto it = min_bitrate_it; it != candidates.end(); ++it) { |
144 if (it->bitrate_bps() && | 106 if (it->bitrate_bps() && |
145 it->bitrate_bps() <= min_bitrate_it->bitrate_bps()) { | 107 it->bitrate_bps() <= min_bitrate_it->bitrate_bps()) { |
146 // Get min bitrate. | 108 // Get min bitrate. |
147 min_bitrate_it = it; | 109 min_bitrate_it = it; |
148 } | 110 } |
149 } | 111 } |
150 | 112 |
151 bounding_set->clear(); | 113 std::vector<rtcp::TmmbItem> bounding_set; |
152 bounding_set->reserve(num_candidates); | 114 bounding_set.reserve(num_candidates); |
153 std::vector<float> intersection(num_candidates); | 115 std::vector<float> intersection(num_candidates); |
154 std::vector<float> max_packet_rate(num_candidates); | 116 std::vector<float> max_packet_rate(num_candidates); |
155 | 117 |
156 // First member of selected list. | 118 // First member of selected list. |
157 bounding_set->push_back(*min_bitrate_it); | 119 bounding_set.push_back(*min_bitrate_it); |
158 intersection[0] = 0; | 120 intersection[0] = 0; |
159 // Calculate its maximum packet rate (where its line crosses x-axis). | 121 // Calculate its maximum packet rate (where its line crosses x-axis). |
160 uint16_t packet_overhead = bounding_set->back().packet_overhead(); | 122 uint16_t packet_overhead = bounding_set.back().packet_overhead(); |
161 if (packet_overhead == 0) { | 123 if (packet_overhead == 0) { |
162 // Avoid division by zero. | 124 // Avoid division by zero. |
163 max_packet_rate[0] = std::numeric_limits<float>::max(); | 125 max_packet_rate[0] = std::numeric_limits<float>::max(); |
164 } else { | 126 } else { |
165 max_packet_rate[0] = bounding_set->back().bitrate_bps() / | 127 max_packet_rate[0] = |
166 static_cast<float>(packet_overhead); | 128 bounding_set.back().bitrate_bps() / static_cast<float>(packet_overhead); |
167 } | 129 } |
168 // Remove from candidate list. | 130 // Remove from candidate list. |
169 min_bitrate_it->set_bitrate_bps(0); | 131 min_bitrate_it->set_bitrate_bps(0); |
170 --num_candidates; | 132 --num_candidates; |
171 | 133 |
172 // 4. Discard from candidate list all tuple with lower overhead | 134 // 4. Discard from candidate list all tuple with lower overhead |
173 // (next tuple must be steeper). | 135 // (next tuple must be steeper). |
174 for (auto it = candidates.begin(); it != candidates.end(); ++it) { | 136 for (auto it = candidates.begin(); it != candidates.end(); ++it) { |
175 if (it->bitrate_bps() && | 137 if (it->bitrate_bps() && |
176 it->packet_overhead() < bounding_set->front().packet_overhead()) { | 138 it->packet_overhead() < bounding_set.front().packet_overhead()) { |
177 it->set_bitrate_bps(0); | 139 it->set_bitrate_bps(0); |
178 --num_candidates; | 140 --num_candidates; |
179 } | 141 } |
180 } | 142 } |
181 | 143 |
182 bool get_new_candidate = true; | 144 bool get_new_candidate = true; |
183 rtcp::TmmbItem cur_candidate; | 145 rtcp::TmmbItem cur_candidate; |
184 while (num_candidates > 0) { | 146 while (num_candidates > 0) { |
185 if (get_new_candidate) { | 147 if (get_new_candidate) { |
186 // 5. Remove first remaining tuple from candidate list. | 148 // 5. Remove first remaining tuple from candidate list. |
187 for (auto it = candidates.begin(); it != candidates.end(); ++it) { | 149 for (auto it = candidates.begin(); it != candidates.end(); ++it) { |
188 if (it->bitrate_bps()) { | 150 if (it->bitrate_bps()) { |
189 cur_candidate = *it; | 151 cur_candidate = *it; |
190 it->set_bitrate_bps(0); | 152 it->set_bitrate_bps(0); |
191 break; | 153 break; |
192 } | 154 } |
193 } | 155 } |
194 } | 156 } |
195 | 157 |
196 // 6. Calculate packet rate and intersection of the current | 158 // 6. Calculate packet rate and intersection of the current |
197 // line with line of last tuple in selected list. | 159 // line with line of last tuple in selected list. |
198 RTC_DCHECK_NE(cur_candidate.packet_overhead(), | 160 RTC_DCHECK_NE(cur_candidate.packet_overhead(), |
199 bounding_set->back().packet_overhead()); | 161 bounding_set.back().packet_overhead()); |
200 float packet_rate = static_cast<float>(cur_candidate.bitrate_bps() - | 162 float packet_rate = static_cast<float>(cur_candidate.bitrate_bps() - |
201 bounding_set->back().bitrate_bps()) / | 163 bounding_set.back().bitrate_bps()) / |
202 (cur_candidate.packet_overhead() - | 164 (cur_candidate.packet_overhead() - |
203 bounding_set->back().packet_overhead()); | 165 bounding_set.back().packet_overhead()); |
204 | 166 |
205 // 7. If the packet rate is equal or lower than intersection of | 167 // 7. If the packet rate is equal or lower than intersection of |
206 // last tuple in selected list, | 168 // last tuple in selected list, |
207 // remove last tuple in selected list & go back to step 6. | 169 // remove last tuple in selected list & go back to step 6. |
208 if (packet_rate <= intersection[bounding_set->size() - 1]) { | 170 if (packet_rate <= intersection[bounding_set.size() - 1]) { |
209 // Remove last tuple and goto step 6. | 171 // Remove last tuple and goto step 6. |
210 bounding_set->pop_back(); | 172 bounding_set.pop_back(); |
211 get_new_candidate = false; | 173 get_new_candidate = false; |
212 } else { | 174 } else { |
213 // 8. If packet rate is lower than maximum packet rate of | 175 // 8. If packet rate is lower than maximum packet rate of |
214 // last tuple in selected list, add current tuple to selected | 176 // last tuple in selected list, add current tuple to selected |
215 // list. | 177 // list. |
216 if (packet_rate < max_packet_rate[bounding_set->size() - 1]) { | 178 if (packet_rate < max_packet_rate[bounding_set.size() - 1]) { |
217 bounding_set->push_back(cur_candidate); | 179 bounding_set.push_back(cur_candidate); |
218 intersection[bounding_set->size() - 1] = packet_rate; | 180 intersection[bounding_set.size() - 1] = packet_rate; |
219 uint16_t packet_overhead = bounding_set->back().packet_overhead(); | 181 uint16_t packet_overhead = bounding_set.back().packet_overhead(); |
220 RTC_DCHECK_NE(packet_overhead, 0); | 182 RTC_DCHECK_NE(packet_overhead, 0); |
221 max_packet_rate[bounding_set->size() - 1] = | 183 max_packet_rate[bounding_set.size() - 1] = |
222 bounding_set->back().bitrate_bps() / | 184 bounding_set.back().bitrate_bps() / |
223 static_cast<float>(packet_overhead); | 185 static_cast<float>(packet_overhead); |
224 } | 186 } |
225 --num_candidates; | 187 --num_candidates; |
226 get_new_candidate = true; | 188 get_new_candidate = true; |
227 } | 189 } |
228 | 190 |
229 // 9. Go back to step 5 if any tuple remains in candidate list. | 191 // 9. Go back to step 5 if any tuple remains in candidate list. |
230 } | 192 } |
| 193 RTC_DCHECK(!bounding_set.empty()); |
| 194 return bounding_set; |
231 } | 195 } |
232 | 196 |
233 bool TMMBRHelp::IsOwner(const std::vector<rtcp::TmmbItem>& bounding, | 197 bool TMMBRHelp::IsOwner(const std::vector<rtcp::TmmbItem>& bounding, |
234 uint32_t ssrc) { | 198 uint32_t ssrc) { |
235 for (const rtcp::TmmbItem& item : bounding) { | 199 for (const rtcp::TmmbItem& item : bounding) { |
236 if (item.ssrc() == ssrc) { | 200 if (item.ssrc() == ssrc) { |
237 return true; | 201 return true; |
238 } | 202 } |
239 } | 203 } |
240 return false; | 204 return false; |
241 } | 205 } |
242 | 206 |
243 bool TMMBRHelp::CalcMinBitRate(uint32_t* minBitrateKbit) const { | 207 uint64_t TMMBRHelp::CalcMinBitrateBps( |
244 if (_candidateSet.size() == 0) { | 208 const std::vector<rtcp::TmmbItem>& candidates) { |
245 // Empty bounding set. | 209 RTC_DCHECK(!candidates.empty()); |
246 return false; | 210 uint64_t min_bitrate_bps = std::numeric_limits<uint64_t>::max(); |
247 } | 211 for (const rtcp::TmmbItem& item : candidates) |
248 *minBitrateKbit = std::numeric_limits<uint32_t>::max(); | 212 if (item.bitrate_bps() < min_bitrate_bps) |
249 | 213 min_bitrate_bps = item.bitrate_bps(); |
250 for (size_t i = 0; i < _candidateSet.lengthOfSet(); ++i) { | 214 return min_bitrate_bps; |
251 uint32_t curNetBitRateKbit = _candidateSet.Tmmbr(i); | |
252 if (curNetBitRateKbit < MIN_VIDEO_BW_MANAGEMENT_BITRATE) { | |
253 curNetBitRateKbit = MIN_VIDEO_BW_MANAGEMENT_BITRATE; | |
254 } | |
255 *minBitrateKbit = curNetBitRateKbit < *minBitrateKbit ? curNetBitRateKbit | |
256 : *minBitrateKbit; | |
257 } | |
258 return true; | |
259 } | 215 } |
260 } // namespace webrtc | 216 } // namespace webrtc |
OLD | NEW |